blob: b982cb12f898f91469e115030fadec8c1d533c5b [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 Vlasenko8b8c75e2006-09-26 10:07:41 +0000164 int accepted_socket;
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000165# define a_c_r config->accepted_socket
166# define a_c_w config->accepted_socket
Glenn L McGrath06e95652003-02-09 06:51:14 +0000167#else
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000168# define a_c_r 0
169# define a_c_w 1
Glenn L McGrath06e95652003-02-09 06:51:14 +0000170#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000171 volatile int alarm_signaled;
Eric Andersen07f2fea2004-10-08 08:03:29 +0000172
Denis Vlasenko55a99402006-09-30 20:41:44 +0000173#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000174 Htaccess *script_i; /* config script interpreters */
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000175#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000176} HttpdConfig;
177
178static HttpdConfig *config;
179
Mike Frysingerfa6c4842006-05-26 01:48:17 +0000180static const char request_GET[] = "GET"; /* size algorithmic optimize */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000181
182static const char* const suffixTable [] = {
Eric Andersenaff114c2004-04-14 17:51:38 +0000183/* Warning: shorted equivalent suffix in one line must be first */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000184 ".htm.html", "text/html",
185 ".jpg.jpeg", "image/jpeg",
186 ".gif", "image/gif",
187 ".png", "image/png",
188 ".txt.h.c.cc.cpp", "text/plain",
189 ".css", "text/css",
190 ".wav", "audio/wav",
191 ".avi", "video/x-msvideo",
192 ".qt.mov", "video/quicktime",
193 ".mpe.mpeg", "video/mpeg",
194 ".mid.midi", "audio/midi",
195 ".mp3", "audio/mpeg",
Glenn L McGrath06e95652003-02-09 06:51:14 +0000196#if 0 /* unpopular */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000197 ".au", "audio/basic",
198 ".pac", "application/x-ns-proxy-autoconfig",
199 ".vrml.wrl", "model/vrml",
Glenn L McGrath06e95652003-02-09 06:51:14 +0000200#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000201 0, "application/octet-stream" /* default */
202};
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000203
Denis Vlasenko55a99402006-09-30 20:41:44 +0000204typedef enum {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000205 HTTP_OK = 200,
206 HTTP_MOVED_TEMPORARILY = 302,
207 HTTP_BAD_REQUEST = 400, /* malformed syntax */
208 HTTP_UNAUTHORIZED = 401, /* authentication needed, respond with auth hdr */
209 HTTP_NOT_FOUND = 404,
210 HTTP_FORBIDDEN = 403,
211 HTTP_REQUEST_TIMEOUT = 408,
212 HTTP_NOT_IMPLEMENTED = 501, /* used for unrecognized requests */
213 HTTP_INTERNAL_SERVER_ERROR = 500,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000214#if 0 /* future use */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000215 HTTP_CONTINUE = 100,
216 HTTP_SWITCHING_PROTOCOLS = 101,
217 HTTP_CREATED = 201,
218 HTTP_ACCEPTED = 202,
219 HTTP_NON_AUTHORITATIVE_INFO = 203,
220 HTTP_NO_CONTENT = 204,
221 HTTP_MULTIPLE_CHOICES = 300,
222 HTTP_MOVED_PERMANENTLY = 301,
223 HTTP_NOT_MODIFIED = 304,
224 HTTP_PAYMENT_REQUIRED = 402,
225 HTTP_BAD_GATEWAY = 502,
226 HTTP_SERVICE_UNAVAILABLE = 503, /* overload, maintenance */
227 HTTP_RESPONSE_SETSIZE = 0xffffffff
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000228#endif
229} HttpResponseNum;
230
Denis Vlasenko55a99402006-09-30 20:41:44 +0000231typedef struct {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000232 HttpResponseNum type;
233 const char *name;
234 const char *info;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000235} HttpEnumString;
236
237static const HttpEnumString httpResponseNames[] = {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000238 { HTTP_OK, "OK", NULL },
239 { HTTP_MOVED_TEMPORARILY, "Found", "Directories must end with a slash." },
240 { HTTP_REQUEST_TIMEOUT, "Request Timeout",
241 "No request appeared within a reasonable time period." },
242 { HTTP_NOT_IMPLEMENTED, "Not Implemented",
243 "The requested method is not recognized by this server." },
Denis Vlasenko55a99402006-09-30 20:41:44 +0000244#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000245 { HTTP_UNAUTHORIZED, "Unauthorized", "" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000246#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000247 { HTTP_NOT_FOUND, "Not Found",
248 "The requested URL was not found on this server." },
249 { HTTP_BAD_REQUEST, "Bad Request", "Unsupported method." },
250 { HTTP_FORBIDDEN, "Forbidden", "" },
251 { HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error",
252 "Internal Server Error" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000253#if 0 /* not implemented */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000254 { HTTP_CREATED, "Created" },
255 { HTTP_ACCEPTED, "Accepted" },
256 { HTTP_NO_CONTENT, "No Content" },
257 { HTTP_MULTIPLE_CHOICES, "Multiple Choices" },
258 { HTTP_MOVED_PERMANENTLY, "Moved Permanently" },
259 { HTTP_NOT_MODIFIED, "Not Modified" },
260 { HTTP_BAD_GATEWAY, "Bad Gateway", "" },
261 { HTTP_SERVICE_UNAVAILABLE, "Service Unavailable", "" },
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000262#endif
263};
264
Glenn L McGrath06e95652003-02-09 06:51:14 +0000265
266static const char RFC1123FMT[] = "%a, %d %b %Y %H:%M:%S GMT";
267static const char Content_length[] = "Content-length:";
268
269
Denis Vlasenko55a99402006-09-30 20:41:44 +0000270static int scan_ip(const char **ep, unsigned int *ip, unsigned char endc)
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000271{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000272 const char *p = *ep;
273 int auto_mask = 8;
274 int j;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000275
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000276 *ip = 0;
277 for (j = 0; j < 4; j++) {
278 unsigned int octet;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000279
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000280 if ((*p < '0' || *p > '9') && (*p != '/' || j == 0) && *p != 0)
281 return -auto_mask;
282 octet = 0;
283 while (*p >= '0' && *p <= '9') {
284 octet *= 10;
285 octet += *p - '0';
286 if (octet > 255)
287 return -auto_mask;
288 p++;
289 }
290 if (*p == '.')
291 p++;
292 if (*p != '/' && *p != 0)
293 auto_mask += 8;
294 *ip = ((*ip) << 8) | octet;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000295 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000296 if (*p != 0) {
297 if (*p != endc)
298 return -auto_mask;
299 p++;
300 if (*p == 0)
301 return -auto_mask;
302 }
303 *ep = p;
304 return auto_mask;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000305}
306
Denis Vlasenko55a99402006-09-30 20:41:44 +0000307static int scan_ip_mask(const char *ipm, unsigned int *ip, unsigned int *mask)
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000308{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000309 int i;
310 unsigned int msk;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000311
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000312 i = scan_ip(&ipm, ip, '/');
313 if (i < 0)
314 return i;
315 if (*ipm) {
316 const char *p = ipm;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000317
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000318 i = 0;
319 while (*p) {
Denis Vlasenko92758142006-10-03 19:56:34 +0000320 if (*p < '0' || *p > '9') {
321 if (*p == '.') {
322 i = scan_ip(&ipm, mask, 0);
323 return i != 32;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000324 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000325 return -1;
326 }
327 i *= 10;
328 i += *p - '0';
329 p++;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000330 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000331 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000332 if (i > 32 || i < 0)
Denis Vlasenko92758142006-10-03 19:56:34 +0000333 return -1;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000334 msk = 0x80000000;
335 *mask = 0;
336 while (i > 0) {
337 *mask |= msk;
338 msk >>= 1;
339 i--;
340 }
341 return 0;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000342}
343
Denis Vlasenko55a99402006-09-30 20:41:44 +0000344#if ENABLE_FEATURE_HTTPD_BASIC_AUTH || ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000345static void free_config_lines(Htaccess **pprev)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000346{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000347 Htaccess *prev = *pprev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000348
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000349 while (prev) {
350 Htaccess *cur = prev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000351
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000352 prev = cur->next;
353 free(cur);
354 }
355 *pprev = NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000356}
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000357#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000358
Glenn L McGrath06e95652003-02-09 06:51:14 +0000359/* flag */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000360#define FIRST_PARSE 0
361#define SUBDIR_PARSE 1
362#define SIGNALED_PARSE 2
363#define FIND_FROM_HTTPD_ROOT 3
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000364/****************************************************************************
365 *
366 > $Function: parse_conf()
367 *
368 * $Description: parse configuration file into in-memory linked list.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000369 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000370 * The first non-white character is examined to determine if the config line
371 * is one of the following:
372 * .ext:mime/type # new mime type not compiled into httpd
373 * [adAD]:from # ip address allow/deny, * for wildcard
374 * /path:user:pass # username/password
375 *
376 * Any previous IP rules are discarded.
377 * If the flag argument is not SUBDIR_PARSE then all /path and mime rules
378 * are also discarded. That is, previous settings are retained if flag is
379 * SUBDIR_PARSE.
380 *
381 * $Parameters:
382 * (const char *) path . . null for ip address checks, path for password
383 * checks.
384 * (int) flag . . . . . . the source of the parse request.
385 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000386 * $Return: (None)
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000387 *
388 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000389static void parse_conf(const char *path, int flag)
390{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000391 FILE *f;
Denis Vlasenko55a99402006-09-30 20:41:44 +0000392#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000393 Htaccess *prev, *cur;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000394#elif ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000395 Htaccess *cur;
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000396#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000397
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000398 const char *cf = config->configFile;
399 char buf[160];
400 char *p0 = NULL;
401 char *c, *p;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000402
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000403 /* free previous ip setup if present */
404 Htaccess_IP *pip = config->ip_a_d;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000405
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000406 while (pip) {
407 Htaccess_IP *cur_ipl = pip;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000408
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000409 pip = cur_ipl->next;
410 free(cur_ipl);
411 }
412 config->ip_a_d = NULL;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000413
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000414 config->flg_deny_all = 0;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000415
Denis Vlasenko55a99402006-09-30 20:41:44 +0000416#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 +0000417 /* retain previous auth and mime config only for subdir parse */
418 if (flag != SUBDIR_PARSE) {
Denis Vlasenko55a99402006-09-30 20:41:44 +0000419#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000420 free_config_lines(&config->auth);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000421#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000422#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000423 free_config_lines(&config->mime_a);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000424#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000425#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000426 free_config_lines(&config->script_i);
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000427#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000428 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000429#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000430
431 if (flag == SUBDIR_PARSE || cf == NULL) {
432 cf = alloca(strlen(path) + sizeof(httpd_conf) + 2);
433 if (cf == NULL) {
434 if (flag == FIRST_PARSE)
435 bb_error_msg_and_die(bb_msg_memory_exhausted);
436 return;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000437 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000438 sprintf((char *)cf, "%s/%s", path, httpd_conf);
Glenn L McGrath393183d2003-05-26 14:07:50 +0000439 }
440
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000441 while ((f = fopen(cf, "r")) == NULL) {
442 if (flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) {
443 /* config file not found, no changes to config */
444 return;
445 }
446 if (config->configFile && flag == FIRST_PARSE) /* if -c option given */
447 bb_perror_msg_and_die("%s", cf);
448 flag = FIND_FROM_HTTPD_ROOT;
449 cf = httpd_conf;
450 }
451
Denis Vlasenko55a99402006-09-30 20:41:44 +0000452#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000453 prev = config->auth;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000454#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000455 /* This could stand some work */
Denis Vlasenko55a99402006-09-30 20:41:44 +0000456 while ((p0 = fgets(buf, sizeof(buf), f)) != NULL) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000457 c = NULL;
458 for (p = p0; *p0 != 0 && *p0 != '#'; p0++) {
459 if (!isspace(*p0)) {
460 *p++ = *p0;
461 if (*p0 == ':' && c == NULL)
462 c = p;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000463 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000464 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000465 *p = 0;
466
467 /* test for empty or strange line */
468 if (c == NULL || *c == 0)
469 continue;
470 p0 = buf;
471 if (*p0 == 'd')
472 *p0 = 'D';
473 if (*c == '*') {
474 if (*p0 == 'D') {
475 /* memorize deny all */
476 config->flg_deny_all++;
477 }
478 /* skip default other "word:*" config lines */
479 continue;
480 }
481
482 if (*p0 == 'a')
483 *p0 = 'A';
484 else if (*p0 != 'D' && *p0 != 'A'
Denis Vlasenko55a99402006-09-30 20:41:44 +0000485#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000486 && *p0 != '/'
487#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000488#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000489 && *p0 != '.'
490#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000491#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000492 && *p0 != '*'
493#endif
494 )
495 continue;
496 if (*p0 == 'A' || *p0 == 'D') {
497 /* storing current config IP line */
498 pip = calloc(1, sizeof(Htaccess_IP));
499 if (pip) {
Denis Vlasenko55a99402006-09-30 20:41:44 +0000500 if (scan_ip_mask(c, &(pip->ip), &(pip->mask))) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000501 /* syntax IP{/mask} error detected, protect all */
502 *p0 = 'D';
503 pip->mask = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000504 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000505 pip->allow_deny = *p0;
506 if (*p0 == 'D') {
507 /* Deny:form_IP move top */
508 pip->next = config->ip_a_d;
509 config->ip_a_d = pip;
510 } else {
511 /* add to bottom A:form_IP config line */
512 Htaccess_IP *prev_IP = config->ip_a_d;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000513
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000514 if (prev_IP == NULL) {
515 config->ip_a_d = pip;
516 } else {
517 while (prev_IP->next)
518 prev_IP = prev_IP->next;
519 prev_IP->next = pip;
520 }
521 }
522 }
523 continue;
524 }
Denis Vlasenko55a99402006-09-30 20:41:44 +0000525#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000526 if (*p0 == '/') {
527 /* make full path from httpd root / curent_path / config_line_path */
528 cf = flag == SUBDIR_PARSE ? path : "";
529 p0 = malloc(strlen(cf) + (c - buf) + 2 + strlen(c));
530 if (p0 == NULL)
531 continue;
532 c[-1] = 0;
533 sprintf(p0, "/%s%s", cf, buf);
534
535 /* another call bb_simplify_path */
536 cf = p = p0;
537
538 do {
539 if (*p == '/') {
540 if (*cf == '/') { /* skip duplicate (or initial) slash */
541 continue;
542 } else if (*cf == '.') {
543 if (cf[1] == '/' || cf[1] == 0) { /* remove extra '.' */
544 continue;
545 } else if ((cf[1] == '.') && (cf[2] == '/' || cf[2] == 0)) {
546 ++cf;
547 if (p > p0) {
Denis Vlasenko92758142006-10-03 19:56:34 +0000548 while (*--p != '/') /* omit previous dir */;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000549 }
550 continue;
551 }
552 }
553 }
554 *++p = *cf;
555 } while (*++cf);
556
557 if ((p == p0) || (*p != '/')) { /* not a trailing slash */
558 ++p; /* so keep last character */
559 }
560 *p = 0;
561 sprintf(p0, "%s:%s", p0, c);
562 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000563#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000564
Denis Vlasenko55a99402006-09-30 20:41:44 +0000565#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 +0000566 /* storing current config line */
567 cur = calloc(1, sizeof(Htaccess) + strlen(p0));
568 if (cur) {
569 cf = strcpy(cur->before_colon, p0);
570 c = strchr(cf, ':');
571 *c++ = 0;
572 cur->after_colon = c;
Denis Vlasenko55a99402006-09-30 20:41:44 +0000573#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000574 if (*cf == '.') {
575 /* config .mime line move top for overwrite previous */
576 cur->next = config->mime_a;
577 config->mime_a = cur;
578 continue;
579 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000580#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000581#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000582 if (*cf == '*' && cf[1] == '.') {
583 /* config script interpreter line move top for overwrite previous */
584 cur->next = config->script_i;
585 config->script_i = cur;
586 continue;
587 }
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000588#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000589#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000590 free(p0);
591 if (prev == NULL) {
592 /* first line */
593 config->auth = prev = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000594 } else {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000595 /* sort path, if current lenght eq or bigger then move up */
596 Htaccess *prev_hti = config->auth;
597 size_t l = strlen(cf);
598 Htaccess *hti;
599
600 for (hti = prev_hti; hti; hti = hti->next) {
601 if (l >= strlen(hti->before_colon)) {
602 /* insert before hti */
603 cur->next = hti;
604 if (prev_hti != hti) {
605 prev_hti->next = cur;
606 } else {
607 /* insert as top */
608 config->auth = cur;
609 }
610 break;
611 }
612 if (prev_hti != hti)
613 prev_hti = prev_hti->next;
614 }
615 if (!hti) { /* not inserted, add to bottom */
616 prev->next = cur;
617 prev = cur;
618 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000619 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000620#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000621 }
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000622#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000623 }
624 fclose(f);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000625}
626
Denis Vlasenko55a99402006-09-30 20:41:44 +0000627#if ENABLE_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000628/****************************************************************************
629 *
630 > $Function: encodeString()
631 *
632 * $Description: Given a string, html encode special characters.
633 * This is used for the -e command line option to provide an easy way
634 * for scripts to encode result data without confusing browsers. The
635 * returned string pointer is memory allocated by malloc().
636 *
637 * $Parameters:
638 * (const char *) string . . The first string to encode.
639 *
640 * $Return: (char *) . . . .. . . A pointer to the encoded string.
641 *
642 * $Errors: Returns a null string ("") if memory is not available.
643 *
644 ****************************************************************************/
645static char *encodeString(const char *string)
646{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000647 /* take the simple route and encode everything */
648 /* could possibly scan once to get length. */
649 int len = strlen(string);
650 char *out = malloc(len * 6 + 1);
651 char *p = out;
652 char ch;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000653
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000654 if (!out) return "";
655 while ((ch = *string++)) {
656 // very simple check for what to encode
657 if (isalnum(ch)) *p++ = ch;
658 else p += sprintf(p, "&#%d;", (unsigned char) ch);
659 }
660 *p = 0;
661 return out;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000662}
Glenn L McGrath06e95652003-02-09 06:51:14 +0000663#endif /* CONFIG_FEATURE_HTTPD_ENCODE_URL_STR */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000664
665/****************************************************************************
666 *
667 > $Function: decodeString()
668 *
669 * $Description: Given a URL encoded string, convert it to plain ascii.
670 * Since decoding always makes strings smaller, the decode is done in-place.
671 * Thus, callers should strdup() the argument if they do not want the
672 * argument modified. The return is the original pointer, allowing this
673 * function to be easily used as arguments to other functions.
674 *
675 * $Parameters:
676 * (char *) string . . . The first string to decode.
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000677 * (int) flag . . . 1 if require decode '+' as ' ' for CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000678 *
679 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
680 *
681 * $Errors: None
682 *
683 ****************************************************************************/
Eric Andersena3bb3e62003-06-26 09:05:32 +0000684static char *decodeString(char *orig, int flag_plus_to_space)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000685{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000686 /* note that decoded string is always shorter than original */
687 char *string = orig;
688 char *ptr = string;
Eric Andersena3bb3e62003-06-26 09:05:32 +0000689
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000690 while (*ptr) {
691 if (*ptr == '+' && flag_plus_to_space) { *string++ = ' '; ptr++; }
692 else if (*ptr != '%') *string++ = *ptr++;
693 else {
694 unsigned int value1, value2;
"Vladimir N. Oleynik"0bf67e82005-12-26 17:26:59 +0000695
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000696 ptr++;
697 if (sscanf(ptr, "%1X", &value1) != 1 ||
698 sscanf(ptr+1, "%1X", &value2) != 1) {
699 if (!flag_plus_to_space)
700 return NULL;
701 *string++ = '%';
702 } else {
703 value1 = value1 * 16 + value2;
704 if (value1 == '/' || value1 == 0)
705 return orig+1;
706 *string++ = value1;
707 ptr += 2;
708 }
709 }
710 }
711 *string = '\0';
712 return orig;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000713}
714
715
Denis Vlasenko55a99402006-09-30 20:41:44 +0000716#if ENABLE_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000717/****************************************************************************
718 *
719 > $Function: addEnv()
720 *
Eric Andersenaff114c2004-04-14 17:51:38 +0000721 * $Description: Add an environment variable setting to the global list.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000722 * A NAME=VALUE string is allocated, filled, and added to the list of
723 * environment settings passed to the cgi execution script.
724 *
725 * $Parameters:
Glenn L McGrath06e95652003-02-09 06:51:14 +0000726 * (char *) name_before_underline - The first part environment variable name.
727 * (char *) name_after_underline - The second part environment variable name.
728 * (char *) value . . The value to which the env variable is set.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000729 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000730 * $Return: (void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000731 *
732 * $Errors: Silently returns if the env runs out of space to hold the new item
733 *
734 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000735static void addEnv(const char *name_before_underline,
Denis Vlasenko55a99402006-09-30 20:41:44 +0000736 const char *name_after_underline, const char *value)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000737{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000738 char *s = NULL;
739 const char *underline;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000740
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000741 if (!value)
742 value = "";
743 underline = *name_after_underline ? "_" : "";
744 asprintf(&s, "%s%s%s=%s", name_before_underline, underline,
745 name_after_underline, value);
746 if (s) {
747 putenv(s);
748 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000749}
750
Denis Vlasenko55a99402006-09-30 20:41:44 +0000751#if ENABLE_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV || ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Glenn L McGrath06e95652003-02-09 06:51:14 +0000752/* set environs SERVER_PORT and REMOTE_PORT */
753static void addEnvPort(const char *port_name)
754{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000755 char buf[16];
Glenn L McGrath06e95652003-02-09 06:51:14 +0000756
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000757 sprintf(buf, "%u", config->port);
758 addEnv(port_name, "PORT", buf);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000759}
Eric Andersena3bb3e62003-06-26 09:05:32 +0000760#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000761#endif /* CONFIG_FEATURE_HTTPD_CGI */
762
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000763
Denis Vlasenko55a99402006-09-30 20:41:44 +0000764#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000765/****************************************************************************
766 *
767 > $Function: decodeBase64()
768 *
769 > $Description: Decode a base 64 data stream as per rfc1521.
770 * Note that the rfc states that none base64 chars are to be ignored.
771 * Since the decode always results in a shorter size than the input, it is
772 * OK to pass the input arg as an output arg.
773 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000774 * $Parameter:
775 * (char *) Data . . . . A pointer to a base64 encoded string.
776 * Where to place the decoded data.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000777 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000778 * $Return: void
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000779 *
780 * $Errors: None
781 *
782 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000783static void decodeBase64(char *Data)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000784{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000785
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000786 const unsigned char *in = (const unsigned char *)Data;
787 // The decoded size will be at most 3/4 the size of the encoded
788 unsigned long ch = 0;
789 int i = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000790
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000791 while (*in) {
792 int t = *in++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000793
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000794 if (t >= '0' && t <= '9')
795 t = t - '0' + 52;
796 else if (t >= 'A' && t <= 'Z')
797 t = t - 'A';
798 else if (t >= 'a' && t <= 'z')
799 t = t - 'a' + 26;
800 else if (t == '+')
801 t = 62;
802 else if (t == '/')
803 t = 63;
804 else if (t == '=')
805 t = 0;
806 else
807 continue;
Glenn L McGrath874e3382003-05-14 12:11:36 +0000808
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000809 ch = (ch << 6) | t;
810 i++;
811 if (i == 4) {
812 *Data++ = (char) (ch >> 16);
813 *Data++ = (char) (ch >> 8);
814 *Data++ = (char) ch;
815 i = 0;
816 }
817 }
818 *Data = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000819}
820#endif
821
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000822
Denis Vlasenko55a99402006-09-30 20:41:44 +0000823#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000824/****************************************************************************
825 *
826 > $Function: openServer()
827 *
828 * $Description: create a listen server socket on the designated port.
829 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000830 * $Return: (int) . . . A connection socket. -1 for errors.
831 *
832 * $Errors: None
833 *
834 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000835static int openServer(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000836{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000837 struct sockaddr_in lsocket;
838 int fd;
839 int on = 1;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000840
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000841 /* create the socket right now */
842 /* inet_addr() returns a value that is already in network order */
843 memset(&lsocket, 0, sizeof(lsocket));
844 lsocket.sin_family = AF_INET;
845 lsocket.sin_addr.s_addr = INADDR_ANY;
846 lsocket.sin_port = htons(config->port);
847 fd = xsocket(AF_INET, SOCK_STREAM, 0);
848 /* tell the OS it's OK to reuse a previous address even though */
849 /* it may still be in a close down state. Allows bind to succeed. */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000850#ifdef SO_REUSEPORT
Denis Vlasenko55a99402006-09-30 20:41:44 +0000851 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *)&on, sizeof(on));
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000852#else
Denis Vlasenko55a99402006-09-30 20:41:44 +0000853 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000854#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000855 xbind(fd, (struct sockaddr *)&lsocket, sizeof(lsocket));
856 xlisten(fd, 9);
857 signal(SIGCHLD, SIG_IGN); /* prevent zombie (defunct) processes */
858 return fd;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000859}
Rob Landleya2d9a172006-04-28 19:38:04 +0000860#endif /* CONFIG_FEATURE_HTTPD_WITHOUT_INETD */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000861
862/****************************************************************************
863 *
864 > $Function: sendHeaders()
865 *
866 * $Description: Create and send HTTP response headers.
867 * The arguments are combined and sent as one write operation. Note that
868 * IE will puke big-time if the headers are not sent in one packet and the
Glenn L McGrath06e95652003-02-09 06:51:14 +0000869 * second packet is delayed for any reason.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000870 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000871 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000872 * (HttpResponseNum) responseNum . . . The result code to send.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000873 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000874 * $Return: (int) . . . . writing errors
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000875 *
876 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000877static int sendHeaders(HttpResponseNum responseNum)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000878{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000879 char *buf = config->buf;
880 const char *responseString = "";
881 const char *infoString = 0;
882 const char *mime_type;
883 unsigned int i;
884 time_t timer = time(0);
885 char timeStr[80];
886 int len;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000887
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000888 for (i = 0;
889 i < (sizeof(httpResponseNames)/sizeof(httpResponseNames[0])); i++) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000890 if (httpResponseNames[i].type == responseNum) {
891 responseString = httpResponseNames[i].name;
892 infoString = httpResponseNames[i].info;
893 break;
894 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000895 }
896 /* error message is HTML */
897 mime_type = responseNum == HTTP_OK ?
898 config->httpd_found.found_mime_type : "text/html";
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000899
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000900 /* emit the current date */
901 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&timer));
902 len = sprintf(buf,
903 "HTTP/1.0 %d %s\r\nContent-type: %s\r\n"
904 "Date: %s\r\nConnection: close\r\n",
905 responseNum, responseString, mime_type, timeStr);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000906
Denis Vlasenko55a99402006-09-30 20:41:44 +0000907#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000908 if (responseNum == HTTP_UNAUTHORIZED) {
909 len += sprintf(buf+len, "WWW-Authenticate: Basic realm=\"%s\"\r\n",
910 config->realm);
911 }
Glenn L McGrath3d2405c2003-02-10 22:28:21 +0000912#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000913 if (responseNum == HTTP_MOVED_TEMPORARILY) {
914 len += sprintf(buf+len, "Location: %s/%s%s\r\n",
915 config->httpd_found.found_moved_temporarily,
916 (config->query ? "?" : ""),
917 (config->query ? config->query : ""));
918 }
Eric Andersen07f2fea2004-10-08 08:03:29 +0000919
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000920 if (config->ContentLength != -1) { /* file */
921 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&config->last_mod));
Denis Vlasenko5c759602006-10-28 12:37:16 +0000922 len += sprintf(buf+len, "Last-Modified: %s\r\n%s %"OFF_FMT"\r\n",
Denis Vlasenko7039a662006-10-08 17:54:47 +0000923 timeStr, Content_length, (off_t) config->ContentLength);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000924 }
925 strcat(buf, "\r\n");
926 len += 2;
927 if (infoString) {
928 len += sprintf(buf+len,
929 "<HEAD><TITLE>%d %s</TITLE></HEAD>\n"
930 "<BODY><H1>%d %s</H1>\n%s\n</BODY>\n",
931 responseNum, responseString,
932 responseNum, responseString, infoString);
933 }
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000934#if DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000935 fprintf(stderr, "Headers: '%s'", buf);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000936#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000937 return full_write(a_c_w, buf, len);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000938}
939
940/****************************************************************************
941 *
942 > $Function: getLine()
943 *
944 * $Description: Read from the socket until an end of line char found.
945 *
946 * Characters are read one at a time until an eol sequence is found.
947 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000948 * $Return: (int) . . . . number of characters read. -1 if error.
949 *
950 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000951static int getLine(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000952{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000953 int count = 0;
954 char *buf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000955
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000956 while (read(a_c_r, buf + count, 1) == 1) {
957 if (buf[count] == '\r') continue;
958 if (buf[count] == '\n') {
959 buf[count] = 0;
960 return count;
961 }
962 if (count < (MAX_MEMORY_BUFF-1)) /* check owerflow */
963 count++;
964 }
965 if (count) return count;
966 else return -1;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000967}
968
Denis Vlasenko55a99402006-09-30 20:41:44 +0000969#if ENABLE_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000970/****************************************************************************
971 *
972 > $Function: sendCgi()
973 *
974 * $Description: Execute a CGI script and send it's stdout back
975 *
976 * Environment variables are set up and the script is invoked with pipes
977 * for stdin/stdout. If a post is being done the script is fed the POST
978 * data in addition to setting the QUERY_STRING variable (for GETs or POSTs).
979 *
980 * $Parameters:
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000981 * (const char *) url . . . . . . The requested URL (with leading /).
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000982 * (int bodyLen) . . . . . . . . Length of the post body.
983 * (const char *cookie) . . . . . For set HTTP_COOKIE.
984 * (const char *content_type) . . For set CONTENT_TYPE.
Glenn L McGrath06e95652003-02-09 06:51:14 +0000985
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000986 *
987 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
988 *
989 * $Errors: None
990 *
991 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000992static int sendCgi(const char *url,
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000993 const char *request, int bodyLen, const char *cookie,
994 const char *content_type)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000995{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000996 int fromCgi[2]; /* pipe for reading data from CGI */
997 int toCgi[2]; /* pipe for sending data to CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000998
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000999 static char * argp[] = { 0, 0 };
1000 int pid = 0;
1001 int inFd;
1002 int outFd;
1003 int firstLine = 1;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001004
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001005 do {
1006 if (pipe(fromCgi) != 0) {
1007 break;
1008 }
1009 if (pipe(toCgi) != 0) {
1010 break;
1011 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001012
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001013 pid = fork();
1014 if (pid < 0) {
1015 pid = 0;
1016 break;
1017 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001018
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001019 if (!pid) {
1020 /* child process */
1021 char *script;
1022 char *purl = strdup(url);
1023 char realpath_buff[MAXPATHLEN];
Glenn L McGrath06e95652003-02-09 06:51:14 +00001024
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001025 if (purl == NULL)
1026 _exit(242);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001027
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001028 inFd = toCgi[0];
1029 outFd = fromCgi[1];
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001030
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001031 dup2(inFd, 0); // replace stdin with the pipe
1032 dup2(outFd, 1); // replace stdout with the pipe
1033 if (!DEBUG)
1034 dup2(outFd, 2); // replace stderr with the pipe
Glenn L McGrath06e95652003-02-09 06:51:14 +00001035
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001036 close(toCgi[0]);
1037 close(toCgi[1]);
1038 close(fromCgi[0]);
1039 close(fromCgi[1]);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001040
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001041 /*
1042 * Find PATH_INFO.
1043 */
1044 script = purl;
1045 while ((script = strchr(script + 1, '/')) != NULL) {
1046 /* have script.cgi/PATH_INFO or dirs/script.cgi[/PATH_INFO] */
1047 struct stat sb;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001048
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001049 *script = '\0';
1050 if (is_directory(purl + 1, 1, &sb) == 0) {
1051 /* not directory, found script.cgi/PATH_INFO */
1052 *script = '/';
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +00001053 break;
1054 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001055 *script = '/'; /* is directory, find next '/' */
1056 }
1057 addEnv("PATH", "INFO", script); /* set /PATH_INFO or NULL */
1058 addEnv("PATH", "", getenv("PATH"));
1059 addEnv("REQUEST", "METHOD", request);
1060 if (config->query) {
1061 char *uri = alloca(strlen(purl) + 2 + strlen(config->query));
1062 if (uri)
1063 sprintf(uri, "%s?%s", purl, config->query);
1064 addEnv("REQUEST", "URI", uri);
1065 } else {
1066 addEnv("REQUEST", "URI", purl);
1067 }
1068 if (script != NULL)
1069 *script = '\0'; /* reduce /PATH_INFO */
1070 /* SCRIPT_FILENAME required by PHP in CGI mode */
1071 if (realpath(purl + 1, realpath_buff))
1072 addEnv("SCRIPT", "FILENAME", realpath_buff);
1073 else
1074 *realpath_buff = 0;
1075 /* set SCRIPT_NAME as full path: /cgi-bin/dirs/script.cgi */
1076 addEnv("SCRIPT_NAME", "", purl);
1077 addEnv("QUERY_STRING", "", config->query);
1078 addEnv("SERVER", "SOFTWARE", httpdVersion);
1079 addEnv("SERVER", "PROTOCOL", "HTTP/1.0");
1080 addEnv("GATEWAY_INTERFACE", "", "CGI/1.1");
1081 addEnv("REMOTE", "ADDR", config->rmt_ip_str);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001082#if ENABLE_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001083 addEnvPort("REMOTE");
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +00001084#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001085 if (bodyLen) {
1086 char sbl[32];
1087
1088 sprintf(sbl, "%d", bodyLen);
1089 addEnv("CONTENT", "LENGTH", sbl);
1090 }
1091 if (cookie)
1092 addEnv("HTTP", "COOKIE", cookie);
1093 if (content_type)
1094 addEnv("CONTENT", "TYPE", content_type);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001095#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001096 if (config->remoteuser) {
1097 addEnv("REMOTE", "USER", config->remoteuser);
1098 addEnv("AUTH_TYPE", "", "Basic");
1099 }
1100#endif
1101 if (config->referer)
1102 addEnv("HTTP", "REFERER", config->referer);
1103
1104 /* set execve argp[0] without path */
1105 argp[0] = strrchr(purl, '/') + 1;
1106 /* but script argp[0] must have absolute path and chdiring to this */
1107 if (*realpath_buff) {
1108 script = strrchr(realpath_buff, '/');
1109 if (script) {
1110 *script = '\0';
1111 if (chdir(realpath_buff) == 0) {
1112 // now run the program. If it fails,
1113 // use _exit() so no destructors
1114 // get called and make a mess.
Denis Vlasenko55a99402006-09-30 20:41:44 +00001115#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001116 char *interpr = NULL;
1117 char *suffix = strrchr(purl, '.');
1118
1119 if (suffix) {
1120 Htaccess * cur;
1121 for (cur = config->script_i; cur; cur = cur->next)
1122 if (strcmp(cur->before_colon + 1, suffix) == 0) {
1123 interpr = cur->after_colon;
1124 break;
1125 }
1126 }
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +00001127#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001128 *script = '/';
Denis Vlasenko55a99402006-09-30 20:41:44 +00001129#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001130 if (interpr)
1131 execv(interpr, argp);
1132 else
1133#endif
1134 execv(realpath_buff, argp);
1135 }
1136 }
1137 }
Denis Vlasenko55a99402006-09-30 20:41:44 +00001138#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001139 config->accepted_socket = 1; /* send to stdout */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001140#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001141 sendHeaders(HTTP_NOT_FOUND);
1142 _exit(242);
1143 } /* end child */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001144
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001145 } while (0);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001146
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001147 if (pid) {
1148 /* parent process */
1149 int status;
1150 size_t post_readed_size = 0, post_readed_idx = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001151
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001152 inFd = fromCgi[0];
1153 outFd = toCgi[1];
1154 close(fromCgi[1]);
1155 close(toCgi[0]);
1156 signal(SIGPIPE, SIG_IGN);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001157
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001158 while (1) {
1159 fd_set readSet;
1160 fd_set writeSet;
1161 char wbuf[128];
1162 int nfound;
1163 int count;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001164
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001165 FD_ZERO(&readSet);
1166 FD_ZERO(&writeSet);
1167 FD_SET(inFd, &readSet);
1168 if (bodyLen > 0 || post_readed_size > 0) {
1169 FD_SET(outFd, &writeSet);
1170 nfound = outFd > inFd ? outFd : inFd;
1171 if (post_readed_size == 0) {
1172 FD_SET(a_c_r, &readSet);
1173 if (nfound < a_c_r)
1174 nfound = a_c_r;
1175 }
1176 /* Now wait on the set of sockets! */
1177 nfound = select(nfound + 1, &readSet, &writeSet, 0, NULL);
1178 } else {
1179 if (!bodyLen) {
1180 close(outFd);
1181 bodyLen = -1;
1182 }
1183 nfound = select(inFd + 1, &readSet, 0, 0, NULL);
1184 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001185
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001186 if (nfound <= 0) {
1187 if (waitpid(pid, &status, WNOHANG) > 0) {
1188 close(inFd);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001189#if DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001190 if (WIFEXITED(status))
1191 bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status));
1192 if (WIFSIGNALED(status))
1193 bb_error_msg("piped has exited with signal=%d", WTERMSIG(status));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001194#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001195 break;
1196 }
1197 } else if (post_readed_size > 0 && FD_ISSET(outFd, &writeSet)) {
1198 count = full_write(outFd, wbuf + post_readed_idx, post_readed_size);
1199 if (count > 0) {
1200 post_readed_size -= count;
1201 post_readed_idx += count;
1202 if (post_readed_size == 0)
1203 post_readed_idx = 0;
1204 } else {
1205 post_readed_size = post_readed_idx = bodyLen = 0; /* broken pipe to CGI */
1206 }
1207 } else if (bodyLen > 0 && post_readed_size == 0 && FD_ISSET(a_c_r, &readSet)) {
1208 count = bodyLen > (int)sizeof(wbuf) ? (int)sizeof(wbuf) : bodyLen;
1209 count = safe_read(a_c_r, wbuf, count);
1210 if (count > 0) {
1211 post_readed_size += count;
1212 bodyLen -= count;
1213 } else {
1214 bodyLen = 0; /* closed */
1215 }
1216 }
1217 if (FD_ISSET(inFd, &readSet)) {
1218 int s = a_c_w;
1219 char *rbuf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001220
Eric Andersen97a1de12004-08-26 22:22:50 +00001221#ifndef PIPE_BUF
1222# define PIPESIZE 4096 /* amount of buffering in a pipe */
1223#else
1224# define PIPESIZE PIPE_BUF
1225#endif
1226#if PIPESIZE >= MAX_MEMORY_BUFF
1227# error "PIPESIZE >= MAX_MEMORY_BUFF"
1228#endif
1229
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001230 // There is something to read
1231 count = safe_read(inFd, rbuf, PIPESIZE);
1232 if (count == 0)
1233 break; /* closed */
1234 if (count > 0) {
1235 if (firstLine) {
1236 rbuf[count] = 0;
1237 /* check to see if the user script added headers */
1238 if (strncmp(rbuf, "HTTP/1.0 200 OK\r\n", 4) != 0) {
1239 full_write(s, "HTTP/1.0 200 OK\r\n", 17);
1240 }
1241 if (strstr(rbuf, "ontent-") == 0) {
1242 full_write(s, "Content-type: text/plain\r\n\r\n", 28);
1243 }
1244 firstLine = 0;
1245 }
1246 if (full_write(s, rbuf, count) != count)
1247 break;
Eric Andersenef437492004-02-04 11:10:28 +00001248
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001249#if DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001250 fprintf(stderr, "cgi read %d bytes\n", count);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001251#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001252 }
1253 }
1254 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001255 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001256 return 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001257}
Glenn L McGrath06e95652003-02-09 06:51:14 +00001258#endif /* CONFIG_FEATURE_HTTPD_CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001259
1260/****************************************************************************
1261 *
1262 > $Function: sendFile()
1263 *
1264 * $Description: Send a file response to an HTTP request
1265 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00001266 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001267 * (const char *) url . . The URL requested.
1268 *
1269 * $Return: (int) . . . . . . Always 0.
1270 *
1271 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001272static int sendFile(const char *url)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001273{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001274 char * suffix;
1275 int f;
1276 const char * const * table;
1277 const char * try_suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001278
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001279 suffix = strrchr(url, '.');
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001280
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001281 for (table = suffixTable; *table; table += 2)
1282 if (suffix != NULL && (try_suffix = strstr(*table, suffix)) != 0) {
1283 try_suffix += strlen(suffix);
1284 if (*try_suffix == 0 || *try_suffix == '.')
1285 break;
1286 }
1287 /* also, if not found, set default as "application/octet-stream"; */
1288 config->httpd_found.found_mime_type = *(table+1);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001289#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001290 if (suffix) {
1291 Htaccess * cur;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001292
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001293 for (cur = config->mime_a; cur; cur = cur->next) {
1294 if (strcmp(cur->before_colon, suffix) == 0) {
1295 config->httpd_found.found_mime_type = cur->after_colon;
1296 break;
1297 }
1298 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001299 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001300#endif /* CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES */
1301
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001302#if DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001303 fprintf(stderr, "Sending file '%s' Content-type: %s\n",
1304 url, config->httpd_found.found_mime_type);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001305#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001306
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001307 f = open(url, O_RDONLY);
1308 if (f >= 0) {
1309 int count;
1310 char *buf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001311
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001312 sendHeaders(HTTP_OK);
1313 while ((count = full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
1314 if (full_write(a_c_w, buf, count) != count)
1315 break;
1316 }
1317 close(f);
1318 } else {
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001319#if DEBUG
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001320 bb_perror_msg("unable to open '%s'", url);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001321#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001322 sendHeaders(HTTP_NOT_FOUND);
1323 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001324
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001325 return 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001326}
1327
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001328static int checkPermIP(void)
1329{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001330 Htaccess_IP * cur;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001331
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001332 /* This could stand some work */
1333 for (cur = config->ip_a_d; cur; cur = cur->next) {
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001334#if DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001335 fprintf(stderr, "checkPermIP: '%s' ? ", config->rmt_ip_str);
1336 fprintf(stderr, "'%u.%u.%u.%u/%u.%u.%u.%u'\n",
1337 (unsigned char)(cur->ip >> 24),
1338 (unsigned char)(cur->ip >> 16),
1339 (unsigned char)(cur->ip >> 8),
1340 cur->ip & 0xff,
1341 (unsigned char)(cur->mask >> 24),
1342 (unsigned char)(cur->mask >> 16),
1343 (unsigned char)(cur->mask >> 8),
1344 cur->mask & 0xff);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001345#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001346 if ((config->rmt_ip & cur->mask) == cur->ip)
1347 return cur->allow_deny == 'A'; /* Allow/Deny */
1348 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001349
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001350 /* if unconfigured, return 1 - access from all */
1351 return !config->flg_deny_all;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001352}
1353
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001354/****************************************************************************
1355 *
1356 > $Function: checkPerm()
1357 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001358 * $Description: Check the permission file for access password protected.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001359 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001360 * If config file isn't present, everything is allowed.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001361 * Entries are of the form you can see example from header source
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001362 *
1363 * $Parameters:
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001364 * (const char *) path . . . . The file path.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001365 * (const char *) request . . . User information to validate.
1366 *
1367 * $Return: (int) . . . . . . . . . 1 if request OK, 0 otherwise.
1368 *
1369 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001370
Denis Vlasenko55a99402006-09-30 20:41:44 +00001371#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001372static int checkPerm(const char *path, const char *request)
1373{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001374 Htaccess * cur;
1375 const char *p;
1376 const char *p0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001377
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001378 const char *prev = NULL;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001379
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001380 /* This could stand some work */
1381 for (cur = config->auth; cur; cur = cur->next) {
1382 p0 = cur->before_colon;
1383 if (prev != NULL && strcmp(prev, p0) != 0)
1384 continue; /* find next identical */
1385 p = cur->after_colon;
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001386#if DEBUG
Denis Vlasenko55a99402006-09-30 20:41:44 +00001387 fprintf(stderr, "checkPerm: '%s' ? '%s'\n", p0, request);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001388#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001389 {
1390 size_t l = strlen(p0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001391
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001392 if (strncmp(p0, path, l) == 0 &&
1393 (l == 1 || path[l] == '/' || path[l] == 0)) {
1394 char *u;
1395 /* path match found. Check request */
1396 /* for check next /path:user:password */
1397 prev = p0;
1398 u = strchr(request, ':');
1399 if (u == NULL) {
1400 /* bad request, ':' required */
1401 break;
1402 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001403
Denis Vlasenko55a99402006-09-30 20:41:44 +00001404#if ENABLE_FEATURE_HTTPD_AUTH_MD5
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001405 {
1406 char *cipher;
1407 char *pp;
Eric Andersen35e643b2003-07-28 07:40:39 +00001408
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001409 if (strncmp(p, request, u-request) != 0) {
1410 /* user uncompared */
1411 continue;
1412 }
1413 pp = strchr(p, ':');
1414 if (pp && pp[1] == '$' && pp[2] == '1' &&
1415 pp[3] == '$' && pp[4]) {
1416 pp++;
1417 cipher = pw_encrypt(u+1, pp);
1418 if (strcmp(cipher, pp) == 0)
1419 goto set_remoteuser_var; /* Ok */
1420 /* unauthorized */
1421 continue;
1422 }
1423 }
Eric Andersen35e643b2003-07-28 07:40:39 +00001424#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001425 if (strcmp(p, request) == 0) {
Denis Vlasenko55a99402006-09-30 20:41:44 +00001426#if ENABLE_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001427set_remoteuser_var:
Glenn L McGrath9d1a33c2003-10-06 13:23:06 +00001428#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001429 config->remoteuser = strdup(request);
1430 if (config->remoteuser)
1431 config->remoteuser[(u - request)] = 0;
1432 return 1; /* Ok */
1433 }
1434 /* unauthorized */
1435 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001436 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001437 } /* for */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001438
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001439 return prev == NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001440}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001441
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001442#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1443
Eric Andersen07f2fea2004-10-08 08:03:29 +00001444/****************************************************************************
1445 *
Mike Frysingerbb12d6f2006-01-03 23:59:01 +00001446 > $Function: handle_sigalrm()
Eric Andersen07f2fea2004-10-08 08:03:29 +00001447 *
Mike Frysingerbb12d6f2006-01-03 23:59:01 +00001448 * $Description: Handle timeouts
Eric Andersen07f2fea2004-10-08 08:03:29 +00001449 *
1450 ****************************************************************************/
1451
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001452static void handle_sigalrm(int sig)
Eric Andersen07f2fea2004-10-08 08:03:29 +00001453{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001454 sendHeaders(HTTP_REQUEST_TIMEOUT);
1455 config->alarm_signaled = sig;
Eric Andersen07f2fea2004-10-08 08:03:29 +00001456}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001457
1458/****************************************************************************
1459 *
1460 > $Function: handleIncoming()
1461 *
1462 * $Description: Handle an incoming http request.
1463 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001464 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001465static void handleIncoming(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001466{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001467 char *buf = config->buf;
1468 char *url;
1469 char *purl;
1470 int blank = -1;
1471 char *test;
1472 struct stat sb;
1473 int ip_allowed;
Denis Vlasenko55a99402006-09-30 20:41:44 +00001474#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001475 const char *prequest = request_GET;
1476 long length=0;
1477 char *cookie = 0;
1478 char *content_type = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001479#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001480 fd_set s_fd;
1481 struct timeval tv;
1482 int retval;
1483 struct sigaction sa;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001484
Denis Vlasenko55a99402006-09-30 20:41:44 +00001485#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001486 int credentials = -1; /* if not requred this is Ok */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001487#endif
1488
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001489 sa.sa_handler = handle_sigalrm;
1490 sigemptyset(&sa.sa_mask);
1491 sa.sa_flags = 0; /* no SA_RESTART */
1492 sigaction(SIGALRM, &sa, NULL);
Eric Andersen07f2fea2004-10-08 08:03:29 +00001493
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001494 do {
1495 int count;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001496
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001497 (void) alarm(TIMEOUT);
1498 if (getLine() <= 0)
1499 break; /* closed */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001500
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001501 purl = strpbrk(buf, " \t");
1502 if (purl == NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001503BAD_REQUEST:
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001504 sendHeaders(HTTP_BAD_REQUEST);
1505 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001506 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001507 *purl = 0;
Denis Vlasenko55a99402006-09-30 20:41:44 +00001508#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001509 if (strcasecmp(buf, prequest) != 0) {
1510 prequest = "POST";
1511 if (strcasecmp(buf, prequest) != 0) {
1512 sendHeaders(HTTP_NOT_IMPLEMENTED);
1513 break;
1514 }
1515 }
1516#else
1517 if (strcasecmp(buf, request_GET) != 0) {
1518 sendHeaders(HTTP_NOT_IMPLEMENTED);
1519 break;
1520 }
1521#endif
1522 *purl = ' ';
1523 count = sscanf(purl, " %[^ ] HTTP/%d.%*d", buf, &blank);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001524
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001525 if (count < 1 || buf[0] != '/') {
1526 /* Garbled request/URL */
1527 goto BAD_REQUEST;
1528 }
1529 url = alloca(strlen(buf) + 12); /* + sizeof("/index.html\0") */
1530 if (url == NULL) {
1531 sendHeaders(HTTP_INTERNAL_SERVER_ERROR);
1532 break;
1533 }
1534 strcpy(url, buf);
1535 /* extract url args if present */
1536 test = strchr(url, '?');
1537 if (test) {
1538 *test++ = 0;
1539 config->query = test;
1540 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001541
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001542 test = decodeString(url, 0);
1543 if (test == NULL)
1544 goto BAD_REQUEST;
1545 if (test == (buf+1)) {
1546 sendHeaders(HTTP_NOT_FOUND);
1547 break;
1548 }
1549 /* algorithm stolen from libbb bb_simplify_path(),
Denis Vlasenko55a99402006-09-30 20:41:44 +00001550 but don't strdup and reducing trailing slash and protect out root */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001551 purl = test = url;
1552
1553 do {
1554 if (*purl == '/') {
1555 if (*test == '/') { /* skip duplicate (or initial) slash */
1556 continue;
1557 } else if (*test == '.') {
1558 if (test[1] == '/' || test[1] == 0) { /* skip extra '.' */
1559 continue;
1560 } else if ((test[1] == '.') && (test[2] == '/' || test[2] == 0)) {
1561 ++test;
1562 if (purl == url) {
1563 /* protect out root */
1564 goto BAD_REQUEST;
1565 }
Denis Vlasenko92758142006-10-03 19:56:34 +00001566 while (*--purl != '/') /* omit previous dir */;
1567 continue;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001568 }
1569 }
1570 }
1571 *++purl = *test;
1572 } while (*++test);
1573
1574 *++purl = 0; /* so keep last character */
1575 test = purl; /* end ptr */
1576
1577 /* If URL is directory, adding '/' */
1578 if (test[-1] != '/') {
1579 if (is_directory(url + 1, 1, &sb)) {
1580 config->httpd_found.found_moved_temporarily = url;
1581 }
1582 }
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001583#if DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001584 fprintf(stderr, "url='%s', args=%s\n", url, config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001585#endif
1586
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001587 test = url;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001588 ip_allowed = checkPermIP();
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001589 while (ip_allowed && (test = strchr(test + 1, '/')) != NULL) {
1590 /* have path1/path2 */
1591 *test = '\0';
1592 if (is_directory(url + 1, 1, &sb)) {
1593 /* may be having subdir config */
1594 parse_conf(url + 1, SUBDIR_PARSE);
1595 ip_allowed = checkPermIP();
1596 }
1597 *test = '/';
1598 }
1599 if (blank >= 0) {
1600 // read until blank line for HTTP version specified, else parse immediate
1601 while (1) {
1602 alarm(TIMEOUT);
1603 count = getLine();
1604 if (count <= 0)
1605 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001606
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001607#if DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001608 fprintf(stderr, "Header: '%s'\n", buf);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001609#endif
1610
Denis Vlasenko55a99402006-09-30 20:41:44 +00001611#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001612 /* try and do our best to parse more lines */
1613 if ((strncasecmp(buf, Content_length, 15) == 0)) {
1614 if (prequest != request_GET)
1615 length = strtol(buf + 15, 0, 0); // extra read only for POST
1616 } else if ((strncasecmp(buf, "Cookie:", 7) == 0)) {
1617 for (test = buf + 7; isspace(*test); test++)
1618 ;
1619 cookie = strdup(test);
1620 } else if ((strncasecmp(buf, "Content-Type:", 13) == 0)) {
1621 for (test = buf + 13; isspace(*test); test++)
1622 ;
1623 content_type = strdup(test);
1624 } else if ((strncasecmp(buf, "Referer:", 8) == 0)) {
1625 for (test = buf + 8; isspace(*test); test++)
1626 ;
1627 config->referer = strdup(test);
1628 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001629#endif
1630
Denis Vlasenko55a99402006-09-30 20:41:44 +00001631#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001632 if (strncasecmp(buf, "Authorization:", 14) == 0) {
1633 /* We only allow Basic credentials.
1634 * It shows up as "Authorization: Basic <userid:password>" where
1635 * the userid:password is base64 encoded.
1636 */
1637 for (test = buf + 14; isspace(*test); test++)
1638 ;
1639 if (strncasecmp(test, "Basic", 5) != 0)
1640 continue;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001641
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001642 test += 5; /* decodeBase64() skiping space self */
1643 decodeBase64(test);
1644 credentials = checkPerm(url, test);
1645 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001646#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1647
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001648 } /* while extra header reading */
1649 }
1650 (void) alarm(0);
1651 if (config->alarm_signaled)
1652 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001653
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001654 if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) {
1655 /* protect listing [/path]/httpd_conf or IP deny */
Denis Vlasenko55a99402006-09-30 20:41:44 +00001656#if ENABLE_FEATURE_HTTPD_CGI
Glenn L McGrath06e95652003-02-09 06:51:14 +00001657FORBIDDEN: /* protect listing /cgi-bin */
1658#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001659 sendHeaders(HTTP_FORBIDDEN);
1660 break;
1661 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001662
Denis Vlasenko55a99402006-09-30 20:41:44 +00001663#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001664 if (credentials <= 0 && checkPerm(url, ":") == 0) {
1665 sendHeaders(HTTP_UNAUTHORIZED);
1666 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001667 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001668#endif
1669
1670 if (config->httpd_found.found_moved_temporarily) {
1671 sendHeaders(HTTP_MOVED_TEMPORARILY);
1672#if DEBUG
1673 /* clear unforked memory flag */
1674 config->httpd_found.found_moved_temporarily = NULL;
1675#endif
1676 break;
1677 }
1678
1679 test = url + 1; /* skip first '/' */
1680
Denis Vlasenko55a99402006-09-30 20:41:44 +00001681#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001682 /* if strange Content-Length */
1683 if (length < 0)
1684 break;
1685
1686 if (strncmp(test, "cgi-bin", 7) == 0) {
1687 if (test[7] == '/' && test[8] == 0)
1688 goto FORBIDDEN; // protect listing cgi-bin/
1689 sendCgi(url, prequest, length, cookie, content_type);
1690 } else {
1691 if (prequest != request_GET)
1692 sendHeaders(HTTP_NOT_IMPLEMENTED);
1693 else {
1694#endif /* CONFIG_FEATURE_HTTPD_CGI */
1695 if (purl[-1] == '/')
1696 strcpy(purl, "index.html");
1697 if (stat(test, &sb) == 0) {
1698 config->ContentLength = sb.st_size;
1699 config->last_mod = sb.st_mtime;
1700 }
1701 sendFile(test);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001702#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001703 /* unset if non inetd looped */
1704 config->ContentLength = -1;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001705#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001706
Denis Vlasenko55a99402006-09-30 20:41:44 +00001707#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001708 }
1709 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001710#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001711
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001712 } while (0);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001713
Glenn L McGrath06e95652003-02-09 06:51:14 +00001714
Denis Vlasenko55a99402006-09-30 20:41:44 +00001715#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
1716/* from inetd don't looping: freeing, closing automatic from exit always */
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001717# if DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001718 fprintf(stderr, "closing socket\n");
Glenn L McGrath06e95652003-02-09 06:51:14 +00001719# endif
1720# ifdef CONFIG_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001721 free(cookie);
1722 free(content_type);
1723 free(config->referer);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001724#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001725 free(config->remoteuser);
Eric Andersen769a3ef2003-12-19 11:23:47 +00001726#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001727# endif
Rob Landleya2d9a172006-04-28 19:38:04 +00001728#endif /* CONFIG_FEATURE_HTTPD_WITHOUT_INETD */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001729 shutdown(a_c_w, SHUT_WR);
Eric Andersend8746cd2004-02-24 07:28:38 +00001730
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001731 /* Properly wait for remote to closed */
Denis Vlasenko55a99402006-09-30 20:41:44 +00001732 FD_ZERO(&s_fd);
1733 FD_SET(a_c_r, &s_fd);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001734
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001735 do {
Denis Vlasenko55a99402006-09-30 20:41:44 +00001736 tv.tv_sec = 2;
1737 tv.tv_usec = 0;
1738 retval = select(a_c_r + 1, &s_fd, NULL, NULL, &tv);
1739 } while (retval > 0 && read(a_c_r, buf, sizeof(config->buf) > 0));
Eric Andersend8746cd2004-02-24 07:28:38 +00001740
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001741 shutdown(a_c_r, SHUT_RD);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001742#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001743 close(config->accepted_socket);
Rob Landleya2d9a172006-04-28 19:38:04 +00001744#endif /* CONFIG_FEATURE_HTTPD_WITHOUT_INETD */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001745}
1746
1747/****************************************************************************
1748 *
1749 > $Function: miniHttpd()
1750 *
1751 * $Description: The main http server function.
1752 *
1753 * Given an open socket fildes, listen for new connections and farm out
1754 * the processing as a forked process.
1755 *
1756 * $Parameters:
1757 * (int) server. . . The server socket fildes.
1758 *
1759 * $Return: (int) . . . . Always 0.
1760 *
1761 ****************************************************************************/
Denis Vlasenko55a99402006-09-30 20:41:44 +00001762#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001763static int miniHttpd(int server)
1764{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001765 fd_set readfd, portfd;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001766
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001767 FD_ZERO(&portfd);
1768 FD_SET(server, &portfd);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001769
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001770 /* copy the ports we are watching to the readfd set */
1771 while (1) {
1772 readfd = portfd;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001773
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001774 /* Now wait INDEFINITELY on the set of sockets! */
1775 if (select(server + 1, &readfd, 0, 0, 0) > 0) {
1776 if (FD_ISSET(server, &readfd)) {
1777 int on;
1778 struct sockaddr_in fromAddr;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001779
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001780 socklen_t fromAddrLen = sizeof(fromAddr);
1781 int s = accept(server,
1782 (struct sockaddr *)&fromAddr, &fromAddrLen);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001783
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001784 if (s < 0) {
1785 continue;
1786 }
1787 config->accepted_socket = s;
1788 config->rmt_ip = ntohl(fromAddr.sin_addr.s_addr);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001789#if ENABLE_FEATURE_HTTPD_CGI || DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001790 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1791 (unsigned char)(config->rmt_ip >> 24),
1792 (unsigned char)(config->rmt_ip >> 16),
1793 (unsigned char)(config->rmt_ip >> 8),
1794 config->rmt_ip & 0xff);
1795 config->port = ntohs(fromAddr.sin_port);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001796#if DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001797 bb_error_msg("connection from IP=%s, port %u",
1798 config->rmt_ip_str, config->port);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001799#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001800#endif /* CONFIG_FEATURE_HTTPD_CGI */
1801
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001802 /* set the KEEPALIVE option to cull dead connections */
1803 on = 1;
Denis Vlasenko55a99402006-09-30 20:41:44 +00001804 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001805
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001806#if !DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001807 if (fork() == 0)
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001808#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001809 {
1810 /* This is the spawned thread */
Denis Vlasenko55a99402006-09-30 20:41:44 +00001811#if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001812 /* protect reload config, may be confuse checking */
1813 signal(SIGHUP, SIG_IGN);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001814#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001815 handleIncoming();
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001816#if !DEBUG
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001817 exit(0);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001818#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001819 }
1820 close(s);
1821 }
1822 }
1823 } // while (1)
1824 return 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001825}
1826
Glenn L McGrath06e95652003-02-09 06:51:14 +00001827#else
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001828 /* from inetd */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001829
1830static int miniHttpd(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001831{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001832 struct sockaddr_in fromAddrLen;
Denis Vlasenko55a99402006-09-30 20:41:44 +00001833 socklen_t sinlen = sizeof(struct sockaddr_in);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001834
Denis Vlasenko55a99402006-09-30 20:41:44 +00001835 getpeername(0, (struct sockaddr *)&fromAddrLen, &sinlen);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001836 config->rmt_ip = ntohl(fromAddrLen.sin_addr.s_addr);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001837#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001838 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1839 (unsigned char)(config->rmt_ip >> 24),
1840 (unsigned char)(config->rmt_ip >> 16),
1841 (unsigned char)(config->rmt_ip >> 8),
1842 config->rmt_ip & 0xff);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001843#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001844 config->port = ntohs(fromAddrLen.sin_port);
1845 handleIncoming();
1846 return 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001847}
Rob Landleya2d9a172006-04-28 19:38:04 +00001848#endif /* CONFIG_FEATURE_HTTPD_WITHOUT_INETD */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001849
Denis Vlasenko55a99402006-09-30 20:41:44 +00001850#if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
Glenn L McGrath06e95652003-02-09 06:51:14 +00001851static void sighup_handler(int sig)
1852{
1853 /* set and reset */
1854 struct sigaction sa;
1855
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001856 parse_conf(default_path_httpd_conf, sig == SIGHUP ? SIGNALED_PARSE : FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001857 sa.sa_handler = sighup_handler;
1858 sigemptyset(&sa.sa_mask);
1859 sa.sa_flags = SA_RESTART;
1860 sigaction(SIGHUP, &sa, NULL);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001861}
1862#endif
1863
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001864enum httpd_opts_nums {
1865 c_opt_config_file = 0,
1866 d_opt_decode_url,
1867 h_opt_home_httpd,
1868 USE_FEATURE_HTTPD_ENCODE_URL_STR(e_opt_encode_url,)
1869 USE_FEATURE_HTTPD_BASIC_AUTH(r_opt_realm,)
1870 USE_FEATURE_HTTPD_AUTH_MD5(m_opt_md5,)
1871 USE_FEATURE_HTTPD_SETUID(u_opt_setuid,)
Rob Landleya2d9a172006-04-28 19:38:04 +00001872 USE_FEATURE_HTTPD_WITHOUT_INETD(p_opt_port,)
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001873};
Eric Andersena3bb3e62003-06-26 09:05:32 +00001874
Denis Vlasenko55a99402006-09-30 20:41:44 +00001875static const char httpd_opts[] = "c:d:h:"
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001876 USE_FEATURE_HTTPD_ENCODE_URL_STR("e:")
1877 USE_FEATURE_HTTPD_BASIC_AUTH("r:")
1878 USE_FEATURE_HTTPD_AUTH_MD5("m:")
1879 USE_FEATURE_HTTPD_SETUID("u:")
Rob Landleya2d9a172006-04-28 19:38:04 +00001880 USE_FEATURE_HTTPD_WITHOUT_INETD("p:");
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001881
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001882#define OPT_CONFIG_FILE (1<<c_opt_config_file)
1883#define OPT_DECODE_URL (1<<d_opt_decode_url)
1884#define OPT_HOME_HTTPD (1<<h_opt_home_httpd)
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001885
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001886#define OPT_ENCODE_URL USE_FEATURE_HTTPD_ENCODE_URL_STR((1<<e_opt_encode_url)) \
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001887 SKIP_FEATURE_HTTPD_ENCODE_URL_STR(0)
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001888
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001889#define OPT_REALM USE_FEATURE_HTTPD_BASIC_AUTH((1<<r_opt_realm)) \
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001890 SKIP_FEATURE_HTTPD_BASIC_AUTH(0)
Eric Andersena3bb3e62003-06-26 09:05:32 +00001891
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001892#define OPT_MD5 USE_FEATURE_HTTPD_AUTH_MD5((1<<m_opt_md5)) \
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001893 SKIP_FEATURE_HTTPD_AUTH_MD5(0)
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001894
1895#define OPT_SETUID USE_FEATURE_HTTPD_SETUID((1<<u_opt_setuid)) \
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001896 SKIP_FEATURE_HTTPD_SETUID(0)
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001897
Rob Landleya2d9a172006-04-28 19:38:04 +00001898#define OPT_PORT USE_FEATURE_HTTPD_WITHOUT_INETD((1<<p_opt_port)) \
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001899 SKIP_FEATURE_HTTPD_WITHOUT_INETD(0)
Eric Andersena3bb3e62003-06-26 09:05:32 +00001900
1901
Glenn L McGrath06e95652003-02-09 06:51:14 +00001902int httpd_main(int argc, char *argv[])
Glenn L McGrath06e95652003-02-09 06:51:14 +00001903{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001904 unsigned opt;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001905 const char *home_httpd = home;
1906 char *url_for_decode;
1907 USE_FEATURE_HTTPD_ENCODE_URL_STR(const char *url_for_encode;)
1908 USE_FEATURE_HTTPD_WITHOUT_INETD(const char *s_port;)
1909 USE_FEATURE_HTTPD_WITHOUT_INETD(int server;)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001910
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001911 USE_FEATURE_HTTPD_SETUID(const char *s_ugid = NULL;)
1912 USE_FEATURE_HTTPD_SETUID(struct bb_uidgid_t ugid;)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001913
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001914 USE_FEATURE_HTTPD_AUTH_MD5(const char *pass;)
Eric Andersen35e643b2003-07-28 07:40:39 +00001915
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001916 config = xzalloc(sizeof(*config));
Denis Vlasenko55a99402006-09-30 20:41:44 +00001917#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001918 config->realm = "Web Server Authentication";
Glenn L McGrath06e95652003-02-09 06:51:14 +00001919#endif
1920
Denis Vlasenko55a99402006-09-30 20:41:44 +00001921#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001922 config->port = 80;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001923#endif
1924
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001925 config->ContentLength = -1;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001926
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001927 opt = getopt32(argc, argv, httpd_opts,
Eric Andersena3bb3e62003-06-26 09:05:32 +00001928 &(config->configFile), &url_for_decode, &home_httpd
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001929 USE_FEATURE_HTTPD_ENCODE_URL_STR(, &url_for_encode)
1930 USE_FEATURE_HTTPD_BASIC_AUTH(, &(config->realm))
1931 USE_FEATURE_HTTPD_AUTH_MD5(, &pass)
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001932 USE_FEATURE_HTTPD_SETUID(, &s_ugid)
Rob Landleya2d9a172006-04-28 19:38:04 +00001933 USE_FEATURE_HTTPD_WITHOUT_INETD(, &s_port)
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001934 );
Eric Andersena3bb3e62003-06-26 09:05:32 +00001935
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001936 if (opt & OPT_DECODE_URL) {
1937 printf("%s", decodeString(url_for_decode, 1));
1938 return 0;
1939 }
Denis Vlasenko55a99402006-09-30 20:41:44 +00001940#if ENABLE_FEATURE_HTTPD_ENCODE_URL_STR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001941 if (opt & OPT_ENCODE_URL) {
1942 printf("%s", encodeString(url_for_encode));
1943 return 0;
1944 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001945#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +00001946#if ENABLE_FEATURE_HTTPD_AUTH_MD5
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001947 if (opt & OPT_MD5) {
Denis Vlasenko55a99402006-09-30 20:41:44 +00001948 puts(pw_encrypt(pass, "$1$"));
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001949 return 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001950 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001951#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +00001952#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001953 if (opt & OPT_PORT)
Denis Vlasenko13858992006-10-08 12:49:22 +00001954 config->port = xatou16(s_port);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001955#if ENABLE_FEATURE_HTTPD_SETUID
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001956 if (opt & OPT_SETUID) {
1957 char *e;
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001958 // FIXME: what the default group should be?
1959 ugid.gid = -1;
1960 ugid.uid = strtoul(s_ugid, &e, 0);
1961 if (*e == ':') {
1962 e++;
1963 ugid.gid = strtoul(e, &e, 0);
1964 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001965 if (*e != '\0') {
1966 /* not integer */
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001967 if (!uidgid_get(&ugid, s_ugid))
1968 bb_error_msg_and_die("unrecognized user[:group] "
1969 "name '%s'", s_ugid);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001970 }
1971 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001972#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00001973#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001974
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001975 xchdir(home_httpd);
Denis Vlasenko55a99402006-09-30 20:41:44 +00001976#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001977 server = openServer();
Glenn L McGrath06e95652003-02-09 06:51:14 +00001978# ifdef CONFIG_FEATURE_HTTPD_SETUID
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001979 /* drop privileges */
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001980 if (opt & OPT_SETUID) {
1981 if (ugid.gid != (gid_t)-1) {
1982 // FIXME: needed?
1983 //if (setgroups(1, &ugid.gid) == -1)
1984 // bb_perror_msg_and_die("setgroups");
1985 xsetgid(ugid.gid);
1986 }
1987 xsetuid(ugid.uid);
1988 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001989# endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001990#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001991
Denis Vlasenko55a99402006-09-30 20:41:44 +00001992#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001993 {
1994 char *p = getenv("PATH");
1995 if (p) {
1996 p = xstrdup(p);
1997 }
1998 clearenv();
1999 if (p)
2000 setenv("PATH", p, 1);
Rob Landleya2d9a172006-04-28 19:38:04 +00002001# ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002002 addEnvPort("SERVER");
Glenn L McGrath14092a12003-09-12 00:44:50 +00002003# endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002004 }
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002005#endif
2006
Denis Vlasenko55a99402006-09-30 20:41:44 +00002007#if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002008 sighup_handler(0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002009#else
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002010 parse_conf(default_path_httpd_conf, FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002011#endif
2012
Denis Vlasenko55a99402006-09-30 20:41:44 +00002013#if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00002014# if !DEBUG
Denis Vlasenko55a99402006-09-30 20:41:44 +00002015 xdaemon(1, 0); /* don't change curent directory */
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00002016# endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002017 return miniHttpd(server);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002018#else
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002019 return miniHttpd();
Glenn L McGrath06e95652003-02-09 06:51:14 +00002020#endif
2021}