blob: 0de60ba06895b28a16b01e42e11e7f83dffb1f70 [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 Vlasenko55a99402006-09-30 20:41:44 +0000341#if ENABLE_FEATURE_HTTPD_BASIC_AUTH || ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000342static void free_config_lines(Htaccess **pprev)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000343{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000344 Htaccess *prev = *pprev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000345
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000346 while (prev) {
347 Htaccess *cur = prev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000348
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000349 prev = cur->next;
350 free(cur);
351 }
352 *pprev = NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000353}
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000354#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000355
Glenn L McGrath06e95652003-02-09 06:51:14 +0000356/* flag */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000357#define FIRST_PARSE 0
358#define SUBDIR_PARSE 1
359#define SIGNALED_PARSE 2
360#define FIND_FROM_HTTPD_ROOT 3
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000361/****************************************************************************
362 *
363 > $Function: parse_conf()
364 *
365 * $Description: parse configuration file into in-memory linked list.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000366 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000367 * The first non-white character is examined to determine if the config line
368 * is one of the following:
369 * .ext:mime/type # new mime type not compiled into httpd
370 * [adAD]:from # ip address allow/deny, * for wildcard
371 * /path:user:pass # username/password
372 *
373 * Any previous IP rules are discarded.
374 * If the flag argument is not SUBDIR_PARSE then all /path and mime rules
375 * are also discarded. That is, previous settings are retained if flag is
376 * SUBDIR_PARSE.
377 *
378 * $Parameters:
379 * (const char *) path . . null for ip address checks, path for password
380 * checks.
381 * (int) flag . . . . . . the source of the parse request.
382 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000383 * $Return: (None)
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000384 *
385 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000386static void parse_conf(const char *path, int flag)
387{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000388 FILE *f;
Denis Vlasenko55a99402006-09-30 20:41:44 +0000389#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000390 Htaccess *prev, *cur;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000391#elif ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000392 Htaccess *cur;
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000393#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000394
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000395 const char *cf = config->configFile;
396 char buf[160];
397 char *p0 = NULL;
398 char *c, *p;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000399
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000400 /* free previous ip setup if present */
401 Htaccess_IP *pip = config->ip_a_d;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000402
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000403 while (pip) {
404 Htaccess_IP *cur_ipl = pip;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000405
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000406 pip = cur_ipl->next;
407 free(cur_ipl);
408 }
409 config->ip_a_d = NULL;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000410
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000411 config->flg_deny_all = 0;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000412
Denis Vlasenko55a99402006-09-30 20:41:44 +0000413#if ENABLE_FEATURE_HTTPD_BASIC_AUTH || ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES || ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000414 /* retain previous auth and mime config only for subdir parse */
415 if (flag != SUBDIR_PARSE) {
Denis Vlasenko55a99402006-09-30 20:41:44 +0000416#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000417 free_config_lines(&config->auth);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000418#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000419#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000420 free_config_lines(&config->mime_a);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000421#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000422#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000423 free_config_lines(&config->script_i);
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000424#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000425 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000426#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000427
428 if (flag == SUBDIR_PARSE || cf == NULL) {
429 cf = alloca(strlen(path) + sizeof(httpd_conf) + 2);
430 if (cf == NULL) {
431 if (flag == FIRST_PARSE)
432 bb_error_msg_and_die(bb_msg_memory_exhausted);
433 return;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000434 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000435 sprintf((char *)cf, "%s/%s", path, httpd_conf);
Glenn L McGrath393183d2003-05-26 14:07:50 +0000436 }
437
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000438 while ((f = fopen(cf, "r")) == NULL) {
439 if (flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) {
440 /* config file not found, no changes to config */
441 return;
442 }
443 if (config->configFile && flag == FIRST_PARSE) /* if -c option given */
444 bb_perror_msg_and_die("%s", cf);
445 flag = FIND_FROM_HTTPD_ROOT;
446 cf = httpd_conf;
447 }
448
Denis Vlasenko55a99402006-09-30 20:41:44 +0000449#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000450 prev = config->auth;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000451#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000452 /* This could stand some work */
Denis Vlasenko55a99402006-09-30 20:41:44 +0000453 while ((p0 = fgets(buf, sizeof(buf), f)) != NULL) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000454 c = NULL;
455 for (p = p0; *p0 != 0 && *p0 != '#'; p0++) {
456 if (!isspace(*p0)) {
457 *p++ = *p0;
458 if (*p0 == ':' && c == NULL)
459 c = p;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000460 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000461 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000462 *p = 0;
463
464 /* test for empty or strange line */
465 if (c == NULL || *c == 0)
466 continue;
467 p0 = buf;
468 if (*p0 == 'd')
469 *p0 = 'D';
470 if (*c == '*') {
471 if (*p0 == 'D') {
472 /* memorize deny all */
473 config->flg_deny_all++;
474 }
475 /* skip default other "word:*" config lines */
476 continue;
477 }
478
479 if (*p0 == 'a')
480 *p0 = 'A';
481 else if (*p0 != 'D' && *p0 != 'A'
Denis Vlasenko55a99402006-09-30 20:41:44 +0000482#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000483 && *p0 != '/'
484#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000485#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000486 && *p0 != '.'
487#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000488#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000489 && *p0 != '*'
490#endif
491 )
492 continue;
493 if (*p0 == 'A' || *p0 == 'D') {
494 /* storing current config IP line */
495 pip = calloc(1, sizeof(Htaccess_IP));
496 if (pip) {
Denis Vlasenko55a99402006-09-30 20:41:44 +0000497 if (scan_ip_mask(c, &(pip->ip), &(pip->mask))) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000498 /* syntax IP{/mask} error detected, protect all */
499 *p0 = 'D';
500 pip->mask = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000501 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000502 pip->allow_deny = *p0;
503 if (*p0 == 'D') {
504 /* Deny:form_IP move top */
505 pip->next = config->ip_a_d;
506 config->ip_a_d = pip;
507 } else {
508 /* add to bottom A:form_IP config line */
509 Htaccess_IP *prev_IP = config->ip_a_d;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000510
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000511 if (prev_IP == NULL) {
512 config->ip_a_d = pip;
513 } else {
514 while (prev_IP->next)
515 prev_IP = prev_IP->next;
516 prev_IP->next = pip;
517 }
518 }
519 }
520 continue;
521 }
Denis Vlasenko55a99402006-09-30 20:41:44 +0000522#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000523 if (*p0 == '/') {
524 /* make full path from httpd root / curent_path / config_line_path */
525 cf = flag == SUBDIR_PARSE ? path : "";
526 p0 = malloc(strlen(cf) + (c - buf) + 2 + strlen(c));
527 if (p0 == NULL)
528 continue;
529 c[-1] = 0;
530 sprintf(p0, "/%s%s", cf, buf);
531
532 /* another call bb_simplify_path */
533 cf = p = p0;
534
535 do {
536 if (*p == '/') {
537 if (*cf == '/') { /* skip duplicate (or initial) slash */
538 continue;
539 } else if (*cf == '.') {
540 if (cf[1] == '/' || cf[1] == 0) { /* remove extra '.' */
541 continue;
542 } else if ((cf[1] == '.') && (cf[2] == '/' || cf[2] == 0)) {
543 ++cf;
544 if (p > p0) {
Denis Vlasenko92758142006-10-03 19:56:34 +0000545 while (*--p != '/') /* omit previous dir */;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000546 }
547 continue;
548 }
549 }
550 }
551 *++p = *cf;
552 } while (*++cf);
553
554 if ((p == p0) || (*p != '/')) { /* not a trailing slash */
555 ++p; /* so keep last character */
556 }
557 *p = 0;
558 sprintf(p0, "%s:%s", p0, c);
559 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000560#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000561
Denis Vlasenko55a99402006-09-30 20:41:44 +0000562#if ENABLE_FEATURE_HTTPD_BASIC_AUTH || ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES || ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000563 /* storing current config line */
564 cur = calloc(1, sizeof(Htaccess) + strlen(p0));
565 if (cur) {
566 cf = strcpy(cur->before_colon, p0);
567 c = strchr(cf, ':');
568 *c++ = 0;
569 cur->after_colon = c;
Denis Vlasenko55a99402006-09-30 20:41:44 +0000570#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000571 if (*cf == '.') {
572 /* config .mime line move top for overwrite previous */
573 cur->next = config->mime_a;
574 config->mime_a = cur;
575 continue;
576 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000577#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000578#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000579 if (*cf == '*' && cf[1] == '.') {
580 /* config script interpreter line move top for overwrite previous */
581 cur->next = config->script_i;
582 config->script_i = cur;
583 continue;
584 }
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000585#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000586#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000587 free(p0);
588 if (prev == NULL) {
589 /* first line */
590 config->auth = prev = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000591 } else {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000592 /* sort path, if current lenght eq or bigger then move up */
593 Htaccess *prev_hti = config->auth;
594 size_t l = strlen(cf);
595 Htaccess *hti;
596
597 for (hti = prev_hti; hti; hti = hti->next) {
598 if (l >= strlen(hti->before_colon)) {
599 /* insert before hti */
600 cur->next = hti;
601 if (prev_hti != hti) {
602 prev_hti->next = cur;
603 } else {
604 /* insert as top */
605 config->auth = cur;
606 }
607 break;
608 }
609 if (prev_hti != hti)
610 prev_hti = prev_hti->next;
611 }
612 if (!hti) { /* not inserted, add to bottom */
613 prev->next = cur;
614 prev = cur;
615 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000616 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000617#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000618 }
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000619#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000620 }
621 fclose(f);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000622}
623
Denis Vlasenko55a99402006-09-30 20:41:44 +0000624#if ENABLE_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000625/****************************************************************************
626 *
627 > $Function: encodeString()
628 *
629 * $Description: Given a string, html encode special characters.
630 * This is used for the -e command line option to provide an easy way
631 * for scripts to encode result data without confusing browsers. The
632 * returned string pointer is memory allocated by malloc().
633 *
634 * $Parameters:
635 * (const char *) string . . The first string to encode.
636 *
637 * $Return: (char *) . . . .. . . A pointer to the encoded string.
638 *
639 * $Errors: Returns a null string ("") if memory is not available.
640 *
641 ****************************************************************************/
642static char *encodeString(const char *string)
643{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000644 /* take the simple route and encode everything */
645 /* could possibly scan once to get length. */
646 int len = strlen(string);
647 char *out = malloc(len * 6 + 1);
648 char *p = out;
649 char ch;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000650
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000651 if (!out) return "";
652 while ((ch = *string++)) {
653 // very simple check for what to encode
654 if (isalnum(ch)) *p++ = ch;
655 else p += sprintf(p, "&#%d;", (unsigned char) ch);
656 }
657 *p = 0;
658 return out;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000659}
Denis Vlasenkob3a07152006-11-16 18:04:43 +0000660#endif /* FEATURE_HTTPD_ENCODE_URL_STR */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000661
662/****************************************************************************
663 *
664 > $Function: decodeString()
665 *
666 * $Description: Given a URL encoded string, convert it to plain ascii.
667 * Since decoding always makes strings smaller, the decode is done in-place.
668 * Thus, callers should strdup() the argument if they do not want the
669 * argument modified. The return is the original pointer, allowing this
670 * function to be easily used as arguments to other functions.
671 *
672 * $Parameters:
673 * (char *) string . . . The first string to decode.
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000674 * (int) flag . . . 1 if require decode '+' as ' ' for CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000675 *
676 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
677 *
678 * $Errors: None
679 *
680 ****************************************************************************/
Eric Andersena3bb3e62003-06-26 09:05:32 +0000681static char *decodeString(char *orig, int flag_plus_to_space)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000682{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000683 /* note that decoded string is always shorter than original */
684 char *string = orig;
685 char *ptr = string;
Eric Andersena3bb3e62003-06-26 09:05:32 +0000686
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000687 while (*ptr) {
688 if (*ptr == '+' && flag_plus_to_space) { *string++ = ' '; ptr++; }
689 else if (*ptr != '%') *string++ = *ptr++;
690 else {
691 unsigned int value1, value2;
"Vladimir N. Oleynik"0bf67e82005-12-26 17:26:59 +0000692
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000693 ptr++;
694 if (sscanf(ptr, "%1X", &value1) != 1 ||
695 sscanf(ptr+1, "%1X", &value2) != 1) {
696 if (!flag_plus_to_space)
697 return NULL;
698 *string++ = '%';
699 } else {
700 value1 = value1 * 16 + value2;
701 if (value1 == '/' || value1 == 0)
702 return orig+1;
703 *string++ = value1;
704 ptr += 2;
705 }
706 }
707 }
708 *string = '\0';
709 return orig;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000710}
711
712
Denis Vlasenko55a99402006-09-30 20:41:44 +0000713#if ENABLE_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000714/****************************************************************************
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000715 * setenv helpers
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000716 ****************************************************************************/
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000717static void setenv1(const char *name, const char *value)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000718{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000719 if (!value)
720 value = "";
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000721 setenv(name, value, 1);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000722}
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000723static void setenv_long(const char *name, long value)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000724{
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000725 char buf[sizeof(value)*3 + 1];
726 sprintf(buf, "%ld", value);
727 setenv(name, buf, 1);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000728}
Eric Andersena3bb3e62003-06-26 09:05:32 +0000729#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000730
Denis Vlasenko55a99402006-09-30 20:41:44 +0000731#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000732/****************************************************************************
733 *
734 > $Function: decodeBase64()
735 *
736 > $Description: Decode a base 64 data stream as per rfc1521.
737 * Note that the rfc states that none base64 chars are to be ignored.
738 * Since the decode always results in a shorter size than the input, it is
739 * OK to pass the input arg as an output arg.
740 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000741 * $Parameter:
742 * (char *) Data . . . . A pointer to a base64 encoded string.
743 * Where to place the decoded data.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000744 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000745 * $Return: void
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000746 *
747 * $Errors: None
748 *
749 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000750static void decodeBase64(char *Data)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000751{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000752
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000753 const unsigned char *in = (const unsigned char *)Data;
754 // The decoded size will be at most 3/4 the size of the encoded
755 unsigned long ch = 0;
756 int i = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000757
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000758 while (*in) {
759 int t = *in++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000760
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000761 if (t >= '0' && t <= '9')
762 t = t - '0' + 52;
763 else if (t >= 'A' && t <= 'Z')
764 t = t - 'A';
765 else if (t >= 'a' && t <= 'z')
766 t = t - 'a' + 26;
767 else if (t == '+')
768 t = 62;
769 else if (t == '/')
770 t = 63;
771 else if (t == '=')
772 t = 0;
773 else
774 continue;
Glenn L McGrath874e3382003-05-14 12:11:36 +0000775
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000776 ch = (ch << 6) | t;
777 i++;
778 if (i == 4) {
779 *Data++ = (char) (ch >> 16);
780 *Data++ = (char) (ch >> 8);
781 *Data++ = (char) ch;
782 i = 0;
783 }
784 }
785 *Data = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000786}
787#endif
788
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000789
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000790/****************************************************************************
791 *
792 > $Function: openServer()
793 *
794 * $Description: create a listen server socket on the designated port.
795 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000796 * $Return: (int) . . . A connection socket. -1 for errors.
797 *
798 * $Errors: None
799 *
800 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000801static int openServer(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000802{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000803 struct sockaddr_in lsocket;
804 int fd;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000805
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000806 /* create the socket right now */
807 /* inet_addr() returns a value that is already in network order */
808 memset(&lsocket, 0, sizeof(lsocket));
809 lsocket.sin_family = AF_INET;
810 lsocket.sin_addr.s_addr = INADDR_ANY;
811 lsocket.sin_port = htons(config->port);
812 fd = xsocket(AF_INET, SOCK_STREAM, 0);
813 /* tell the OS it's OK to reuse a previous address even though */
814 /* it may still be in a close down state. Allows bind to succeed. */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000815#ifdef SO_REUSEPORT
Denis Vlasenko48237b02006-11-22 23:22:06 +0000816 {
817 static const int on = 1;
818 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT,
819 (void *)&on, sizeof(on));
820 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000821#else
Denis Vlasenko48237b02006-11-22 23:22:06 +0000822 setsockopt_reuseaddr(fd);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000823#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000824 xbind(fd, (struct sockaddr *)&lsocket, sizeof(lsocket));
825 xlisten(fd, 9);
826 signal(SIGCHLD, SIG_IGN); /* prevent zombie (defunct) processes */
827 return fd;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000828}
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000829
830/****************************************************************************
831 *
832 > $Function: sendHeaders()
833 *
834 * $Description: Create and send HTTP response headers.
835 * The arguments are combined and sent as one write operation. Note that
836 * IE will puke big-time if the headers are not sent in one packet and the
Glenn L McGrath06e95652003-02-09 06:51:14 +0000837 * second packet is delayed for any reason.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000838 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000839 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000840 * (HttpResponseNum) responseNum . . . The result code to send.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000841 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000842 * $Return: (int) . . . . writing errors
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000843 *
844 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000845static int sendHeaders(HttpResponseNum responseNum)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000846{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000847 char *buf = config->buf;
848 const char *responseString = "";
849 const char *infoString = 0;
850 const char *mime_type;
851 unsigned int i;
852 time_t timer = time(0);
853 char timeStr[80];
854 int len;
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +0000855 enum {
856 numNames = sizeof(httpResponseNames) / sizeof(httpResponseNames[0])
857 };
Glenn L McGrath06e95652003-02-09 06:51:14 +0000858
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +0000859 for (i = 0; i < numNames; i++) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000860 if (httpResponseNames[i].type == responseNum) {
861 responseString = httpResponseNames[i].name;
862 infoString = httpResponseNames[i].info;
863 break;
864 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000865 }
866 /* error message is HTML */
867 mime_type = responseNum == HTTP_OK ?
Denis Vlasenkod4f3d1a2006-11-16 16:20:12 +0000868 config->found_mime_type : "text/html";
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000869
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000870 /* emit the current date */
871 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&timer));
872 len = sprintf(buf,
873 "HTTP/1.0 %d %s\r\nContent-type: %s\r\n"
874 "Date: %s\r\nConnection: close\r\n",
875 responseNum, responseString, mime_type, timeStr);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000876
Denis Vlasenko55a99402006-09-30 20:41:44 +0000877#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000878 if (responseNum == HTTP_UNAUTHORIZED) {
879 len += sprintf(buf+len, "WWW-Authenticate: Basic realm=\"%s\"\r\n",
880 config->realm);
881 }
Glenn L McGrath3d2405c2003-02-10 22:28:21 +0000882#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000883 if (responseNum == HTTP_MOVED_TEMPORARILY) {
884 len += sprintf(buf+len, "Location: %s/%s%s\r\n",
Denis Vlasenkod4f3d1a2006-11-16 16:20:12 +0000885 config->found_moved_temporarily,
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000886 (config->query ? "?" : ""),
887 (config->query ? config->query : ""));
888 }
Eric Andersen07f2fea2004-10-08 08:03:29 +0000889
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000890 if (config->ContentLength != -1) { /* file */
891 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&config->last_mod));
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000892 len += sprintf(buf+len, "Last-Modified: %s\r\n%s %"OFF_FMT"d\r\n",
893 timeStr, "Content-length:", config->ContentLength);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000894 }
895 strcat(buf, "\r\n");
896 len += 2;
897 if (infoString) {
898 len += sprintf(buf+len,
899 "<HEAD><TITLE>%d %s</TITLE></HEAD>\n"
900 "<BODY><H1>%d %s</H1>\n%s\n</BODY>\n",
901 responseNum, responseString,
902 responseNum, responseString, infoString);
903 }
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +0000904 if (DEBUG)
905 fprintf(stderr, "headers: '%s'\n", buf);
Denis Vlasenko0871bc82006-11-16 16:17:02 +0000906 return full_write(config->accepted_socket, buf, len);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000907}
908
909/****************************************************************************
910 *
911 > $Function: getLine()
912 *
913 * $Description: Read from the socket until an end of line char found.
914 *
915 * Characters are read one at a time until an eol sequence is found.
916 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000917 * $Return: (int) . . . . number of characters read. -1 if error.
918 *
919 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000920static int getLine(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000921{
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +0000922 int count = 0;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000923 char *buf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000924
Denis Vlasenko0871bc82006-11-16 16:17:02 +0000925 while (read(config->accepted_socket, buf + count, 1) == 1) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000926 if (buf[count] == '\r') continue;
927 if (buf[count] == '\n') {
928 buf[count] = 0;
929 return count;
930 }
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +0000931 if (count < (MAX_MEMORY_BUFF-1)) /* check overflow */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000932 count++;
933 }
934 if (count) return count;
935 else return -1;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000936}
937
Denis Vlasenko55a99402006-09-30 20:41:44 +0000938#if ENABLE_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000939/****************************************************************************
940 *
941 > $Function: sendCgi()
942 *
943 * $Description: Execute a CGI script and send it's stdout back
944 *
945 * Environment variables are set up and the script is invoked with pipes
946 * for stdin/stdout. If a post is being done the script is fed the POST
947 * data in addition to setting the QUERY_STRING variable (for GETs or POSTs).
948 *
949 * $Parameters:
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000950 * (const char *) url . . . . . . The requested URL (with leading /).
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000951 * (int bodyLen) . . . . . . . . Length of the post body.
952 * (const char *cookie) . . . . . For set HTTP_COOKIE.
953 * (const char *content_type) . . For set CONTENT_TYPE.
Glenn L McGrath06e95652003-02-09 06:51:14 +0000954
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000955 *
956 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
957 *
958 * $Errors: None
959 *
960 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000961static int sendCgi(const char *url,
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +0000962 const char *request, int bodyLen, const char *cookie,
963 const char *content_type)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000964{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000965 int fromCgi[2]; /* pipe for reading data from CGI */
966 int toCgi[2]; /* pipe for sending data to CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000967
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000968 static char * argp[] = { 0, 0 };
969 int pid = 0;
970 int inFd;
971 int outFd;
972 int firstLine = 1;
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +0000973 int status;
974 size_t post_readed_size, post_readed_idx;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000975
Denis Vlasenko0bb993f2006-11-21 00:06:28 +0000976 if (pipe(fromCgi) != 0)
977 return 0;
978 if (pipe(toCgi) != 0)
979 return 0;
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +0000980
Denis Vlasenko0bb993f2006-11-21 00:06:28 +0000981 pid = fork();
982 if (pid < 0)
983 return 0;
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +0000984
985 if (!pid) {
986 /* child process */
987 char *script;
988 char *purl = strdup(url);
989 char realpath_buff[MAXPATHLEN];
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000990
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +0000991 if (purl == NULL)
992 _exit(242);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000993
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +0000994 inFd = toCgi[0];
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +0000995 outFd = fromCgi[1];
Glenn L McGrath06e95652003-02-09 06:51:14 +0000996
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +0000997 dup2(inFd, 0); // replace stdin with the pipe
998 dup2(outFd, 1); // replace stdout with the pipe
999 if (!DEBUG)
1000 dup2(outFd, 2); // replace stderr with the pipe
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001001
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001002 close(toCgi[0]);
1003 close(toCgi[1]);
1004 close(fromCgi[0]);
1005 close(fromCgi[1]);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001006
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001007 close(config->accepted_socket);
1008 close(config->server_socket);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001009
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001010 /*
1011 * Find PATH_INFO.
1012 */
1013 script = purl;
1014 while ((script = strchr(script + 1, '/')) != NULL) {
1015 /* have script.cgi/PATH_INFO or dirs/script.cgi[/PATH_INFO] */
1016 struct stat sb;
Denis Vlasenko9f609292006-11-05 19:47:33 +00001017
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001018 *script = '\0';
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001019 if (is_directory(purl + 1, 1, &sb) == 0) {
1020 /* not directory, found script.cgi/PATH_INFO */
1021 *script = '/';
1022 break;
1023 }
1024 *script = '/'; /* is directory, find next '/' */
1025 }
1026 setenv1("PATH_INFO", script); /* set /PATH_INFO or "" */
1027 /* setenv1("PATH", getenv("PATH")); redundant */
1028 setenv1("REQUEST_METHOD", request);
1029 if (config->query) {
1030 char *uri = alloca(strlen(purl) + 2 + strlen(config->query));
1031 if (uri)
1032 sprintf(uri, "%s?%s", purl, config->query);
1033 setenv1("REQUEST_URI", uri);
1034 } else {
1035 setenv1("REQUEST_URI", purl);
1036 }
1037 if (script != NULL)
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001038 *script = '\0'; /* cut off /PATH_INFO */
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001039 /* SCRIPT_FILENAME required by PHP in CGI mode */
1040 if (!realpath(purl + 1, realpath_buff))
1041 goto error_execing_cgi;
1042 setenv1("SCRIPT_FILENAME", realpath_buff);
1043 /* set SCRIPT_NAME as full path: /cgi-bin/dirs/script.cgi */
1044 setenv1("SCRIPT_NAME", purl);
Denis Vlasenko428f7ae2006-11-21 21:35:14 +00001045 /* http://hoohoo.ncsa.uiuc.edu/cgi/env.html:
1046 * QUERY_STRING: The information which follows the ? in the URL
1047 * which referenced this script. This is the query information.
1048 * It should not be decoded in any fashion. This variable
1049 * should always be set when there is query information,
1050 * regardless of command line decoding. */
1051 /* (Older versions of bbox seemed to do some decoding) */
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001052 setenv1("QUERY_STRING", config->query);
1053 setenv1("SERVER_SOFTWARE", httpdVersion);
1054 putenv("SERVER_PROTOCOL=HTTP/1.0");
1055 putenv("GATEWAY_INTERFACE=CGI/1.1");
1056 setenv1("REMOTE_ADDR", config->rmt_ip_str);
1057#if ENABLE_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
1058 setenv_long("REMOTE_PORT", config->port);
1059#endif
1060 if (bodyLen)
1061 setenv_long("CONTENT_LENGTH", bodyLen);
1062 if (cookie)
1063 setenv1("HTTP_COOKIE", cookie);
1064 if (content_type)
1065 setenv1("CONTENT_TYPE", content_type);
1066#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
1067 if (config->remoteuser) {
1068 setenv1("REMOTE_USER", config->remoteuser);
1069 putenv("AUTH_TYPE=Basic");
1070 }
1071#endif
1072 if (config->referer)
1073 setenv1("HTTP_REFERER", config->referer);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001074
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001075 /* set execve argp[0] without path */
1076 argp[0] = strrchr(purl, '/') + 1;
1077 /* but script argp[0] must have absolute path and chdiring to this */
1078 script = strrchr(realpath_buff, '/');
1079 if (!script)
1080 goto error_execing_cgi;
1081 *script = '\0';
1082 if (chdir(realpath_buff) == 0) {
1083 // now run the program. If it fails,
1084 // use _exit() so no destructors
1085 // get called and make a mess.
1086#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
1087 char *interpr = NULL;
1088 char *suffix = strrchr(purl, '.');
1089
1090 if (suffix) {
1091 Htaccess *cur;
1092 for (cur = config->script_i; cur; cur = cur->next) {
1093 if (strcmp(cur->before_colon + 1, suffix) == 0) {
1094 interpr = cur->after_colon;
1095 break;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001096 }
1097 }
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001098 }
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001099#endif
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001100 *script = '/';
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001101#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001102 if (interpr)
1103 execv(interpr, argp);
1104 else
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001105#endif
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001106 execv(realpath_buff, argp);
1107 }
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001108 error_execing_cgi:
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001109 /* send to stdout (even if we are not from inetd) */
1110 config->accepted_socket = 1;
1111 sendHeaders(HTTP_NOT_FOUND);
1112 _exit(242);
1113 } /* end child */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001114
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001115 /* parent process */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001116
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001117 post_readed_size = 0;
1118 post_readed_idx = 0;
Denis Vlasenko5d148e22006-11-21 00:12:09 +00001119 inFd = fromCgi[0];
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001120 outFd = toCgi[1];
1121 close(fromCgi[1]);
1122 close(toCgi[0]);
1123 signal(SIGPIPE, SIG_IGN);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001124
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001125 while (1) {
1126 fd_set readSet;
1127 fd_set writeSet;
1128 char wbuf[128];
1129 int nfound;
1130 int count;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001131
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001132 FD_ZERO(&readSet);
1133 FD_ZERO(&writeSet);
1134 FD_SET(inFd, &readSet);
1135 if (bodyLen > 0 || post_readed_size > 0) {
1136 FD_SET(outFd, &writeSet);
1137 nfound = outFd > inFd ? outFd : inFd;
1138 if (post_readed_size == 0) {
1139 FD_SET(config->accepted_socket, &readSet);
1140 if (nfound < config->accepted_socket)
1141 nfound = config->accepted_socket;
1142 }
1143 /* Now wait on the set of sockets! */
1144 nfound = select(nfound + 1, &readSet, &writeSet, 0, NULL);
1145 } else {
1146 if (!bodyLen) {
1147 close(outFd);
1148 bodyLen = -1;
1149 }
1150 nfound = select(inFd + 1, &readSet, 0, 0, NULL);
1151 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001152
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001153 if (nfound <= 0) {
1154 if (waitpid(pid, &status, WNOHANG) > 0) {
1155 close(inFd);
1156 if (DEBUG && WIFEXITED(status))
1157 bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status));
1158 if (DEBUG && WIFSIGNALED(status))
1159 bb_error_msg("piped has exited with signal=%d", WTERMSIG(status));
1160 break;
1161 }
1162 } else if (post_readed_size > 0 && FD_ISSET(outFd, &writeSet)) {
1163 count = full_write(outFd, wbuf + post_readed_idx, post_readed_size);
1164 if (count > 0) {
1165 post_readed_size -= count;
1166 post_readed_idx += count;
1167 if (post_readed_size == 0)
1168 post_readed_idx = 0;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001169 } else {
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001170 post_readed_size = post_readed_idx = bodyLen = 0; /* broken pipe to CGI */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001171 }
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001172 } else if (bodyLen > 0 && post_readed_size == 0 && FD_ISSET(config->accepted_socket, &readSet)) {
1173 count = bodyLen > (int)sizeof(wbuf) ? (int)sizeof(wbuf) : bodyLen;
1174 count = safe_read(config->accepted_socket, wbuf, count);
1175 if (count > 0) {
1176 post_readed_size += count;
1177 bodyLen -= count;
1178 } else {
1179 bodyLen = 0; /* closed */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001180 }
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001181 }
1182 if (FD_ISSET(inFd, &readSet)) {
1183 int s = config->accepted_socket;
1184 char *rbuf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001185
Eric Andersen97a1de12004-08-26 22:22:50 +00001186#ifndef PIPE_BUF
1187# define PIPESIZE 4096 /* amount of buffering in a pipe */
1188#else
1189# define PIPESIZE PIPE_BUF
1190#endif
1191#if PIPESIZE >= MAX_MEMORY_BUFF
1192# error "PIPESIZE >= MAX_MEMORY_BUFF"
1193#endif
1194
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +00001195 /* There is something to read */
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001196 count = safe_read(inFd, rbuf, PIPESIZE);
1197 if (count == 0)
1198 break; /* closed */
1199 if (count > 0) {
1200 if (firstLine) {
1201 rbuf[count] = 0;
1202 /* check to see if the user script added headers */
1203 if (strncmp(rbuf, "HTTP/1.0 200 OK\r\n", 4) != 0) {
1204 full_write(s, "HTTP/1.0 200 OK\r\n", 17);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001205 }
Denis Vlasenko5d148e22006-11-21 00:12:09 +00001206 /* Sometimes CGI is writing to pipe in small chunks
1207 * and we don't see Content-type (because the read
1208 * is too short) and we emit bogus "text/plain"!
1209 * Is it a bug or CGI *has to* write it in one piece? */
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001210 if (strstr(rbuf, "ontent-") == 0) {
1211 full_write(s, "Content-type: text/plain\r\n\r\n", 28);
1212 }
1213 firstLine = 0;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001214 }
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001215 if (full_write(s, rbuf, count) != count)
1216 break;
1217
1218 if (DEBUG)
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +00001219 fprintf(stderr, "cgi read %d bytes: '%.*s'\n", count, count, rbuf);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001220 }
1221 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001222 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001223 return 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001224}
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001225#endif /* FEATURE_HTTPD_CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001226
1227/****************************************************************************
1228 *
1229 > $Function: sendFile()
1230 *
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001231 * $Description: Send a file response to a HTTP request
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001232 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00001233 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001234 * (const char *) url . . The URL requested.
1235 *
1236 * $Return: (int) . . . . . . Always 0.
1237 *
1238 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001239static int sendFile(const char *url)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001240{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001241 char * suffix;
1242 int f;
1243 const char * const * table;
1244 const char * try_suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001245
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001246 suffix = strrchr(url, '.');
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001247
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001248 for (table = suffixTable; *table; table += 2)
1249 if (suffix != NULL && (try_suffix = strstr(*table, suffix)) != 0) {
1250 try_suffix += strlen(suffix);
1251 if (*try_suffix == 0 || *try_suffix == '.')
1252 break;
1253 }
1254 /* also, if not found, set default as "application/octet-stream"; */
Denis Vlasenkod4f3d1a2006-11-16 16:20:12 +00001255 config->found_mime_type = table[1];
Denis Vlasenko55a99402006-09-30 20:41:44 +00001256#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001257 if (suffix) {
1258 Htaccess * cur;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001259
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001260 for (cur = config->mime_a; cur; cur = cur->next) {
1261 if (strcmp(cur->before_colon, suffix) == 0) {
Denis Vlasenkod4f3d1a2006-11-16 16:20:12 +00001262 config->found_mime_type = cur->after_colon;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001263 break;
1264 }
1265 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001266 }
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001267#endif /* FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001268
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001269 if (DEBUG)
1270 fprintf(stderr, "sending file '%s' content-type: %s\n",
Denis Vlasenkod4f3d1a2006-11-16 16:20:12 +00001271 url, config->found_mime_type);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001272
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001273 f = open(url, O_RDONLY);
1274 if (f >= 0) {
1275 int count;
1276 char *buf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001277
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001278 sendHeaders(HTTP_OK);
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001279 /* TODO: sendfile() */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001280 while ((count = full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001281 if (full_write(config->accepted_socket, buf, count) != count)
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001282 break;
1283 }
1284 close(f);
1285 } else {
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001286 if (DEBUG)
1287 bb_perror_msg("cannot open '%s'", url);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001288 sendHeaders(HTTP_NOT_FOUND);
1289 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001290
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001291 return 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001292}
1293
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001294static int checkPermIP(void)
1295{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001296 Htaccess_IP * cur;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001297
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001298 /* This could stand some work */
1299 for (cur = config->ip_a_d; cur; cur = cur->next) {
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001300 if (DEBUG)
1301 fprintf(stderr, "checkPermIP: '%s' ? ", config->rmt_ip_str);
1302 if (DEBUG)
1303 fprintf(stderr, "'%u.%u.%u.%u/%u.%u.%u.%u'\n",
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001304 (unsigned char)(cur->ip >> 24),
1305 (unsigned char)(cur->ip >> 16),
1306 (unsigned char)(cur->ip >> 8),
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001307 cur->ip & 0xff,
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001308 (unsigned char)(cur->mask >> 24),
1309 (unsigned char)(cur->mask >> 16),
1310 (unsigned char)(cur->mask >> 8),
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001311 cur->mask & 0xff);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001312 if ((config->rmt_ip & cur->mask) == cur->ip)
1313 return cur->allow_deny == 'A'; /* Allow/Deny */
1314 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001315
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001316 /* if unconfigured, return 1 - access from all */
1317 return !config->flg_deny_all;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001318}
1319
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001320/****************************************************************************
1321 *
1322 > $Function: checkPerm()
1323 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001324 * $Description: Check the permission file for access password protected.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001325 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001326 * If config file isn't present, everything is allowed.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001327 * Entries are of the form you can see example from header source
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001328 *
1329 * $Parameters:
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001330 * (const char *) path . . . . The file path.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001331 * (const char *) request . . . User information to validate.
1332 *
1333 * $Return: (int) . . . . . . . . . 1 if request OK, 0 otherwise.
1334 *
1335 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001336
Denis Vlasenko55a99402006-09-30 20:41:44 +00001337#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001338static int checkPerm(const char *path, const char *request)
1339{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001340 Htaccess * cur;
1341 const char *p;
1342 const char *p0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001343
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001344 const char *prev = NULL;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001345
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001346 /* This could stand some work */
1347 for (cur = config->auth; cur; cur = cur->next) {
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001348 size_t l;
1349
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001350 p0 = cur->before_colon;
1351 if (prev != NULL && strcmp(prev, p0) != 0)
1352 continue; /* find next identical */
1353 p = cur->after_colon;
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001354 if (DEBUG)
1355 fprintf(stderr, "checkPerm: '%s' ? '%s'\n", p0, request);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001356
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001357 l = strlen(p0);
1358 if (strncmp(p0, path, l) == 0
1359 && (l == 1 || path[l] == '/' || path[l] == '\0')
1360 ) {
1361 char *u;
1362 /* path match found. Check request */
1363 /* for check next /path:user:password */
1364 prev = p0;
1365 u = strchr(request, ':');
1366 if (u == NULL) {
1367 /* bad request, ':' required */
1368 break;
1369 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001370
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001371 if (ENABLE_FEATURE_HTTPD_AUTH_MD5) {
1372 char *cipher;
1373 char *pp;
Eric Andersen35e643b2003-07-28 07:40:39 +00001374
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001375 if (strncmp(p, request, u-request) != 0) {
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001376 /* user uncompared */
1377 continue;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001378 }
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001379 pp = strchr(p, ':');
1380 if (pp && pp[1] == '$' && pp[2] == '1' &&
1381 pp[3] == '$' && pp[4]) {
1382 pp++;
1383 cipher = pw_encrypt(u+1, pp);
1384 if (strcmp(cipher, pp) == 0)
1385 goto set_remoteuser_var; /* Ok */
1386 /* unauthorized */
1387 continue;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001388 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001389 }
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001390
1391 if (strcmp(p, request) == 0) {
1392set_remoteuser_var:
1393 config->remoteuser = strdup(request);
1394 if (config->remoteuser)
1395 config->remoteuser[(u - request)] = 0;
1396 return 1; /* Ok */
1397 }
1398 /* unauthorized */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001399 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001400 } /* for */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001401
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001402 return prev == NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001403}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001404
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001405#endif /* FEATURE_HTTPD_BASIC_AUTH */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001406
Eric Andersen07f2fea2004-10-08 08:03:29 +00001407/****************************************************************************
1408 *
Mike Frysingerbb12d6f2006-01-03 23:59:01 +00001409 > $Function: handle_sigalrm()
Eric Andersen07f2fea2004-10-08 08:03:29 +00001410 *
Mike Frysingerbb12d6f2006-01-03 23:59:01 +00001411 * $Description: Handle timeouts
Eric Andersen07f2fea2004-10-08 08:03:29 +00001412 *
1413 ****************************************************************************/
1414
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001415static void handle_sigalrm(int sig)
Eric Andersen07f2fea2004-10-08 08:03:29 +00001416{
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001417 sendHeaders(HTTP_REQUEST_TIMEOUT);
1418 config->alarm_signaled = sig;
Eric Andersen07f2fea2004-10-08 08:03:29 +00001419}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001420
1421/****************************************************************************
1422 *
1423 > $Function: handleIncoming()
1424 *
1425 * $Description: Handle an incoming http request.
1426 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001427 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001428static void handleIncoming(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001429{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001430 char *buf = config->buf;
1431 char *url;
1432 char *purl;
1433 int blank = -1;
1434 char *test;
1435 struct stat sb;
1436 int ip_allowed;
Denis Vlasenko55a99402006-09-30 20:41:44 +00001437#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001438 const char *prequest = request_GET;
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001439 unsigned long length = 0;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001440 char *cookie = 0;
1441 char *content_type = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001442#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001443 fd_set s_fd;
1444 struct timeval tv;
1445 int retval;
1446 struct sigaction sa;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001447
Denis Vlasenko55a99402006-09-30 20:41:44 +00001448#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001449 int credentials = -1; /* if not required this is Ok */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001450#endif
1451
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001452 sa.sa_handler = handle_sigalrm;
1453 sigemptyset(&sa.sa_mask);
1454 sa.sa_flags = 0; /* no SA_RESTART */
1455 sigaction(SIGALRM, &sa, NULL);
Eric Andersen07f2fea2004-10-08 08:03:29 +00001456
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001457 do {
1458 int count;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001459
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001460 (void) alarm(TIMEOUT);
1461 if (getLine() <= 0)
1462 break; /* closed */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001463
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001464 purl = strpbrk(buf, " \t");
1465 if (purl == NULL) {
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001466 BAD_REQUEST:
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001467 sendHeaders(HTTP_BAD_REQUEST);
1468 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001469 }
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001470 *purl = '\0';
Denis Vlasenko55a99402006-09-30 20:41:44 +00001471#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001472 if (strcasecmp(buf, prequest) != 0) {
1473 prequest = "POST";
1474 if (strcasecmp(buf, prequest) != 0) {
1475 sendHeaders(HTTP_NOT_IMPLEMENTED);
1476 break;
1477 }
1478 }
1479#else
1480 if (strcasecmp(buf, request_GET) != 0) {
1481 sendHeaders(HTTP_NOT_IMPLEMENTED);
1482 break;
1483 }
1484#endif
1485 *purl = ' ';
1486 count = sscanf(purl, " %[^ ] HTTP/%d.%*d", buf, &blank);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001487
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001488 if (count < 1 || buf[0] != '/') {
1489 /* Garbled request/URL */
1490 goto BAD_REQUEST;
1491 }
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001492 url = alloca(strlen(buf) + sizeof("/index.html"));
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001493 if (url == NULL) {
1494 sendHeaders(HTTP_INTERNAL_SERVER_ERROR);
1495 break;
1496 }
1497 strcpy(url, buf);
1498 /* extract url args if present */
1499 test = strchr(url, '?');
Denis Vlasenko5d148e22006-11-21 00:12:09 +00001500 config->query = NULL;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001501 if (test) {
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001502 *test++ = '\0';
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001503 config->query = test;
1504 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001505
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001506 test = decodeString(url, 0);
1507 if (test == NULL)
1508 goto BAD_REQUEST;
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001509 /* FIXME: bug? should be "url+1"? */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001510 if (test == (buf+1)) {
1511 sendHeaders(HTTP_NOT_FOUND);
1512 break;
1513 }
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001514
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001515 /* algorithm stolen from libbb bb_simplify_path(),
Denis Vlasenko55a99402006-09-30 20:41:44 +00001516 but don't strdup and reducing trailing slash and protect out root */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001517 purl = test = url;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001518 do {
1519 if (*purl == '/') {
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001520 /* skip duplicate (or initial) slash */
1521 if (*test == '/') {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001522 continue;
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001523 }
1524 if (*test == '.') {
1525 /* skip extra '.' */
1526 if (test[1] == '/' || test[1] == 0) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001527 continue;
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001528 } else
1529 /* '..': be careful */
1530 if (test[1] == '.' && (test[2] == '/' || test[2] == 0)) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001531 ++test;
1532 if (purl == url) {
1533 /* protect out root */
1534 goto BAD_REQUEST;
1535 }
Denis Vlasenko92758142006-10-03 19:56:34 +00001536 while (*--purl != '/') /* omit previous dir */;
1537 continue;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001538 }
1539 }
1540 }
1541 *++purl = *test;
1542 } while (*++test);
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001543 *++purl = '\0'; /* so keep last character */
1544 test = purl; /* end ptr */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001545
1546 /* If URL is directory, adding '/' */
1547 if (test[-1] != '/') {
1548 if (is_directory(url + 1, 1, &sb)) {
Denis Vlasenkod4f3d1a2006-11-16 16:20:12 +00001549 config->found_moved_temporarily = url;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001550 }
1551 }
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001552 if (DEBUG)
1553 fprintf(stderr, "url='%s', args=%s\n", url, config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001554
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001555 test = url;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001556 ip_allowed = checkPermIP();
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001557 while (ip_allowed && (test = strchr(test + 1, '/')) != NULL) {
1558 /* have path1/path2 */
1559 *test = '\0';
1560 if (is_directory(url + 1, 1, &sb)) {
1561 /* may be having subdir config */
1562 parse_conf(url + 1, SUBDIR_PARSE);
1563 ip_allowed = checkPermIP();
1564 }
1565 *test = '/';
1566 }
1567 if (blank >= 0) {
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001568 /* read until blank line for HTTP version specified, else parse immediate */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001569 while (1) {
1570 alarm(TIMEOUT);
1571 count = getLine();
1572 if (count <= 0)
1573 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001574
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001575 if (DEBUG)
1576 fprintf(stderr, "header: '%s'\n", buf);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001577
Denis Vlasenko55a99402006-09-30 20:41:44 +00001578#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001579 /* try and do our best to parse more lines */
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001580 if ((STRNCASECMP(buf, "Content-length:") == 0)) {
1581 /* extra read only for POST */
1582 if (prequest != request_GET) {
1583 test = buf + sizeof("Content-length:")-1;
1584 if (!test[0]) goto bail_out;
1585 errno = 0;
1586 /* not using strtoul: it ignores leading munis! */
1587 length = strtol(test, &test, 10);
1588 /* length is "ulong", but we need to pass it to int later */
1589 /* so we check for negative or too large values in one go: */
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001590 /* (long -> ulong conv caused negatives to be seen as > INT_MAX) */
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001591 if (test[0] || errno || length > INT_MAX)
1592 goto bail_out;
1593 }
1594 } else if ((STRNCASECMP(buf, "Cookie:") == 0)) {
1595 cookie = strdup(skip_whitespace(buf + sizeof("Cookie:")-1));
1596 } else if ((STRNCASECMP(buf, "Content-Type:") == 0)) {
1597 content_type = strdup(skip_whitespace(buf + sizeof("Content-Type:")-1));
1598 } else if ((STRNCASECMP(buf, "Referer:") == 0)) {
1599 config->referer = strdup(skip_whitespace(buf + sizeof("Referer:")-1));
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001600 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001601#endif
1602
Denis Vlasenko55a99402006-09-30 20:41:44 +00001603#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001604 if (STRNCASECMP(buf, "Authorization:") == 0) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001605 /* We only allow Basic credentials.
1606 * It shows up as "Authorization: Basic <userid:password>" where
1607 * the userid:password is base64 encoded.
1608 */
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001609 test = skip_whitespace(buf + sizeof("Authorization:")-1);
1610 if (STRNCASECMP(test, "Basic") != 0)
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001611 continue;
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001612 test += sizeof("Basic")-1;
1613 /* decodeBase64() skips whitespace itself */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001614 decodeBase64(test);
1615 credentials = checkPerm(url, test);
1616 }
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001617#endif /* FEATURE_HTTPD_BASIC_AUTH */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001618
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001619 } /* while extra header reading */
1620 }
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001621 alarm(0);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001622 if (config->alarm_signaled)
1623 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001624
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001625 if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) {
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001626 /* protect listing [/path]/httpd_conf or IP deny */
Denis Vlasenko55a99402006-09-30 20:41:44 +00001627#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001628 FORBIDDEN: /* protect listing /cgi-bin */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001629#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001630 sendHeaders(HTTP_FORBIDDEN);
1631 break;
1632 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001633
Denis Vlasenko55a99402006-09-30 20:41:44 +00001634#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001635 if (credentials <= 0 && checkPerm(url, ":") == 0) {
1636 sendHeaders(HTTP_UNAUTHORIZED);
1637 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001638 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001639#endif
1640
Denis Vlasenkod4f3d1a2006-11-16 16:20:12 +00001641 if (config->found_moved_temporarily) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001642 sendHeaders(HTTP_MOVED_TEMPORARILY);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001643 /* clear unforked memory flag */
Denis Vlasenkod4f3d1a2006-11-16 16:20:12 +00001644 config->found_moved_temporarily = NULL;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001645 break;
1646 }
1647
1648 test = url + 1; /* skip first '/' */
1649
Denis Vlasenko55a99402006-09-30 20:41:44 +00001650#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001651 if (strncmp(test, "cgi-bin", 7) == 0) {
1652 if (test[7] == '/' && test[8] == 0)
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001653 goto FORBIDDEN; /* protect listing cgi-bin/ */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001654 sendCgi(url, prequest, length, cookie, content_type);
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001655 break;
1656 }
1657 if (prequest != request_GET) {
1658 sendHeaders(HTTP_NOT_IMPLEMENTED);
1659 break;
1660 }
Denis Vlasenko5d148e22006-11-21 00:12:09 +00001661#endif /* FEATURE_HTTPD_CGI */
1662 if (purl[-1] == '/')
1663 strcpy(purl, "index.html");
1664 if (stat(test, &sb) == 0) {
1665 /* It's a dir URL and there is index.html */
1666 config->ContentLength = sb.st_size;
1667 config->last_mod = sb.st_mtime;
1668 }
1669#if ENABLE_FEATURE_HTTPD_CGI
1670 else if (purl[-1] == '/') {
1671 /* It's a dir URL and there is no index.html
1672 * Try cgi-bin/index.cgi */
1673 if (access("/cgi-bin/index.cgi"+1, X_OK) == 0) {
1674 purl[0] = '\0';
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001675 config->query = url;
1676 sendCgi("/cgi-bin/index.cgi", prequest, length, cookie, content_type);
1677 break;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001678 }
1679 }
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001680#endif /* FEATURE_HTTPD_CGI */
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001681 sendFile(test);
1682 config->ContentLength = -1;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001683 } while (0);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001684
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001685 bail_out:
1686
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001687 if (DEBUG)
1688 fprintf(stderr, "closing socket\n\n");
1689#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001690 free(cookie);
1691 free(content_type);
Denis Vlasenkoa5342b42006-11-17 18:26:57 +00001692 free(config->referer);
1693 config->referer = NULL;
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001694# if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenkoa5342b42006-11-17 18:26:57 +00001695 free(config->remoteuser);
1696 config->remoteuser = NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001697# endif
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001698#endif
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001699 shutdown(config->accepted_socket, SHUT_WR);
Eric Andersend8746cd2004-02-24 07:28:38 +00001700
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001701 /* Properly wait for remote to closed */
Denis Vlasenko55a99402006-09-30 20:41:44 +00001702 FD_ZERO(&s_fd);
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001703 FD_SET(config->accepted_socket, &s_fd);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001704
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001705 do {
Denis Vlasenko55a99402006-09-30 20:41:44 +00001706 tv.tv_sec = 2;
1707 tv.tv_usec = 0;
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001708 retval = select(config->accepted_socket + 1, &s_fd, NULL, NULL, &tv);
1709 } while (retval > 0 && read(config->accepted_socket, buf, sizeof(config->buf) > 0));
Eric Andersend8746cd2004-02-24 07:28:38 +00001710
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001711 shutdown(config->accepted_socket, SHUT_RD);
1712 /* In inetd case, we close fd 1 (stdout) here. We will exit soon anyway */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001713 close(config->accepted_socket);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001714}
1715
1716/****************************************************************************
1717 *
1718 > $Function: miniHttpd()
1719 *
1720 * $Description: The main http server function.
1721 *
1722 * Given an open socket fildes, listen for new connections and farm out
1723 * the processing as a forked process.
1724 *
1725 * $Parameters:
1726 * (int) server. . . The server socket fildes.
1727 *
1728 * $Return: (int) . . . . Always 0.
1729 *
1730 ****************************************************************************/
1731static int miniHttpd(int server)
1732{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001733 fd_set readfd, portfd;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001734
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001735 FD_ZERO(&portfd);
1736 FD_SET(server, &portfd);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001737
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001738 /* copy the ports we are watching to the readfd set */
1739 while (1) {
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001740 int on, s;
1741 socklen_t fromAddrLen;
1742 struct sockaddr_in fromAddr;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001743
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001744 /* Now wait INDEFINITELY on the set of sockets! */
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001745 readfd = portfd;
1746 if (select(server + 1, &readfd, 0, 0, 0) <= 0)
1747 continue;
1748 if (!FD_ISSET(server, &readfd))
1749 continue;
1750 fromAddrLen = sizeof(fromAddr);
1751 s = accept(server, (struct sockaddr *)&fromAddr, &fromAddrLen);
1752 if (s < 0)
1753 continue;
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001754 config->accepted_socket = s;
1755 config->rmt_ip = ntohl(fromAddr.sin_addr.s_addr);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001756#if ENABLE_FEATURE_HTTPD_CGI || DEBUG
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001757 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1758 (unsigned char)(config->rmt_ip >> 24),
1759 (unsigned char)(config->rmt_ip >> 16),
1760 (unsigned char)(config->rmt_ip >> 8),
1761 config->rmt_ip & 0xff);
1762 config->port = ntohs(fromAddr.sin_port);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001763#if DEBUG
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001764 bb_error_msg("connection from IP=%s, port %u",
1765 config->rmt_ip_str, config->port);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001766#endif
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001767#endif /* FEATURE_HTTPD_CGI */
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001768
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001769 /* set the KEEPALIVE option to cull dead connections */
1770 on = 1;
1771 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on));
Denis Vlasenko04291bc2006-11-21 10:15:25 +00001772
1773 if (DEBUG || fork() == 0) {
1774 /* child */
Denis Vlasenko55a99402006-09-30 20:41:44 +00001775#if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001776 /* protect reload config, may be confuse checking */
1777 signal(SIGHUP, SIG_IGN);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001778#endif
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001779 handleIncoming();
Denis Vlasenko04291bc2006-11-21 10:15:25 +00001780 if (!DEBUG)
1781 exit(0);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001782 }
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001783 close(s);
Denis Vlasenko04291bc2006-11-21 10:15:25 +00001784 } /* while (1) */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001785 return 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001786}
1787
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001788/* from inetd */
1789static int miniHttpd_inetd(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001790{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001791 struct sockaddr_in fromAddrLen;
Denis Vlasenko55a99402006-09-30 20:41:44 +00001792 socklen_t sinlen = sizeof(struct sockaddr_in);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001793
Denis Vlasenko55a99402006-09-30 20:41:44 +00001794 getpeername(0, (struct sockaddr *)&fromAddrLen, &sinlen);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001795 config->rmt_ip = ntohl(fromAddrLen.sin_addr.s_addr);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001796#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001797 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1798 (unsigned char)(config->rmt_ip >> 24),
1799 (unsigned char)(config->rmt_ip >> 16),
1800 (unsigned char)(config->rmt_ip >> 8),
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001801 config->rmt_ip & 0xff);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001802#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001803 config->port = ntohs(fromAddrLen.sin_port);
1804 handleIncoming();
1805 return 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001806}
Glenn L McGrath06e95652003-02-09 06:51:14 +00001807
Denis Vlasenko55a99402006-09-30 20:41:44 +00001808#if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
Glenn L McGrath06e95652003-02-09 06:51:14 +00001809static void sighup_handler(int sig)
1810{
1811 /* set and reset */
1812 struct sigaction sa;
1813
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001814 parse_conf(default_path_httpd_conf, sig == SIGHUP ? SIGNALED_PARSE : FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001815 sa.sa_handler = sighup_handler;
1816 sigemptyset(&sa.sa_mask);
1817 sa.sa_flags = SA_RESTART;
1818 sigaction(SIGHUP, &sa, NULL);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001819}
1820#endif
1821
Denis Vlasenko9f609292006-11-05 19:47:33 +00001822enum {
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001823 c_opt_config_file = 0,
1824 d_opt_decode_url,
1825 h_opt_home_httpd,
1826 USE_FEATURE_HTTPD_ENCODE_URL_STR(e_opt_encode_url,)
Denis Vlasenko9f609292006-11-05 19:47:33 +00001827 USE_FEATURE_HTTPD_BASIC_AUTH( r_opt_realm ,)
1828 USE_FEATURE_HTTPD_AUTH_MD5( m_opt_md5 ,)
1829 USE_FEATURE_HTTPD_SETUID( u_opt_setuid ,)
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001830 p_opt_port ,
1831 p_opt_inetd ,
1832 p_opt_foreground,
Denis Vlasenko9f609292006-11-05 19:47:33 +00001833 OPT_CONFIG_FILE = 1 << c_opt_config_file,
1834 OPT_DECODE_URL = 1 << d_opt_decode_url,
1835 OPT_HOME_HTTPD = 1 << h_opt_home_httpd,
1836 OPT_ENCODE_URL = USE_FEATURE_HTTPD_ENCODE_URL_STR((1 << e_opt_encode_url)) + 0,
1837 OPT_REALM = USE_FEATURE_HTTPD_BASIC_AUTH( (1 << r_opt_realm )) + 0,
1838 OPT_MD5 = USE_FEATURE_HTTPD_AUTH_MD5( (1 << m_opt_md5 )) + 0,
1839 OPT_SETUID = USE_FEATURE_HTTPD_SETUID( (1 << u_opt_setuid )) + 0,
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001840 OPT_PORT = 1 << p_opt_port,
1841 OPT_INETD = 1 << p_opt_inetd,
1842 OPT_FOREGROUND = 1 << p_opt_foreground,
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001843};
Eric Andersena3bb3e62003-06-26 09:05:32 +00001844
Denis Vlasenko55a99402006-09-30 20:41:44 +00001845static const char httpd_opts[] = "c:d:h:"
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001846 USE_FEATURE_HTTPD_ENCODE_URL_STR("e:")
1847 USE_FEATURE_HTTPD_BASIC_AUTH("r:")
1848 USE_FEATURE_HTTPD_AUTH_MD5("m:")
1849 USE_FEATURE_HTTPD_SETUID("u:")
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001850 "p:if";
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001851
Eric Andersena3bb3e62003-06-26 09:05:32 +00001852
Glenn L McGrath06e95652003-02-09 06:51:14 +00001853int httpd_main(int argc, char *argv[])
Glenn L McGrath06e95652003-02-09 06:51:14 +00001854{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001855 unsigned opt;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001856 const char *home_httpd = home;
1857 char *url_for_decode;
1858 USE_FEATURE_HTTPD_ENCODE_URL_STR(const char *url_for_encode;)
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001859 const char *s_port;
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001860 USE_FEATURE_HTTPD_SETUID(const char *s_ugid = NULL;)
1861 USE_FEATURE_HTTPD_SETUID(struct bb_uidgid_t ugid;)
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001862 USE_FEATURE_HTTPD_AUTH_MD5(const char *pass;)
Eric Andersen35e643b2003-07-28 07:40:39 +00001863
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +00001864#if ENABLE_LOCALE_SUPPORT
1865 /* Undo busybox.c: we want to speak English in http (dates etc) */
1866 setlocale(LC_TIME, "C");
1867#endif
1868
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001869 config = xzalloc(sizeof(*config));
Denis Vlasenko55a99402006-09-30 20:41:44 +00001870#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001871 config->realm = "Web Server Authentication";
Glenn L McGrath06e95652003-02-09 06:51:14 +00001872#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001873 config->port = 80;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001874 config->ContentLength = -1;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001875
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001876 opt = getopt32(argc, argv, httpd_opts,
Eric Andersena3bb3e62003-06-26 09:05:32 +00001877 &(config->configFile), &url_for_decode, &home_httpd
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001878 USE_FEATURE_HTTPD_ENCODE_URL_STR(, &url_for_encode)
1879 USE_FEATURE_HTTPD_BASIC_AUTH(, &(config->realm))
1880 USE_FEATURE_HTTPD_AUTH_MD5(, &pass)
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001881 USE_FEATURE_HTTPD_SETUID(, &s_ugid)
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001882 , &s_port
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001883 );
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001884 if (opt & OPT_DECODE_URL) {
1885 printf("%s", decodeString(url_for_decode, 1));
1886 return 0;
1887 }
Denis Vlasenko55a99402006-09-30 20:41:44 +00001888#if ENABLE_FEATURE_HTTPD_ENCODE_URL_STR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001889 if (opt & OPT_ENCODE_URL) {
1890 printf("%s", encodeString(url_for_encode));
1891 return 0;
1892 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001893#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +00001894#if ENABLE_FEATURE_HTTPD_AUTH_MD5
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001895 if (opt & OPT_MD5) {
Denis Vlasenko55a99402006-09-30 20:41:44 +00001896 puts(pw_encrypt(pass, "$1$"));
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001897 return 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001898 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001899#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001900 if (opt & OPT_PORT)
Denis Vlasenko13858992006-10-08 12:49:22 +00001901 config->port = xatou16(s_port);
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001902
Denis Vlasenko55a99402006-09-30 20:41:44 +00001903#if ENABLE_FEATURE_HTTPD_SETUID
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001904 if (opt & OPT_SETUID) {
1905 char *e;
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001906 // FIXME: what the default group should be?
1907 ugid.gid = -1;
1908 ugid.uid = strtoul(s_ugid, &e, 0);
1909 if (*e == ':') {
1910 e++;
1911 ugid.gid = strtoul(e, &e, 0);
1912 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001913 if (*e != '\0') {
1914 /* not integer */
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001915 if (!uidgid_get(&ugid, s_ugid))
1916 bb_error_msg_and_die("unrecognized user[:group] "
1917 "name '%s'", s_ugid);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001918 }
1919 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001920#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001921
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001922 xchdir(home_httpd);
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001923 if (!(opt & OPT_INETD)) {
1924 config->server_socket = openServer();
1925#if ENABLE_FEATURE_HTTPD_SETUID
1926 /* drop privileges */
1927 if (opt & OPT_SETUID) {
1928 if (ugid.gid != (gid_t)-1) {
1929 if (setgroups(1, &ugid.gid) == -1)
1930 bb_perror_msg_and_die("setgroups");
1931 xsetgid(ugid.gid);
1932 }
1933 xsetuid(ugid.uid);
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001934 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001935#endif
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001936 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001937
Denis Vlasenko55a99402006-09-30 20:41:44 +00001938#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001939 {
1940 char *p = getenv("PATH");
1941 if (p) {
1942 p = xstrdup(p);
1943 }
1944 clearenv();
1945 if (p)
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001946 setenv1("PATH", p);
1947 if (!(opt & OPT_INETD))
1948 setenv_long("SERVER_PORT", config->port);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001949 }
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00001950#endif
1951
Denis Vlasenko55a99402006-09-30 20:41:44 +00001952#if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001953 sighup_handler(0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001954#else
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001955 parse_conf(default_path_httpd_conf, FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001956#endif
1957
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001958 if (opt & OPT_INETD)
1959 return miniHttpd_inetd();
1960
1961 if (!(opt & OPT_FOREGROUND))
1962 xdaemon(1, 0); /* don't change current directory */
Denis Vlasenko9f609292006-11-05 19:47:33 +00001963 return miniHttpd(config->server_socket);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001964}