blob: e50955ddc12af992eb7a3fd4a3dcd85b0de0dbe0 [file] [log] [blame]
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002/*
3 * httpd implementation for busybox
4 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00005 * Copyright (C) 2002,2003 Glenn Engel <glenne@engel.org>
"Vladimir N. Oleynik"79af7d52006-01-26 10:58:12 +00006 * Copyright (C) 2003-2006 Vladimir Oleynik <dzo@simtreas.ru>
Glenn L McGrath58c708a2003-01-05 04:01:56 +00007 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00008 * simplify patch stolen from libbb without using strdup
Glenn L McGrath58c708a2003-01-05 04:01:56 +00009 *
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +000010 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000011 *
12 *****************************************************************************
13 *
Glenn L McGrath06e95652003-02-09 06:51:14 +000014 * 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 McGrath58c708a2003-01-05 04:01:56 +000022 *
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 McGrath1dc0cca2003-10-03 10:50:56 +000025 * after setting QUERY_STRING and other environment variables.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000026 *
Denis Vlasenko8b458372006-11-21 21:23:21 +000027 * Doc:
28 * "CGI Environment Variables": http://hoohoo.ncsa.uiuc.edu/cgi/env.html
29 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +000030 * The server can also be invoked as a url arg decoder and html text encoder
31 * as follows:
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000032 * foo=`httpd -d $foo` # decode "Hello%20World" as "Hello World"
Glenn L McGrathc9163fe2003-05-13 16:20:11 +000033 * bar=`httpd -e "<Hello World>"` # encode as "&#60Hello&#32World&#62"
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000034 * Note that url encoding for arguments is not the same as html encoding for
Eric Andersenaff114c2004-04-14 17:51:38 +000035 * presentation. -d decodes a url-encoded argument while -e encodes in html
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000036 * for page display.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000037 *
38 * httpd.conf has the following format:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000039 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +000040 * A:172.20. # Allow address from 172.20.0.0/16
41 * A:10.0.0.0/25 # Allow any address from 10.0.0.0-10.0.0.127
42 * A:10.0.0.0/255.255.255.128 # Allow any address that previous set
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000043 * A:127.0.0.1 # Allow local loopback connections
44 * D:* # Deny from other IP connections
45 * /cgi-bin:foo:bar # Require user foo, pwd bar on urls starting with /cgi-bin/
46 * /adm:admin:setup # Require user admin, pwd setup on urls starting with /adm/
47 * /adm:toor:PaSsWd # or user toor, pwd PaSsWd on urls starting with /adm/
48 * .au:audio/basic # additional mime type for audio.au files
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +000049 * *.php:/path/php # running cgi.php scripts through an interpreter
Eric Andersenc7bda1c2004-03-15 08:29:22 +000050 *
Eric Andersenaff114c2004-04-14 17:51:38 +000051 * A/D may be as a/d or allow/deny - first char case insensitive
Glenn L McGrath393183d2003-05-26 14:07:50 +000052 * Deny IP rules take precedence over allow rules.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000053 *
54 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000055 * The Deny/Allow IP logic:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000056 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000057 * - Default is to allow all. No addresses are denied unless
Eric Andersen97a1de12004-08-26 22:22:50 +000058 * denied with a D: rule.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000059 * - Order of Deny/Allow rules is significant
60 * - Deny rules take precedence over allow rules.
61 * - If a deny all rule (D:*) is used it acts as a catch-all for unmatched
Eric Andersen97a1de12004-08-26 22:22:50 +000062 * addresses.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000063 * - Specification of Allow all (A:*) is a no-op
Eric Andersenc7bda1c2004-03-15 08:29:22 +000064 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000065 * Example:
66 * 1. Allow only specified addresses
Glenn L McGrathb65422c2003-09-08 10:59:27 +000067 * A:172.20 # Allow any address that begins with 172.20.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000068 * A:10.10. # Allow any address that begins with 10.10.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000069 * A:127.0.0.1 # Allow local loopback connections
70 * D:* # Deny from other IP connections
Eric Andersenc7bda1c2004-03-15 08:29:22 +000071 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000072 * 2. Only deny specified addresses
73 * D:1.2.3. # deny from 1.2.3.0 - 1.2.3.255
74 * D:2.3.4. # deny from 2.3.4.0 - 2.3.4.255
75 * A:* # (optional line added for clarity)
Eric Andersenc7bda1c2004-03-15 08:29:22 +000076 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000077 * If a sub directory contains a config file it is parsed and merged with
Glenn L McGrathb65422c2003-09-08 10:59:27 +000078 * any existing settings as if it was appended to the original configuration.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000079 *
80 * subdir paths are relative to the containing subdir and thus cannot
81 * affect the parent rules.
82 *
83 * Note that since the sub dir is parsed in the forked thread servicing the
84 * subdir http request, any merge is discarded when the process exits. As a
85 * result, the subdir settings only have a lifetime of a single request.
86 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000087 *
88 * If -c is not set, an attempt will be made to open the default
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000089 * root configuration file. If -c is set and the file is not found, the
90 * server exits with an error.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000091 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000092*/
Glenn L McGrath58c708a2003-01-05 04:01:56 +000093
Glenn L McGrath06e95652003-02-09 06:51:14 +000094
Glenn L McGrath06e95652003-02-09 06:51:14 +000095#include "busybox.h"
Glenn L McGrath58c708a2003-01-05 04:01:56 +000096
Glenn L McGrath58c708a2003-01-05 04:01:56 +000097
Eric Andersen07f2fea2004-10-08 08:03:29 +000098static const char httpdVersion[] = "busybox httpd/1.35 6-Oct-2004";
Glenn L McGrathc9163fe2003-05-13 16:20:11 +000099static const char default_path_httpd_conf[] = "/etc";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000100static const char httpd_conf[] = "httpd.conf";
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000101static const char home[] = "./";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000102
Eric Andersen07f2fea2004-10-08 08:03:29 +0000103#define TIMEOUT 60
104
Eric Andersenaff114c2004-04-14 17:51:38 +0000105// Note: busybox xfuncs are not used because we want the server to keep running
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000106// if something bad happens due to a malformed user request.
Glenn L McGrath06e95652003-02-09 06:51:14 +0000107// As a result, all memory allocation after daemonize
108// is checked rigorously
109
110//#define DEBUG 1
111
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000112#ifndef DEBUG
113# define DEBUG 0
114#endif
115
Glenn L McGrath06e95652003-02-09 06:51:14 +0000116#define MAX_MEMORY_BUFF 8192 /* IO buffer */
117
118typedef struct HT_ACCESS {
119 char *after_colon;
120 struct HT_ACCESS *next;
121 char before_colon[1]; /* really bigger, must last */
122} Htaccess;
123
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000124typedef struct HT_ACCESS_IP {
125 unsigned int ip;
126 unsigned int mask;
127 int allow_deny;
128 struct HT_ACCESS_IP *next;
129} Htaccess_IP;
130
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000131typedef struct {
132 char buf[MAX_MEMORY_BUFF];
Glenn L McGrath06e95652003-02-09 06:51:14 +0000133
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000134 USE_FEATURE_HTTPD_BASIC_AUTH(const char *realm;)
135 USE_FEATURE_HTTPD_BASIC_AUTH(char *remoteuser;)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000136
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000137 const char *query;
Eric Andersen07f2fea2004-10-08 08:03:29 +0000138
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000139 USE_FEATURE_HTTPD_CGI(char *referer;)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000140
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000141 const char *configFile;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000142
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000143 unsigned int rmt_ip;
Denis Vlasenko55a99402006-09-30 20:41:44 +0000144#if ENABLE_FEATURE_HTTPD_CGI || DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000145 char rmt_ip_str[16]; /* for set env REMOTE_ADDR */
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000146#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000147 unsigned port; /* server initial port and for
148 set env REMOTE_PORT */
Denis Vlasenkod4f3d1a2006-11-16 16:20:12 +0000149 const char *found_mime_type;
150 const char *found_moved_temporarily;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000151
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000152 off_t ContentLength; /* -1 - unknown */
153 time_t last_mod;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000154
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000155 Htaccess_IP *ip_a_d; /* config allow/deny lines */
156 int flg_deny_all;
Denis Vlasenko55a99402006-09-30 20:41:44 +0000157#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000158 Htaccess *auth; /* config user:password lines */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000159#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000160#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000161 Htaccess *mime_a; /* config mime types */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000162#endif
163
Denis Vlasenko9f609292006-11-05 19:47:33 +0000164 int server_socket;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000165 int accepted_socket;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000166 volatile int alarm_signaled;
Eric Andersen07f2fea2004-10-08 08:03:29 +0000167
Denis Vlasenko55a99402006-09-30 20:41:44 +0000168#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000169 Htaccess *script_i; /* config script interpreters */
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000170#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000171} HttpdConfig;
172
173static HttpdConfig *config;
174
Mike Frysingerfa6c4842006-05-26 01:48:17 +0000175static const char request_GET[] = "GET"; /* size algorithmic optimize */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000176
177static const char* const suffixTable [] = {
Eric Andersenaff114c2004-04-14 17:51:38 +0000178/* Warning: shorted equivalent suffix in one line must be first */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000179 ".htm.html", "text/html",
180 ".jpg.jpeg", "image/jpeg",
181 ".gif", "image/gif",
182 ".png", "image/png",
183 ".txt.h.c.cc.cpp", "text/plain",
184 ".css", "text/css",
185 ".wav", "audio/wav",
186 ".avi", "video/x-msvideo",
187 ".qt.mov", "video/quicktime",
188 ".mpe.mpeg", "video/mpeg",
189 ".mid.midi", "audio/midi",
190 ".mp3", "audio/mpeg",
Glenn L McGrath06e95652003-02-09 06:51:14 +0000191#if 0 /* unpopular */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000192 ".au", "audio/basic",
193 ".pac", "application/x-ns-proxy-autoconfig",
194 ".vrml.wrl", "model/vrml",
Glenn L McGrath06e95652003-02-09 06:51:14 +0000195#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000196 0, "application/octet-stream" /* default */
197};
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000198
Denis Vlasenko55a99402006-09-30 20:41:44 +0000199typedef enum {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000200 HTTP_OK = 200,
201 HTTP_MOVED_TEMPORARILY = 302,
202 HTTP_BAD_REQUEST = 400, /* malformed syntax */
203 HTTP_UNAUTHORIZED = 401, /* authentication needed, respond with auth hdr */
204 HTTP_NOT_FOUND = 404,
205 HTTP_FORBIDDEN = 403,
206 HTTP_REQUEST_TIMEOUT = 408,
207 HTTP_NOT_IMPLEMENTED = 501, /* used for unrecognized requests */
208 HTTP_INTERNAL_SERVER_ERROR = 500,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000209#if 0 /* future use */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000210 HTTP_CONTINUE = 100,
211 HTTP_SWITCHING_PROTOCOLS = 101,
212 HTTP_CREATED = 201,
213 HTTP_ACCEPTED = 202,
214 HTTP_NON_AUTHORITATIVE_INFO = 203,
215 HTTP_NO_CONTENT = 204,
216 HTTP_MULTIPLE_CHOICES = 300,
217 HTTP_MOVED_PERMANENTLY = 301,
218 HTTP_NOT_MODIFIED = 304,
219 HTTP_PAYMENT_REQUIRED = 402,
220 HTTP_BAD_GATEWAY = 502,
221 HTTP_SERVICE_UNAVAILABLE = 503, /* overload, maintenance */
222 HTTP_RESPONSE_SETSIZE = 0xffffffff
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000223#endif
224} HttpResponseNum;
225
Denis Vlasenko55a99402006-09-30 20:41:44 +0000226typedef struct {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000227 HttpResponseNum type;
228 const char *name;
229 const char *info;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000230} HttpEnumString;
231
232static const HttpEnumString httpResponseNames[] = {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000233 { HTTP_OK, "OK", NULL },
234 { HTTP_MOVED_TEMPORARILY, "Found", "Directories must end with a slash." },
235 { HTTP_REQUEST_TIMEOUT, "Request Timeout",
236 "No request appeared within a reasonable time period." },
237 { HTTP_NOT_IMPLEMENTED, "Not Implemented",
238 "The requested method is not recognized by this server." },
Denis Vlasenko55a99402006-09-30 20:41:44 +0000239#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000240 { HTTP_UNAUTHORIZED, "Unauthorized", "" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000241#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000242 { HTTP_NOT_FOUND, "Not Found",
243 "The requested URL was not found on this server." },
244 { HTTP_BAD_REQUEST, "Bad Request", "Unsupported method." },
245 { HTTP_FORBIDDEN, "Forbidden", "" },
246 { HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error",
247 "Internal Server Error" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000248#if 0 /* not implemented */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000249 { HTTP_CREATED, "Created" },
250 { HTTP_ACCEPTED, "Accepted" },
251 { HTTP_NO_CONTENT, "No Content" },
252 { HTTP_MULTIPLE_CHOICES, "Multiple Choices" },
253 { HTTP_MOVED_PERMANENTLY, "Moved Permanently" },
254 { HTTP_NOT_MODIFIED, "Not Modified" },
255 { HTTP_BAD_GATEWAY, "Bad Gateway", "" },
256 { HTTP_SERVICE_UNAVAILABLE, "Service Unavailable", "" },
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000257#endif
258};
259
Glenn L McGrath06e95652003-02-09 06:51:14 +0000260
261static const char RFC1123FMT[] = "%a, %d %b %Y %H:%M:%S GMT";
Denis Vlasenko0bb993f2006-11-21 00:06:28 +0000262
263
264#define STRNCASECMP(a, str) strncasecmp((a), (str), sizeof(str)-1)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000265
266
Denis Vlasenko55a99402006-09-30 20:41:44 +0000267static int scan_ip(const char **ep, unsigned int *ip, unsigned char endc)
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000268{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000269 const char *p = *ep;
270 int auto_mask = 8;
271 int j;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000272
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000273 *ip = 0;
274 for (j = 0; j < 4; j++) {
275 unsigned int octet;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000276
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000277 if ((*p < '0' || *p > '9') && (*p != '/' || j == 0) && *p != 0)
278 return -auto_mask;
279 octet = 0;
280 while (*p >= '0' && *p <= '9') {
281 octet *= 10;
282 octet += *p - '0';
283 if (octet > 255)
284 return -auto_mask;
285 p++;
286 }
287 if (*p == '.')
288 p++;
289 if (*p != '/' && *p != 0)
290 auto_mask += 8;
291 *ip = ((*ip) << 8) | octet;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000292 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000293 if (*p != 0) {
294 if (*p != endc)
295 return -auto_mask;
296 p++;
297 if (*p == 0)
298 return -auto_mask;
299 }
300 *ep = p;
301 return auto_mask;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000302}
303
Denis Vlasenko55a99402006-09-30 20:41:44 +0000304static int scan_ip_mask(const char *ipm, unsigned int *ip, unsigned int *mask)
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000305{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000306 int i;
307 unsigned int msk;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000308
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000309 i = scan_ip(&ipm, ip, '/');
310 if (i < 0)
311 return i;
312 if (*ipm) {
313 const char *p = ipm;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000314
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000315 i = 0;
316 while (*p) {
Denis Vlasenko92758142006-10-03 19:56:34 +0000317 if (*p < '0' || *p > '9') {
318 if (*p == '.') {
319 i = scan_ip(&ipm, mask, 0);
320 return i != 32;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000321 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000322 return -1;
323 }
324 i *= 10;
325 i += *p - '0';
326 p++;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000327 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000328 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000329 if (i > 32 || i < 0)
Denis Vlasenko92758142006-10-03 19:56:34 +0000330 return -1;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000331 msk = 0x80000000;
332 *mask = 0;
333 while (i > 0) {
334 *mask |= msk;
335 msk >>= 1;
336 i--;
337 }
338 return 0;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000339}
340
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000341#if ENABLE_FEATURE_HTTPD_BASIC_AUTH \
342 || ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES \
343 || ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000344static void free_config_lines(Htaccess **pprev)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000345{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000346 Htaccess *prev = *pprev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000347
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000348 while (prev) {
349 Htaccess *cur = prev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000350
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000351 prev = cur->next;
352 free(cur);
353 }
354 *pprev = NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000355}
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000356#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000357
Glenn L McGrath06e95652003-02-09 06:51:14 +0000358/* flag */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000359#define FIRST_PARSE 0
360#define SUBDIR_PARSE 1
361#define SIGNALED_PARSE 2
362#define FIND_FROM_HTTPD_ROOT 3
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000363/****************************************************************************
364 *
365 > $Function: parse_conf()
366 *
367 * $Description: parse configuration file into in-memory linked list.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000368 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000369 * The first non-white character is examined to determine if the config line
370 * is one of the following:
371 * .ext:mime/type # new mime type not compiled into httpd
372 * [adAD]:from # ip address allow/deny, * for wildcard
373 * /path:user:pass # username/password
374 *
375 * Any previous IP rules are discarded.
376 * If the flag argument is not SUBDIR_PARSE then all /path and mime rules
377 * are also discarded. That is, previous settings are retained if flag is
378 * SUBDIR_PARSE.
379 *
380 * $Parameters:
381 * (const char *) path . . null for ip address checks, path for password
382 * checks.
383 * (int) flag . . . . . . the source of the parse request.
384 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000385 * $Return: (None)
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000386 *
387 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000388static void parse_conf(const char *path, int flag)
389{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000390 FILE *f;
Denis Vlasenko55a99402006-09-30 20:41:44 +0000391#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000392 Htaccess *prev;
393#endif
394#if ENABLE_FEATURE_HTTPD_BASIC_AUTH \
395 || ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES \
396 || ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000397 Htaccess *cur;
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000398#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000399
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000400 const char *cf = config->configFile;
401 char buf[160];
402 char *p0 = NULL;
403 char *c, *p;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000404
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000405 /* free previous ip setup if present */
406 Htaccess_IP *pip = config->ip_a_d;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000407
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000408 while (pip) {
409 Htaccess_IP *cur_ipl = pip;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000410
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000411 pip = cur_ipl->next;
412 free(cur_ipl);
413 }
414 config->ip_a_d = NULL;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000415
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000416 config->flg_deny_all = 0;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000417
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000418#if ENABLE_FEATURE_HTTPD_BASIC_AUTH \
419 || ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES \
420 || ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000421 /* retain previous auth and mime config only for subdir parse */
422 if (flag != SUBDIR_PARSE) {
Denis Vlasenko55a99402006-09-30 20:41:44 +0000423#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000424 free_config_lines(&config->auth);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000425#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000426#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000427 free_config_lines(&config->mime_a);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000428#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000429#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000430 free_config_lines(&config->script_i);
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000431#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000432 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000433#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000434
435 if (flag == SUBDIR_PARSE || cf == NULL) {
436 cf = alloca(strlen(path) + sizeof(httpd_conf) + 2);
437 if (cf == NULL) {
438 if (flag == FIRST_PARSE)
439 bb_error_msg_and_die(bb_msg_memory_exhausted);
440 return;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000441 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000442 sprintf((char *)cf, "%s/%s", path, httpd_conf);
Glenn L McGrath393183d2003-05-26 14:07:50 +0000443 }
444
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000445 while ((f = fopen(cf, "r")) == NULL) {
446 if (flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) {
447 /* config file not found, no changes to config */
448 return;
449 }
450 if (config->configFile && flag == FIRST_PARSE) /* if -c option given */
451 bb_perror_msg_and_die("%s", cf);
452 flag = FIND_FROM_HTTPD_ROOT;
453 cf = httpd_conf;
454 }
455
Denis Vlasenko55a99402006-09-30 20:41:44 +0000456#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000457 prev = config->auth;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000458#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000459 /* This could stand some work */
Denis Vlasenko55a99402006-09-30 20:41:44 +0000460 while ((p0 = fgets(buf, sizeof(buf), f)) != NULL) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000461 c = NULL;
462 for (p = p0; *p0 != 0 && *p0 != '#'; p0++) {
463 if (!isspace(*p0)) {
464 *p++ = *p0;
465 if (*p0 == ':' && c == NULL)
466 c = p;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000467 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000468 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000469 *p = 0;
470
471 /* test for empty or strange line */
472 if (c == NULL || *c == 0)
473 continue;
474 p0 = buf;
475 if (*p0 == 'd')
476 *p0 = 'D';
477 if (*c == '*') {
478 if (*p0 == 'D') {
479 /* memorize deny all */
480 config->flg_deny_all++;
481 }
482 /* skip default other "word:*" config lines */
483 continue;
484 }
485
486 if (*p0 == 'a')
487 *p0 = 'A';
488 else if (*p0 != 'D' && *p0 != 'A'
Denis Vlasenko55a99402006-09-30 20:41:44 +0000489#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000490 && *p0 != '/'
491#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000492#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000493 && *p0 != '.'
494#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000495#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000496 && *p0 != '*'
497#endif
498 )
499 continue;
500 if (*p0 == 'A' || *p0 == 'D') {
501 /* storing current config IP line */
502 pip = calloc(1, sizeof(Htaccess_IP));
503 if (pip) {
Denis Vlasenko55a99402006-09-30 20:41:44 +0000504 if (scan_ip_mask(c, &(pip->ip), &(pip->mask))) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000505 /* syntax IP{/mask} error detected, protect all */
506 *p0 = 'D';
507 pip->mask = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000508 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000509 pip->allow_deny = *p0;
510 if (*p0 == 'D') {
511 /* Deny:form_IP move top */
512 pip->next = config->ip_a_d;
513 config->ip_a_d = pip;
514 } else {
515 /* add to bottom A:form_IP config line */
516 Htaccess_IP *prev_IP = config->ip_a_d;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000517
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000518 if (prev_IP == NULL) {
519 config->ip_a_d = pip;
520 } else {
521 while (prev_IP->next)
522 prev_IP = prev_IP->next;
523 prev_IP->next = pip;
524 }
525 }
526 }
527 continue;
528 }
Denis Vlasenko55a99402006-09-30 20:41:44 +0000529#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000530 if (*p0 == '/') {
531 /* make full path from httpd root / curent_path / config_line_path */
532 cf = flag == SUBDIR_PARSE ? path : "";
533 p0 = malloc(strlen(cf) + (c - buf) + 2 + strlen(c));
534 if (p0 == NULL)
535 continue;
536 c[-1] = 0;
537 sprintf(p0, "/%s%s", cf, buf);
538
539 /* another call bb_simplify_path */
540 cf = p = p0;
541
542 do {
543 if (*p == '/') {
544 if (*cf == '/') { /* skip duplicate (or initial) slash */
545 continue;
546 } else if (*cf == '.') {
547 if (cf[1] == '/' || cf[1] == 0) { /* remove extra '.' */
548 continue;
549 } else if ((cf[1] == '.') && (cf[2] == '/' || cf[2] == 0)) {
550 ++cf;
551 if (p > p0) {
Denis Vlasenko92758142006-10-03 19:56:34 +0000552 while (*--p != '/') /* omit previous dir */;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000553 }
554 continue;
555 }
556 }
557 }
558 *++p = *cf;
559 } while (*++cf);
560
561 if ((p == p0) || (*p != '/')) { /* not a trailing slash */
562 ++p; /* so keep last character */
563 }
564 *p = 0;
565 sprintf(p0, "%s:%s", p0, c);
566 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000567#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000568
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000569#if ENABLE_FEATURE_HTTPD_BASIC_AUTH \
570 || ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES \
571 || ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000572 /* storing current config line */
573 cur = calloc(1, sizeof(Htaccess) + strlen(p0));
574 if (cur) {
575 cf = strcpy(cur->before_colon, p0);
576 c = strchr(cf, ':');
577 *c++ = 0;
578 cur->after_colon = c;
Denis Vlasenko55a99402006-09-30 20:41:44 +0000579#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000580 if (*cf == '.') {
581 /* config .mime line move top for overwrite previous */
582 cur->next = config->mime_a;
583 config->mime_a = cur;
584 continue;
585 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000586#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000587#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000588 if (*cf == '*' && cf[1] == '.') {
589 /* config script interpreter line move top for overwrite previous */
590 cur->next = config->script_i;
591 config->script_i = cur;
592 continue;
593 }
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000594#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000595#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000596 free(p0);
597 if (prev == NULL) {
598 /* first line */
599 config->auth = prev = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000600 } else {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000601 /* sort path, if current lenght eq or bigger then move up */
602 Htaccess *prev_hti = config->auth;
603 size_t l = strlen(cf);
604 Htaccess *hti;
605
606 for (hti = prev_hti; hti; hti = hti->next) {
607 if (l >= strlen(hti->before_colon)) {
608 /* insert before hti */
609 cur->next = hti;
610 if (prev_hti != hti) {
611 prev_hti->next = cur;
612 } else {
613 /* insert as top */
614 config->auth = cur;
615 }
616 break;
617 }
618 if (prev_hti != hti)
619 prev_hti = prev_hti->next;
620 }
621 if (!hti) { /* not inserted, add to bottom */
622 prev->next = cur;
623 prev = cur;
624 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000625 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000626#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000627 }
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000628#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000629 }
630 fclose(f);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000631}
632
Denis Vlasenko55a99402006-09-30 20:41:44 +0000633#if ENABLE_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000634/****************************************************************************
635 *
636 > $Function: encodeString()
637 *
638 * $Description: Given a string, html encode special characters.
639 * This is used for the -e command line option to provide an easy way
640 * for scripts to encode result data without confusing browsers. The
641 * returned string pointer is memory allocated by malloc().
642 *
643 * $Parameters:
644 * (const char *) string . . The first string to encode.
645 *
646 * $Return: (char *) . . . .. . . A pointer to the encoded string.
647 *
648 * $Errors: Returns a null string ("") if memory is not available.
649 *
650 ****************************************************************************/
651static char *encodeString(const char *string)
652{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000653 /* take the simple route and encode everything */
654 /* could possibly scan once to get length. */
655 int len = strlen(string);
656 char *out = malloc(len * 6 + 1);
657 char *p = out;
658 char ch;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000659
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000660 if (!out) return "";
661 while ((ch = *string++)) {
662 // very simple check for what to encode
663 if (isalnum(ch)) *p++ = ch;
664 else p += sprintf(p, "&#%d;", (unsigned char) ch);
665 }
666 *p = 0;
667 return out;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000668}
Denis Vlasenkob3a07152006-11-16 18:04:43 +0000669#endif /* FEATURE_HTTPD_ENCODE_URL_STR */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000670
671/****************************************************************************
672 *
673 > $Function: decodeString()
674 *
675 * $Description: Given a URL encoded string, convert it to plain ascii.
676 * Since decoding always makes strings smaller, the decode is done in-place.
677 * Thus, callers should strdup() the argument if they do not want the
678 * argument modified. The return is the original pointer, allowing this
679 * function to be easily used as arguments to other functions.
680 *
681 * $Parameters:
682 * (char *) string . . . The first string to decode.
Denis Vlasenkoa35c9e92006-11-29 15:58:50 +0000683 * (int) option_d . . 1 if called for httpd -d
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000684 *
685 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
686 *
687 * $Errors: None
688 *
689 ****************************************************************************/
Denis Vlasenkoa35c9e92006-11-29 15:58:50 +0000690static char *decodeString(char *orig, int option_d)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000691{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000692 /* note that decoded string is always shorter than original */
693 char *string = orig;
694 char *ptr = string;
Denis Vlasenkoa35c9e92006-11-29 15:58:50 +0000695 char c;
Eric Andersena3bb3e62003-06-26 09:05:32 +0000696
Denis Vlasenkoa35c9e92006-11-29 15:58:50 +0000697 while ((c = *ptr++) != '\0') {
698 unsigned value1, value2;
699
700 if (option_d && c == '+') {
Denis Vlasenko601ae132006-11-28 23:37:46 +0000701 *string++ = ' ';
Denis Vlasenkoa35c9e92006-11-29 15:58:50 +0000702 continue;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000703 }
Denis Vlasenkoa35c9e92006-11-29 15:58:50 +0000704 if (c != '%') {
705 *string++ = c;
706 continue;
707 }
708 if (sscanf(ptr, "%1X", &value1) != 1
709 || sscanf(ptr+1, "%1X", &value2) != 1
710 ) {
711 if (!option_d)
712 return NULL;
713 *string++ = '%';
714 continue;
715 }
716 value1 = value1 * 16 + value2;
717 if (!option_d && (value1 == '/' || value1 == '\0')) {
718 /* caller takes it as indication of invalid
719 * (dangerous wrt exploits) chars */
720 return orig + 1;
721 }
722 *string++ = value1;
723 ptr += 2;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000724 }
725 *string = '\0';
726 return orig;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000727}
728
729
Denis Vlasenko55a99402006-09-30 20:41:44 +0000730#if ENABLE_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000731/****************************************************************************
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000732 * setenv helpers
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000733 ****************************************************************************/
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000734static void setenv1(const char *name, const char *value)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000735{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000736 if (!value)
737 value = "";
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000738 setenv(name, value, 1);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000739}
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000740static void setenv_long(const char *name, long value)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000741{
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000742 char buf[sizeof(value)*3 + 1];
743 sprintf(buf, "%ld", value);
744 setenv(name, buf, 1);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000745}
Eric Andersena3bb3e62003-06-26 09:05:32 +0000746#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000747
Denis Vlasenko55a99402006-09-30 20:41:44 +0000748#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000749/****************************************************************************
750 *
751 > $Function: decodeBase64()
752 *
753 > $Description: Decode a base 64 data stream as per rfc1521.
754 * Note that the rfc states that none base64 chars are to be ignored.
755 * Since the decode always results in a shorter size than the input, it is
756 * OK to pass the input arg as an output arg.
757 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000758 * $Parameter:
759 * (char *) Data . . . . A pointer to a base64 encoded string.
760 * Where to place the decoded data.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000761 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000762 * $Return: void
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000763 *
764 * $Errors: None
765 *
766 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000767static void decodeBase64(char *Data)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000768{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000769
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000770 const unsigned char *in = (const unsigned char *)Data;
771 // The decoded size will be at most 3/4 the size of the encoded
772 unsigned long ch = 0;
773 int i = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000774
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000775 while (*in) {
776 int t = *in++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000777
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000778 if (t >= '0' && t <= '9')
779 t = t - '0' + 52;
780 else if (t >= 'A' && t <= 'Z')
781 t = t - 'A';
782 else if (t >= 'a' && t <= 'z')
783 t = t - 'a' + 26;
784 else if (t == '+')
785 t = 62;
786 else if (t == '/')
787 t = 63;
788 else if (t == '=')
789 t = 0;
790 else
791 continue;
Glenn L McGrath874e3382003-05-14 12:11:36 +0000792
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000793 ch = (ch << 6) | t;
794 i++;
795 if (i == 4) {
796 *Data++ = (char) (ch >> 16);
797 *Data++ = (char) (ch >> 8);
798 *Data++ = (char) ch;
799 i = 0;
800 }
801 }
802 *Data = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000803}
804#endif
805
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000806
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000807/****************************************************************************
808 *
809 > $Function: openServer()
810 *
811 * $Description: create a listen server socket on the designated port.
812 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000813 * $Return: (int) . . . A connection socket. -1 for errors.
814 *
815 * $Errors: None
816 *
817 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000818static int openServer(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000819{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000820 struct sockaddr_in lsocket;
821 int fd;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000822
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000823 /* create the socket right now */
824 /* inet_addr() returns a value that is already in network order */
825 memset(&lsocket, 0, sizeof(lsocket));
826 lsocket.sin_family = AF_INET;
827 lsocket.sin_addr.s_addr = INADDR_ANY;
828 lsocket.sin_port = htons(config->port);
829 fd = xsocket(AF_INET, SOCK_STREAM, 0);
830 /* tell the OS it's OK to reuse a previous address even though */
831 /* it may still be in a close down state. Allows bind to succeed. */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000832#ifdef SO_REUSEPORT
Denis Vlasenko48237b02006-11-22 23:22:06 +0000833 {
834 static const int on = 1;
835 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT,
836 (void *)&on, sizeof(on));
837 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000838#else
Denis Vlasenko48237b02006-11-22 23:22:06 +0000839 setsockopt_reuseaddr(fd);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000840#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000841 xbind(fd, (struct sockaddr *)&lsocket, sizeof(lsocket));
842 xlisten(fd, 9);
843 signal(SIGCHLD, SIG_IGN); /* prevent zombie (defunct) processes */
844 return fd;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000845}
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000846
847/****************************************************************************
848 *
849 > $Function: sendHeaders()
850 *
851 * $Description: Create and send HTTP response headers.
852 * The arguments are combined and sent as one write operation. Note that
853 * IE will puke big-time if the headers are not sent in one packet and the
Glenn L McGrath06e95652003-02-09 06:51:14 +0000854 * second packet is delayed for any reason.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000855 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000856 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000857 * (HttpResponseNum) responseNum . . . The result code to send.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000858 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000859 * $Return: (int) . . . . writing errors
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000860 *
861 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000862static int sendHeaders(HttpResponseNum responseNum)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000863{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000864 char *buf = config->buf;
865 const char *responseString = "";
866 const char *infoString = 0;
867 const char *mime_type;
868 unsigned int i;
869 time_t timer = time(0);
870 char timeStr[80];
871 int len;
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +0000872 enum {
873 numNames = sizeof(httpResponseNames) / sizeof(httpResponseNames[0])
874 };
Glenn L McGrath06e95652003-02-09 06:51:14 +0000875
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +0000876 for (i = 0; i < numNames; i++) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000877 if (httpResponseNames[i].type == responseNum) {
878 responseString = httpResponseNames[i].name;
879 infoString = httpResponseNames[i].info;
880 break;
881 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000882 }
883 /* error message is HTML */
884 mime_type = responseNum == HTTP_OK ?
Denis Vlasenkod4f3d1a2006-11-16 16:20:12 +0000885 config->found_mime_type : "text/html";
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000886
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000887 /* emit the current date */
888 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&timer));
889 len = sprintf(buf,
890 "HTTP/1.0 %d %s\r\nContent-type: %s\r\n"
891 "Date: %s\r\nConnection: close\r\n",
892 responseNum, responseString, mime_type, timeStr);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000893
Denis Vlasenko55a99402006-09-30 20:41:44 +0000894#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000895 if (responseNum == HTTP_UNAUTHORIZED) {
896 len += sprintf(buf+len, "WWW-Authenticate: Basic realm=\"%s\"\r\n",
897 config->realm);
898 }
Glenn L McGrath3d2405c2003-02-10 22:28:21 +0000899#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000900 if (responseNum == HTTP_MOVED_TEMPORARILY) {
901 len += sprintf(buf+len, "Location: %s/%s%s\r\n",
Denis Vlasenkod4f3d1a2006-11-16 16:20:12 +0000902 config->found_moved_temporarily,
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000903 (config->query ? "?" : ""),
904 (config->query ? config->query : ""));
905 }
Eric Andersen07f2fea2004-10-08 08:03:29 +0000906
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000907 if (config->ContentLength != -1) { /* file */
908 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&config->last_mod));
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000909 len += sprintf(buf+len, "Last-Modified: %s\r\n%s %"OFF_FMT"d\r\n",
910 timeStr, "Content-length:", config->ContentLength);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000911 }
912 strcat(buf, "\r\n");
913 len += 2;
914 if (infoString) {
915 len += sprintf(buf+len,
916 "<HEAD><TITLE>%d %s</TITLE></HEAD>\n"
917 "<BODY><H1>%d %s</H1>\n%s\n</BODY>\n",
918 responseNum, responseString,
919 responseNum, responseString, infoString);
920 }
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +0000921 if (DEBUG)
922 fprintf(stderr, "headers: '%s'\n", buf);
Denis Vlasenko0871bc82006-11-16 16:17:02 +0000923 return full_write(config->accepted_socket, buf, len);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000924}
925
926/****************************************************************************
927 *
928 > $Function: getLine()
929 *
930 * $Description: Read from the socket until an end of line char found.
931 *
932 * Characters are read one at a time until an eol sequence is found.
933 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000934 * $Return: (int) . . . . number of characters read. -1 if error.
935 *
936 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000937static int getLine(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000938{
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +0000939 int count = 0;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000940 char *buf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000941
Denis Vlasenko0871bc82006-11-16 16:17:02 +0000942 while (read(config->accepted_socket, buf + count, 1) == 1) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000943 if (buf[count] == '\r') continue;
944 if (buf[count] == '\n') {
945 buf[count] = 0;
946 return count;
947 }
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +0000948 if (count < (MAX_MEMORY_BUFF-1)) /* check overflow */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000949 count++;
950 }
951 if (count) return count;
952 else return -1;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000953}
954
Denis Vlasenko55a99402006-09-30 20:41:44 +0000955#if ENABLE_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000956/****************************************************************************
957 *
958 > $Function: sendCgi()
959 *
960 * $Description: Execute a CGI script and send it's stdout back
961 *
962 * Environment variables are set up and the script is invoked with pipes
963 * for stdin/stdout. If a post is being done the script is fed the POST
964 * data in addition to setting the QUERY_STRING variable (for GETs or POSTs).
965 *
966 * $Parameters:
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000967 * (const char *) url . . . . . . The requested URL (with leading /).
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000968 * (int bodyLen) . . . . . . . . Length of the post body.
969 * (const char *cookie) . . . . . For set HTTP_COOKIE.
970 * (const char *content_type) . . For set CONTENT_TYPE.
Glenn L McGrath06e95652003-02-09 06:51:14 +0000971
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000972 *
973 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
974 *
975 * $Errors: None
976 *
977 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000978static int sendCgi(const char *url,
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +0000979 const char *request, int bodyLen, const char *cookie,
980 const char *content_type)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000981{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000982 int fromCgi[2]; /* pipe for reading data from CGI */
983 int toCgi[2]; /* pipe for sending data to CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000984
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000985 static char * argp[] = { 0, 0 };
986 int pid = 0;
987 int inFd;
988 int outFd;
989 int firstLine = 1;
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +0000990 int status;
991 size_t post_readed_size, post_readed_idx;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000992
Denis Vlasenko0bb993f2006-11-21 00:06:28 +0000993 if (pipe(fromCgi) != 0)
994 return 0;
995 if (pipe(toCgi) != 0)
996 return 0;
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +0000997
Denis Vlasenko0bb993f2006-11-21 00:06:28 +0000998 pid = fork();
999 if (pid < 0)
1000 return 0;
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001001
1002 if (!pid) {
1003 /* child process */
1004 char *script;
1005 char *purl = strdup(url);
1006 char realpath_buff[MAXPATHLEN];
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001007
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001008 if (purl == NULL)
1009 _exit(242);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001010
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001011 inFd = toCgi[0];
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001012 outFd = fromCgi[1];
Glenn L McGrath06e95652003-02-09 06:51:14 +00001013
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001014 dup2(inFd, 0); // replace stdin with the pipe
1015 dup2(outFd, 1); // replace stdout with the pipe
1016 if (!DEBUG)
1017 dup2(outFd, 2); // replace stderr with the pipe
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001018
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001019 close(toCgi[0]);
1020 close(toCgi[1]);
1021 close(fromCgi[0]);
1022 close(fromCgi[1]);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001023
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001024 close(config->accepted_socket);
1025 close(config->server_socket);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001026
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001027 /*
1028 * Find PATH_INFO.
1029 */
1030 script = purl;
1031 while ((script = strchr(script + 1, '/')) != NULL) {
1032 /* have script.cgi/PATH_INFO or dirs/script.cgi[/PATH_INFO] */
1033 struct stat sb;
Denis Vlasenko9f609292006-11-05 19:47:33 +00001034
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001035 *script = '\0';
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001036 if (is_directory(purl + 1, 1, &sb) == 0) {
1037 /* not directory, found script.cgi/PATH_INFO */
1038 *script = '/';
1039 break;
1040 }
1041 *script = '/'; /* is directory, find next '/' */
1042 }
1043 setenv1("PATH_INFO", script); /* set /PATH_INFO or "" */
1044 /* setenv1("PATH", getenv("PATH")); redundant */
1045 setenv1("REQUEST_METHOD", request);
1046 if (config->query) {
1047 char *uri = alloca(strlen(purl) + 2 + strlen(config->query));
1048 if (uri)
1049 sprintf(uri, "%s?%s", purl, config->query);
1050 setenv1("REQUEST_URI", uri);
1051 } else {
1052 setenv1("REQUEST_URI", purl);
1053 }
1054 if (script != NULL)
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001055 *script = '\0'; /* cut off /PATH_INFO */
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001056 /* SCRIPT_FILENAME required by PHP in CGI mode */
1057 if (!realpath(purl + 1, realpath_buff))
1058 goto error_execing_cgi;
1059 setenv1("SCRIPT_FILENAME", realpath_buff);
1060 /* set SCRIPT_NAME as full path: /cgi-bin/dirs/script.cgi */
1061 setenv1("SCRIPT_NAME", purl);
Denis Vlasenko428f7ae2006-11-21 21:35:14 +00001062 /* http://hoohoo.ncsa.uiuc.edu/cgi/env.html:
1063 * QUERY_STRING: The information which follows the ? in the URL
1064 * which referenced this script. This is the query information.
1065 * It should not be decoded in any fashion. This variable
1066 * should always be set when there is query information,
1067 * regardless of command line decoding. */
1068 /* (Older versions of bbox seemed to do some decoding) */
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001069 setenv1("QUERY_STRING", config->query);
1070 setenv1("SERVER_SOFTWARE", httpdVersion);
1071 putenv("SERVER_PROTOCOL=HTTP/1.0");
1072 putenv("GATEWAY_INTERFACE=CGI/1.1");
1073 setenv1("REMOTE_ADDR", config->rmt_ip_str);
1074#if ENABLE_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
1075 setenv_long("REMOTE_PORT", config->port);
1076#endif
1077 if (bodyLen)
1078 setenv_long("CONTENT_LENGTH", bodyLen);
1079 if (cookie)
1080 setenv1("HTTP_COOKIE", cookie);
1081 if (content_type)
1082 setenv1("CONTENT_TYPE", content_type);
1083#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
1084 if (config->remoteuser) {
1085 setenv1("REMOTE_USER", config->remoteuser);
1086 putenv("AUTH_TYPE=Basic");
1087 }
1088#endif
1089 if (config->referer)
1090 setenv1("HTTP_REFERER", config->referer);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001091
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001092 /* set execve argp[0] without path */
1093 argp[0] = strrchr(purl, '/') + 1;
1094 /* but script argp[0] must have absolute path and chdiring to this */
1095 script = strrchr(realpath_buff, '/');
1096 if (!script)
1097 goto error_execing_cgi;
1098 *script = '\0';
1099 if (chdir(realpath_buff) == 0) {
1100 // now run the program. If it fails,
1101 // use _exit() so no destructors
1102 // get called and make a mess.
1103#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
1104 char *interpr = NULL;
1105 char *suffix = strrchr(purl, '.');
1106
1107 if (suffix) {
1108 Htaccess *cur;
1109 for (cur = config->script_i; cur; cur = cur->next) {
1110 if (strcmp(cur->before_colon + 1, suffix) == 0) {
1111 interpr = cur->after_colon;
1112 break;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001113 }
1114 }
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001115 }
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001116#endif
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001117 *script = '/';
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001118#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001119 if (interpr)
1120 execv(interpr, argp);
1121 else
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001122#endif
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001123 execv(realpath_buff, argp);
1124 }
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001125 error_execing_cgi:
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001126 /* send to stdout (even if we are not from inetd) */
1127 config->accepted_socket = 1;
1128 sendHeaders(HTTP_NOT_FOUND);
1129 _exit(242);
1130 } /* end child */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001131
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001132 /* parent process */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001133
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001134 post_readed_size = 0;
1135 post_readed_idx = 0;
Denis Vlasenko5d148e22006-11-21 00:12:09 +00001136 inFd = fromCgi[0];
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001137 outFd = toCgi[1];
1138 close(fromCgi[1]);
1139 close(toCgi[0]);
1140 signal(SIGPIPE, SIG_IGN);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001141
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001142 while (1) {
1143 fd_set readSet;
1144 fd_set writeSet;
1145 char wbuf[128];
1146 int nfound;
1147 int count;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001148
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001149 FD_ZERO(&readSet);
1150 FD_ZERO(&writeSet);
1151 FD_SET(inFd, &readSet);
1152 if (bodyLen > 0 || post_readed_size > 0) {
1153 FD_SET(outFd, &writeSet);
1154 nfound = outFd > inFd ? outFd : inFd;
1155 if (post_readed_size == 0) {
1156 FD_SET(config->accepted_socket, &readSet);
1157 if (nfound < config->accepted_socket)
1158 nfound = config->accepted_socket;
1159 }
1160 /* Now wait on the set of sockets! */
1161 nfound = select(nfound + 1, &readSet, &writeSet, 0, NULL);
1162 } else {
1163 if (!bodyLen) {
1164 close(outFd);
1165 bodyLen = -1;
1166 }
1167 nfound = select(inFd + 1, &readSet, 0, 0, NULL);
1168 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001169
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001170 if (nfound <= 0) {
1171 if (waitpid(pid, &status, WNOHANG) > 0) {
1172 close(inFd);
1173 if (DEBUG && WIFEXITED(status))
1174 bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status));
1175 if (DEBUG && WIFSIGNALED(status))
1176 bb_error_msg("piped has exited with signal=%d", WTERMSIG(status));
1177 break;
1178 }
1179 } else if (post_readed_size > 0 && FD_ISSET(outFd, &writeSet)) {
1180 count = full_write(outFd, wbuf + post_readed_idx, post_readed_size);
1181 if (count > 0) {
1182 post_readed_size -= count;
1183 post_readed_idx += count;
1184 if (post_readed_size == 0)
1185 post_readed_idx = 0;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001186 } else {
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001187 post_readed_size = post_readed_idx = bodyLen = 0; /* broken pipe to CGI */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001188 }
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001189 } else if (bodyLen > 0 && post_readed_size == 0 && FD_ISSET(config->accepted_socket, &readSet)) {
1190 count = bodyLen > (int)sizeof(wbuf) ? (int)sizeof(wbuf) : bodyLen;
1191 count = safe_read(config->accepted_socket, wbuf, count);
1192 if (count > 0) {
1193 post_readed_size += count;
1194 bodyLen -= count;
1195 } else {
1196 bodyLen = 0; /* closed */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001197 }
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001198 }
1199 if (FD_ISSET(inFd, &readSet)) {
1200 int s = config->accepted_socket;
1201 char *rbuf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001202
Eric Andersen97a1de12004-08-26 22:22:50 +00001203#ifndef PIPE_BUF
1204# define PIPESIZE 4096 /* amount of buffering in a pipe */
1205#else
1206# define PIPESIZE PIPE_BUF
1207#endif
1208#if PIPESIZE >= MAX_MEMORY_BUFF
1209# error "PIPESIZE >= MAX_MEMORY_BUFF"
1210#endif
1211
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +00001212 /* There is something to read */
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001213 count = safe_read(inFd, rbuf, PIPESIZE);
1214 if (count == 0)
1215 break; /* closed */
1216 if (count > 0) {
1217 if (firstLine) {
1218 rbuf[count] = 0;
1219 /* check to see if the user script added headers */
1220 if (strncmp(rbuf, "HTTP/1.0 200 OK\r\n", 4) != 0) {
1221 full_write(s, "HTTP/1.0 200 OK\r\n", 17);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001222 }
Denis Vlasenko5d148e22006-11-21 00:12:09 +00001223 /* Sometimes CGI is writing to pipe in small chunks
1224 * and we don't see Content-type (because the read
1225 * is too short) and we emit bogus "text/plain"!
1226 * Is it a bug or CGI *has to* write it in one piece? */
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001227 if (strstr(rbuf, "ontent-") == 0) {
1228 full_write(s, "Content-type: text/plain\r\n\r\n", 28);
1229 }
1230 firstLine = 0;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001231 }
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001232 if (full_write(s, rbuf, count) != count)
1233 break;
1234
1235 if (DEBUG)
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +00001236 fprintf(stderr, "cgi read %d bytes: '%.*s'\n", count, count, rbuf);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001237 }
1238 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001239 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001240 return 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001241}
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001242#endif /* FEATURE_HTTPD_CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001243
1244/****************************************************************************
1245 *
1246 > $Function: sendFile()
1247 *
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001248 * $Description: Send a file response to a HTTP request
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001249 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00001250 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001251 * (const char *) url . . The URL requested.
1252 *
1253 * $Return: (int) . . . . . . Always 0.
1254 *
1255 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001256static int sendFile(const char *url)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001257{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001258 char * suffix;
1259 int f;
1260 const char * const * table;
1261 const char * try_suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001262
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001263 suffix = strrchr(url, '.');
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001264
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001265 for (table = suffixTable; *table; table += 2)
1266 if (suffix != NULL && (try_suffix = strstr(*table, suffix)) != 0) {
1267 try_suffix += strlen(suffix);
1268 if (*try_suffix == 0 || *try_suffix == '.')
1269 break;
1270 }
1271 /* also, if not found, set default as "application/octet-stream"; */
Denis Vlasenkod4f3d1a2006-11-16 16:20:12 +00001272 config->found_mime_type = table[1];
Denis Vlasenko55a99402006-09-30 20:41:44 +00001273#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001274 if (suffix) {
1275 Htaccess * cur;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001276
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001277 for (cur = config->mime_a; cur; cur = cur->next) {
1278 if (strcmp(cur->before_colon, suffix) == 0) {
Denis Vlasenkod4f3d1a2006-11-16 16:20:12 +00001279 config->found_mime_type = cur->after_colon;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001280 break;
1281 }
1282 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001283 }
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001284#endif /* FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001285
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001286 if (DEBUG)
1287 fprintf(stderr, "sending file '%s' content-type: %s\n",
Denis Vlasenkod4f3d1a2006-11-16 16:20:12 +00001288 url, config->found_mime_type);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001289
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001290 f = open(url, O_RDONLY);
1291 if (f >= 0) {
1292 int count;
1293 char *buf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001294
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001295 sendHeaders(HTTP_OK);
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001296 /* TODO: sendfile() */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001297 while ((count = full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001298 if (full_write(config->accepted_socket, buf, count) != count)
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001299 break;
1300 }
1301 close(f);
1302 } else {
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001303 if (DEBUG)
1304 bb_perror_msg("cannot open '%s'", url);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001305 sendHeaders(HTTP_NOT_FOUND);
1306 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001307
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001308 return 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001309}
1310
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001311static int checkPermIP(void)
1312{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001313 Htaccess_IP * cur;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001314
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001315 /* This could stand some work */
1316 for (cur = config->ip_a_d; cur; cur = cur->next) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001317#if DEBUG
1318 fprintf(stderr, "checkPermIP: '%s' ? ", config->rmt_ip_str);
1319#endif
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001320 if (DEBUG)
1321 fprintf(stderr, "'%u.%u.%u.%u/%u.%u.%u.%u'\n",
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001322 (unsigned char)(cur->ip >> 24),
1323 (unsigned char)(cur->ip >> 16),
1324 (unsigned char)(cur->ip >> 8),
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001325 cur->ip & 0xff,
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001326 (unsigned char)(cur->mask >> 24),
1327 (unsigned char)(cur->mask >> 16),
1328 (unsigned char)(cur->mask >> 8),
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001329 cur->mask & 0xff);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001330 if ((config->rmt_ip & cur->mask) == cur->ip)
1331 return cur->allow_deny == 'A'; /* Allow/Deny */
1332 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001333
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001334 /* if unconfigured, return 1 - access from all */
1335 return !config->flg_deny_all;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001336}
1337
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001338/****************************************************************************
1339 *
1340 > $Function: checkPerm()
1341 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001342 * $Description: Check the permission file for access password protected.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001343 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001344 * If config file isn't present, everything is allowed.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001345 * Entries are of the form you can see example from header source
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001346 *
1347 * $Parameters:
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001348 * (const char *) path . . . . The file path.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001349 * (const char *) request . . . User information to validate.
1350 *
1351 * $Return: (int) . . . . . . . . . 1 if request OK, 0 otherwise.
1352 *
1353 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001354
Denis Vlasenko55a99402006-09-30 20:41:44 +00001355#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001356static int checkPerm(const char *path, const char *request)
1357{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001358 Htaccess * cur;
1359 const char *p;
1360 const char *p0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001361
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001362 const char *prev = NULL;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001363
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001364 /* This could stand some work */
1365 for (cur = config->auth; cur; cur = cur->next) {
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001366 size_t l;
1367
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001368 p0 = cur->before_colon;
1369 if (prev != NULL && strcmp(prev, p0) != 0)
1370 continue; /* find next identical */
1371 p = cur->after_colon;
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001372 if (DEBUG)
1373 fprintf(stderr, "checkPerm: '%s' ? '%s'\n", p0, request);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001374
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001375 l = strlen(p0);
1376 if (strncmp(p0, path, l) == 0
1377 && (l == 1 || path[l] == '/' || path[l] == '\0')
1378 ) {
1379 char *u;
1380 /* path match found. Check request */
1381 /* for check next /path:user:password */
1382 prev = p0;
1383 u = strchr(request, ':');
1384 if (u == NULL) {
1385 /* bad request, ':' required */
1386 break;
1387 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001388
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001389 if (ENABLE_FEATURE_HTTPD_AUTH_MD5) {
1390 char *cipher;
1391 char *pp;
Eric Andersen35e643b2003-07-28 07:40:39 +00001392
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001393 if (strncmp(p, request, u-request) != 0) {
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001394 /* user uncompared */
1395 continue;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001396 }
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001397 pp = strchr(p, ':');
1398 if (pp && pp[1] == '$' && pp[2] == '1' &&
1399 pp[3] == '$' && pp[4]) {
1400 pp++;
1401 cipher = pw_encrypt(u+1, pp);
1402 if (strcmp(cipher, pp) == 0)
1403 goto set_remoteuser_var; /* Ok */
1404 /* unauthorized */
1405 continue;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001406 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001407 }
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001408
1409 if (strcmp(p, request) == 0) {
1410set_remoteuser_var:
1411 config->remoteuser = strdup(request);
1412 if (config->remoteuser)
1413 config->remoteuser[(u - request)] = 0;
1414 return 1; /* Ok */
1415 }
1416 /* unauthorized */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001417 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001418 } /* for */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001419
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001420 return prev == NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001421}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001422
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001423#endif /* FEATURE_HTTPD_BASIC_AUTH */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001424
Eric Andersen07f2fea2004-10-08 08:03:29 +00001425/****************************************************************************
1426 *
Mike Frysingerbb12d6f2006-01-03 23:59:01 +00001427 > $Function: handle_sigalrm()
Eric Andersen07f2fea2004-10-08 08:03:29 +00001428 *
Mike Frysingerbb12d6f2006-01-03 23:59:01 +00001429 * $Description: Handle timeouts
Eric Andersen07f2fea2004-10-08 08:03:29 +00001430 *
1431 ****************************************************************************/
1432
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001433static void handle_sigalrm(int sig)
Eric Andersen07f2fea2004-10-08 08:03:29 +00001434{
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001435 sendHeaders(HTTP_REQUEST_TIMEOUT);
1436 config->alarm_signaled = sig;
Eric Andersen07f2fea2004-10-08 08:03:29 +00001437}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001438
1439/****************************************************************************
1440 *
1441 > $Function: handleIncoming()
1442 *
1443 * $Description: Handle an incoming http request.
1444 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001445 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001446static void handleIncoming(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001447{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001448 char *buf = config->buf;
1449 char *url;
1450 char *purl;
1451 int blank = -1;
1452 char *test;
1453 struct stat sb;
1454 int ip_allowed;
Denis Vlasenko55a99402006-09-30 20:41:44 +00001455#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001456 const char *prequest = request_GET;
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001457 unsigned long length = 0;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001458 char *cookie = 0;
1459 char *content_type = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001460#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001461 fd_set s_fd;
1462 struct timeval tv;
1463 int retval;
1464 struct sigaction sa;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001465
Denis Vlasenko55a99402006-09-30 20:41:44 +00001466#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001467 int credentials = -1; /* if not required this is Ok */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001468#endif
1469
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001470 sa.sa_handler = handle_sigalrm;
1471 sigemptyset(&sa.sa_mask);
1472 sa.sa_flags = 0; /* no SA_RESTART */
1473 sigaction(SIGALRM, &sa, NULL);
Eric Andersen07f2fea2004-10-08 08:03:29 +00001474
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001475 do {
1476 int count;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001477
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001478 (void) alarm(TIMEOUT);
1479 if (getLine() <= 0)
1480 break; /* closed */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001481
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001482 purl = strpbrk(buf, " \t");
1483 if (purl == NULL) {
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001484 BAD_REQUEST:
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001485 sendHeaders(HTTP_BAD_REQUEST);
1486 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001487 }
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001488 *purl = '\0';
Denis Vlasenko55a99402006-09-30 20:41:44 +00001489#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001490 if (strcasecmp(buf, prequest) != 0) {
1491 prequest = "POST";
1492 if (strcasecmp(buf, prequest) != 0) {
1493 sendHeaders(HTTP_NOT_IMPLEMENTED);
1494 break;
1495 }
1496 }
1497#else
1498 if (strcasecmp(buf, request_GET) != 0) {
1499 sendHeaders(HTTP_NOT_IMPLEMENTED);
1500 break;
1501 }
1502#endif
1503 *purl = ' ';
1504 count = sscanf(purl, " %[^ ] HTTP/%d.%*d", buf, &blank);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001505
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001506 if (count < 1 || buf[0] != '/') {
1507 /* Garbled request/URL */
1508 goto BAD_REQUEST;
1509 }
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001510 url = alloca(strlen(buf) + sizeof("/index.html"));
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001511 if (url == NULL) {
1512 sendHeaders(HTTP_INTERNAL_SERVER_ERROR);
1513 break;
1514 }
1515 strcpy(url, buf);
1516 /* extract url args if present */
1517 test = strchr(url, '?');
Denis Vlasenko5d148e22006-11-21 00:12:09 +00001518 config->query = NULL;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001519 if (test) {
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001520 *test++ = '\0';
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001521 config->query = test;
1522 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001523
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001524 test = decodeString(url, 0);
1525 if (test == NULL)
1526 goto BAD_REQUEST;
Denis Vlasenkoa35c9e92006-11-29 15:58:50 +00001527 if (test == url+1) {
1528 /* '/' or NUL is encoded */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001529 sendHeaders(HTTP_NOT_FOUND);
1530 break;
1531 }
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001532
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001533 /* algorithm stolen from libbb bb_simplify_path(),
Denis Vlasenko55a99402006-09-30 20:41:44 +00001534 but don't strdup and reducing trailing slash and protect out root */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001535 purl = test = url;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001536 do {
1537 if (*purl == '/') {
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001538 /* skip duplicate (or initial) slash */
1539 if (*test == '/') {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001540 continue;
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001541 }
1542 if (*test == '.') {
1543 /* skip extra '.' */
1544 if (test[1] == '/' || test[1] == 0) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001545 continue;
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001546 } else
1547 /* '..': be careful */
1548 if (test[1] == '.' && (test[2] == '/' || test[2] == 0)) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001549 ++test;
1550 if (purl == url) {
1551 /* protect out root */
1552 goto BAD_REQUEST;
1553 }
Denis Vlasenko92758142006-10-03 19:56:34 +00001554 while (*--purl != '/') /* omit previous dir */;
1555 continue;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001556 }
1557 }
1558 }
1559 *++purl = *test;
1560 } while (*++test);
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001561 *++purl = '\0'; /* so keep last character */
1562 test = purl; /* end ptr */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001563
1564 /* If URL is directory, adding '/' */
1565 if (test[-1] != '/') {
1566 if (is_directory(url + 1, 1, &sb)) {
Denis Vlasenkod4f3d1a2006-11-16 16:20:12 +00001567 config->found_moved_temporarily = url;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001568 }
1569 }
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001570 if (DEBUG)
1571 fprintf(stderr, "url='%s', args=%s\n", url, config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001572
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001573 test = url;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001574 ip_allowed = checkPermIP();
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001575 while (ip_allowed && (test = strchr(test + 1, '/')) != NULL) {
1576 /* have path1/path2 */
1577 *test = '\0';
1578 if (is_directory(url + 1, 1, &sb)) {
1579 /* may be having subdir config */
1580 parse_conf(url + 1, SUBDIR_PARSE);
1581 ip_allowed = checkPermIP();
1582 }
1583 *test = '/';
1584 }
1585 if (blank >= 0) {
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001586 /* read until blank line for HTTP version specified, else parse immediate */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001587 while (1) {
1588 alarm(TIMEOUT);
1589 count = getLine();
1590 if (count <= 0)
1591 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001592
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001593 if (DEBUG)
1594 fprintf(stderr, "header: '%s'\n", buf);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001595
Denis Vlasenko55a99402006-09-30 20:41:44 +00001596#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001597 /* try and do our best to parse more lines */
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001598 if ((STRNCASECMP(buf, "Content-length:") == 0)) {
1599 /* extra read only for POST */
1600 if (prequest != request_GET) {
1601 test = buf + sizeof("Content-length:")-1;
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001602 if (!test[0])
1603 goto bail_out;
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001604 errno = 0;
1605 /* not using strtoul: it ignores leading munis! */
1606 length = strtol(test, &test, 10);
1607 /* length is "ulong", but we need to pass it to int later */
1608 /* so we check for negative or too large values in one go: */
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001609 /* (long -> ulong conv caused negatives to be seen as > INT_MAX) */
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001610 if (test[0] || errno || length > INT_MAX)
1611 goto bail_out;
1612 }
1613 } else if ((STRNCASECMP(buf, "Cookie:") == 0)) {
1614 cookie = strdup(skip_whitespace(buf + sizeof("Cookie:")-1));
1615 } else if ((STRNCASECMP(buf, "Content-Type:") == 0)) {
1616 content_type = strdup(skip_whitespace(buf + sizeof("Content-Type:")-1));
1617 } else if ((STRNCASECMP(buf, "Referer:") == 0)) {
1618 config->referer = strdup(skip_whitespace(buf + sizeof("Referer:")-1));
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001619 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001620#endif
1621
Denis Vlasenko55a99402006-09-30 20:41:44 +00001622#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001623 if (STRNCASECMP(buf, "Authorization:") == 0) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001624 /* We only allow Basic credentials.
1625 * It shows up as "Authorization: Basic <userid:password>" where
1626 * the userid:password is base64 encoded.
1627 */
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001628 test = skip_whitespace(buf + sizeof("Authorization:")-1);
1629 if (STRNCASECMP(test, "Basic") != 0)
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001630 continue;
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001631 test += sizeof("Basic")-1;
1632 /* decodeBase64() skips whitespace itself */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001633 decodeBase64(test);
1634 credentials = checkPerm(url, test);
1635 }
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001636#endif /* FEATURE_HTTPD_BASIC_AUTH */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001637
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001638 } /* while extra header reading */
1639 }
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001640 alarm(0);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001641 if (config->alarm_signaled)
1642 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001643
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001644 if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) {
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001645 /* protect listing [/path]/httpd_conf or IP deny */
Denis Vlasenko55a99402006-09-30 20:41:44 +00001646#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001647 FORBIDDEN: /* protect listing /cgi-bin */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001648#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001649 sendHeaders(HTTP_FORBIDDEN);
1650 break;
1651 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001652
Denis Vlasenko55a99402006-09-30 20:41:44 +00001653#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001654 if (credentials <= 0 && checkPerm(url, ":") == 0) {
1655 sendHeaders(HTTP_UNAUTHORIZED);
1656 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001657 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001658#endif
1659
Denis Vlasenkod4f3d1a2006-11-16 16:20:12 +00001660 if (config->found_moved_temporarily) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001661 sendHeaders(HTTP_MOVED_TEMPORARILY);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001662 /* clear unforked memory flag */
Denis Vlasenkod4f3d1a2006-11-16 16:20:12 +00001663 config->found_moved_temporarily = NULL;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001664 break;
1665 }
1666
1667 test = url + 1; /* skip first '/' */
1668
Denis Vlasenko55a99402006-09-30 20:41:44 +00001669#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001670 if (strncmp(test, "cgi-bin", 7) == 0) {
1671 if (test[7] == '/' && test[8] == 0)
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001672 goto FORBIDDEN; /* protect listing cgi-bin/ */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001673 sendCgi(url, prequest, length, cookie, content_type);
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001674 break;
1675 }
1676 if (prequest != request_GET) {
1677 sendHeaders(HTTP_NOT_IMPLEMENTED);
1678 break;
1679 }
Denis Vlasenko5d148e22006-11-21 00:12:09 +00001680#endif /* FEATURE_HTTPD_CGI */
1681 if (purl[-1] == '/')
1682 strcpy(purl, "index.html");
1683 if (stat(test, &sb) == 0) {
1684 /* It's a dir URL and there is index.html */
1685 config->ContentLength = sb.st_size;
1686 config->last_mod = sb.st_mtime;
1687 }
1688#if ENABLE_FEATURE_HTTPD_CGI
1689 else if (purl[-1] == '/') {
1690 /* It's a dir URL and there is no index.html
1691 * Try cgi-bin/index.cgi */
1692 if (access("/cgi-bin/index.cgi"+1, X_OK) == 0) {
1693 purl[0] = '\0';
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001694 config->query = url;
1695 sendCgi("/cgi-bin/index.cgi", prequest, length, cookie, content_type);
1696 break;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001697 }
1698 }
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001699#endif /* FEATURE_HTTPD_CGI */
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001700 sendFile(test);
1701 config->ContentLength = -1;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001702 } while (0);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001703
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001704#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001705 bail_out:
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001706#endif
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001707
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001708 if (DEBUG)
1709 fprintf(stderr, "closing socket\n\n");
1710#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001711 free(cookie);
1712 free(content_type);
Denis Vlasenkoa5342b42006-11-17 18:26:57 +00001713 free(config->referer);
1714 config->referer = NULL;
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001715# if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenkoa5342b42006-11-17 18:26:57 +00001716 free(config->remoteuser);
1717 config->remoteuser = NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001718# endif
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001719#endif
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001720 shutdown(config->accepted_socket, SHUT_WR);
Eric Andersend8746cd2004-02-24 07:28:38 +00001721
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001722 /* Properly wait for remote to closed */
Denis Vlasenko55a99402006-09-30 20:41:44 +00001723 FD_ZERO(&s_fd);
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001724 FD_SET(config->accepted_socket, &s_fd);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001725
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001726 do {
Denis Vlasenko55a99402006-09-30 20:41:44 +00001727 tv.tv_sec = 2;
1728 tv.tv_usec = 0;
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001729 retval = select(config->accepted_socket + 1, &s_fd, NULL, NULL, &tv);
1730 } while (retval > 0 && read(config->accepted_socket, buf, sizeof(config->buf) > 0));
Eric Andersend8746cd2004-02-24 07:28:38 +00001731
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001732 shutdown(config->accepted_socket, SHUT_RD);
1733 /* In inetd case, we close fd 1 (stdout) here. We will exit soon anyway */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001734 close(config->accepted_socket);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001735}
1736
1737/****************************************************************************
1738 *
1739 > $Function: miniHttpd()
1740 *
1741 * $Description: The main http server function.
1742 *
1743 * Given an open socket fildes, listen for new connections and farm out
1744 * the processing as a forked process.
1745 *
1746 * $Parameters:
1747 * (int) server. . . The server socket fildes.
1748 *
1749 * $Return: (int) . . . . Always 0.
1750 *
1751 ****************************************************************************/
1752static int miniHttpd(int server)
1753{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001754 fd_set readfd, portfd;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001755
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001756 FD_ZERO(&portfd);
1757 FD_SET(server, &portfd);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001758
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001759 /* copy the ports we are watching to the readfd set */
1760 while (1) {
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001761 int on, s;
1762 socklen_t fromAddrLen;
1763 struct sockaddr_in fromAddr;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001764
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001765 /* Now wait INDEFINITELY on the set of sockets! */
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001766 readfd = portfd;
1767 if (select(server + 1, &readfd, 0, 0, 0) <= 0)
1768 continue;
1769 if (!FD_ISSET(server, &readfd))
1770 continue;
1771 fromAddrLen = sizeof(fromAddr);
1772 s = accept(server, (struct sockaddr *)&fromAddr, &fromAddrLen);
1773 if (s < 0)
1774 continue;
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001775 config->accepted_socket = s;
1776 config->rmt_ip = ntohl(fromAddr.sin_addr.s_addr);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001777#if ENABLE_FEATURE_HTTPD_CGI || DEBUG
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001778 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1779 (unsigned char)(config->rmt_ip >> 24),
1780 (unsigned char)(config->rmt_ip >> 16),
1781 (unsigned char)(config->rmt_ip >> 8),
1782 config->rmt_ip & 0xff);
1783 config->port = ntohs(fromAddr.sin_port);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001784#if DEBUG
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001785 bb_error_msg("connection from IP=%s, port %u",
1786 config->rmt_ip_str, config->port);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001787#endif
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001788#endif /* FEATURE_HTTPD_CGI */
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001789
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001790 /* set the KEEPALIVE option to cull dead connections */
1791 on = 1;
1792 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on));
Denis Vlasenko04291bc2006-11-21 10:15:25 +00001793
1794 if (DEBUG || fork() == 0) {
1795 /* child */
Denis Vlasenko55a99402006-09-30 20:41:44 +00001796#if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001797 /* protect reload config, may be confuse checking */
1798 signal(SIGHUP, SIG_IGN);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001799#endif
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001800 handleIncoming();
Denis Vlasenko04291bc2006-11-21 10:15:25 +00001801 if (!DEBUG)
1802 exit(0);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001803 }
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001804 close(s);
Denis Vlasenko04291bc2006-11-21 10:15:25 +00001805 } /* while (1) */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001806 return 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001807}
1808
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001809/* from inetd */
1810static int miniHttpd_inetd(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001811{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001812 struct sockaddr_in fromAddrLen;
Denis Vlasenko55a99402006-09-30 20:41:44 +00001813 socklen_t sinlen = sizeof(struct sockaddr_in);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001814
Denis Vlasenko55a99402006-09-30 20:41:44 +00001815 getpeername(0, (struct sockaddr *)&fromAddrLen, &sinlen);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001816 config->rmt_ip = ntohl(fromAddrLen.sin_addr.s_addr);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001817#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001818 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),
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001822 config->rmt_ip & 0xff);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001823#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001824 config->port = ntohs(fromAddrLen.sin_port);
1825 handleIncoming();
1826 return 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001827}
Glenn L McGrath06e95652003-02-09 06:51:14 +00001828
Denis Vlasenko55a99402006-09-30 20:41:44 +00001829#if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
Glenn L McGrath06e95652003-02-09 06:51:14 +00001830static void sighup_handler(int sig)
1831{
1832 /* set and reset */
1833 struct sigaction sa;
1834
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001835 parse_conf(default_path_httpd_conf, sig == SIGHUP ? SIGNALED_PARSE : FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001836 sa.sa_handler = sighup_handler;
1837 sigemptyset(&sa.sa_mask);
1838 sa.sa_flags = SA_RESTART;
1839 sigaction(SIGHUP, &sa, NULL);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001840}
1841#endif
1842
Denis Vlasenko9f609292006-11-05 19:47:33 +00001843enum {
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001844 c_opt_config_file = 0,
1845 d_opt_decode_url,
1846 h_opt_home_httpd,
1847 USE_FEATURE_HTTPD_ENCODE_URL_STR(e_opt_encode_url,)
Denis Vlasenko9f609292006-11-05 19:47:33 +00001848 USE_FEATURE_HTTPD_BASIC_AUTH( r_opt_realm ,)
1849 USE_FEATURE_HTTPD_AUTH_MD5( m_opt_md5 ,)
1850 USE_FEATURE_HTTPD_SETUID( u_opt_setuid ,)
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001851 p_opt_port ,
1852 p_opt_inetd ,
1853 p_opt_foreground,
Denis Vlasenko9f609292006-11-05 19:47:33 +00001854 OPT_CONFIG_FILE = 1 << c_opt_config_file,
1855 OPT_DECODE_URL = 1 << d_opt_decode_url,
1856 OPT_HOME_HTTPD = 1 << h_opt_home_httpd,
1857 OPT_ENCODE_URL = USE_FEATURE_HTTPD_ENCODE_URL_STR((1 << e_opt_encode_url)) + 0,
1858 OPT_REALM = USE_FEATURE_HTTPD_BASIC_AUTH( (1 << r_opt_realm )) + 0,
1859 OPT_MD5 = USE_FEATURE_HTTPD_AUTH_MD5( (1 << m_opt_md5 )) + 0,
1860 OPT_SETUID = USE_FEATURE_HTTPD_SETUID( (1 << u_opt_setuid )) + 0,
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001861 OPT_PORT = 1 << p_opt_port,
1862 OPT_INETD = 1 << p_opt_inetd,
1863 OPT_FOREGROUND = 1 << p_opt_foreground,
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001864};
Eric Andersena3bb3e62003-06-26 09:05:32 +00001865
Denis Vlasenko55a99402006-09-30 20:41:44 +00001866static const char httpd_opts[] = "c:d:h:"
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001867 USE_FEATURE_HTTPD_ENCODE_URL_STR("e:")
1868 USE_FEATURE_HTTPD_BASIC_AUTH("r:")
1869 USE_FEATURE_HTTPD_AUTH_MD5("m:")
1870 USE_FEATURE_HTTPD_SETUID("u:")
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001871 "p:if";
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001872
Eric Andersena3bb3e62003-06-26 09:05:32 +00001873
Glenn L McGrath06e95652003-02-09 06:51:14 +00001874int httpd_main(int argc, char *argv[])
Glenn L McGrath06e95652003-02-09 06:51:14 +00001875{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001876 unsigned opt;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001877 const char *home_httpd = home;
1878 char *url_for_decode;
1879 USE_FEATURE_HTTPD_ENCODE_URL_STR(const char *url_for_encode;)
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001880 const char *s_port;
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001881 USE_FEATURE_HTTPD_SETUID(const char *s_ugid = NULL;)
1882 USE_FEATURE_HTTPD_SETUID(struct bb_uidgid_t ugid;)
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001883 USE_FEATURE_HTTPD_AUTH_MD5(const char *pass;)
Eric Andersen35e643b2003-07-28 07:40:39 +00001884
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +00001885#if ENABLE_LOCALE_SUPPORT
1886 /* Undo busybox.c: we want to speak English in http (dates etc) */
1887 setlocale(LC_TIME, "C");
1888#endif
1889
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001890 config = xzalloc(sizeof(*config));
Denis Vlasenko55a99402006-09-30 20:41:44 +00001891#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001892 config->realm = "Web Server Authentication";
Glenn L McGrath06e95652003-02-09 06:51:14 +00001893#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001894 config->port = 80;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001895 config->ContentLength = -1;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001896
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001897 opt = getopt32(argc, argv, httpd_opts,
Eric Andersena3bb3e62003-06-26 09:05:32 +00001898 &(config->configFile), &url_for_decode, &home_httpd
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001899 USE_FEATURE_HTTPD_ENCODE_URL_STR(, &url_for_encode)
1900 USE_FEATURE_HTTPD_BASIC_AUTH(, &(config->realm))
1901 USE_FEATURE_HTTPD_AUTH_MD5(, &pass)
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001902 USE_FEATURE_HTTPD_SETUID(, &s_ugid)
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001903 , &s_port
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001904 );
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001905 if (opt & OPT_DECODE_URL) {
1906 printf("%s", decodeString(url_for_decode, 1));
1907 return 0;
1908 }
Denis Vlasenko55a99402006-09-30 20:41:44 +00001909#if ENABLE_FEATURE_HTTPD_ENCODE_URL_STR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001910 if (opt & OPT_ENCODE_URL) {
1911 printf("%s", encodeString(url_for_encode));
1912 return 0;
1913 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001914#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +00001915#if ENABLE_FEATURE_HTTPD_AUTH_MD5
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001916 if (opt & OPT_MD5) {
Denis Vlasenko55a99402006-09-30 20:41:44 +00001917 puts(pw_encrypt(pass, "$1$"));
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001918 return 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001919 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001920#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001921 if (opt & OPT_PORT)
Denis Vlasenko13858992006-10-08 12:49:22 +00001922 config->port = xatou16(s_port);
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001923
Denis Vlasenko55a99402006-09-30 20:41:44 +00001924#if ENABLE_FEATURE_HTTPD_SETUID
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001925 if (opt & OPT_SETUID) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001926 if (!get_uidgid(&ugid, s_ugid, 1))
1927 bb_error_msg_and_die("unrecognized user[:group] "
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001928 "name '%s'", s_ugid);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001929 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001930#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001931
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001932 xchdir(home_httpd);
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001933 if (!(opt & OPT_INETD)) {
1934 config->server_socket = openServer();
1935#if ENABLE_FEATURE_HTTPD_SETUID
1936 /* drop privileges */
1937 if (opt & OPT_SETUID) {
1938 if (ugid.gid != (gid_t)-1) {
1939 if (setgroups(1, &ugid.gid) == -1)
1940 bb_perror_msg_and_die("setgroups");
1941 xsetgid(ugid.gid);
1942 }
1943 xsetuid(ugid.uid);
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001944 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001945#endif
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001946 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001947
Denis Vlasenko55a99402006-09-30 20:41:44 +00001948#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001949 {
1950 char *p = getenv("PATH");
Denis Vlasenkoa35c9e92006-11-29 15:58:50 +00001951 p = xstrdup(p); /* if gets NULL, returns NULL */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001952 clearenv();
1953 if (p)
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001954 setenv1("PATH", p);
1955 if (!(opt & OPT_INETD))
1956 setenv_long("SERVER_PORT", config->port);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001957 }
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00001958#endif
1959
Denis Vlasenko55a99402006-09-30 20:41:44 +00001960#if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001961 sighup_handler(0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001962#else
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001963 parse_conf(default_path_httpd_conf, FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001964#endif
1965
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001966 if (opt & OPT_INETD)
1967 return miniHttpd_inetd();
1968
1969 if (!(opt & OPT_FOREGROUND))
1970 xdaemon(1, 0); /* don't change current directory */
Denis Vlasenko9f609292006-11-05 19:47:33 +00001971 return miniHttpd(config->server_socket);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001972}