blob: 737b030a76a210241afa0dc38043a09c44d155c8 [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 *
27 * The server can also be invoked as a url arg decoder and html text encoder
28 * as follows:
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000029 * foo=`httpd -d $foo` # decode "Hello%20World" as "Hello World"
Glenn L McGrathc9163fe2003-05-13 16:20:11 +000030 * bar=`httpd -e "<Hello World>"` # encode as "&#60Hello&#32World&#62"
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000031 * Note that url encoding for arguments is not the same as html encoding for
Eric Andersenaff114c2004-04-14 17:51:38 +000032 * presentation. -d decodes a url-encoded argument while -e encodes in html
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000033 * for page display.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000034 *
35 * httpd.conf has the following format:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000036 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +000037 * A:172.20. # Allow address from 172.20.0.0/16
38 * A:10.0.0.0/25 # Allow any address from 10.0.0.0-10.0.0.127
39 * A:10.0.0.0/255.255.255.128 # Allow any address that previous set
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000040 * A:127.0.0.1 # Allow local loopback connections
41 * D:* # Deny from other IP connections
42 * /cgi-bin:foo:bar # Require user foo, pwd bar on urls starting with /cgi-bin/
43 * /adm:admin:setup # Require user admin, pwd setup on urls starting with /adm/
44 * /adm:toor:PaSsWd # or user toor, pwd PaSsWd on urls starting with /adm/
45 * .au:audio/basic # additional mime type for audio.au files
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +000046 * *.php:/path/php # running cgi.php scripts through an interpreter
Eric Andersenc7bda1c2004-03-15 08:29:22 +000047 *
Eric Andersenaff114c2004-04-14 17:51:38 +000048 * A/D may be as a/d or allow/deny - first char case insensitive
Glenn L McGrath393183d2003-05-26 14:07:50 +000049 * Deny IP rules take precedence over allow rules.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000050 *
51 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000052 * The Deny/Allow IP logic:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000053 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000054 * - Default is to allow all. No addresses are denied unless
Eric Andersen97a1de12004-08-26 22:22:50 +000055 * denied with a D: rule.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000056 * - Order of Deny/Allow rules is significant
57 * - Deny rules take precedence over allow rules.
58 * - If a deny all rule (D:*) is used it acts as a catch-all for unmatched
Eric Andersen97a1de12004-08-26 22:22:50 +000059 * addresses.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000060 * - Specification of Allow all (A:*) is a no-op
Eric Andersenc7bda1c2004-03-15 08:29:22 +000061 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000062 * Example:
63 * 1. Allow only specified addresses
Glenn L McGrathb65422c2003-09-08 10:59:27 +000064 * A:172.20 # Allow any address that begins with 172.20.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000065 * A:10.10. # Allow any address that begins with 10.10.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000066 * A:127.0.0.1 # Allow local loopback connections
67 * D:* # Deny from other IP connections
Eric Andersenc7bda1c2004-03-15 08:29:22 +000068 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000069 * 2. Only deny specified addresses
70 * D:1.2.3. # deny from 1.2.3.0 - 1.2.3.255
71 * D:2.3.4. # deny from 2.3.4.0 - 2.3.4.255
72 * A:* # (optional line added for clarity)
Eric Andersenc7bda1c2004-03-15 08:29:22 +000073 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000074 * If a sub directory contains a config file it is parsed and merged with
Glenn L McGrathb65422c2003-09-08 10:59:27 +000075 * any existing settings as if it was appended to the original configuration.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000076 *
77 * subdir paths are relative to the containing subdir and thus cannot
78 * affect the parent rules.
79 *
80 * Note that since the sub dir is parsed in the forked thread servicing the
81 * subdir http request, any merge is discarded when the process exits. As a
82 * result, the subdir settings only have a lifetime of a single request.
83 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000084 *
85 * If -c is not set, an attempt will be made to open the default
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000086 * root configuration file. If -c is set and the file is not found, the
87 * server exits with an error.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000088 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000089*/
Glenn L McGrath58c708a2003-01-05 04:01:56 +000090
Glenn L McGrath06e95652003-02-09 06:51:14 +000091
Glenn L McGrath06e95652003-02-09 06:51:14 +000092#include "busybox.h"
Glenn L McGrath58c708a2003-01-05 04:01:56 +000093
Glenn L McGrath58c708a2003-01-05 04:01:56 +000094
Eric Andersen07f2fea2004-10-08 08:03:29 +000095static const char httpdVersion[] = "busybox httpd/1.35 6-Oct-2004";
Glenn L McGrathc9163fe2003-05-13 16:20:11 +000096static const char default_path_httpd_conf[] = "/etc";
Glenn L McGrath06e95652003-02-09 06:51:14 +000097static const char httpd_conf[] = "httpd.conf";
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000098static const char home[] = "./";
Glenn L McGrath06e95652003-02-09 06:51:14 +000099
Eric Andersen07f2fea2004-10-08 08:03:29 +0000100#define TIMEOUT 60
101
Eric Andersenaff114c2004-04-14 17:51:38 +0000102// Note: busybox xfuncs are not used because we want the server to keep running
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000103// if something bad happens due to a malformed user request.
Glenn L McGrath06e95652003-02-09 06:51:14 +0000104// As a result, all memory allocation after daemonize
105// is checked rigorously
106
107//#define DEBUG 1
108
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000109#ifndef DEBUG
110# define DEBUG 0
111#endif
112
Glenn L McGrath06e95652003-02-09 06:51:14 +0000113#define MAX_MEMORY_BUFF 8192 /* IO buffer */
114
115typedef struct HT_ACCESS {
116 char *after_colon;
117 struct HT_ACCESS *next;
118 char before_colon[1]; /* really bigger, must last */
119} Htaccess;
120
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000121typedef struct HT_ACCESS_IP {
122 unsigned int ip;
123 unsigned int mask;
124 int allow_deny;
125 struct HT_ACCESS_IP *next;
126} Htaccess_IP;
127
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000128typedef struct {
129 char buf[MAX_MEMORY_BUFF];
Glenn L McGrath06e95652003-02-09 06:51:14 +0000130
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000131 USE_FEATURE_HTTPD_BASIC_AUTH(const char *realm;)
132 USE_FEATURE_HTTPD_BASIC_AUTH(char *remoteuser;)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000133
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000134 const char *query;
Eric Andersen07f2fea2004-10-08 08:03:29 +0000135
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000136 USE_FEATURE_HTTPD_CGI(char *referer;)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000137
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000138 const char *configFile;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000139
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000140 unsigned int rmt_ip;
Denis Vlasenko55a99402006-09-30 20:41:44 +0000141#if ENABLE_FEATURE_HTTPD_CGI || DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000142 char rmt_ip_str[16]; /* for set env REMOTE_ADDR */
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000143#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000144 unsigned port; /* server initial port and for
145 set env REMOTE_PORT */
146 union HTTPD_FOUND {
147 const char *found_mime_type;
148 const char *found_moved_temporarily;
149 } httpd_found;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000150
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000151 off_t ContentLength; /* -1 - unknown */
152 time_t last_mod;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000153
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000154 Htaccess_IP *ip_a_d; /* config allow/deny lines */
155 int flg_deny_all;
Denis Vlasenko55a99402006-09-30 20:41:44 +0000156#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000157 Htaccess *auth; /* config user:password lines */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000158#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000159#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000160 Htaccess *mime_a; /* config mime types */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000161#endif
162
Denis Vlasenko55a99402006-09-30 20:41:44 +0000163#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Denis Vlasenko9f609292006-11-05 19:47:33 +0000164 int server_socket;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000165 int accepted_socket;
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000166# define a_c_r config->accepted_socket
167# define a_c_w config->accepted_socket
Glenn L McGrath06e95652003-02-09 06:51:14 +0000168#else
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000169# define a_c_r 0
170# define a_c_w 1
Glenn L McGrath06e95652003-02-09 06:51:14 +0000171#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000172 volatile int alarm_signaled;
Eric Andersen07f2fea2004-10-08 08:03:29 +0000173
Denis Vlasenko55a99402006-09-30 20:41:44 +0000174#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000175 Htaccess *script_i; /* config script interpreters */
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000176#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000177} HttpdConfig;
178
179static HttpdConfig *config;
180
Mike Frysingerfa6c4842006-05-26 01:48:17 +0000181static const char request_GET[] = "GET"; /* size algorithmic optimize */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000182
183static const char* const suffixTable [] = {
Eric Andersenaff114c2004-04-14 17:51:38 +0000184/* Warning: shorted equivalent suffix in one line must be first */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000185 ".htm.html", "text/html",
186 ".jpg.jpeg", "image/jpeg",
187 ".gif", "image/gif",
188 ".png", "image/png",
189 ".txt.h.c.cc.cpp", "text/plain",
190 ".css", "text/css",
191 ".wav", "audio/wav",
192 ".avi", "video/x-msvideo",
193 ".qt.mov", "video/quicktime",
194 ".mpe.mpeg", "video/mpeg",
195 ".mid.midi", "audio/midi",
196 ".mp3", "audio/mpeg",
Glenn L McGrath06e95652003-02-09 06:51:14 +0000197#if 0 /* unpopular */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000198 ".au", "audio/basic",
199 ".pac", "application/x-ns-proxy-autoconfig",
200 ".vrml.wrl", "model/vrml",
Glenn L McGrath06e95652003-02-09 06:51:14 +0000201#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000202 0, "application/octet-stream" /* default */
203};
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000204
Denis Vlasenko55a99402006-09-30 20:41:44 +0000205typedef enum {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000206 HTTP_OK = 200,
207 HTTP_MOVED_TEMPORARILY = 302,
208 HTTP_BAD_REQUEST = 400, /* malformed syntax */
209 HTTP_UNAUTHORIZED = 401, /* authentication needed, respond with auth hdr */
210 HTTP_NOT_FOUND = 404,
211 HTTP_FORBIDDEN = 403,
212 HTTP_REQUEST_TIMEOUT = 408,
213 HTTP_NOT_IMPLEMENTED = 501, /* used for unrecognized requests */
214 HTTP_INTERNAL_SERVER_ERROR = 500,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000215#if 0 /* future use */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000216 HTTP_CONTINUE = 100,
217 HTTP_SWITCHING_PROTOCOLS = 101,
218 HTTP_CREATED = 201,
219 HTTP_ACCEPTED = 202,
220 HTTP_NON_AUTHORITATIVE_INFO = 203,
221 HTTP_NO_CONTENT = 204,
222 HTTP_MULTIPLE_CHOICES = 300,
223 HTTP_MOVED_PERMANENTLY = 301,
224 HTTP_NOT_MODIFIED = 304,
225 HTTP_PAYMENT_REQUIRED = 402,
226 HTTP_BAD_GATEWAY = 502,
227 HTTP_SERVICE_UNAVAILABLE = 503, /* overload, maintenance */
228 HTTP_RESPONSE_SETSIZE = 0xffffffff
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000229#endif
230} HttpResponseNum;
231
Denis Vlasenko55a99402006-09-30 20:41:44 +0000232typedef struct {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000233 HttpResponseNum type;
234 const char *name;
235 const char *info;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000236} HttpEnumString;
237
238static const HttpEnumString httpResponseNames[] = {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000239 { HTTP_OK, "OK", NULL },
240 { HTTP_MOVED_TEMPORARILY, "Found", "Directories must end with a slash." },
241 { HTTP_REQUEST_TIMEOUT, "Request Timeout",
242 "No request appeared within a reasonable time period." },
243 { HTTP_NOT_IMPLEMENTED, "Not Implemented",
244 "The requested method is not recognized by this server." },
Denis Vlasenko55a99402006-09-30 20:41:44 +0000245#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000246 { HTTP_UNAUTHORIZED, "Unauthorized", "" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000247#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000248 { HTTP_NOT_FOUND, "Not Found",
249 "The requested URL was not found on this server." },
250 { HTTP_BAD_REQUEST, "Bad Request", "Unsupported method." },
251 { HTTP_FORBIDDEN, "Forbidden", "" },
252 { HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error",
253 "Internal Server Error" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000254#if 0 /* not implemented */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000255 { HTTP_CREATED, "Created" },
256 { HTTP_ACCEPTED, "Accepted" },
257 { HTTP_NO_CONTENT, "No Content" },
258 { HTTP_MULTIPLE_CHOICES, "Multiple Choices" },
259 { HTTP_MOVED_PERMANENTLY, "Moved Permanently" },
260 { HTTP_NOT_MODIFIED, "Not Modified" },
261 { HTTP_BAD_GATEWAY, "Bad Gateway", "" },
262 { HTTP_SERVICE_UNAVAILABLE, "Service Unavailable", "" },
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000263#endif
264};
265
Glenn L McGrath06e95652003-02-09 06:51:14 +0000266
267static const char RFC1123FMT[] = "%a, %d %b %Y %H:%M:%S GMT";
268static const char Content_length[] = "Content-length:";
269
270
Denis Vlasenko55a99402006-09-30 20:41:44 +0000271static int scan_ip(const char **ep, unsigned int *ip, unsigned char endc)
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000272{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000273 const char *p = *ep;
274 int auto_mask = 8;
275 int j;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000276
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000277 *ip = 0;
278 for (j = 0; j < 4; j++) {
279 unsigned int octet;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000280
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000281 if ((*p < '0' || *p > '9') && (*p != '/' || j == 0) && *p != 0)
282 return -auto_mask;
283 octet = 0;
284 while (*p >= '0' && *p <= '9') {
285 octet *= 10;
286 octet += *p - '0';
287 if (octet > 255)
288 return -auto_mask;
289 p++;
290 }
291 if (*p == '.')
292 p++;
293 if (*p != '/' && *p != 0)
294 auto_mask += 8;
295 *ip = ((*ip) << 8) | octet;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000296 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000297 if (*p != 0) {
298 if (*p != endc)
299 return -auto_mask;
300 p++;
301 if (*p == 0)
302 return -auto_mask;
303 }
304 *ep = p;
305 return auto_mask;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000306}
307
Denis Vlasenko55a99402006-09-30 20:41:44 +0000308static int scan_ip_mask(const char *ipm, unsigned int *ip, unsigned int *mask)
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000309{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000310 int i;
311 unsigned int msk;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000312
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000313 i = scan_ip(&ipm, ip, '/');
314 if (i < 0)
315 return i;
316 if (*ipm) {
317 const char *p = ipm;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000318
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000319 i = 0;
320 while (*p) {
Denis Vlasenko92758142006-10-03 19:56:34 +0000321 if (*p < '0' || *p > '9') {
322 if (*p == '.') {
323 i = scan_ip(&ipm, mask, 0);
324 return i != 32;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000325 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000326 return -1;
327 }
328 i *= 10;
329 i += *p - '0';
330 p++;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000331 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000332 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000333 if (i > 32 || i < 0)
Denis Vlasenko92758142006-10-03 19:56:34 +0000334 return -1;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000335 msk = 0x80000000;
336 *mask = 0;
337 while (i > 0) {
338 *mask |= msk;
339 msk >>= 1;
340 i--;
341 }
342 return 0;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000343}
344
Denis Vlasenko55a99402006-09-30 20:41:44 +0000345#if ENABLE_FEATURE_HTTPD_BASIC_AUTH || ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000346static void free_config_lines(Htaccess **pprev)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000347{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000348 Htaccess *prev = *pprev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000349
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000350 while (prev) {
351 Htaccess *cur = prev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000352
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000353 prev = cur->next;
354 free(cur);
355 }
356 *pprev = NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000357}
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000358#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000359
Glenn L McGrath06e95652003-02-09 06:51:14 +0000360/* flag */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000361#define FIRST_PARSE 0
362#define SUBDIR_PARSE 1
363#define SIGNALED_PARSE 2
364#define FIND_FROM_HTTPD_ROOT 3
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000365/****************************************************************************
366 *
367 > $Function: parse_conf()
368 *
369 * $Description: parse configuration file into in-memory linked list.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000370 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000371 * The first non-white character is examined to determine if the config line
372 * is one of the following:
373 * .ext:mime/type # new mime type not compiled into httpd
374 * [adAD]:from # ip address allow/deny, * for wildcard
375 * /path:user:pass # username/password
376 *
377 * Any previous IP rules are discarded.
378 * If the flag argument is not SUBDIR_PARSE then all /path and mime rules
379 * are also discarded. That is, previous settings are retained if flag is
380 * SUBDIR_PARSE.
381 *
382 * $Parameters:
383 * (const char *) path . . null for ip address checks, path for password
384 * checks.
385 * (int) flag . . . . . . the source of the parse request.
386 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000387 * $Return: (None)
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000388 *
389 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000390static void parse_conf(const char *path, int flag)
391{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000392 FILE *f;
Denis Vlasenko55a99402006-09-30 20:41:44 +0000393#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000394 Htaccess *prev, *cur;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000395#elif ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000396 Htaccess *cur;
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000397#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000398
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000399 const char *cf = config->configFile;
400 char buf[160];
401 char *p0 = NULL;
402 char *c, *p;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000403
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000404 /* free previous ip setup if present */
405 Htaccess_IP *pip = config->ip_a_d;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000406
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000407 while (pip) {
408 Htaccess_IP *cur_ipl = pip;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000409
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000410 pip = cur_ipl->next;
411 free(cur_ipl);
412 }
413 config->ip_a_d = NULL;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000414
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000415 config->flg_deny_all = 0;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000416
Denis Vlasenko55a99402006-09-30 20:41:44 +0000417#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 +0000418 /* retain previous auth and mime config only for subdir parse */
419 if (flag != SUBDIR_PARSE) {
Denis Vlasenko55a99402006-09-30 20:41:44 +0000420#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000421 free_config_lines(&config->auth);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000422#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000423#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000424 free_config_lines(&config->mime_a);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000425#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000426#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000427 free_config_lines(&config->script_i);
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000428#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000429 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000430#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000431
432 if (flag == SUBDIR_PARSE || cf == NULL) {
433 cf = alloca(strlen(path) + sizeof(httpd_conf) + 2);
434 if (cf == NULL) {
435 if (flag == FIRST_PARSE)
436 bb_error_msg_and_die(bb_msg_memory_exhausted);
437 return;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000438 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000439 sprintf((char *)cf, "%s/%s", path, httpd_conf);
Glenn L McGrath393183d2003-05-26 14:07:50 +0000440 }
441
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000442 while ((f = fopen(cf, "r")) == NULL) {
443 if (flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) {
444 /* config file not found, no changes to config */
445 return;
446 }
447 if (config->configFile && flag == FIRST_PARSE) /* if -c option given */
448 bb_perror_msg_and_die("%s", cf);
449 flag = FIND_FROM_HTTPD_ROOT;
450 cf = httpd_conf;
451 }
452
Denis Vlasenko55a99402006-09-30 20:41:44 +0000453#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000454 prev = config->auth;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000455#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000456 /* This could stand some work */
Denis Vlasenko55a99402006-09-30 20:41:44 +0000457 while ((p0 = fgets(buf, sizeof(buf), f)) != NULL) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000458 c = NULL;
459 for (p = p0; *p0 != 0 && *p0 != '#'; p0++) {
460 if (!isspace(*p0)) {
461 *p++ = *p0;
462 if (*p0 == ':' && c == NULL)
463 c = p;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000464 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000465 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000466 *p = 0;
467
468 /* test for empty or strange line */
469 if (c == NULL || *c == 0)
470 continue;
471 p0 = buf;
472 if (*p0 == 'd')
473 *p0 = 'D';
474 if (*c == '*') {
475 if (*p0 == 'D') {
476 /* memorize deny all */
477 config->flg_deny_all++;
478 }
479 /* skip default other "word:*" config lines */
480 continue;
481 }
482
483 if (*p0 == 'a')
484 *p0 = 'A';
485 else if (*p0 != 'D' && *p0 != 'A'
Denis Vlasenko55a99402006-09-30 20:41:44 +0000486#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000487 && *p0 != '/'
488#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000489#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000490 && *p0 != '.'
491#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000492#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000493 && *p0 != '*'
494#endif
495 )
496 continue;
497 if (*p0 == 'A' || *p0 == 'D') {
498 /* storing current config IP line */
499 pip = calloc(1, sizeof(Htaccess_IP));
500 if (pip) {
Denis Vlasenko55a99402006-09-30 20:41:44 +0000501 if (scan_ip_mask(c, &(pip->ip), &(pip->mask))) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000502 /* syntax IP{/mask} error detected, protect all */
503 *p0 = 'D';
504 pip->mask = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000505 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000506 pip->allow_deny = *p0;
507 if (*p0 == 'D') {
508 /* Deny:form_IP move top */
509 pip->next = config->ip_a_d;
510 config->ip_a_d = pip;
511 } else {
512 /* add to bottom A:form_IP config line */
513 Htaccess_IP *prev_IP = config->ip_a_d;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000514
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000515 if (prev_IP == NULL) {
516 config->ip_a_d = pip;
517 } else {
518 while (prev_IP->next)
519 prev_IP = prev_IP->next;
520 prev_IP->next = pip;
521 }
522 }
523 }
524 continue;
525 }
Denis Vlasenko55a99402006-09-30 20:41:44 +0000526#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000527 if (*p0 == '/') {
528 /* make full path from httpd root / curent_path / config_line_path */
529 cf = flag == SUBDIR_PARSE ? path : "";
530 p0 = malloc(strlen(cf) + (c - buf) + 2 + strlen(c));
531 if (p0 == NULL)
532 continue;
533 c[-1] = 0;
534 sprintf(p0, "/%s%s", cf, buf);
535
536 /* another call bb_simplify_path */
537 cf = p = p0;
538
539 do {
540 if (*p == '/') {
541 if (*cf == '/') { /* skip duplicate (or initial) slash */
542 continue;
543 } else if (*cf == '.') {
544 if (cf[1] == '/' || cf[1] == 0) { /* remove extra '.' */
545 continue;
546 } else if ((cf[1] == '.') && (cf[2] == '/' || cf[2] == 0)) {
547 ++cf;
548 if (p > p0) {
Denis Vlasenko92758142006-10-03 19:56:34 +0000549 while (*--p != '/') /* omit previous dir */;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000550 }
551 continue;
552 }
553 }
554 }
555 *++p = *cf;
556 } while (*++cf);
557
558 if ((p == p0) || (*p != '/')) { /* not a trailing slash */
559 ++p; /* so keep last character */
560 }
561 *p = 0;
562 sprintf(p0, "%s:%s", p0, c);
563 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000564#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000565
Denis Vlasenko55a99402006-09-30 20:41:44 +0000566#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 +0000567 /* storing current config line */
568 cur = calloc(1, sizeof(Htaccess) + strlen(p0));
569 if (cur) {
570 cf = strcpy(cur->before_colon, p0);
571 c = strchr(cf, ':');
572 *c++ = 0;
573 cur->after_colon = c;
Denis Vlasenko55a99402006-09-30 20:41:44 +0000574#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000575 if (*cf == '.') {
576 /* config .mime line move top for overwrite previous */
577 cur->next = config->mime_a;
578 config->mime_a = cur;
579 continue;
580 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000581#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000582#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000583 if (*cf == '*' && cf[1] == '.') {
584 /* config script interpreter line move top for overwrite previous */
585 cur->next = config->script_i;
586 config->script_i = cur;
587 continue;
588 }
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000589#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000590#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000591 free(p0);
592 if (prev == NULL) {
593 /* first line */
594 config->auth = prev = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000595 } else {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000596 /* sort path, if current lenght eq or bigger then move up */
597 Htaccess *prev_hti = config->auth;
598 size_t l = strlen(cf);
599 Htaccess *hti;
600
601 for (hti = prev_hti; hti; hti = hti->next) {
602 if (l >= strlen(hti->before_colon)) {
603 /* insert before hti */
604 cur->next = hti;
605 if (prev_hti != hti) {
606 prev_hti->next = cur;
607 } else {
608 /* insert as top */
609 config->auth = cur;
610 }
611 break;
612 }
613 if (prev_hti != hti)
614 prev_hti = prev_hti->next;
615 }
616 if (!hti) { /* not inserted, add to bottom */
617 prev->next = cur;
618 prev = cur;
619 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000620 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000621#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000622 }
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000623#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000624 }
625 fclose(f);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000626}
627
Denis Vlasenko55a99402006-09-30 20:41:44 +0000628#if ENABLE_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000629/****************************************************************************
630 *
631 > $Function: encodeString()
632 *
633 * $Description: Given a string, html encode special characters.
634 * This is used for the -e command line option to provide an easy way
635 * for scripts to encode result data without confusing browsers. The
636 * returned string pointer is memory allocated by malloc().
637 *
638 * $Parameters:
639 * (const char *) string . . The first string to encode.
640 *
641 * $Return: (char *) . . . .. . . A pointer to the encoded string.
642 *
643 * $Errors: Returns a null string ("") if memory is not available.
644 *
645 ****************************************************************************/
646static char *encodeString(const char *string)
647{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000648 /* take the simple route and encode everything */
649 /* could possibly scan once to get length. */
650 int len = strlen(string);
651 char *out = malloc(len * 6 + 1);
652 char *p = out;
653 char ch;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000654
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000655 if (!out) return "";
656 while ((ch = *string++)) {
657 // very simple check for what to encode
658 if (isalnum(ch)) *p++ = ch;
659 else p += sprintf(p, "&#%d;", (unsigned char) ch);
660 }
661 *p = 0;
662 return out;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000663}
Glenn L McGrath06e95652003-02-09 06:51:14 +0000664#endif /* CONFIG_FEATURE_HTTPD_ENCODE_URL_STR */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000665
666/****************************************************************************
667 *
668 > $Function: decodeString()
669 *
670 * $Description: Given a URL encoded string, convert it to plain ascii.
671 * Since decoding always makes strings smaller, the decode is done in-place.
672 * Thus, callers should strdup() the argument if they do not want the
673 * argument modified. The return is the original pointer, allowing this
674 * function to be easily used as arguments to other functions.
675 *
676 * $Parameters:
677 * (char *) string . . . The first string to decode.
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000678 * (int) flag . . . 1 if require decode '+' as ' ' for CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000679 *
680 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
681 *
682 * $Errors: None
683 *
684 ****************************************************************************/
Eric Andersena3bb3e62003-06-26 09:05:32 +0000685static char *decodeString(char *orig, int flag_plus_to_space)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000686{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000687 /* note that decoded string is always shorter than original */
688 char *string = orig;
689 char *ptr = string;
Eric Andersena3bb3e62003-06-26 09:05:32 +0000690
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000691 while (*ptr) {
692 if (*ptr == '+' && flag_plus_to_space) { *string++ = ' '; ptr++; }
693 else if (*ptr != '%') *string++ = *ptr++;
694 else {
695 unsigned int value1, value2;
"Vladimir N. Oleynik"0bf67e82005-12-26 17:26:59 +0000696
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000697 ptr++;
698 if (sscanf(ptr, "%1X", &value1) != 1 ||
699 sscanf(ptr+1, "%1X", &value2) != 1) {
700 if (!flag_plus_to_space)
701 return NULL;
702 *string++ = '%';
703 } else {
704 value1 = value1 * 16 + value2;
705 if (value1 == '/' || value1 == 0)
706 return orig+1;
707 *string++ = value1;
708 ptr += 2;
709 }
710 }
711 }
712 *string = '\0';
713 return orig;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000714}
715
716
Denis Vlasenko55a99402006-09-30 20:41:44 +0000717#if ENABLE_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000718/****************************************************************************
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000719 * setenv helpers
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000720 ****************************************************************************/
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000721static void setenv1(const char *name, const char *value)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000722{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000723 if (!value)
724 value = "";
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000725 setenv(name, value, 1);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000726}
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000727static void setenv_long(const char *name, long value)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000728{
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000729 char buf[sizeof(value)*3 + 1];
730 sprintf(buf, "%ld", value);
731 setenv(name, buf, 1);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000732}
Eric Andersena3bb3e62003-06-26 09:05:32 +0000733#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000734
Denis Vlasenko55a99402006-09-30 20:41:44 +0000735#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000736/****************************************************************************
737 *
738 > $Function: decodeBase64()
739 *
740 > $Description: Decode a base 64 data stream as per rfc1521.
741 * Note that the rfc states that none base64 chars are to be ignored.
742 * Since the decode always results in a shorter size than the input, it is
743 * OK to pass the input arg as an output arg.
744 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000745 * $Parameter:
746 * (char *) Data . . . . A pointer to a base64 encoded string.
747 * Where to place the decoded data.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000748 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000749 * $Return: void
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000750 *
751 * $Errors: None
752 *
753 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000754static void decodeBase64(char *Data)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000755{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000756
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000757 const unsigned char *in = (const unsigned char *)Data;
758 // The decoded size will be at most 3/4 the size of the encoded
759 unsigned long ch = 0;
760 int i = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000761
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000762 while (*in) {
763 int t = *in++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000764
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000765 if (t >= '0' && t <= '9')
766 t = t - '0' + 52;
767 else if (t >= 'A' && t <= 'Z')
768 t = t - 'A';
769 else if (t >= 'a' && t <= 'z')
770 t = t - 'a' + 26;
771 else if (t == '+')
772 t = 62;
773 else if (t == '/')
774 t = 63;
775 else if (t == '=')
776 t = 0;
777 else
778 continue;
Glenn L McGrath874e3382003-05-14 12:11:36 +0000779
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000780 ch = (ch << 6) | t;
781 i++;
782 if (i == 4) {
783 *Data++ = (char) (ch >> 16);
784 *Data++ = (char) (ch >> 8);
785 *Data++ = (char) ch;
786 i = 0;
787 }
788 }
789 *Data = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000790}
791#endif
792
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000793
Denis Vlasenko55a99402006-09-30 20:41:44 +0000794#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000795/****************************************************************************
796 *
797 > $Function: openServer()
798 *
799 * $Description: create a listen server socket on the designated port.
800 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000801 * $Return: (int) . . . A connection socket. -1 for errors.
802 *
803 * $Errors: None
804 *
805 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000806static int openServer(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000807{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000808 struct sockaddr_in lsocket;
809 int fd;
810 int on = 1;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000811
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000812 /* create the socket right now */
813 /* inet_addr() returns a value that is already in network order */
814 memset(&lsocket, 0, sizeof(lsocket));
815 lsocket.sin_family = AF_INET;
816 lsocket.sin_addr.s_addr = INADDR_ANY;
817 lsocket.sin_port = htons(config->port);
818 fd = xsocket(AF_INET, SOCK_STREAM, 0);
819 /* tell the OS it's OK to reuse a previous address even though */
820 /* it may still be in a close down state. Allows bind to succeed. */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000821#ifdef SO_REUSEPORT
Denis Vlasenko55a99402006-09-30 20:41:44 +0000822 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *)&on, sizeof(on));
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000823#else
Denis Vlasenko55a99402006-09-30 20:41:44 +0000824 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000825#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000826 xbind(fd, (struct sockaddr *)&lsocket, sizeof(lsocket));
827 xlisten(fd, 9);
828 signal(SIGCHLD, SIG_IGN); /* prevent zombie (defunct) processes */
829 return fd;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000830}
Rob Landleya2d9a172006-04-28 19:38:04 +0000831#endif /* CONFIG_FEATURE_HTTPD_WITHOUT_INETD */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000832
833/****************************************************************************
834 *
835 > $Function: sendHeaders()
836 *
837 * $Description: Create and send HTTP response headers.
838 * The arguments are combined and sent as one write operation. Note that
839 * IE will puke big-time if the headers are not sent in one packet and the
Glenn L McGrath06e95652003-02-09 06:51:14 +0000840 * second packet is delayed for any reason.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000841 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000842 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000843 * (HttpResponseNum) responseNum . . . The result code to send.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000844 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000845 * $Return: (int) . . . . writing errors
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000846 *
847 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000848static int sendHeaders(HttpResponseNum responseNum)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000849{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000850 char *buf = config->buf;
851 const char *responseString = "";
852 const char *infoString = 0;
853 const char *mime_type;
854 unsigned int i;
855 time_t timer = time(0);
856 char timeStr[80];
857 int len;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000858
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000859 for (i = 0;
860 i < (sizeof(httpResponseNames)/sizeof(httpResponseNames[0])); i++) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000861 if (httpResponseNames[i].type == responseNum) {
862 responseString = httpResponseNames[i].name;
863 infoString = httpResponseNames[i].info;
864 break;
865 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000866 }
867 /* error message is HTML */
868 mime_type = responseNum == HTTP_OK ?
869 config->httpd_found.found_mime_type : "text/html";
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000870
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000871 /* emit the current date */
872 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&timer));
873 len = sprintf(buf,
874 "HTTP/1.0 %d %s\r\nContent-type: %s\r\n"
875 "Date: %s\r\nConnection: close\r\n",
876 responseNum, responseString, mime_type, timeStr);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000877
Denis Vlasenko55a99402006-09-30 20:41:44 +0000878#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000879 if (responseNum == HTTP_UNAUTHORIZED) {
880 len += sprintf(buf+len, "WWW-Authenticate: Basic realm=\"%s\"\r\n",
881 config->realm);
882 }
Glenn L McGrath3d2405c2003-02-10 22:28:21 +0000883#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000884 if (responseNum == HTTP_MOVED_TEMPORARILY) {
885 len += sprintf(buf+len, "Location: %s/%s%s\r\n",
886 config->httpd_found.found_moved_temporarily,
887 (config->query ? "?" : ""),
888 (config->query ? config->query : ""));
889 }
Eric Andersen07f2fea2004-10-08 08:03:29 +0000890
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000891 if (config->ContentLength != -1) { /* file */
892 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&config->last_mod));
Denis Vlasenko5c759602006-10-28 12:37:16 +0000893 len += sprintf(buf+len, "Last-Modified: %s\r\n%s %"OFF_FMT"\r\n",
Denis Vlasenko7039a662006-10-08 17:54:47 +0000894 timeStr, Content_length, (off_t) config->ContentLength);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000895 }
896 strcat(buf, "\r\n");
897 len += 2;
898 if (infoString) {
899 len += sprintf(buf+len,
900 "<HEAD><TITLE>%d %s</TITLE></HEAD>\n"
901 "<BODY><H1>%d %s</H1>\n%s\n</BODY>\n",
902 responseNum, responseString,
903 responseNum, responseString, infoString);
904 }
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000905#if DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000906 fprintf(stderr, "Headers: '%s'", buf);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000907#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000908 return full_write(a_c_w, buf, len);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000909}
910
911/****************************************************************************
912 *
913 > $Function: getLine()
914 *
915 * $Description: Read from the socket until an end of line char found.
916 *
917 * Characters are read one at a time until an eol sequence is found.
918 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000919 * $Return: (int) . . . . number of characters read. -1 if error.
920 *
921 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000922static int getLine(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000923{
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +0000924 int count = 0;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000925 char *buf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000926
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000927 while (read(a_c_r, buf + count, 1) == 1) {
928 if (buf[count] == '\r') continue;
929 if (buf[count] == '\n') {
930 buf[count] = 0;
931 return count;
932 }
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +0000933 if (count < (MAX_MEMORY_BUFF-1)) /* check overflow */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000934 count++;
935 }
936 if (count) return count;
937 else return -1;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000938}
939
Denis Vlasenko55a99402006-09-30 20:41:44 +0000940#if ENABLE_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000941/****************************************************************************
942 *
943 > $Function: sendCgi()
944 *
945 * $Description: Execute a CGI script and send it's stdout back
946 *
947 * Environment variables are set up and the script is invoked with pipes
948 * for stdin/stdout. If a post is being done the script is fed the POST
949 * data in addition to setting the QUERY_STRING variable (for GETs or POSTs).
950 *
951 * $Parameters:
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000952 * (const char *) url . . . . . . The requested URL (with leading /).
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000953 * (int bodyLen) . . . . . . . . Length of the post body.
954 * (const char *cookie) . . . . . For set HTTP_COOKIE.
955 * (const char *content_type) . . For set CONTENT_TYPE.
Glenn L McGrath06e95652003-02-09 06:51:14 +0000956
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000957 *
958 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
959 *
960 * $Errors: None
961 *
962 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000963static int sendCgi(const char *url,
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +0000964 const char *request, int bodyLen, const char *cookie,
965 const char *content_type)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000966{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000967 int fromCgi[2]; /* pipe for reading data from CGI */
968 int toCgi[2]; /* pipe for sending data to CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000969
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000970 static char * argp[] = { 0, 0 };
971 int pid = 0;
972 int inFd;
973 int outFd;
974 int firstLine = 1;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000975
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000976 do {
977 if (pipe(fromCgi) != 0) {
978 break;
979 }
980 if (pipe(toCgi) != 0) {
981 break;
982 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000983
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000984 pid = fork();
985 if (pid < 0) {
986 pid = 0;
987 break;
988 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000989
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000990 if (!pid) {
991 /* child process */
992 char *script;
993 char *purl = strdup(url);
994 char realpath_buff[MAXPATHLEN];
Glenn L McGrath06e95652003-02-09 06:51:14 +0000995
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000996 if (purl == NULL)
997 _exit(242);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000998
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000999 inFd = toCgi[0];
1000 outFd = fromCgi[1];
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001001
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001002 dup2(inFd, 0); // replace stdin with the pipe
1003 dup2(outFd, 1); // replace stdout with the pipe
1004 if (!DEBUG)
1005 dup2(outFd, 2); // replace stderr with the pipe
Glenn L McGrath06e95652003-02-09 06:51:14 +00001006
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001007 close(toCgi[0]);
1008 close(toCgi[1]);
1009 close(fromCgi[0]);
1010 close(fromCgi[1]);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001011
Denis Vlasenko9f609292006-11-05 19:47:33 +00001012 close(config->accepted_socket);
1013 close(config->server_socket);
1014
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001015 /*
1016 * Find PATH_INFO.
1017 */
1018 script = purl;
1019 while ((script = strchr(script + 1, '/')) != NULL) {
1020 /* have script.cgi/PATH_INFO or dirs/script.cgi[/PATH_INFO] */
1021 struct stat sb;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001022
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001023 *script = '\0';
1024 if (is_directory(purl + 1, 1, &sb) == 0) {
1025 /* not directory, found script.cgi/PATH_INFO */
1026 *script = '/';
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +00001027 break;
1028 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001029 *script = '/'; /* is directory, find next '/' */
1030 }
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +00001031 setenv1("PATH_INFO", script); /* set /PATH_INFO or "" */
1032 setenv1("PATH", getenv("PATH")); /* Huh?? */
1033 setenv1("REQUEST_METHOD", request);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001034 if (config->query) {
1035 char *uri = alloca(strlen(purl) + 2 + strlen(config->query));
1036 if (uri)
1037 sprintf(uri, "%s?%s", purl, config->query);
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +00001038 setenv1("REQUEST_URI", uri);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001039 } else {
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +00001040 setenv1("REQUEST_URI", purl);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001041 }
1042 if (script != NULL)
1043 *script = '\0'; /* reduce /PATH_INFO */
1044 /* SCRIPT_FILENAME required by PHP in CGI mode */
1045 if (realpath(purl + 1, realpath_buff))
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +00001046 setenv1("SCRIPT_FILENAME", realpath_buff);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001047 else
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +00001048 *realpath_buff = '\0';
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001049 /* set SCRIPT_NAME as full path: /cgi-bin/dirs/script.cgi */
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +00001050 setenv1("SCRIPT_NAME", purl);
1051 setenv1("QUERY_STRING", config->query);
1052 setenv1("SERVER_SOFTWARE", httpdVersion);
1053 putenv("SERVER_PROTOCOL=HTTP/1.0");
1054 putenv("GATEWAY_INTERFACE=CGI/1.1");
1055 setenv1("REMOTE_ADDR", config->rmt_ip_str);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001056#if ENABLE_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +00001057 setenv_long("REMOTE_PORT", config->port);
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +00001058#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001059 if (bodyLen) {
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +00001060 setenv_long("CONTENT_LENGTH", bodyLen);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001061 }
1062 if (cookie)
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +00001063 setenv1("HTTP_COOKIE", cookie);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001064 if (content_type)
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +00001065 setenv1("CONTENT_TYPE", content_type);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001066#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001067 if (config->remoteuser) {
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +00001068 setenv1("REMOTE_USER", config->remoteuser);
1069 putenv("AUTH_TYPE=Basic");
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001070 }
1071#endif
1072 if (config->referer)
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +00001073 setenv1("HTTP_REFERER", config->referer);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001074
1075 /* 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 if (*realpath_buff) {
1079 script = strrchr(realpath_buff, '/');
1080 if (script) {
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.
Denis Vlasenko55a99402006-09-30 20:41:44 +00001086#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001087 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;
1096 }
1097 }
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +00001098#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001099 *script = '/';
Denis Vlasenko55a99402006-09-30 20:41:44 +00001100#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001101 if (interpr)
1102 execv(interpr, argp);
1103 else
1104#endif
1105 execv(realpath_buff, argp);
1106 }
1107 }
1108 }
Denis Vlasenko55a99402006-09-30 20:41:44 +00001109#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001110 config->accepted_socket = 1; /* send to stdout */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001111#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001112 sendHeaders(HTTP_NOT_FOUND);
1113 _exit(242);
1114 } /* end child */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001115
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001116 } while (0);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001117
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001118 if (pid) {
1119 /* parent process */
1120 int status;
1121 size_t post_readed_size = 0, post_readed_idx = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001122
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001123 inFd = fromCgi[0];
1124 outFd = toCgi[1];
1125 close(fromCgi[1]);
1126 close(toCgi[0]);
1127 signal(SIGPIPE, SIG_IGN);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001128
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001129 while (1) {
1130 fd_set readSet;
1131 fd_set writeSet;
1132 char wbuf[128];
1133 int nfound;
1134 int count;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001135
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001136 FD_ZERO(&readSet);
1137 FD_ZERO(&writeSet);
1138 FD_SET(inFd, &readSet);
1139 if (bodyLen > 0 || post_readed_size > 0) {
1140 FD_SET(outFd, &writeSet);
1141 nfound = outFd > inFd ? outFd : inFd;
1142 if (post_readed_size == 0) {
1143 FD_SET(a_c_r, &readSet);
1144 if (nfound < a_c_r)
1145 nfound = a_c_r;
1146 }
1147 /* Now wait on the set of sockets! */
1148 nfound = select(nfound + 1, &readSet, &writeSet, 0, NULL);
1149 } else {
1150 if (!bodyLen) {
1151 close(outFd);
1152 bodyLen = -1;
1153 }
1154 nfound = select(inFd + 1, &readSet, 0, 0, NULL);
1155 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001156
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001157 if (nfound <= 0) {
1158 if (waitpid(pid, &status, WNOHANG) > 0) {
1159 close(inFd);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001160#if DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001161 if (WIFEXITED(status))
1162 bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status));
1163 if (WIFSIGNALED(status))
1164 bb_error_msg("piped has exited with signal=%d", WTERMSIG(status));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001165#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001166 break;
1167 }
1168 } else if (post_readed_size > 0 && FD_ISSET(outFd, &writeSet)) {
1169 count = full_write(outFd, wbuf + post_readed_idx, post_readed_size);
1170 if (count > 0) {
1171 post_readed_size -= count;
1172 post_readed_idx += count;
1173 if (post_readed_size == 0)
1174 post_readed_idx = 0;
1175 } else {
1176 post_readed_size = post_readed_idx = bodyLen = 0; /* broken pipe to CGI */
1177 }
1178 } else if (bodyLen > 0 && post_readed_size == 0 && FD_ISSET(a_c_r, &readSet)) {
1179 count = bodyLen > (int)sizeof(wbuf) ? (int)sizeof(wbuf) : bodyLen;
1180 count = safe_read(a_c_r, wbuf, count);
1181 if (count > 0) {
1182 post_readed_size += count;
1183 bodyLen -= count;
1184 } else {
1185 bodyLen = 0; /* closed */
1186 }
1187 }
1188 if (FD_ISSET(inFd, &readSet)) {
1189 int s = a_c_w;
1190 char *rbuf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001191
Eric Andersen97a1de12004-08-26 22:22:50 +00001192#ifndef PIPE_BUF
1193# define PIPESIZE 4096 /* amount of buffering in a pipe */
1194#else
1195# define PIPESIZE PIPE_BUF
1196#endif
1197#if PIPESIZE >= MAX_MEMORY_BUFF
1198# error "PIPESIZE >= MAX_MEMORY_BUFF"
1199#endif
1200
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001201 // There is something to read
1202 count = safe_read(inFd, rbuf, PIPESIZE);
1203 if (count == 0)
1204 break; /* closed */
1205 if (count > 0) {
1206 if (firstLine) {
1207 rbuf[count] = 0;
1208 /* check to see if the user script added headers */
1209 if (strncmp(rbuf, "HTTP/1.0 200 OK\r\n", 4) != 0) {
1210 full_write(s, "HTTP/1.0 200 OK\r\n", 17);
1211 }
1212 if (strstr(rbuf, "ontent-") == 0) {
1213 full_write(s, "Content-type: text/plain\r\n\r\n", 28);
1214 }
1215 firstLine = 0;
1216 }
1217 if (full_write(s, rbuf, count) != count)
1218 break;
Eric Andersenef437492004-02-04 11:10:28 +00001219
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001220#if DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001221 fprintf(stderr, "cgi read %d bytes\n", count);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001222#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001223 }
1224 }
1225 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001226 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001227 return 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001228}
Glenn L McGrath06e95652003-02-09 06:51:14 +00001229#endif /* CONFIG_FEATURE_HTTPD_CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001230
1231/****************************************************************************
1232 *
1233 > $Function: sendFile()
1234 *
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001235 * $Description: Send a file response to a HTTP request
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001236 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00001237 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001238 * (const char *) url . . The URL requested.
1239 *
1240 * $Return: (int) . . . . . . Always 0.
1241 *
1242 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001243static int sendFile(const char *url)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001244{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001245 char * suffix;
1246 int f;
1247 const char * const * table;
1248 const char * try_suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001249
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001250 suffix = strrchr(url, '.');
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001251
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001252 for (table = suffixTable; *table; table += 2)
1253 if (suffix != NULL && (try_suffix = strstr(*table, suffix)) != 0) {
1254 try_suffix += strlen(suffix);
1255 if (*try_suffix == 0 || *try_suffix == '.')
1256 break;
1257 }
1258 /* also, if not found, set default as "application/octet-stream"; */
1259 config->httpd_found.found_mime_type = *(table+1);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001260#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001261 if (suffix) {
1262 Htaccess * cur;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001263
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001264 for (cur = config->mime_a; cur; cur = cur->next) {
1265 if (strcmp(cur->before_colon, suffix) == 0) {
1266 config->httpd_found.found_mime_type = cur->after_colon;
1267 break;
1268 }
1269 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001270 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001271#endif /* CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES */
1272
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001273#if DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001274 fprintf(stderr, "Sending file '%s' Content-type: %s\n",
1275 url, config->httpd_found.found_mime_type);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001276#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001277
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001278 f = open(url, O_RDONLY);
1279 if (f >= 0) {
1280 int count;
1281 char *buf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001282
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001283 sendHeaders(HTTP_OK);
1284 while ((count = full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
1285 if (full_write(a_c_w, buf, count) != count)
1286 break;
1287 }
1288 close(f);
1289 } else {
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001290#if DEBUG
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001291 bb_perror_msg("unable to open '%s'", url);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001292#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001293 sendHeaders(HTTP_NOT_FOUND);
1294 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001295
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001296 return 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001297}
1298
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001299static int checkPermIP(void)
1300{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001301 Htaccess_IP * cur;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001302
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001303 /* This could stand some work */
1304 for (cur = config->ip_a_d; cur; cur = cur->next) {
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001305#if DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001306 fprintf(stderr, "checkPermIP: '%s' ? ", config->rmt_ip_str);
1307 fprintf(stderr, "'%u.%u.%u.%u/%u.%u.%u.%u'\n",
1308 (unsigned char)(cur->ip >> 24),
1309 (unsigned char)(cur->ip >> 16),
1310 (unsigned char)(cur->ip >> 8),
1311 cur->ip & 0xff,
1312 (unsigned char)(cur->mask >> 24),
1313 (unsigned char)(cur->mask >> 16),
1314 (unsigned char)(cur->mask >> 8),
1315 cur->mask & 0xff);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001316#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001317 if ((config->rmt_ip & cur->mask) == cur->ip)
1318 return cur->allow_deny == 'A'; /* Allow/Deny */
1319 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001320
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001321 /* if unconfigured, return 1 - access from all */
1322 return !config->flg_deny_all;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001323}
1324
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001325/****************************************************************************
1326 *
1327 > $Function: checkPerm()
1328 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001329 * $Description: Check the permission file for access password protected.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001330 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001331 * If config file isn't present, everything is allowed.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001332 * Entries are of the form you can see example from header source
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001333 *
1334 * $Parameters:
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001335 * (const char *) path . . . . The file path.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001336 * (const char *) request . . . User information to validate.
1337 *
1338 * $Return: (int) . . . . . . . . . 1 if request OK, 0 otherwise.
1339 *
1340 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001341
Denis Vlasenko55a99402006-09-30 20:41:44 +00001342#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001343static int checkPerm(const char *path, const char *request)
1344{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001345 Htaccess * cur;
1346 const char *p;
1347 const char *p0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001348
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001349 const char *prev = NULL;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001350
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001351 /* This could stand some work */
1352 for (cur = config->auth; cur; cur = cur->next) {
1353 p0 = cur->before_colon;
1354 if (prev != NULL && strcmp(prev, p0) != 0)
1355 continue; /* find next identical */
1356 p = cur->after_colon;
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001357#if DEBUG
Denis Vlasenko55a99402006-09-30 20:41:44 +00001358 fprintf(stderr, "checkPerm: '%s' ? '%s'\n", p0, request);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001359#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001360 {
1361 size_t l = strlen(p0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001362
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001363 if (strncmp(p0, path, l) == 0 &&
1364 (l == 1 || path[l] == '/' || path[l] == 0)) {
1365 char *u;
1366 /* path match found. Check request */
1367 /* for check next /path:user:password */
1368 prev = p0;
1369 u = strchr(request, ':');
1370 if (u == NULL) {
1371 /* bad request, ':' required */
1372 break;
1373 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001374
Denis Vlasenko55a99402006-09-30 20:41:44 +00001375#if ENABLE_FEATURE_HTTPD_AUTH_MD5
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001376 {
1377 char *cipher;
1378 char *pp;
Eric Andersen35e643b2003-07-28 07:40:39 +00001379
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001380 if (strncmp(p, request, u-request) != 0) {
1381 /* user uncompared */
1382 continue;
1383 }
1384 pp = strchr(p, ':');
1385 if (pp && pp[1] == '$' && pp[2] == '1' &&
1386 pp[3] == '$' && pp[4]) {
1387 pp++;
1388 cipher = pw_encrypt(u+1, pp);
1389 if (strcmp(cipher, pp) == 0)
1390 goto set_remoteuser_var; /* Ok */
1391 /* unauthorized */
1392 continue;
1393 }
1394 }
Eric Andersen35e643b2003-07-28 07:40:39 +00001395#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001396 if (strcmp(p, request) == 0) {
Denis Vlasenko55a99402006-09-30 20:41:44 +00001397#if ENABLE_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001398set_remoteuser_var:
Glenn L McGrath9d1a33c2003-10-06 13:23:06 +00001399#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001400 config->remoteuser = strdup(request);
1401 if (config->remoteuser)
1402 config->remoteuser[(u - request)] = 0;
1403 return 1; /* Ok */
1404 }
1405 /* unauthorized */
1406 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001407 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001408 } /* for */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001409
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001410 return prev == NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001411}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001412
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001413#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1414
Eric Andersen07f2fea2004-10-08 08:03:29 +00001415/****************************************************************************
1416 *
Mike Frysingerbb12d6f2006-01-03 23:59:01 +00001417 > $Function: handle_sigalrm()
Eric Andersen07f2fea2004-10-08 08:03:29 +00001418 *
Mike Frysingerbb12d6f2006-01-03 23:59:01 +00001419 * $Description: Handle timeouts
Eric Andersen07f2fea2004-10-08 08:03:29 +00001420 *
1421 ****************************************************************************/
1422
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001423static void handle_sigalrm(int sig)
Eric Andersen07f2fea2004-10-08 08:03:29 +00001424{
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001425 sendHeaders(HTTP_REQUEST_TIMEOUT);
1426 config->alarm_signaled = sig;
Eric Andersen07f2fea2004-10-08 08:03:29 +00001427}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001428
1429/****************************************************************************
1430 *
1431 > $Function: handleIncoming()
1432 *
1433 * $Description: Handle an incoming http request.
1434 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001435 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001436static void handleIncoming(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001437{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001438 char *buf = config->buf;
1439 char *url;
1440 char *purl;
1441 int blank = -1;
1442 char *test;
1443 struct stat sb;
1444 int ip_allowed;
Denis Vlasenko55a99402006-09-30 20:41:44 +00001445#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001446 const char *prequest = request_GET;
1447 long length=0;
1448 char *cookie = 0;
1449 char *content_type = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001450#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001451 fd_set s_fd;
1452 struct timeval tv;
1453 int retval;
1454 struct sigaction sa;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001455
Denis Vlasenko55a99402006-09-30 20:41:44 +00001456#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001457 int credentials = -1; /* if not required this is Ok */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001458#endif
1459
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001460 sa.sa_handler = handle_sigalrm;
1461 sigemptyset(&sa.sa_mask);
1462 sa.sa_flags = 0; /* no SA_RESTART */
1463 sigaction(SIGALRM, &sa, NULL);
Eric Andersen07f2fea2004-10-08 08:03:29 +00001464
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001465 do {
1466 int count;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001467
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001468 (void) alarm(TIMEOUT);
1469 if (getLine() <= 0)
1470 break; /* closed */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001471
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001472 purl = strpbrk(buf, " \t");
1473 if (purl == NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001474BAD_REQUEST:
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001475 sendHeaders(HTTP_BAD_REQUEST);
1476 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001477 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001478 *purl = 0;
Denis Vlasenko55a99402006-09-30 20:41:44 +00001479#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001480 if (strcasecmp(buf, prequest) != 0) {
1481 prequest = "POST";
1482 if (strcasecmp(buf, prequest) != 0) {
1483 sendHeaders(HTTP_NOT_IMPLEMENTED);
1484 break;
1485 }
1486 }
1487#else
1488 if (strcasecmp(buf, request_GET) != 0) {
1489 sendHeaders(HTTP_NOT_IMPLEMENTED);
1490 break;
1491 }
1492#endif
1493 *purl = ' ';
1494 count = sscanf(purl, " %[^ ] HTTP/%d.%*d", buf, &blank);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001495
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001496 if (count < 1 || buf[0] != '/') {
1497 /* Garbled request/URL */
1498 goto BAD_REQUEST;
1499 }
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001500 url = alloca(strlen(buf) + sizeof("/index.html"));
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001501 if (url == NULL) {
1502 sendHeaders(HTTP_INTERNAL_SERVER_ERROR);
1503 break;
1504 }
1505 strcpy(url, buf);
1506 /* extract url args if present */
1507 test = strchr(url, '?');
1508 if (test) {
1509 *test++ = 0;
1510 config->query = test;
1511 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001512
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001513 test = decodeString(url, 0);
1514 if (test == NULL)
1515 goto BAD_REQUEST;
1516 if (test == (buf+1)) {
1517 sendHeaders(HTTP_NOT_FOUND);
1518 break;
1519 }
1520 /* algorithm stolen from libbb bb_simplify_path(),
Denis Vlasenko55a99402006-09-30 20:41:44 +00001521 but don't strdup and reducing trailing slash and protect out root */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001522 purl = test = url;
1523
1524 do {
1525 if (*purl == '/') {
1526 if (*test == '/') { /* skip duplicate (or initial) slash */
1527 continue;
1528 } else if (*test == '.') {
1529 if (test[1] == '/' || test[1] == 0) { /* skip extra '.' */
1530 continue;
1531 } else if ((test[1] == '.') && (test[2] == '/' || test[2] == 0)) {
1532 ++test;
1533 if (purl == url) {
1534 /* protect out root */
1535 goto BAD_REQUEST;
1536 }
Denis Vlasenko92758142006-10-03 19:56:34 +00001537 while (*--purl != '/') /* omit previous dir */;
1538 continue;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001539 }
1540 }
1541 }
1542 *++purl = *test;
1543 } while (*++test);
1544
1545 *++purl = 0; /* so keep last character */
1546 test = purl; /* end ptr */
1547
1548 /* If URL is directory, adding '/' */
1549 if (test[-1] != '/') {
1550 if (is_directory(url + 1, 1, &sb)) {
1551 config->httpd_found.found_moved_temporarily = url;
1552 }
1553 }
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001554#if DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001555 fprintf(stderr, "url='%s', args=%s\n", url, config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001556#endif
1557
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001558 test = url;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001559 ip_allowed = checkPermIP();
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001560 while (ip_allowed && (test = strchr(test + 1, '/')) != NULL) {
1561 /* have path1/path2 */
1562 *test = '\0';
1563 if (is_directory(url + 1, 1, &sb)) {
1564 /* may be having subdir config */
1565 parse_conf(url + 1, SUBDIR_PARSE);
1566 ip_allowed = checkPermIP();
1567 }
1568 *test = '/';
1569 }
1570 if (blank >= 0) {
1571 // read until blank line for HTTP version specified, else parse immediate
1572 while (1) {
1573 alarm(TIMEOUT);
1574 count = getLine();
1575 if (count <= 0)
1576 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001577
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001578#if DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001579 fprintf(stderr, "Header: '%s'\n", buf);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001580#endif
1581
Denis Vlasenko55a99402006-09-30 20:41:44 +00001582#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001583 /* try and do our best to parse more lines */
1584 if ((strncasecmp(buf, Content_length, 15) == 0)) {
1585 if (prequest != request_GET)
1586 length = strtol(buf + 15, 0, 0); // extra read only for POST
1587 } else if ((strncasecmp(buf, "Cookie:", 7) == 0)) {
1588 for (test = buf + 7; isspace(*test); test++)
1589 ;
1590 cookie = strdup(test);
1591 } else if ((strncasecmp(buf, "Content-Type:", 13) == 0)) {
1592 for (test = buf + 13; isspace(*test); test++)
1593 ;
1594 content_type = strdup(test);
1595 } else if ((strncasecmp(buf, "Referer:", 8) == 0)) {
1596 for (test = buf + 8; isspace(*test); test++)
1597 ;
1598 config->referer = strdup(test);
1599 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001600#endif
1601
Denis Vlasenko55a99402006-09-30 20:41:44 +00001602#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001603 if (strncasecmp(buf, "Authorization:", 14) == 0) {
1604 /* We only allow Basic credentials.
1605 * It shows up as "Authorization: Basic <userid:password>" where
1606 * the userid:password is base64 encoded.
1607 */
1608 for (test = buf + 14; isspace(*test); test++)
1609 ;
1610 if (strncasecmp(test, "Basic", 5) != 0)
1611 continue;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001612
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001613 test += 5; /* decodeBase64() skiping space self */
1614 decodeBase64(test);
1615 credentials = checkPerm(url, test);
1616 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001617#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1618
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001619 } /* while extra header reading */
1620 }
1621 (void) alarm(0);
1622 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) {
1626 /* protect listing [/path]/httpd_conf or IP deny */
Denis Vlasenko55a99402006-09-30 20:41:44 +00001627#if ENABLE_FEATURE_HTTPD_CGI
Glenn L McGrath06e95652003-02-09 06:51:14 +00001628FORBIDDEN: /* protect listing /cgi-bin */
1629#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
1641 if (config->httpd_found.found_moved_temporarily) {
1642 sendHeaders(HTTP_MOVED_TEMPORARILY);
1643#if DEBUG
1644 /* clear unforked memory flag */
1645 config->httpd_found.found_moved_temporarily = NULL;
1646#endif
1647 break;
1648 }
1649
1650 test = url + 1; /* skip first '/' */
1651
Denis Vlasenko55a99402006-09-30 20:41:44 +00001652#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001653 /* if strange Content-Length */
1654 if (length < 0)
1655 break;
1656
1657 if (strncmp(test, "cgi-bin", 7) == 0) {
1658 if (test[7] == '/' && test[8] == 0)
1659 goto FORBIDDEN; // protect listing cgi-bin/
1660 sendCgi(url, prequest, length, cookie, content_type);
1661 } else {
1662 if (prequest != request_GET)
1663 sendHeaders(HTTP_NOT_IMPLEMENTED);
1664 else {
1665#endif /* CONFIG_FEATURE_HTTPD_CGI */
1666 if (purl[-1] == '/')
1667 strcpy(purl, "index.html");
1668 if (stat(test, &sb) == 0) {
1669 config->ContentLength = sb.st_size;
1670 config->last_mod = sb.st_mtime;
1671 }
1672 sendFile(test);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001673#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001674 /* unset if non inetd looped */
1675 config->ContentLength = -1;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001676#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +00001677#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001678 }
1679 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001680#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001681 } while (0);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001682
Denis Vlasenko55a99402006-09-30 20:41:44 +00001683#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
1684/* from inetd don't looping: freeing, closing automatic from exit always */
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001685# if DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001686 fprintf(stderr, "closing socket\n");
Glenn L McGrath06e95652003-02-09 06:51:14 +00001687# endif
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001688# if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001689 free(cookie);
1690 free(content_type);
1691 free(config->referer);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001692#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001693 free(config->remoteuser);
Eric Andersen769a3ef2003-12-19 11:23:47 +00001694#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001695# endif
Rob Landleya2d9a172006-04-28 19:38:04 +00001696#endif /* CONFIG_FEATURE_HTTPD_WITHOUT_INETD */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001697 shutdown(a_c_w, SHUT_WR);
Eric Andersend8746cd2004-02-24 07:28:38 +00001698
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001699 /* Properly wait for remote to closed */
Denis Vlasenko55a99402006-09-30 20:41:44 +00001700 FD_ZERO(&s_fd);
1701 FD_SET(a_c_r, &s_fd);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001702
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001703 do {
Denis Vlasenko55a99402006-09-30 20:41:44 +00001704 tv.tv_sec = 2;
1705 tv.tv_usec = 0;
1706 retval = select(a_c_r + 1, &s_fd, NULL, NULL, &tv);
1707 } while (retval > 0 && read(a_c_r, buf, sizeof(config->buf) > 0));
Eric Andersend8746cd2004-02-24 07:28:38 +00001708
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001709 shutdown(a_c_r, SHUT_RD);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001710#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001711 close(config->accepted_socket);
Rob Landleya2d9a172006-04-28 19:38:04 +00001712#endif /* CONFIG_FEATURE_HTTPD_WITHOUT_INETD */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001713}
1714
1715/****************************************************************************
1716 *
1717 > $Function: miniHttpd()
1718 *
1719 * $Description: The main http server function.
1720 *
1721 * Given an open socket fildes, listen for new connections and farm out
1722 * the processing as a forked process.
1723 *
1724 * $Parameters:
1725 * (int) server. . . The server socket fildes.
1726 *
1727 * $Return: (int) . . . . Always 0.
1728 *
1729 ****************************************************************************/
Denis Vlasenko55a99402006-09-30 20:41:44 +00001730#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001731static 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;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001754
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001755 config->accepted_socket = s;
1756 config->rmt_ip = ntohl(fromAddr.sin_addr.s_addr);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001757#if ENABLE_FEATURE_HTTPD_CGI || DEBUG
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001758 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1759 (unsigned char)(config->rmt_ip >> 24),
1760 (unsigned char)(config->rmt_ip >> 16),
1761 (unsigned char)(config->rmt_ip >> 8),
1762 config->rmt_ip & 0xff);
1763 config->port = ntohs(fromAddr.sin_port);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001764#if DEBUG
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001765 bb_error_msg("connection from IP=%s, port %u",
1766 config->rmt_ip_str, config->port);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001767#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001768#endif /* CONFIG_FEATURE_HTTPD_CGI */
1769
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001770 /* set the KEEPALIVE option to cull dead connections */
1771 on = 1;
1772 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on));
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001773#if !DEBUG
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001774 if (fork() == 0)
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001775#endif
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001776 {
1777 /* This is the spawned thread */
Denis Vlasenko55a99402006-09-30 20:41:44 +00001778#if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001779 /* protect reload config, may be confuse checking */
1780 signal(SIGHUP, SIG_IGN);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001781#endif
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001782 handleIncoming();
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001783#if !DEBUG
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001784 exit(0);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001785#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001786 }
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001787 close(s);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001788 } // while (1)
1789 return 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001790}
1791
Glenn L McGrath06e95652003-02-09 06:51:14 +00001792#else
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001793 /* from inetd */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001794
1795static int miniHttpd(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001796{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001797 struct sockaddr_in fromAddrLen;
Denis Vlasenko55a99402006-09-30 20:41:44 +00001798 socklen_t sinlen = sizeof(struct sockaddr_in);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001799
Denis Vlasenko55a99402006-09-30 20:41:44 +00001800 getpeername(0, (struct sockaddr *)&fromAddrLen, &sinlen);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001801 config->rmt_ip = ntohl(fromAddrLen.sin_addr.s_addr);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001802#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001803 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1804 (unsigned char)(config->rmt_ip >> 24),
1805 (unsigned char)(config->rmt_ip >> 16),
1806 (unsigned char)(config->rmt_ip >> 8),
1807 config->rmt_ip & 0xff);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001808#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001809 config->port = ntohs(fromAddrLen.sin_port);
1810 handleIncoming();
1811 return 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001812}
Rob Landleya2d9a172006-04-28 19:38:04 +00001813#endif /* CONFIG_FEATURE_HTTPD_WITHOUT_INETD */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001814
Denis Vlasenko55a99402006-09-30 20:41:44 +00001815#if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
Glenn L McGrath06e95652003-02-09 06:51:14 +00001816static void sighup_handler(int sig)
1817{
1818 /* set and reset */
1819 struct sigaction sa;
1820
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001821 parse_conf(default_path_httpd_conf, sig == SIGHUP ? SIGNALED_PARSE : FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001822 sa.sa_handler = sighup_handler;
1823 sigemptyset(&sa.sa_mask);
1824 sa.sa_flags = SA_RESTART;
1825 sigaction(SIGHUP, &sa, NULL);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001826}
1827#endif
1828
Denis Vlasenko9f609292006-11-05 19:47:33 +00001829enum {
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001830 c_opt_config_file = 0,
1831 d_opt_decode_url,
1832 h_opt_home_httpd,
1833 USE_FEATURE_HTTPD_ENCODE_URL_STR(e_opt_encode_url,)
Denis Vlasenko9f609292006-11-05 19:47:33 +00001834 USE_FEATURE_HTTPD_BASIC_AUTH( r_opt_realm ,)
1835 USE_FEATURE_HTTPD_AUTH_MD5( m_opt_md5 ,)
1836 USE_FEATURE_HTTPD_SETUID( u_opt_setuid ,)
1837 USE_FEATURE_HTTPD_WITHOUT_INETD( p_opt_port ,)
1838 OPT_CONFIG_FILE = 1 << c_opt_config_file,
1839 OPT_DECODE_URL = 1 << d_opt_decode_url,
1840 OPT_HOME_HTTPD = 1 << h_opt_home_httpd,
1841 OPT_ENCODE_URL = USE_FEATURE_HTTPD_ENCODE_URL_STR((1 << e_opt_encode_url)) + 0,
1842 OPT_REALM = USE_FEATURE_HTTPD_BASIC_AUTH( (1 << r_opt_realm )) + 0,
1843 OPT_MD5 = USE_FEATURE_HTTPD_AUTH_MD5( (1 << m_opt_md5 )) + 0,
1844 OPT_SETUID = USE_FEATURE_HTTPD_SETUID( (1 << u_opt_setuid )) + 0,
1845 OPT_PORT = USE_FEATURE_HTTPD_WITHOUT_INETD( (1 << p_opt_port )) + 0,
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001846};
Eric Andersena3bb3e62003-06-26 09:05:32 +00001847
Denis Vlasenko55a99402006-09-30 20:41:44 +00001848static const char httpd_opts[] = "c:d:h:"
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001849 USE_FEATURE_HTTPD_ENCODE_URL_STR("e:")
1850 USE_FEATURE_HTTPD_BASIC_AUTH("r:")
1851 USE_FEATURE_HTTPD_AUTH_MD5("m:")
1852 USE_FEATURE_HTTPD_SETUID("u:")
Rob Landleya2d9a172006-04-28 19:38:04 +00001853 USE_FEATURE_HTTPD_WITHOUT_INETD("p:");
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001854
Eric Andersena3bb3e62003-06-26 09:05:32 +00001855
Glenn L McGrath06e95652003-02-09 06:51:14 +00001856int httpd_main(int argc, char *argv[])
Glenn L McGrath06e95652003-02-09 06:51:14 +00001857{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001858 unsigned opt;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001859 const char *home_httpd = home;
1860 char *url_for_decode;
1861 USE_FEATURE_HTTPD_ENCODE_URL_STR(const char *url_for_encode;)
1862 USE_FEATURE_HTTPD_WITHOUT_INETD(const char *s_port;)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001863
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001864 USE_FEATURE_HTTPD_SETUID(const char *s_ugid = NULL;)
1865 USE_FEATURE_HTTPD_SETUID(struct bb_uidgid_t ugid;)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001866
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001867 USE_FEATURE_HTTPD_AUTH_MD5(const char *pass;)
Eric Andersen35e643b2003-07-28 07:40:39 +00001868
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
1873
Denis Vlasenko55a99402006-09-30 20:41:44 +00001874#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001875 config->port = 80;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001876#endif
1877
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001878 config->ContentLength = -1;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001879
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001880 opt = getopt32(argc, argv, httpd_opts,
Eric Andersena3bb3e62003-06-26 09:05:32 +00001881 &(config->configFile), &url_for_decode, &home_httpd
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001882 USE_FEATURE_HTTPD_ENCODE_URL_STR(, &url_for_encode)
1883 USE_FEATURE_HTTPD_BASIC_AUTH(, &(config->realm))
1884 USE_FEATURE_HTTPD_AUTH_MD5(, &pass)
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001885 USE_FEATURE_HTTPD_SETUID(, &s_ugid)
Rob Landleya2d9a172006-04-28 19:38:04 +00001886 USE_FEATURE_HTTPD_WITHOUT_INETD(, &s_port)
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001887 );
Eric Andersena3bb3e62003-06-26 09:05:32 +00001888
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001889 if (opt & OPT_DECODE_URL) {
1890 printf("%s", decodeString(url_for_decode, 1));
1891 return 0;
1892 }
Denis Vlasenko55a99402006-09-30 20:41:44 +00001893#if ENABLE_FEATURE_HTTPD_ENCODE_URL_STR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001894 if (opt & OPT_ENCODE_URL) {
1895 printf("%s", encodeString(url_for_encode));
1896 return 0;
1897 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001898#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +00001899#if ENABLE_FEATURE_HTTPD_AUTH_MD5
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001900 if (opt & OPT_MD5) {
Denis Vlasenko55a99402006-09-30 20:41:44 +00001901 puts(pw_encrypt(pass, "$1$"));
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001902 return 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001903 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001904#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +00001905#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001906 if (opt & OPT_PORT)
Denis Vlasenko13858992006-10-08 12:49:22 +00001907 config->port = xatou16(s_port);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001908#if ENABLE_FEATURE_HTTPD_SETUID
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001909 if (opt & OPT_SETUID) {
1910 char *e;
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001911 // FIXME: what the default group should be?
1912 ugid.gid = -1;
1913 ugid.uid = strtoul(s_ugid, &e, 0);
1914 if (*e == ':') {
1915 e++;
1916 ugid.gid = strtoul(e, &e, 0);
1917 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001918 if (*e != '\0') {
1919 /* not integer */
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001920 if (!uidgid_get(&ugid, s_ugid))
1921 bb_error_msg_and_die("unrecognized user[:group] "
1922 "name '%s'", s_ugid);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001923 }
1924 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001925#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00001926#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001927
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001928 xchdir(home_httpd);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001929#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Denis Vlasenko9f609292006-11-05 19:47:33 +00001930 config->server_socket = openServer();
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001931# if ENABLE_FEATURE_HTTPD_SETUID
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001932 /* drop privileges */
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001933 if (opt & OPT_SETUID) {
1934 if (ugid.gid != (gid_t)-1) {
1935 // FIXME: needed?
1936 //if (setgroups(1, &ugid.gid) == -1)
1937 // bb_perror_msg_and_die("setgroups");
1938 xsetgid(ugid.gid);
1939 }
1940 xsetuid(ugid.uid);
1941 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001942# endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001943#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001944
Denis Vlasenko55a99402006-09-30 20:41:44 +00001945#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001946 {
1947 char *p = getenv("PATH");
1948 if (p) {
1949 p = xstrdup(p);
1950 }
1951 clearenv();
1952 if (p)
1953 setenv("PATH", p, 1);
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001954# if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +00001955 setenv_long("SERVER_PORT", config->port);
Glenn L McGrath14092a12003-09-12 00:44:50 +00001956# endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001957 }
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00001958#endif
1959
Denis Vlasenko55a99402006-09-30 20:41:44 +00001960#if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001961 sighup_handler(0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001962#else
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001963 parse_conf(default_path_httpd_conf, FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001964#endif
1965
Denis Vlasenko55a99402006-09-30 20:41:44 +00001966#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001967# if !DEBUG
Denis Vlasenko9f609292006-11-05 19:47:33 +00001968 xdaemon(1, 0); /* don't change current directory */
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001969# endif
Denis Vlasenko9f609292006-11-05 19:47:33 +00001970 return miniHttpd(config->server_socket);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001971#else
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001972 return miniHttpd();
Glenn L McGrath06e95652003-02-09 06:51:14 +00001973#endif
1974}