blob: 529f66bc4b8a3555526ba22148c05d8bc44cfb2f [file] [log] [blame]
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002/*
3 * httpd implementation for busybox
4 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00005 * Copyright (C) 2002,2003 Glenn Engel <glenne@engel.org>
"Vladimir N. Oleynik"79af7d52006-01-26 10:58:12 +00006 * Copyright (C) 2003-2006 Vladimir Oleynik <dzo@simtreas.ru>
Glenn L McGrath58c708a2003-01-05 04:01:56 +00007 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00008 * simplify patch stolen from libbb without using strdup
Glenn L McGrath58c708a2003-01-05 04:01:56 +00009 *
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +000010 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000011 *
12 *****************************************************************************
13 *
Glenn L McGrath06e95652003-02-09 06:51:14 +000014 * Typical usage:
15 * for non root user
16 * httpd -p 8080 -h $HOME/public_html
17 * or for daemon start from rc script with uid=0:
18 * httpd -u www
19 * This is equivalent if www user have uid=80 to
20 * httpd -p 80 -u 80 -h /www -c /etc/httpd.conf -r "Web Server Authentication"
21 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +000022 *
23 * When a url contains "cgi-bin" it is assumed to be a cgi script. The
24 * server changes directory to the location of the script and executes it
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +000025 * after setting QUERY_STRING and other environment variables.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000026 *
Denis Vlasenko8b458372006-11-21 21:23:21 +000027 * Doc:
28 * "CGI Environment Variables": http://hoohoo.ncsa.uiuc.edu/cgi/env.html
29 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +000030 * The server can also be invoked as a url arg decoder and html text encoder
31 * as follows:
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000032 * foo=`httpd -d $foo` # decode "Hello%20World" as "Hello World"
Glenn L McGrathc9163fe2003-05-13 16:20:11 +000033 * bar=`httpd -e "<Hello World>"` # encode as "&#60Hello&#32World&#62"
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000034 * Note that url encoding for arguments is not the same as html encoding for
Eric Andersenaff114c2004-04-14 17:51:38 +000035 * presentation. -d decodes a url-encoded argument while -e encodes in html
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000036 * for page display.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000037 *
38 * httpd.conf has the following format:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000039 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +000040 * A:172.20. # Allow address from 172.20.0.0/16
41 * A:10.0.0.0/25 # Allow any address from 10.0.0.0-10.0.0.127
42 * A:10.0.0.0/255.255.255.128 # Allow any address that previous set
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000043 * A:127.0.0.1 # Allow local loopback connections
44 * D:* # Deny from other IP connections
45 * /cgi-bin:foo:bar # Require user foo, pwd bar on urls starting with /cgi-bin/
46 * /adm:admin:setup # Require user admin, pwd setup on urls starting with /adm/
47 * /adm:toor:PaSsWd # or user toor, pwd PaSsWd on urls starting with /adm/
48 * .au:audio/basic # additional mime type for audio.au files
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +000049 * *.php:/path/php # running cgi.php scripts through an interpreter
Eric Andersenc7bda1c2004-03-15 08:29:22 +000050 *
Eric Andersenaff114c2004-04-14 17:51:38 +000051 * A/D may be as a/d or allow/deny - first char case insensitive
Glenn L McGrath393183d2003-05-26 14:07:50 +000052 * Deny IP rules take precedence over allow rules.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000053 *
54 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000055 * The Deny/Allow IP logic:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000056 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000057 * - Default is to allow all. No addresses are denied unless
Eric Andersen97a1de12004-08-26 22:22:50 +000058 * denied with a D: rule.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000059 * - Order of Deny/Allow rules is significant
60 * - Deny rules take precedence over allow rules.
61 * - If a deny all rule (D:*) is used it acts as a catch-all for unmatched
Eric Andersen97a1de12004-08-26 22:22:50 +000062 * addresses.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000063 * - Specification of Allow all (A:*) is a no-op
Eric Andersenc7bda1c2004-03-15 08:29:22 +000064 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000065 * Example:
66 * 1. Allow only specified addresses
Glenn L McGrathb65422c2003-09-08 10:59:27 +000067 * A:172.20 # Allow any address that begins with 172.20.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000068 * A:10.10. # Allow any address that begins with 10.10.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000069 * A:127.0.0.1 # Allow local loopback connections
70 * D:* # Deny from other IP connections
Eric Andersenc7bda1c2004-03-15 08:29:22 +000071 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000072 * 2. Only deny specified addresses
73 * D:1.2.3. # deny from 1.2.3.0 - 1.2.3.255
74 * D:2.3.4. # deny from 2.3.4.0 - 2.3.4.255
75 * A:* # (optional line added for clarity)
Eric Andersenc7bda1c2004-03-15 08:29:22 +000076 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000077 * If a sub directory contains a config file it is parsed and merged with
Glenn L McGrathb65422c2003-09-08 10:59:27 +000078 * any existing settings as if it was appended to the original configuration.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000079 *
80 * subdir paths are relative to the containing subdir and thus cannot
81 * affect the parent rules.
82 *
83 * Note that since the sub dir is parsed in the forked thread servicing the
84 * subdir http request, any merge is discarded when the process exits. As a
85 * result, the subdir settings only have a lifetime of a single request.
86 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000087 *
88 * If -c is not set, an attempt will be made to open the default
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000089 * root configuration file. If -c is set and the file is not found, the
90 * server exits with an error.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000091 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000092*/
Glenn L McGrath58c708a2003-01-05 04:01:56 +000093
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000094#include "libbb.h"
Glenn L McGrath58c708a2003-01-05 04:01:56 +000095
Denis Vlasenko69981422007-01-07 21:25:12 +000096/* amount of buffering in a pipe */
97#ifndef PIPE_BUF
98# define PIPE_BUF 4096
99#endif
100
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000101static const char default_path_httpd_conf[] = "/etc";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000102static const char httpd_conf[] = "httpd.conf";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000103
Eric Andersen07f2fea2004-10-08 08:03:29 +0000104#define TIMEOUT 60
105
Eric Andersenaff114c2004-04-14 17:51:38 +0000106// Note: busybox xfuncs are not used because we want the server to keep running
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000107// if something bad happens due to a malformed user request.
Glenn L McGrath06e95652003-02-09 06:51:14 +0000108// As a result, all memory allocation after daemonize
109// is checked rigorously
110
111//#define DEBUG 1
Denis Vlasenko69981422007-01-07 21:25:12 +0000112#define DEBUG 0
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000113
Glenn L McGrath06e95652003-02-09 06:51:14 +0000114#define MAX_MEMORY_BUFF 8192 /* IO buffer */
115
116typedef struct HT_ACCESS {
117 char *after_colon;
118 struct HT_ACCESS *next;
119 char before_colon[1]; /* really bigger, must last */
120} Htaccess;
121
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000122typedef struct HT_ACCESS_IP {
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000123 unsigned ip;
124 unsigned mask;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000125 int allow_deny;
126 struct HT_ACCESS_IP *next;
127} Htaccess_IP;
128
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000129struct globals {
130 int server_socket;
131 int accepted_socket;
132 volatile smallint alarm_signaled;
133 smallint flg_deny_all;
134 const char *g_query;
135 const char *configFile;
136 const char *home_httpd;
137 unsigned rmt_ip;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000138
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000139 const char *found_mime_type;
140 const char *found_moved_temporarily;
141 off_t ContentLength; /* -1 - unknown */
142 time_t last_mod;
143 Htaccess_IP *ip_a_d; /* config allow/deny lines */
144
145 USE_FEATURE_HTTPD_BASIC_AUTH(const char *g_realm;)
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000146 USE_FEATURE_HTTPD_BASIC_AUTH(char *remoteuser;)
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000147 USE_FEATURE_HTTPD_CGI(char *referer;)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000148
Denis Vlasenko55a99402006-09-30 20:41:44 +0000149#if ENABLE_FEATURE_HTTPD_CGI || DEBUG
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000150 char *rmt_ip_str; /* for set env REMOTE_ADDR */
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000151#endif
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000152 unsigned tcp_port; /* server initial port and for
153 set env REMOTE_PORT */
Denis Vlasenko55a99402006-09-30 20:41:44 +0000154#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000155 Htaccess *g_auth; /* config user:password lines */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000156#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000157#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000158 Htaccess *mime_a; /* config mime types */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000159#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000160#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000161 Htaccess *script_i; /* config script interpreters */
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000162#endif
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000163 char iobuf[MAX_MEMORY_BUFF];
164};
165#define G (*ptr_to_globals)
166#define server_socket (G.server_socket )
167#define accepted_socket (G.accepted_socket)
168#define alarm_signaled (G.alarm_signaled )
169#define flg_deny_all (G.flg_deny_all )
170#define g_query (G.g_query )
171#define configFile (G.configFile )
172#define home_httpd (G.home_httpd )
173#define rmt_ip (G.rmt_ip )
174#define found_mime_type (G.found_mime_type)
175#define found_moved_temporarily (G.found_moved_temporarily)
176#define ContentLength (G.ContentLength )
177#define last_mod (G.last_mod )
178#define ip_a_d (G.ip_a_d )
179#define g_realm (G.g_realm )
180#define remoteuser (G.remoteuser )
181#define referer (G.referer )
182#if ENABLE_FEATURE_HTTPD_CGI || DEBUG
183#define rmt_ip_str (G.rmt_ip_str )
184#endif
185#define tcp_port (G.tcp_port )
186#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
187#define g_auth (G.g_auth )
188#endif
189#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
190#define mime_a (G.mime_a )
191#endif
192#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
193#define script_i (G.script_i )
194#endif
195#define iobuf (G.iobuf )
196#define INIT_G() do { \
197 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \
198 USE_FEATURE_HTTPD_BASIC_AUTH(g_realm = "Web Server Authentication";) \
199 tcp_port = 80; \
200 ContentLength = -1; \
201} while (0)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000202
Mike Frysingerfa6c4842006-05-26 01:48:17 +0000203static const char request_GET[] = "GET"; /* size algorithmic optimize */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000204
205static const char* const suffixTable [] = {
Eric Andersenaff114c2004-04-14 17:51:38 +0000206/* Warning: shorted equivalent suffix in one line must be first */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000207 ".htm.html", "text/html",
208 ".jpg.jpeg", "image/jpeg",
209 ".gif", "image/gif",
210 ".png", "image/png",
211 ".txt.h.c.cc.cpp", "text/plain",
212 ".css", "text/css",
213 ".wav", "audio/wav",
214 ".avi", "video/x-msvideo",
215 ".qt.mov", "video/quicktime",
216 ".mpe.mpeg", "video/mpeg",
217 ".mid.midi", "audio/midi",
218 ".mp3", "audio/mpeg",
Glenn L McGrath06e95652003-02-09 06:51:14 +0000219#if 0 /* unpopular */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000220 ".au", "audio/basic",
221 ".pac", "application/x-ns-proxy-autoconfig",
222 ".vrml.wrl", "model/vrml",
Glenn L McGrath06e95652003-02-09 06:51:14 +0000223#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000224 0, "application/octet-stream" /* default */
225};
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000226
Denis Vlasenko55a99402006-09-30 20:41:44 +0000227typedef enum {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000228 HTTP_OK = 200,
229 HTTP_MOVED_TEMPORARILY = 302,
230 HTTP_BAD_REQUEST = 400, /* malformed syntax */
231 HTTP_UNAUTHORIZED = 401, /* authentication needed, respond with auth hdr */
232 HTTP_NOT_FOUND = 404,
233 HTTP_FORBIDDEN = 403,
234 HTTP_REQUEST_TIMEOUT = 408,
235 HTTP_NOT_IMPLEMENTED = 501, /* used for unrecognized requests */
236 HTTP_INTERNAL_SERVER_ERROR = 500,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000237#if 0 /* future use */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000238 HTTP_CONTINUE = 100,
239 HTTP_SWITCHING_PROTOCOLS = 101,
240 HTTP_CREATED = 201,
241 HTTP_ACCEPTED = 202,
242 HTTP_NON_AUTHORITATIVE_INFO = 203,
243 HTTP_NO_CONTENT = 204,
244 HTTP_MULTIPLE_CHOICES = 300,
245 HTTP_MOVED_PERMANENTLY = 301,
246 HTTP_NOT_MODIFIED = 304,
247 HTTP_PAYMENT_REQUIRED = 402,
248 HTTP_BAD_GATEWAY = 502,
249 HTTP_SERVICE_UNAVAILABLE = 503, /* overload, maintenance */
250 HTTP_RESPONSE_SETSIZE = 0xffffffff
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000251#endif
252} HttpResponseNum;
253
Denis Vlasenko55a99402006-09-30 20:41:44 +0000254typedef struct {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000255 HttpResponseNum type;
256 const char *name;
257 const char *info;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000258} HttpEnumString;
259
260static const HttpEnumString httpResponseNames[] = {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000261 { HTTP_OK, "OK", NULL },
262 { HTTP_MOVED_TEMPORARILY, "Found", "Directories must end with a slash." },
263 { HTTP_REQUEST_TIMEOUT, "Request Timeout",
264 "No request appeared within a reasonable time period." },
265 { HTTP_NOT_IMPLEMENTED, "Not Implemented",
266 "The requested method is not recognized by this server." },
Denis Vlasenko55a99402006-09-30 20:41:44 +0000267#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000268 { HTTP_UNAUTHORIZED, "Unauthorized", "" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000269#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000270 { HTTP_NOT_FOUND, "Not Found",
271 "The requested URL was not found on this server." },
272 { HTTP_BAD_REQUEST, "Bad Request", "Unsupported method." },
273 { HTTP_FORBIDDEN, "Forbidden", "" },
274 { HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error",
275 "Internal Server Error" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000276#if 0 /* not implemented */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000277 { HTTP_CREATED, "Created" },
278 { HTTP_ACCEPTED, "Accepted" },
279 { HTTP_NO_CONTENT, "No Content" },
280 { HTTP_MULTIPLE_CHOICES, "Multiple Choices" },
281 { HTTP_MOVED_PERMANENTLY, "Moved Permanently" },
282 { HTTP_NOT_MODIFIED, "Not Modified" },
283 { HTTP_BAD_GATEWAY, "Bad Gateway", "" },
284 { HTTP_SERVICE_UNAVAILABLE, "Service Unavailable", "" },
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000285#endif
286};
287
Glenn L McGrath06e95652003-02-09 06:51:14 +0000288
289static const char RFC1123FMT[] = "%a, %d %b %Y %H:%M:%S GMT";
Denis Vlasenko0bb993f2006-11-21 00:06:28 +0000290
291
292#define STRNCASECMP(a, str) strncasecmp((a), (str), sizeof(str)-1)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000293
294
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000295static int scan_ip(const char **ep, unsigned *ip, unsigned char endc)
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000296{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000297 const char *p = *ep;
298 int auto_mask = 8;
299 int j;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000300
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000301 *ip = 0;
302 for (j = 0; j < 4; j++) {
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000303 unsigned octet;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000304
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000305 if ((*p < '0' || *p > '9') && (*p != '/' || j == 0) && *p != 0)
306 return -auto_mask;
307 octet = 0;
308 while (*p >= '0' && *p <= '9') {
309 octet *= 10;
310 octet += *p - '0';
311 if (octet > 255)
312 return -auto_mask;
313 p++;
314 }
315 if (*p == '.')
316 p++;
317 if (*p != '/' && *p != 0)
318 auto_mask += 8;
319 *ip = ((*ip) << 8) | octet;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000320 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000321 if (*p != 0) {
322 if (*p != endc)
323 return -auto_mask;
324 p++;
325 if (*p == 0)
326 return -auto_mask;
327 }
328 *ep = p;
329 return auto_mask;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000330}
331
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000332static int scan_ip_mask(const char *ipm, unsigned *ip, unsigned *mask)
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000333{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000334 int i;
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000335 unsigned msk;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000336
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000337 i = scan_ip(&ipm, ip, '/');
338 if (i < 0)
339 return i;
340 if (*ipm) {
341 const char *p = ipm;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000342
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000343 i = 0;
344 while (*p) {
Denis Vlasenko92758142006-10-03 19:56:34 +0000345 if (*p < '0' || *p > '9') {
346 if (*p == '.') {
347 i = scan_ip(&ipm, mask, 0);
348 return i != 32;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000349 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000350 return -1;
351 }
352 i *= 10;
353 i += *p - '0';
354 p++;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000355 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000356 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000357 if (i > 32 || i < 0)
Denis Vlasenko92758142006-10-03 19:56:34 +0000358 return -1;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000359 msk = 0x80000000;
360 *mask = 0;
361 while (i > 0) {
362 *mask |= msk;
363 msk >>= 1;
364 i--;
365 }
366 return 0;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000367}
368
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000369#if ENABLE_FEATURE_HTTPD_BASIC_AUTH \
370 || ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES \
371 || ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000372static void free_config_lines(Htaccess **pprev)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000373{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000374 Htaccess *prev = *pprev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000375
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000376 while (prev) {
377 Htaccess *cur = prev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000378
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000379 prev = cur->next;
380 free(cur);
381 }
382 *pprev = NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000383}
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000384#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000385
Glenn L McGrath06e95652003-02-09 06:51:14 +0000386/* flag */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000387#define FIRST_PARSE 0
388#define SUBDIR_PARSE 1
389#define SIGNALED_PARSE 2
390#define FIND_FROM_HTTPD_ROOT 3
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000391/****************************************************************************
392 *
393 > $Function: parse_conf()
394 *
395 * $Description: parse configuration file into in-memory linked list.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000396 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000397 * The first non-white character is examined to determine if the config line
398 * is one of the following:
399 * .ext:mime/type # new mime type not compiled into httpd
400 * [adAD]:from # ip address allow/deny, * for wildcard
401 * /path:user:pass # username/password
402 *
403 * Any previous IP rules are discarded.
404 * If the flag argument is not SUBDIR_PARSE then all /path and mime rules
405 * are also discarded. That is, previous settings are retained if flag is
406 * SUBDIR_PARSE.
407 *
408 * $Parameters:
409 * (const char *) path . . null for ip address checks, path for password
410 * checks.
411 * (int) flag . . . . . . the source of the parse request.
412 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000413 * $Return: (None)
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000414 *
415 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000416static void parse_conf(const char *path, int flag)
417{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000418 FILE *f;
Denis Vlasenko55a99402006-09-30 20:41:44 +0000419#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000420 Htaccess *prev;
421#endif
422#if ENABLE_FEATURE_HTTPD_BASIC_AUTH \
423 || ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES \
424 || ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000425 Htaccess *cur;
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000426#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000427
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000428 const char *cf = configFile;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000429 char buf[160];
430 char *p0 = NULL;
431 char *c, *p;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000432
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000433 /* free previous ip setup if present */
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000434 Htaccess_IP *pip = ip_a_d;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000435
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000436 while (pip) {
437 Htaccess_IP *cur_ipl = pip;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000438
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000439 pip = cur_ipl->next;
440 free(cur_ipl);
441 }
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000442 ip_a_d = NULL;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000443
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000444 flg_deny_all = 0;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000445
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000446#if ENABLE_FEATURE_HTTPD_BASIC_AUTH \
447 || ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES \
448 || ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000449 /* retain previous auth and mime config only for subdir parse */
450 if (flag != SUBDIR_PARSE) {
Denis Vlasenko55a99402006-09-30 20:41:44 +0000451#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000452 free_config_lines(&g_auth);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000453#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000454#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000455 free_config_lines(&mime_a);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000456#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000457#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000458 free_config_lines(&script_i);
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000459#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000460 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000461#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000462
463 if (flag == SUBDIR_PARSE || cf == NULL) {
464 cf = alloca(strlen(path) + sizeof(httpd_conf) + 2);
465 if (cf == NULL) {
466 if (flag == FIRST_PARSE)
467 bb_error_msg_and_die(bb_msg_memory_exhausted);
468 return;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000469 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000470 sprintf((char *)cf, "%s/%s", path, httpd_conf);
Glenn L McGrath393183d2003-05-26 14:07:50 +0000471 }
472
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000473 while ((f = fopen(cf, "r")) == NULL) {
474 if (flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) {
475 /* config file not found, no changes to config */
476 return;
477 }
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000478 if (configFile && flag == FIRST_PARSE) /* if -c option given */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000479 bb_perror_msg_and_die("%s", cf);
480 flag = FIND_FROM_HTTPD_ROOT;
481 cf = httpd_conf;
482 }
483
Denis Vlasenko55a99402006-09-30 20:41:44 +0000484#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000485 prev = g_auth;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000486#endif
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000487 /* This could stand some work */
Denis Vlasenko55a99402006-09-30 20:41:44 +0000488 while ((p0 = fgets(buf, sizeof(buf), f)) != NULL) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000489 c = NULL;
490 for (p = p0; *p0 != 0 && *p0 != '#'; p0++) {
491 if (!isspace(*p0)) {
492 *p++ = *p0;
493 if (*p0 == ':' && c == NULL)
494 c = p;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000495 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000496 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000497 *p = 0;
498
499 /* test for empty or strange line */
500 if (c == NULL || *c == 0)
501 continue;
502 p0 = buf;
503 if (*p0 == 'd')
504 *p0 = 'D';
505 if (*c == '*') {
506 if (*p0 == 'D') {
507 /* memorize deny all */
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000508 flg_deny_all = 1;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000509 }
510 /* skip default other "word:*" config lines */
511 continue;
512 }
513
514 if (*p0 == 'a')
515 *p0 = 'A';
516 else if (*p0 != 'D' && *p0 != 'A'
Denis Vlasenko55a99402006-09-30 20:41:44 +0000517#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000518 && *p0 != '/'
519#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000520#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000521 && *p0 != '.'
522#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000523#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000524 && *p0 != '*'
525#endif
526 )
527 continue;
528 if (*p0 == 'A' || *p0 == 'D') {
529 /* storing current config IP line */
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000530 pip = xzalloc(sizeof(Htaccess_IP));
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000531 if (pip) {
Denis Vlasenko55a99402006-09-30 20:41:44 +0000532 if (scan_ip_mask(c, &(pip->ip), &(pip->mask))) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000533 /* syntax IP{/mask} error detected, protect all */
534 *p0 = 'D';
535 pip->mask = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000536 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000537 pip->allow_deny = *p0;
538 if (*p0 == 'D') {
539 /* Deny:form_IP move top */
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000540 pip->next = ip_a_d;
541 ip_a_d = pip;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000542 } else {
543 /* add to bottom A:form_IP config line */
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000544 Htaccess_IP *prev_IP = ip_a_d;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000545
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000546 if (prev_IP == NULL) {
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000547 ip_a_d = pip;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000548 } else {
549 while (prev_IP->next)
550 prev_IP = prev_IP->next;
551 prev_IP->next = pip;
552 }
553 }
554 }
555 continue;
556 }
Denis Vlasenko55a99402006-09-30 20:41:44 +0000557#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000558 if (*p0 == '/') {
559 /* make full path from httpd root / curent_path / config_line_path */
560 cf = flag == SUBDIR_PARSE ? path : "";
561 p0 = malloc(strlen(cf) + (c - buf) + 2 + strlen(c));
562 if (p0 == NULL)
563 continue;
564 c[-1] = 0;
565 sprintf(p0, "/%s%s", cf, buf);
566
567 /* another call bb_simplify_path */
568 cf = p = p0;
569
570 do {
571 if (*p == '/') {
572 if (*cf == '/') { /* skip duplicate (or initial) slash */
573 continue;
574 } else if (*cf == '.') {
575 if (cf[1] == '/' || cf[1] == 0) { /* remove extra '.' */
576 continue;
577 } else if ((cf[1] == '.') && (cf[2] == '/' || cf[2] == 0)) {
578 ++cf;
579 if (p > p0) {
Denis Vlasenko92758142006-10-03 19:56:34 +0000580 while (*--p != '/') /* omit previous dir */;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000581 }
582 continue;
583 }
584 }
585 }
586 *++p = *cf;
587 } while (*++cf);
588
589 if ((p == p0) || (*p != '/')) { /* not a trailing slash */
590 ++p; /* so keep last character */
591 }
592 *p = 0;
593 sprintf(p0, "%s:%s", p0, c);
594 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000595#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000596
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000597#if ENABLE_FEATURE_HTTPD_BASIC_AUTH \
598 || ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES \
599 || ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000600 /* storing current config line */
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000601 cur = xzalloc(sizeof(Htaccess) + strlen(p0));
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000602 if (cur) {
603 cf = strcpy(cur->before_colon, p0);
604 c = strchr(cf, ':');
605 *c++ = 0;
606 cur->after_colon = c;
Denis Vlasenko55a99402006-09-30 20:41:44 +0000607#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000608 if (*cf == '.') {
609 /* config .mime line move top for overwrite previous */
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000610 cur->next = mime_a;
611 mime_a = cur;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000612 continue;
613 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000614#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000615#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000616 if (*cf == '*' && cf[1] == '.') {
617 /* config script interpreter line move top for overwrite previous */
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000618 cur->next = script_i;
619 script_i = cur;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000620 continue;
621 }
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000622#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +0000623#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000624 free(p0);
625 if (prev == NULL) {
626 /* first line */
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000627 g_auth = prev = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000628 } else {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000629 /* sort path, if current lenght eq or bigger then move up */
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000630 Htaccess *prev_hti = g_auth;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000631 size_t l = strlen(cf);
632 Htaccess *hti;
633
634 for (hti = prev_hti; hti; hti = hti->next) {
635 if (l >= strlen(hti->before_colon)) {
636 /* insert before hti */
637 cur->next = hti;
638 if (prev_hti != hti) {
639 prev_hti->next = cur;
640 } else {
641 /* insert as top */
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000642 g_auth = cur;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000643 }
644 break;
645 }
646 if (prev_hti != hti)
647 prev_hti = prev_hti->next;
648 }
649 if (!hti) { /* not inserted, add to bottom */
650 prev->next = cur;
651 prev = cur;
652 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000653 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000654#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000655 }
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000656#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000657 }
658 fclose(f);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000659}
660
Denis Vlasenko55a99402006-09-30 20:41:44 +0000661#if ENABLE_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000662/****************************************************************************
663 *
664 > $Function: encodeString()
665 *
666 * $Description: Given a string, html encode special characters.
667 * This is used for the -e command line option to provide an easy way
668 * for scripts to encode result data without confusing browsers. The
669 * returned string pointer is memory allocated by malloc().
670 *
671 * $Parameters:
672 * (const char *) string . . The first string to encode.
673 *
674 * $Return: (char *) . . . .. . . A pointer to the encoded string.
675 *
676 * $Errors: Returns a null string ("") if memory is not available.
677 *
678 ****************************************************************************/
679static char *encodeString(const char *string)
680{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000681 /* take the simple route and encode everything */
682 /* could possibly scan once to get length. */
683 int len = strlen(string);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000684 char *out = xmalloc(len * 6 + 1);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000685 char *p = out;
686 char ch;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000687
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000688 while ((ch = *string++)) {
689 // very simple check for what to encode
690 if (isalnum(ch)) *p++ = ch;
691 else p += sprintf(p, "&#%d;", (unsigned char) ch);
692 }
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000693 *p = '\0';
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000694 return out;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000695}
Denis Vlasenkob3a07152006-11-16 18:04:43 +0000696#endif /* FEATURE_HTTPD_ENCODE_URL_STR */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000697
698/****************************************************************************
699 *
700 > $Function: decodeString()
701 *
702 * $Description: Given a URL encoded string, convert it to plain ascii.
703 * Since decoding always makes strings smaller, the decode is done in-place.
704 * Thus, callers should strdup() the argument if they do not want the
705 * argument modified. The return is the original pointer, allowing this
706 * function to be easily used as arguments to other functions.
707 *
708 * $Parameters:
709 * (char *) string . . . The first string to decode.
Denis Vlasenkoa35c9e92006-11-29 15:58:50 +0000710 * (int) option_d . . 1 if called for httpd -d
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000711 *
712 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
713 *
714 * $Errors: None
715 *
716 ****************************************************************************/
Denis Vlasenkoa35c9e92006-11-29 15:58:50 +0000717static char *decodeString(char *orig, int option_d)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000718{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000719 /* note that decoded string is always shorter than original */
720 char *string = orig;
721 char *ptr = string;
Denis Vlasenkoa35c9e92006-11-29 15:58:50 +0000722 char c;
Eric Andersena3bb3e62003-06-26 09:05:32 +0000723
Denis Vlasenkoa35c9e92006-11-29 15:58:50 +0000724 while ((c = *ptr++) != '\0') {
725 unsigned value1, value2;
726
727 if (option_d && c == '+') {
Denis Vlasenko601ae132006-11-28 23:37:46 +0000728 *string++ = ' ';
Denis Vlasenkoa35c9e92006-11-29 15:58:50 +0000729 continue;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000730 }
Denis Vlasenkoa35c9e92006-11-29 15:58:50 +0000731 if (c != '%') {
732 *string++ = c;
733 continue;
734 }
735 if (sscanf(ptr, "%1X", &value1) != 1
736 || sscanf(ptr+1, "%1X", &value2) != 1
737 ) {
738 if (!option_d)
739 return NULL;
740 *string++ = '%';
741 continue;
742 }
743 value1 = value1 * 16 + value2;
744 if (!option_d && (value1 == '/' || value1 == '\0')) {
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000745 /* caller takes it as indication of invalid
Denis Vlasenkoa35c9e92006-11-29 15:58:50 +0000746 * (dangerous wrt exploits) chars */
747 return orig + 1;
748 }
749 *string++ = value1;
750 ptr += 2;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000751 }
752 *string = '\0';
753 return orig;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000754}
755
756
Denis Vlasenko55a99402006-09-30 20:41:44 +0000757#if ENABLE_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000758/****************************************************************************
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000759 * setenv helpers
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000760 ****************************************************************************/
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000761static void setenv1(const char *name, const char *value)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000762{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000763 if (!value)
764 value = "";
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000765 setenv(name, value, 1);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000766}
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000767static void setenv_long(const char *name, long value)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000768{
Denis Vlasenkoe867b7c2006-11-16 16:12:09 +0000769 char buf[sizeof(value)*3 + 1];
770 sprintf(buf, "%ld", value);
771 setenv(name, buf, 1);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000772}
Eric Andersena3bb3e62003-06-26 09:05:32 +0000773#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000774
Denis Vlasenko55a99402006-09-30 20:41:44 +0000775#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000776/****************************************************************************
777 *
778 > $Function: decodeBase64()
779 *
780 > $Description: Decode a base 64 data stream as per rfc1521.
781 * Note that the rfc states that none base64 chars are to be ignored.
782 * Since the decode always results in a shorter size than the input, it is
783 * OK to pass the input arg as an output arg.
784 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000785 * $Parameter:
786 * (char *) Data . . . . A pointer to a base64 encoded string.
787 * Where to place the decoded data.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000788 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000789 * $Return: void
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000790 *
791 * $Errors: None
792 *
793 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000794static void decodeBase64(char *Data)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000795{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000796 const unsigned char *in = (const unsigned char *)Data;
797 // The decoded size will be at most 3/4 the size of the encoded
Denis Vlasenko088b9592007-04-18 21:14:46 +0000798 unsigned ch = 0;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000799 int i = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000800
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000801 while (*in) {
802 int t = *in++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000803
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000804 if (t >= '0' && t <= '9')
805 t = t - '0' + 52;
806 else if (t >= 'A' && t <= 'Z')
807 t = t - 'A';
808 else if (t >= 'a' && t <= 'z')
809 t = t - 'a' + 26;
810 else if (t == '+')
811 t = 62;
812 else if (t == '/')
813 t = 63;
814 else if (t == '=')
815 t = 0;
816 else
817 continue;
Glenn L McGrath874e3382003-05-14 12:11:36 +0000818
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000819 ch = (ch << 6) | t;
820 i++;
821 if (i == 4) {
822 *Data++ = (char) (ch >> 16);
823 *Data++ = (char) (ch >> 8);
824 *Data++ = (char) ch;
825 i = 0;
826 }
827 }
Denis Vlasenko088b9592007-04-18 21:14:46 +0000828 *Data = '\0';
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000829}
830#endif
831
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000832
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000833/****************************************************************************
834 *
835 > $Function: openServer()
836 *
837 * $Description: create a listen server socket on the designated port.
838 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000839 * $Return: (int) . . . A connection socket. -1 for errors.
840 *
841 * $Errors: None
842 *
843 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000844static int openServer(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000845{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000846 int fd;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000847
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000848 /* create the socket right now */
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000849 fd = create_and_bind_stream_or_die(NULL, tcp_port);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000850 xlisten(fd, 9);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000851 return fd;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000852}
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000853
854/****************************************************************************
855 *
856 > $Function: sendHeaders()
857 *
858 * $Description: Create and send HTTP response headers.
859 * The arguments are combined and sent as one write operation. Note that
860 * IE will puke big-time if the headers are not sent in one packet and the
Glenn L McGrath06e95652003-02-09 06:51:14 +0000861 * second packet is delayed for any reason.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000862 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000863 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000864 * (HttpResponseNum) responseNum . . . The result code to send.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000865 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000866 * $Return: (int) . . . . writing errors
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000867 *
868 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000869static int sendHeaders(HttpResponseNum responseNum)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000870{
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000871 char *buf = iobuf;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000872 const char *responseString = "";
873 const char *infoString = 0;
874 const char *mime_type;
Denis Vlasenkob64eed62007-01-14 17:06:11 +0000875 unsigned i;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000876 time_t timer = time(0);
877 char timeStr[80];
878 int len;
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +0000879 enum {
880 numNames = sizeof(httpResponseNames) / sizeof(httpResponseNames[0])
881 };
Glenn L McGrath06e95652003-02-09 06:51:14 +0000882
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +0000883 for (i = 0; i < numNames; i++) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000884 if (httpResponseNames[i].type == responseNum) {
885 responseString = httpResponseNames[i].name;
886 infoString = httpResponseNames[i].info;
887 break;
888 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000889 }
890 /* error message is HTML */
891 mime_type = responseNum == HTTP_OK ?
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000892 found_mime_type : "text/html";
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000893
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000894 /* emit the current date */
895 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&timer));
896 len = sprintf(buf,
Denis Vlasenko69981422007-01-07 21:25:12 +0000897 "HTTP/1.0 %d %s\r\nContent-type: %s\r\n"
898 "Date: %s\r\nConnection: close\r\n",
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000899 responseNum, responseString, mime_type, timeStr);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000900
Denis Vlasenko55a99402006-09-30 20:41:44 +0000901#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000902 if (responseNum == HTTP_UNAUTHORIZED) {
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000903 len += sprintf(buf+len,
904 "WWW-Authenticate: Basic realm=\"%s\"\r\n",
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000905 g_realm);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000906 }
Glenn L McGrath3d2405c2003-02-10 22:28:21 +0000907#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000908 if (responseNum == HTTP_MOVED_TEMPORARILY) {
909 len += sprintf(buf+len, "Location: %s/%s%s\r\n",
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000910 found_moved_temporarily,
911 (g_query ? "?" : ""),
912 (g_query ? g_query : ""));
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000913 }
Eric Andersen07f2fea2004-10-08 08:03:29 +0000914
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000915 if (ContentLength != -1) { /* file */
916 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&last_mod));
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000917 len += sprintf(buf+len, "Last-Modified: %s\r\n%s %"OFF_FMT"d\r\n",
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000918 timeStr, "Content-length:", ContentLength);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000919 }
920 strcat(buf, "\r\n");
921 len += 2;
922 if (infoString) {
923 len += sprintf(buf+len,
924 "<HEAD><TITLE>%d %s</TITLE></HEAD>\n"
925 "<BODY><H1>%d %s</H1>\n%s\n</BODY>\n",
926 responseNum, responseString,
927 responseNum, responseString, infoString);
928 }
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +0000929 if (DEBUG)
930 fprintf(stderr, "headers: '%s'\n", buf);
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000931 i = accepted_socket;
Denis Vlasenkob64eed62007-01-14 17:06:11 +0000932 if (i == 0) i++; /* write to fd# 1 in inetd mode */
933 return full_write(i, buf, len);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000934}
935
936/****************************************************************************
937 *
938 > $Function: getLine()
939 *
940 * $Description: Read from the socket until an end of line char found.
941 *
942 * Characters are read one at a time until an eol sequence is found.
943 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000944 * $Return: (int) . . . . number of characters read. -1 if error.
945 *
946 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000947static int getLine(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000948{
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +0000949 int count = 0;
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000950 char *buf = iobuf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000951
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000952 while (read(accepted_socket, buf + count, 1) == 1) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000953 if (buf[count] == '\r') continue;
954 if (buf[count] == '\n') {
955 buf[count] = 0;
956 return count;
957 }
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +0000958 if (count < (MAX_MEMORY_BUFF-1)) /* check overflow */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000959 count++;
960 }
961 if (count) return count;
962 else return -1;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000963}
964
Denis Vlasenko55a99402006-09-30 20:41:44 +0000965#if ENABLE_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000966/****************************************************************************
967 *
968 > $Function: sendCgi()
969 *
970 * $Description: Execute a CGI script and send it's stdout back
971 *
972 * Environment variables are set up and the script is invoked with pipes
973 * for stdin/stdout. If a post is being done the script is fed the POST
974 * data in addition to setting the QUERY_STRING variable (for GETs or POSTs).
975 *
976 * $Parameters:
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000977 * (const char *) url . . . . . . The requested URL (with leading /).
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000978 * (int bodyLen) . . . . . . . . Length of the post body.
979 * (const char *cookie) . . . . . For set HTTP_COOKIE.
980 * (const char *content_type) . . For set CONTENT_TYPE.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000981 *
982 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
983 *
984 * $Errors: None
985 *
986 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000987static int sendCgi(const char *url,
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +0000988 const char *request, int bodyLen, const char *cookie,
989 const char *content_type)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000990{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000991 int fromCgi[2]; /* pipe for reading data from CGI */
992 int toCgi[2]; /* pipe for sending data to CGI */
Denis Vlasenko77e44d62007-06-09 23:49:05 +0000993 char *fullpath;
994 char *argp[] = { NULL, NULL };
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +0000995 int pid = 0;
996 int inFd;
997 int outFd;
Denis Vlasenkob5368bf2007-02-13 23:42:54 +0000998 int buf_count;
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +0000999 int status;
Denis Vlasenko69981422007-01-07 21:25:12 +00001000 size_t post_read_size, post_read_idx;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001001
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001002 if (pipe(fromCgi) != 0)
1003 return 0;
1004 if (pipe(toCgi) != 0)
1005 return 0;
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001006
Denis Vlasenko80281fe2007-03-07 22:16:38 +00001007/*
1008 * Note: We can use vfork() here in the no-mmu case, although
1009 * the child modifies the parent's variables, due to:
1010 * 1) The parent does not use the child-modified variables.
1011 * 2) The allocated memory (in the child) is freed when the process
1012 * exits. This happens instantly after the child finishes,
1013 * since httpd is run from inetd (and it can't run standalone
1014 * in uClinux).
1015 */
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001016
1017// FIXME: setenv leaks memory! (old values of env vars are leaked)
1018// Thus we have a bug on NOMMU.
1019// Need to use this instead:
1020// [malloc +] putenv + (in child: exec) + (in parent: unsetenv [+ free])
1021
Denis Vlasenko473dae02007-04-11 07:04:23 +00001022#if !BB_MMU
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001023 fullpath = NULL;
Denis Vlasenko80281fe2007-03-07 22:16:38 +00001024 pid = vfork();
1025#else
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001026 pid = fork();
Denis Vlasenko80281fe2007-03-07 22:16:38 +00001027#endif
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001028 if (pid < 0)
1029 return 0;
Denis Vlasenkof7996f32007-01-11 17:20:00 +00001030
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001031 if (!pid) {
1032 /* child process */
1033 char *script;
Denis Vlasenkoa3055842007-02-11 19:51:06 +00001034 char *purl;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001035
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001036 if (accepted_socket > 1)
1037 close(accepted_socket);
1038 if (server_socket > 1)
1039 close(server_socket);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001040
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001041 xmove_fd(toCgi[0], 0); /* replace stdin with the pipe */
1042 xmove_fd(fromCgi[1], 1); /* replace stdout with the pipe */
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001043 close(fromCgi[0]);
1044 close(fromCgi[1]);
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001045 /* Huh? User seeing stderr can be a security problem...
1046 * and if CGI really wants that, it can always dup2(1,2)...
1047 * dup2(fromCgi[1], 2); */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001048
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001049 /*
1050 * Find PATH_INFO.
1051 */
Denis Vlasenkoa3055842007-02-11 19:51:06 +00001052 xfunc_error_retval = 242;
1053 purl = xstrdup(url);
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001054 script = purl;
1055 while ((script = strchr(script + 1, '/')) != NULL) {
1056 /* have script.cgi/PATH_INFO or dirs/script.cgi[/PATH_INFO] */
1057 struct stat sb;
Denis Vlasenko9f609292006-11-05 19:47:33 +00001058
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001059 *script = '\0';
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001060 if (is_directory(purl + 1, 1, &sb) == 0) {
1061 /* not directory, found script.cgi/PATH_INFO */
1062 *script = '/';
1063 break;
1064 }
1065 *script = '/'; /* is directory, find next '/' */
1066 }
1067 setenv1("PATH_INFO", script); /* set /PATH_INFO or "" */
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001068 setenv1("REQUEST_METHOD", request);
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001069 if (g_query) {
1070 char *uri = alloca(strlen(purl) + 2 + strlen(g_query));
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001071 if (uri)
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001072 sprintf(uri, "%s?%s", purl, g_query);
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001073 setenv1("REQUEST_URI", uri);
1074 } else {
1075 setenv1("REQUEST_URI", purl);
1076 }
1077 if (script != NULL)
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001078 *script = '\0'; /* cut off /PATH_INFO */
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001079
1080 /* SCRIPT_FILENAME required by PHP in CGI mode */
1081 fullpath = concat_path_file(home_httpd, purl);
1082 setenv1("SCRIPT_FILENAME", fullpath);
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001083 /* set SCRIPT_NAME as full path: /cgi-bin/dirs/script.cgi */
1084 setenv1("SCRIPT_NAME", purl);
Denis Vlasenko428f7ae2006-11-21 21:35:14 +00001085 /* http://hoohoo.ncsa.uiuc.edu/cgi/env.html:
1086 * QUERY_STRING: The information which follows the ? in the URL
1087 * which referenced this script. This is the query information.
1088 * It should not be decoded in any fashion. This variable
1089 * should always be set when there is query information,
1090 * regardless of command line decoding. */
Denis Vlasenkoa773af32007-01-03 23:02:18 +00001091 /* (Older versions of bbox seem to do some decoding) */
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001092 setenv1("QUERY_STRING", g_query);
1093 putenv((char*)"SERVER_SOFTWARE=busybox httpd/"BB_VER);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +00001094 putenv((char*)"SERVER_PROTOCOL=HTTP/1.0");
1095 putenv((char*)"GATEWAY_INTERFACE=CGI/1.1");
Denis Vlasenko6536a9b2007-01-12 10:35:23 +00001096 /* Having _separate_ variables for IP and port defeats
1097 * the purpose of having socket abstraction. Which "port"
1098 * are you using on Unix domain socket?
1099 * IOW - REMOTE_PEER="1.2.3.4:56" makes much more sense.
1100 * Oh well... */
1101 {
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001102 char *p = rmt_ip_str ? : (char*)"";
Denis Vlasenko6536a9b2007-01-12 10:35:23 +00001103 char *cp = strrchr(p, ':');
1104 if (ENABLE_FEATURE_IPV6 && cp && strchr(cp, ']'))
1105 cp = NULL;
1106 if (cp) *cp = '\0'; /* delete :PORT */
1107 setenv1("REMOTE_ADDR", p);
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001108 if (cp) *cp = ':';
Denis Vlasenko6536a9b2007-01-12 10:35:23 +00001109 }
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001110#if ENABLE_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001111 setenv_long("REMOTE_PORT", tcp_port);
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001112#endif
1113 if (bodyLen)
1114 setenv_long("CONTENT_LENGTH", bodyLen);
1115 if (cookie)
1116 setenv1("HTTP_COOKIE", cookie);
1117 if (content_type)
1118 setenv1("CONTENT_TYPE", content_type);
1119#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001120 if (remoteuser) {
1121 setenv1("REMOTE_USER", remoteuser);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +00001122 putenv((char*)"AUTH_TYPE=Basic");
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001123 }
1124#endif
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001125 if (referer)
1126 setenv1("HTTP_REFERER", referer);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001127
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001128 /* set execve argp[0] without path */
1129 argp[0] = strrchr(purl, '/') + 1;
1130 /* but script argp[0] must have absolute path and chdiring to this */
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001131 script = strrchr(fullpath, '/');
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001132 if (!script)
1133 goto error_execing_cgi;
1134 *script = '\0';
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001135 if (chdir(fullpath) == 0) {
Denis Vlasenkoa773af32007-01-03 23:02:18 +00001136 // Now run the program. If it fails,
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001137 // use _exit() so no destructors
1138 // get called and make a mess.
1139#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
1140 char *interpr = NULL;
1141 char *suffix = strrchr(purl, '.');
1142
1143 if (suffix) {
1144 Htaccess *cur;
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001145 for (cur = script_i; cur; cur = cur->next) {
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001146 if (strcmp(cur->before_colon + 1, suffix) == 0) {
1147 interpr = cur->after_colon;
1148 break;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001149 }
1150 }
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001151 }
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001152#endif
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001153 *script = '/';
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001154#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001155 if (interpr)
1156 execv(interpr, argp);
1157 else
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001158#endif
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001159 execv(fullpath, argp);
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001160 }
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001161 error_execing_cgi:
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001162 /* send to stdout (even if we are not from inetd) */
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001163 accepted_socket = 1;
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001164 sendHeaders(HTTP_NOT_FOUND);
1165 _exit(242);
1166 } /* end child */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001167
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001168 /* parent process */
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001169#if !BB_MMU
1170 free(fullpath);
1171#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001172
Denis Vlasenkob5368bf2007-02-13 23:42:54 +00001173 buf_count = 0;
Denis Vlasenko69981422007-01-07 21:25:12 +00001174 post_read_size = 0;
1175 post_read_idx = 0; /* for gcc */
Denis Vlasenko5d148e22006-11-21 00:12:09 +00001176 inFd = fromCgi[0];
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001177 outFd = toCgi[1];
1178 close(fromCgi[1]);
1179 close(toCgi[0]);
1180 signal(SIGPIPE, SIG_IGN);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001181
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001182 while (1) {
1183 fd_set readSet;
1184 fd_set writeSet;
1185 char wbuf[128];
1186 int nfound;
1187 int count;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001188
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001189 FD_ZERO(&readSet);
1190 FD_ZERO(&writeSet);
1191 FD_SET(inFd, &readSet);
Denis Vlasenko69981422007-01-07 21:25:12 +00001192 if (bodyLen > 0 || post_read_size > 0) {
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001193 FD_SET(outFd, &writeSet);
1194 nfound = outFd > inFd ? outFd : inFd;
Denis Vlasenko69981422007-01-07 21:25:12 +00001195 if (post_read_size == 0) {
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001196 FD_SET(accepted_socket, &readSet);
1197 if (nfound < accepted_socket)
1198 nfound = accepted_socket;
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001199 }
1200 /* Now wait on the set of sockets! */
Denis Vlasenko69981422007-01-07 21:25:12 +00001201 nfound = select(nfound + 1, &readSet, &writeSet, NULL, NULL);
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001202 } else {
1203 if (!bodyLen) {
Denis Vlasenko69981422007-01-07 21:25:12 +00001204 close(outFd); /* no more POST data to CGI */
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001205 bodyLen = -1;
1206 }
Denis Vlasenko69981422007-01-07 21:25:12 +00001207 nfound = select(inFd + 1, &readSet, NULL, NULL, NULL);
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001208 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001209
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001210 if (nfound <= 0) {
Denis Vlasenkob5368bf2007-02-13 23:42:54 +00001211 if (waitpid(pid, &status, WNOHANG) <= 0) {
Denis Vlasenko69981422007-01-07 21:25:12 +00001212 /* Weird. CGI didn't exit and no fd's
Denis Vlasenkob5368bf2007-02-13 23:42:54 +00001213 * are ready, yet select returned?! */
Denis Vlasenko69981422007-01-07 21:25:12 +00001214 continue;
Denis Vlasenkob5368bf2007-02-13 23:42:54 +00001215 }
Denis Vlasenko69981422007-01-07 21:25:12 +00001216 close(inFd);
1217 if (DEBUG && WIFEXITED(status))
1218 bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status));
1219 if (DEBUG && WIFSIGNALED(status))
1220 bb_error_msg("piped has exited with signal=%d", WTERMSIG(status));
1221 break;
1222 }
1223
1224 if (post_read_size > 0 && FD_ISSET(outFd, &writeSet)) {
1225 /* Have data from peer and can write to CGI */
1226 // huh? why full_write? what if we will block?
1227 // (imagine that CGI does not read its stdin...)
1228 count = full_write(outFd, wbuf + post_read_idx, post_read_size);
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001229 if (count > 0) {
Denis Vlasenko69981422007-01-07 21:25:12 +00001230 post_read_idx += count;
1231 post_read_size -= count;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001232 } else {
Denis Vlasenko69981422007-01-07 21:25:12 +00001233 post_read_size = bodyLen = 0; /* broken pipe to CGI */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001234 }
Denis Vlasenko69981422007-01-07 21:25:12 +00001235 } else if (bodyLen > 0 && post_read_size == 0
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001236 && FD_ISSET(accepted_socket, &readSet)
Denis Vlasenko69981422007-01-07 21:25:12 +00001237 ) {
1238 /* We expect data, prev data portion is eaten by CGI
1239 * and there *is* data to read from the peer
Denis Vlasenkob5368bf2007-02-13 23:42:54 +00001240 * (POSTDATA?) */
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001241 count = bodyLen > (int)sizeof(wbuf) ? (int)sizeof(wbuf) : bodyLen;
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001242 count = safe_read(accepted_socket, wbuf, count);
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001243 if (count > 0) {
Denis Vlasenko69981422007-01-07 21:25:12 +00001244 post_read_size = count;
1245 post_read_idx = 0;
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001246 bodyLen -= count;
1247 } else {
1248 bodyLen = 0; /* closed */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001249 }
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001250 }
Denis Vlasenko69981422007-01-07 21:25:12 +00001251
Denis Vlasenko69981422007-01-07 21:25:12 +00001252#define PIPESIZE PIPE_BUF
Eric Andersen97a1de12004-08-26 22:22:50 +00001253#if PIPESIZE >= MAX_MEMORY_BUFF
1254# error "PIPESIZE >= MAX_MEMORY_BUFF"
1255#endif
Denis Vlasenkob5368bf2007-02-13 23:42:54 +00001256 if (FD_ISSET(inFd, &readSet)) {
1257 /* There is something to read from CGI */
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001258 int s = accepted_socket;
1259 char *rbuf = iobuf;
Denis Vlasenkoa3ee69f2006-11-21 00:07:31 +00001260
Denis Vlasenkob5368bf2007-02-13 23:42:54 +00001261 /* Are we still buffering CGI output? */
1262 if (buf_count >= 0) {
Denis Vlasenkoec77ba12007-03-05 16:56:25 +00001263 static const char HTTP_200[] = "HTTP/1.0 200 OK\r\n";
Denis Vlasenkob5368bf2007-02-13 23:42:54 +00001264 /* Must use safe_read, not full_read, because
1265 * CGI may output a few first bytes and then wait
1266 * for POSTDATA without closing stdout.
1267 * With full_read we may wait here forever. */
1268 count = safe_read(inFd, rbuf + buf_count, PIPESIZE - 4);
1269 if (count <= 0) {
1270 /* eof (or error) and there was no "HTTP",
1271 * so add one and write out the received data */
1272 if (buf_count) {
1273 full_write(s, HTTP_200, sizeof(HTTP_200)-1);
1274 full_write(s, rbuf, buf_count);
1275 }
1276 break; /* closed */
Denis Vlasenko69981422007-01-07 21:25:12 +00001277 }
Denis Vlasenkob5368bf2007-02-13 23:42:54 +00001278 buf_count += count;
1279 count = 0;
1280 if (buf_count >= 4) {
1281 /* check to see if CGI added "HTTP" */
1282 if (memcmp(rbuf, HTTP_200, 4) != 0) {
1283 /* there is no "HTTP", do it ourself */
1284 if (full_write(s, HTTP_200, sizeof(HTTP_200)-1) != sizeof(HTTP_200)-1)
1285 break;
1286 }
1287 /* example of valid CGI without "Content-type:"
1288 * echo -en "HTTP/1.0 302 Found\r\n"
1289 * echo -en "Location: http://www.busybox.net\r\n"
1290 * echo -en "\r\n"
1291 if (!strstr(rbuf, "ontent-")) {
1292 full_write(s, "Content-type: text/plain\r\n\r\n", 28);
1293 }
1294 */
1295 count = buf_count;
1296 buf_count = -1; /* buffering off */
Denis Vlasenkoa3055842007-02-11 19:51:06 +00001297 }
Denis Vlasenkob5368bf2007-02-13 23:42:54 +00001298 } else {
1299 count = safe_read(inFd, rbuf, PIPESIZE);
1300 if (count <= 0)
1301 break; /* eof (or error) */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001302 }
Denis Vlasenko69981422007-01-07 21:25:12 +00001303 if (full_write(s, rbuf, count) != count)
1304 break;
1305 if (DEBUG)
1306 fprintf(stderr, "cgi read %d bytes: '%.*s'\n", count, count, rbuf);
1307 } /* if (FD_ISSET(inFd)) */
1308 } /* while (1) */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001309 return 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001310}
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001311#endif /* FEATURE_HTTPD_CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001312
1313/****************************************************************************
1314 *
1315 > $Function: sendFile()
1316 *
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001317 * $Description: Send a file response to a HTTP request
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001318 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00001319 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001320 * (const char *) url . . The URL requested.
1321 *
1322 * $Return: (int) . . . . . . Always 0.
1323 *
1324 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001325static int sendFile(const char *url)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001326{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001327 char * suffix;
1328 int f;
1329 const char * const * table;
1330 const char * try_suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001331
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001332 suffix = strrchr(url, '.');
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001333
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001334 for (table = suffixTable; *table; table += 2)
1335 if (suffix != NULL && (try_suffix = strstr(*table, suffix)) != 0) {
1336 try_suffix += strlen(suffix);
1337 if (*try_suffix == 0 || *try_suffix == '.')
1338 break;
1339 }
1340 /* also, if not found, set default as "application/octet-stream"; */
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001341 found_mime_type = table[1];
Denis Vlasenko55a99402006-09-30 20:41:44 +00001342#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001343 if (suffix) {
1344 Htaccess * cur;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001345
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001346 for (cur = mime_a; cur; cur = cur->next) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001347 if (strcmp(cur->before_colon, suffix) == 0) {
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001348 found_mime_type = cur->after_colon;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001349 break;
1350 }
1351 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001352 }
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001353#endif /* FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001354
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001355 if (DEBUG)
1356 fprintf(stderr, "sending file '%s' content-type: %s\n",
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001357 url, found_mime_type);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001358
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001359 f = open(url, O_RDONLY);
1360 if (f >= 0) {
1361 int count;
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001362 char *buf = iobuf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001363
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001364 sendHeaders(HTTP_OK);
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001365 /* TODO: sendfile() */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001366 while ((count = full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001367 int fd = accepted_socket;
Denis Vlasenkob64eed62007-01-14 17:06:11 +00001368 if (fd == 0) fd++; /* write to fd# 1 in inetd mode */
1369 if (full_write(fd, buf, count) != count)
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001370 break;
1371 }
1372 close(f);
1373 } else {
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001374 if (DEBUG)
1375 bb_perror_msg("cannot open '%s'", url);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001376 sendHeaders(HTTP_NOT_FOUND);
1377 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001378
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001379 return 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001380}
1381
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001382static int checkPermIP(void)
1383{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001384 Htaccess_IP * cur;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001385
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001386 /* This could stand some work */
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001387 for (cur = ip_a_d; cur; cur = cur->next) {
Denis Vlasenkob64eed62007-01-14 17:06:11 +00001388#if ENABLE_FEATURE_HTTPD_CGI && DEBUG
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001389 fprintf(stderr, "checkPermIP: '%s' ? ", rmt_ip_str);
Denis Vlasenkob64eed62007-01-14 17:06:11 +00001390#endif
1391#if DEBUG
Denis Vlasenko6536a9b2007-01-12 10:35:23 +00001392 fprintf(stderr, "'%u.%u.%u.%u/%u.%u.%u.%u'\n",
1393 (unsigned char)(cur->ip >> 24),
1394 (unsigned char)(cur->ip >> 16),
1395 (unsigned char)(cur->ip >> 8),
1396 (unsigned char)(cur->ip),
1397 (unsigned char)(cur->mask >> 24),
1398 (unsigned char)(cur->mask >> 16),
1399 (unsigned char)(cur->mask >> 8),
1400 (unsigned char)(cur->mask)
1401 );
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001402#endif
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001403 if ((rmt_ip & cur->mask) == cur->ip)
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001404 return cur->allow_deny == 'A'; /* Allow/Deny */
1405 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001406
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001407 /* if unconfigured, return 1 - access from all */
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001408 return !flg_deny_all;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001409}
1410
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001411/****************************************************************************
1412 *
1413 > $Function: checkPerm()
1414 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001415 * $Description: Check the permission file for access password protected.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001416 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001417 * If config file isn't present, everything is allowed.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001418 * Entries are of the form you can see example from header source
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001419 *
1420 * $Parameters:
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001421 * (const char *) path . . . . The file path.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001422 * (const char *) request . . . User information to validate.
1423 *
1424 * $Return: (int) . . . . . . . . . 1 if request OK, 0 otherwise.
1425 *
1426 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001427
Denis Vlasenko55a99402006-09-30 20:41:44 +00001428#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001429static int checkPerm(const char *path, const char *request)
1430{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001431 Htaccess * cur;
1432 const char *p;
1433 const char *p0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001434
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001435 const char *prev = NULL;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001436
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001437 /* This could stand some work */
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001438 for (cur = g_auth; cur; cur = cur->next) {
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001439 size_t l;
1440
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001441 p0 = cur->before_colon;
1442 if (prev != NULL && strcmp(prev, p0) != 0)
1443 continue; /* find next identical */
1444 p = cur->after_colon;
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001445 if (DEBUG)
1446 fprintf(stderr, "checkPerm: '%s' ? '%s'\n", p0, request);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001447
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001448 l = strlen(p0);
1449 if (strncmp(p0, path, l) == 0
1450 && (l == 1 || path[l] == '/' || path[l] == '\0')
1451 ) {
1452 char *u;
1453 /* path match found. Check request */
1454 /* for check next /path:user:password */
1455 prev = p0;
1456 u = strchr(request, ':');
1457 if (u == NULL) {
1458 /* bad request, ':' required */
1459 break;
1460 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001461
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001462 if (ENABLE_FEATURE_HTTPD_AUTH_MD5) {
1463 char *cipher;
1464 char *pp;
Eric Andersen35e643b2003-07-28 07:40:39 +00001465
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001466 if (strncmp(p, request, u-request) != 0) {
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001467 /* user uncompared */
1468 continue;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001469 }
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001470 pp = strchr(p, ':');
1471 if (pp && pp[1] == '$' && pp[2] == '1' &&
1472 pp[3] == '$' && pp[4]) {
1473 pp++;
1474 cipher = pw_encrypt(u+1, pp);
1475 if (strcmp(cipher, pp) == 0)
1476 goto set_remoteuser_var; /* Ok */
1477 /* unauthorized */
1478 continue;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001479 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001480 }
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001481
1482 if (strcmp(p, request) == 0) {
1483set_remoteuser_var:
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001484 remoteuser = strdup(request);
1485 if (remoteuser)
1486 remoteuser[(u - request)] = 0;
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001487 return 1; /* Ok */
1488 }
1489 /* unauthorized */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001490 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001491 } /* for */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001492
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001493 return prev == NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001494}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001495
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001496#endif /* FEATURE_HTTPD_BASIC_AUTH */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001497
Eric Andersen07f2fea2004-10-08 08:03:29 +00001498/****************************************************************************
1499 *
Mike Frysingerbb12d6f2006-01-03 23:59:01 +00001500 > $Function: handle_sigalrm()
Eric Andersen07f2fea2004-10-08 08:03:29 +00001501 *
Mike Frysingerbb12d6f2006-01-03 23:59:01 +00001502 * $Description: Handle timeouts
Eric Andersen07f2fea2004-10-08 08:03:29 +00001503 *
1504 ****************************************************************************/
1505
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001506static void handle_sigalrm(int sig)
Eric Andersen07f2fea2004-10-08 08:03:29 +00001507{
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001508 sendHeaders(HTTP_REQUEST_TIMEOUT);
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001509 alarm_signaled = 1;
Eric Andersen07f2fea2004-10-08 08:03:29 +00001510}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001511
1512/****************************************************************************
1513 *
1514 > $Function: handleIncoming()
1515 *
1516 * $Description: Handle an incoming http request.
1517 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001518 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001519static void handleIncoming(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001520{
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001521 char *buf = iobuf;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001522 char *url;
1523 char *purl;
1524 int blank = -1;
1525 char *test;
1526 struct stat sb;
1527 int ip_allowed;
Denis Vlasenko55a99402006-09-30 20:41:44 +00001528#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001529 const char *prequest = request_GET;
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001530 unsigned long length = 0;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001531 char *cookie = 0;
1532 char *content_type = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001533#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001534 fd_set s_fd;
1535 struct timeval tv;
1536 int retval;
1537 struct sigaction sa;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001538
Denis Vlasenko55a99402006-09-30 20:41:44 +00001539#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001540 int credentials = -1; /* if not required this is Ok */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001541#endif
1542
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001543 sa.sa_handler = handle_sigalrm;
1544 sigemptyset(&sa.sa_mask);
1545 sa.sa_flags = 0; /* no SA_RESTART */
1546 sigaction(SIGALRM, &sa, NULL);
Eric Andersen07f2fea2004-10-08 08:03:29 +00001547
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001548 do {
1549 int count;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001550
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001551 (void) alarm(TIMEOUT);
1552 if (getLine() <= 0)
1553 break; /* closed */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001554
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001555 purl = strpbrk(buf, " \t");
1556 if (purl == NULL) {
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001557 BAD_REQUEST:
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001558 sendHeaders(HTTP_BAD_REQUEST);
1559 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001560 }
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001561 *purl = '\0';
Denis Vlasenko55a99402006-09-30 20:41:44 +00001562#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001563 if (strcasecmp(buf, prequest) != 0) {
1564 prequest = "POST";
1565 if (strcasecmp(buf, prequest) != 0) {
1566 sendHeaders(HTTP_NOT_IMPLEMENTED);
1567 break;
1568 }
1569 }
1570#else
1571 if (strcasecmp(buf, request_GET) != 0) {
1572 sendHeaders(HTTP_NOT_IMPLEMENTED);
1573 break;
1574 }
1575#endif
1576 *purl = ' ';
1577 count = sscanf(purl, " %[^ ] HTTP/%d.%*d", buf, &blank);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001578
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001579 if (count < 1 || buf[0] != '/') {
1580 /* Garbled request/URL */
1581 goto BAD_REQUEST;
1582 }
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001583 url = alloca(strlen(buf) + sizeof("/index.html"));
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001584 if (url == NULL) {
1585 sendHeaders(HTTP_INTERNAL_SERVER_ERROR);
1586 break;
1587 }
1588 strcpy(url, buf);
1589 /* extract url args if present */
1590 test = strchr(url, '?');
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001591 g_query = NULL;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001592 if (test) {
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001593 *test++ = '\0';
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001594 g_query = test;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001595 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001596
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001597 test = decodeString(url, 0);
1598 if (test == NULL)
1599 goto BAD_REQUEST;
Denis Vlasenkoa35c9e92006-11-29 15:58:50 +00001600 if (test == url+1) {
1601 /* '/' or NUL is encoded */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001602 sendHeaders(HTTP_NOT_FOUND);
1603 break;
1604 }
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001605
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001606 /* algorithm stolen from libbb bb_simplify_path(),
Denis Vlasenko16abcd92007-04-13 23:59:52 +00001607 * but don't strdup and reducing trailing slash and protect out root */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001608 purl = test = url;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001609 do {
1610 if (*purl == '/') {
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001611 /* skip duplicate (or initial) slash */
1612 if (*test == '/') {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001613 continue;
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001614 }
1615 if (*test == '.') {
1616 /* skip extra '.' */
Denis Vlasenko16abcd92007-04-13 23:59:52 +00001617 if (test[1] == '/' || !test[1]) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001618 continue;
Denis Vlasenko16abcd92007-04-13 23:59:52 +00001619 }
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001620 /* '..': be careful */
Denis Vlasenko16abcd92007-04-13 23:59:52 +00001621 if (test[1] == '.' && (test[2] == '/' || !test[2])) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001622 ++test;
1623 if (purl == url) {
1624 /* protect out root */
1625 goto BAD_REQUEST;
1626 }
Denis Vlasenko92758142006-10-03 19:56:34 +00001627 while (*--purl != '/') /* omit previous dir */;
Denis Vlasenko16abcd92007-04-13 23:59:52 +00001628 continue;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001629 }
1630 }
1631 }
1632 *++purl = *test;
1633 } while (*++test);
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001634 *++purl = '\0'; /* so keep last character */
1635 test = purl; /* end ptr */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001636
1637 /* If URL is directory, adding '/' */
1638 if (test[-1] != '/') {
1639 if (is_directory(url + 1, 1, &sb)) {
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001640 found_moved_temporarily = url;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001641 }
1642 }
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001643 if (DEBUG)
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001644 fprintf(stderr, "url='%s', args=%s\n", url, g_query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001645
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001646 test = url;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001647 ip_allowed = checkPermIP();
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001648 while (ip_allowed && (test = strchr(test + 1, '/')) != NULL) {
1649 /* have path1/path2 */
1650 *test = '\0';
1651 if (is_directory(url + 1, 1, &sb)) {
1652 /* may be having subdir config */
1653 parse_conf(url + 1, SUBDIR_PARSE);
1654 ip_allowed = checkPermIP();
1655 }
1656 *test = '/';
1657 }
1658 if (blank >= 0) {
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001659 /* read until blank line for HTTP version specified, else parse immediate */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001660 while (1) {
1661 alarm(TIMEOUT);
1662 count = getLine();
1663 if (count <= 0)
1664 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001665
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001666 if (DEBUG)
1667 fprintf(stderr, "header: '%s'\n", buf);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001668
Denis Vlasenko55a99402006-09-30 20:41:44 +00001669#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001670 /* try and do our best to parse more lines */
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001671 if ((STRNCASECMP(buf, "Content-length:") == 0)) {
1672 /* extra read only for POST */
1673 if (prequest != request_GET) {
1674 test = buf + sizeof("Content-length:")-1;
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001675 if (!test[0])
1676 goto bail_out;
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001677 errno = 0;
1678 /* not using strtoul: it ignores leading munis! */
1679 length = strtol(test, &test, 10);
1680 /* length is "ulong", but we need to pass it to int later */
1681 /* so we check for negative or too large values in one go: */
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001682 /* (long -> ulong conv caused negatives to be seen as > INT_MAX) */
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001683 if (test[0] || errno || length > INT_MAX)
1684 goto bail_out;
1685 }
1686 } else if ((STRNCASECMP(buf, "Cookie:") == 0)) {
1687 cookie = strdup(skip_whitespace(buf + sizeof("Cookie:")-1));
1688 } else if ((STRNCASECMP(buf, "Content-Type:") == 0)) {
1689 content_type = strdup(skip_whitespace(buf + sizeof("Content-Type:")-1));
1690 } else if ((STRNCASECMP(buf, "Referer:") == 0)) {
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001691 referer = strdup(skip_whitespace(buf + sizeof("Referer:")-1));
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001692 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001693#endif
1694
Denis Vlasenko55a99402006-09-30 20:41:44 +00001695#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001696 if (STRNCASECMP(buf, "Authorization:") == 0) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001697 /* We only allow Basic credentials.
1698 * It shows up as "Authorization: Basic <userid:password>" where
1699 * the userid:password is base64 encoded.
1700 */
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001701 test = skip_whitespace(buf + sizeof("Authorization:")-1);
1702 if (STRNCASECMP(test, "Basic") != 0)
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001703 continue;
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001704 test += sizeof("Basic")-1;
1705 /* decodeBase64() skips whitespace itself */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001706 decodeBase64(test);
1707 credentials = checkPerm(url, test);
1708 }
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001709#endif /* FEATURE_HTTPD_BASIC_AUTH */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001710
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001711 } /* while extra header reading */
1712 }
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001713 alarm(0);
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001714 if (alarm_signaled)
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001715 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001716
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001717 if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) {
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001718 /* protect listing [/path]/httpd_conf or IP deny */
Denis Vlasenko55a99402006-09-30 20:41:44 +00001719#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001720 FORBIDDEN: /* protect listing /cgi-bin */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001721#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001722 sendHeaders(HTTP_FORBIDDEN);
1723 break;
1724 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001725
Denis Vlasenko55a99402006-09-30 20:41:44 +00001726#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001727 if (credentials <= 0 && checkPerm(url, ":") == 0) {
1728 sendHeaders(HTTP_UNAUTHORIZED);
1729 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001730 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001731#endif
1732
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001733 if (found_moved_temporarily) {
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001734 sendHeaders(HTTP_MOVED_TEMPORARILY);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001735 /* clear unforked memory flag */
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001736 found_moved_temporarily = NULL;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001737 break;
1738 }
1739
1740 test = url + 1; /* skip first '/' */
1741
Denis Vlasenko55a99402006-09-30 20:41:44 +00001742#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001743 if (strncmp(test, "cgi-bin", 7) == 0) {
1744 if (test[7] == '/' && test[8] == 0)
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001745 goto FORBIDDEN; /* protect listing cgi-bin/ */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001746 sendCgi(url, prequest, length, cookie, content_type);
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001747 break;
1748 }
Denis Vlasenko1ccd96f2007-03-05 19:24:33 +00001749#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
1750 {
1751 char *suffix = strrchr(test, '.');
1752 if (suffix) {
1753 Htaccess *cur;
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001754 for (cur = script_i; cur; cur = cur->next) {
Denis Vlasenko1ccd96f2007-03-05 19:24:33 +00001755 if (strcmp(cur->before_colon + 1, suffix) == 0) {
1756 sendCgi(url, prequest, length, cookie, content_type);
1757 goto bail_out;
1758 }
1759 }
1760 }
1761 }
1762#endif
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001763 if (prequest != request_GET) {
1764 sendHeaders(HTTP_NOT_IMPLEMENTED);
1765 break;
1766 }
Denis Vlasenko5d148e22006-11-21 00:12:09 +00001767#endif /* FEATURE_HTTPD_CGI */
1768 if (purl[-1] == '/')
1769 strcpy(purl, "index.html");
1770 if (stat(test, &sb) == 0) {
1771 /* It's a dir URL and there is index.html */
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001772 ContentLength = sb.st_size;
1773 last_mod = sb.st_mtime;
Denis Vlasenko5d148e22006-11-21 00:12:09 +00001774 }
1775#if ENABLE_FEATURE_HTTPD_CGI
1776 else if (purl[-1] == '/') {
1777 /* It's a dir URL and there is no index.html
1778 * Try cgi-bin/index.cgi */
1779 if (access("/cgi-bin/index.cgi"+1, X_OK) == 0) {
1780 purl[0] = '\0';
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001781 g_query = url;
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001782 sendCgi("/cgi-bin/index.cgi", prequest, length, cookie, content_type);
1783 break;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001784 }
1785 }
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001786#endif /* FEATURE_HTTPD_CGI */
Denis Vlasenko6c85ddc2006-11-21 00:08:39 +00001787 sendFile(test);
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001788 ContentLength = -1;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001789 } while (0);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001790
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001791#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001792 bail_out:
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001793#endif
Denis Vlasenko0bb993f2006-11-21 00:06:28 +00001794
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001795 if (DEBUG)
1796 fprintf(stderr, "closing socket\n\n");
1797#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001798 free(cookie);
1799 free(content_type);
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001800 free(referer);
1801 referer = NULL;
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001802# if ENABLE_FEATURE_HTTPD_BASIC_AUTH
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001803 free(remoteuser);
1804 remoteuser = NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001805# endif
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001806#endif
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001807 shutdown(accepted_socket, SHUT_WR);
Eric Andersend8746cd2004-02-24 07:28:38 +00001808
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001809 /* Properly wait for remote to closed */
Denis Vlasenko55a99402006-09-30 20:41:44 +00001810 FD_ZERO(&s_fd);
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001811 FD_SET(accepted_socket, &s_fd);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001812
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001813 do {
Denis Vlasenko55a99402006-09-30 20:41:44 +00001814 tv.tv_sec = 2;
1815 tv.tv_usec = 0;
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001816 retval = select(accepted_socket + 1, &s_fd, NULL, NULL, &tv);
1817 } while (retval > 0 && read(accepted_socket, buf, sizeof(iobuf) > 0));
Eric Andersend8746cd2004-02-24 07:28:38 +00001818
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001819 shutdown(accepted_socket, SHUT_RD);
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001820 /* In inetd case, we close fd 1 (stdout) here. We will exit soon anyway */
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001821 close(accepted_socket);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001822}
1823
1824/****************************************************************************
1825 *
1826 > $Function: miniHttpd()
1827 *
1828 * $Description: The main http server function.
1829 *
1830 * Given an open socket fildes, listen for new connections and farm out
1831 * the processing as a forked process.
1832 *
1833 * $Parameters:
1834 * (int) server. . . The server socket fildes.
1835 *
1836 * $Return: (int) . . . . Always 0.
1837 *
1838 ****************************************************************************/
1839static int miniHttpd(int server)
1840{
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001841 fd_set readfd, portfd;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001842
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001843 FD_ZERO(&portfd);
1844 FD_SET(server, &portfd);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001845
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001846 /* copy the ports we are watching to the readfd set */
1847 while (1) {
Denis Vlasenko6536a9b2007-01-12 10:35:23 +00001848 int s;
1849 union {
1850 struct sockaddr sa;
1851 struct sockaddr_in sin;
1852 USE_FEATURE_IPV6(struct sockaddr_in6 sin6;)
1853 } fromAddr;
1854 socklen_t fromAddrLen = sizeof(fromAddr);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001855
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001856 /* Now wait INDEFINITELY on the set of sockets! */
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001857 readfd = portfd;
1858 if (select(server + 1, &readfd, 0, 0, 0) <= 0)
1859 continue;
1860 if (!FD_ISSET(server, &readfd))
1861 continue;
Denis Vlasenko6536a9b2007-01-12 10:35:23 +00001862 s = accept(server, &fromAddr.sa, &fromAddrLen);
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001863 if (s < 0)
1864 continue;
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001865 accepted_socket = s;
1866 rmt_ip = 0;
1867 tcp_port = 0;
Denis Vlasenko55a99402006-09-30 20:41:44 +00001868#if ENABLE_FEATURE_HTTPD_CGI || DEBUG
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001869 free(rmt_ip_str);
1870 rmt_ip_str = xmalloc_sockaddr2dotted(&fromAddr.sa, fromAddrLen);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001871#if DEBUG
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001872 bb_error_msg("connection from '%s'", rmt_ip_str);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001873#endif
Denis Vlasenkob3a07152006-11-16 18:04:43 +00001874#endif /* FEATURE_HTTPD_CGI */
Denis Vlasenko6536a9b2007-01-12 10:35:23 +00001875 if (fromAddr.sa.sa_family == AF_INET) {
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001876 rmt_ip = ntohl(fromAddr.sin.sin_addr.s_addr);
1877 tcp_port = ntohs(fromAddr.sin.sin_port);
Denis Vlasenko6536a9b2007-01-12 10:35:23 +00001878 }
1879#if ENABLE_FEATURE_IPV6
1880 if (fromAddr.sa.sa_family == AF_INET6) {
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001881 //rmt_ip = ntohl(fromAddr.sin.sin_addr.s_addr);
1882 tcp_port = ntohs(fromAddr.sin6.sin6_port);
Denis Vlasenko6536a9b2007-01-12 10:35:23 +00001883 }
1884#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001885
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001886 /* set the KEEPALIVE option to cull dead connections */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001887 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
Denis Vlasenko04291bc2006-11-21 10:15:25 +00001888
1889 if (DEBUG || fork() == 0) {
1890 /* child */
Denis Vlasenko55a99402006-09-30 20:41:44 +00001891#if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001892 /* protect reload config, may be confuse checking */
1893 signal(SIGHUP, SIG_IGN);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001894#endif
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001895 handleIncoming();
Denis Vlasenko04291bc2006-11-21 10:15:25 +00001896 if (!DEBUG)
1897 exit(0);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001898 }
Denis Vlasenko6c5e5a02006-11-10 23:28:57 +00001899 close(s);
Denis Vlasenko04291bc2006-11-21 10:15:25 +00001900 } /* while (1) */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001901 return 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001902}
1903
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001904/* from inetd */
1905static int miniHttpd_inetd(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001906{
Denis Vlasenko6536a9b2007-01-12 10:35:23 +00001907 union {
1908 struct sockaddr sa;
1909 struct sockaddr_in sin;
1910 USE_FEATURE_IPV6(struct sockaddr_in6 sin6;)
1911 } fromAddr;
1912 socklen_t fromAddrLen = sizeof(fromAddr);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001913
Denis Vlasenko6536a9b2007-01-12 10:35:23 +00001914 getpeername(0, &fromAddr.sa, &fromAddrLen);
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001915 rmt_ip = 0;
1916 tcp_port = 0;
Denis Vlasenko6536a9b2007-01-12 10:35:23 +00001917#if ENABLE_FEATURE_HTTPD_CGI || DEBUG
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001918 free(rmt_ip_str);
1919 rmt_ip_str = xmalloc_sockaddr2dotted(&fromAddr.sa, fromAddrLen);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001920#endif
Denis Vlasenko6536a9b2007-01-12 10:35:23 +00001921 if (fromAddr.sa.sa_family == AF_INET) {
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001922 rmt_ip = ntohl(fromAddr.sin.sin_addr.s_addr);
1923 tcp_port = ntohs(fromAddr.sin.sin_port);
Denis Vlasenko6536a9b2007-01-12 10:35:23 +00001924 }
1925#if ENABLE_FEATURE_IPV6
1926 if (fromAddr.sa.sa_family == AF_INET6) {
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001927 //rmt_ip = ntohl(fromAddr.sin.sin_addr.s_addr);
1928 tcp_port = ntohs(fromAddr.sin6.sin6_port);
Denis Vlasenko6536a9b2007-01-12 10:35:23 +00001929 }
1930#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001931 handleIncoming();
1932 return 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001933}
Glenn L McGrath06e95652003-02-09 06:51:14 +00001934
Denis Vlasenko55a99402006-09-30 20:41:44 +00001935#if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
Glenn L McGrath06e95652003-02-09 06:51:14 +00001936static void sighup_handler(int sig)
1937{
1938 /* set and reset */
1939 struct sigaction sa;
1940
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001941 parse_conf(default_path_httpd_conf, sig == SIGHUP ? SIGNALED_PARSE : FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001942 sa.sa_handler = sighup_handler;
1943 sigemptyset(&sa.sa_mask);
1944 sa.sa_flags = SA_RESTART;
1945 sigaction(SIGHUP, &sa, NULL);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001946}
1947#endif
1948
Denis Vlasenko9f609292006-11-05 19:47:33 +00001949enum {
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001950 c_opt_config_file = 0,
1951 d_opt_decode_url,
1952 h_opt_home_httpd,
1953 USE_FEATURE_HTTPD_ENCODE_URL_STR(e_opt_encode_url,)
Denis Vlasenko9f609292006-11-05 19:47:33 +00001954 USE_FEATURE_HTTPD_BASIC_AUTH( r_opt_realm ,)
1955 USE_FEATURE_HTTPD_AUTH_MD5( m_opt_md5 ,)
1956 USE_FEATURE_HTTPD_SETUID( u_opt_setuid ,)
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001957 p_opt_port ,
1958 p_opt_inetd ,
1959 p_opt_foreground,
Denis Vlasenko9f609292006-11-05 19:47:33 +00001960 OPT_CONFIG_FILE = 1 << c_opt_config_file,
1961 OPT_DECODE_URL = 1 << d_opt_decode_url,
1962 OPT_HOME_HTTPD = 1 << h_opt_home_httpd,
1963 OPT_ENCODE_URL = USE_FEATURE_HTTPD_ENCODE_URL_STR((1 << e_opt_encode_url)) + 0,
1964 OPT_REALM = USE_FEATURE_HTTPD_BASIC_AUTH( (1 << r_opt_realm )) + 0,
1965 OPT_MD5 = USE_FEATURE_HTTPD_AUTH_MD5( (1 << m_opt_md5 )) + 0,
1966 OPT_SETUID = USE_FEATURE_HTTPD_SETUID( (1 << u_opt_setuid )) + 0,
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001967 OPT_PORT = 1 << p_opt_port,
1968 OPT_INETD = 1 << p_opt_inetd,
1969 OPT_FOREGROUND = 1 << p_opt_foreground,
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001970};
Eric Andersena3bb3e62003-06-26 09:05:32 +00001971
Eric Andersena3bb3e62003-06-26 09:05:32 +00001972
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +00001973int httpd_main(int argc, char **argv);
1974int httpd_main(int argc, char **argv)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001975{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001976 unsigned opt;
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001977 char *url_for_decode;
1978 USE_FEATURE_HTTPD_ENCODE_URL_STR(const char *url_for_encode;)
Denis Vlasenko0871bc82006-11-16 16:17:02 +00001979 const char *s_port;
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00001980 USE_FEATURE_HTTPD_SETUID(const char *s_ugid = NULL;)
1981 USE_FEATURE_HTTPD_SETUID(struct bb_uidgid_t ugid;)
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00001982 USE_FEATURE_HTTPD_AUTH_MD5(const char *pass;)
Eric Andersen35e643b2003-07-28 07:40:39 +00001983
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +00001984#if ENABLE_LOCALE_SUPPORT
1985 /* Undo busybox.c: we want to speak English in http (dates etc) */
1986 setlocale(LC_TIME, "C");
1987#endif
1988
Denis Vlasenko77e44d62007-06-09 23:49:05 +00001989 INIT_G();
1990 home_httpd = xrealloc_getcwd_or_warn(NULL);
1991 /* We do not "absolutize" path given by -h (home) opt.
1992 * If user gives relative path in -h, $SCRIPT_FILENAME can end up
1993 * relative too. */
Denis Vlasenko53091ec2007-03-26 13:35:09 +00001994 opt = getopt32(argc, argv, "c:d:h:"
1995 USE_FEATURE_HTTPD_ENCODE_URL_STR("e:")
1996 USE_FEATURE_HTTPD_BASIC_AUTH("r:")
1997 USE_FEATURE_HTTPD_AUTH_MD5("m:")
1998 USE_FEATURE_HTTPD_SETUID("u:")
1999 "p:if",
Denis Vlasenko77e44d62007-06-09 23:49:05 +00002000 &(configFile), &url_for_decode, &home_httpd
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00002001 USE_FEATURE_HTTPD_ENCODE_URL_STR(, &url_for_encode)
Denis Vlasenko77e44d62007-06-09 23:49:05 +00002002 USE_FEATURE_HTTPD_BASIC_AUTH(, &g_realm)
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00002003 USE_FEATURE_HTTPD_AUTH_MD5(, &pass)
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00002004 USE_FEATURE_HTTPD_SETUID(, &s_ugid)
Denis Vlasenko0871bc82006-11-16 16:17:02 +00002005 , &s_port
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002006 );
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002007 if (opt & OPT_DECODE_URL) {
2008 printf("%s", decodeString(url_for_decode, 1));
2009 return 0;
2010 }
Denis Vlasenko55a99402006-09-30 20:41:44 +00002011#if ENABLE_FEATURE_HTTPD_ENCODE_URL_STR
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002012 if (opt & OPT_ENCODE_URL) {
2013 printf("%s", encodeString(url_for_encode));
2014 return 0;
2015 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002016#endif
Denis Vlasenko55a99402006-09-30 20:41:44 +00002017#if ENABLE_FEATURE_HTTPD_AUTH_MD5
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002018 if (opt & OPT_MD5) {
Denis Vlasenko55a99402006-09-30 20:41:44 +00002019 puts(pw_encrypt(pass, "$1$"));
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002020 return 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00002021 }
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002022#endif
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002023 if (opt & OPT_PORT)
Denis Vlasenko77e44d62007-06-09 23:49:05 +00002024 tcp_port = xatou16(s_port);
Denis Vlasenko0871bc82006-11-16 16:17:02 +00002025
Denis Vlasenko55a99402006-09-30 20:41:44 +00002026#if ENABLE_FEATURE_HTTPD_SETUID
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002027 if (opt & OPT_SETUID) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002028 if (!get_uidgid(&ugid, s_ugid, 1))
2029 bb_error_msg_and_die("unrecognized user[:group] "
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00002030 "name '%s'", s_ugid);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002031 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002032#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002033
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002034 xchdir(home_httpd);
Denis Vlasenko0871bc82006-11-16 16:17:02 +00002035 if (!(opt & OPT_INETD)) {
Denis Vlasenko6536a9b2007-01-12 10:35:23 +00002036 signal(SIGCHLD, SIG_IGN);
Denis Vlasenko77e44d62007-06-09 23:49:05 +00002037 server_socket = openServer();
Denis Vlasenko0871bc82006-11-16 16:17:02 +00002038#if ENABLE_FEATURE_HTTPD_SETUID
2039 /* drop privileges */
2040 if (opt & OPT_SETUID) {
2041 if (ugid.gid != (gid_t)-1) {
2042 if (setgroups(1, &ugid.gid) == -1)
Denis Vlasenko8e858e22007-03-07 09:35:43 +00002043 bb_perror_msg_and_die("setgroups");
Denis Vlasenko0871bc82006-11-16 16:17:02 +00002044 xsetgid(ugid.gid);
2045 }
2046 xsetuid(ugid.uid);
Denis Vlasenkode59c0f2006-10-05 22:50:22 +00002047 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002048#endif
Denis Vlasenko0871bc82006-11-16 16:17:02 +00002049 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002050
Denis Vlasenko55a99402006-09-30 20:41:44 +00002051#if ENABLE_FEATURE_HTTPD_CGI
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002052 {
2053 char *p = getenv("PATH");
Denis Vlasenko77e44d62007-06-09 23:49:05 +00002054 /* env strings themself are not freed, no need to strdup(p): */
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002055 clearenv();
2056 if (p)
Denis Vlasenko77e44d62007-06-09 23:49:05 +00002057 putenv(p - 5);
Denis Vlasenko0871bc82006-11-16 16:17:02 +00002058 if (!(opt & OPT_INETD))
Denis Vlasenko77e44d62007-06-09 23:49:05 +00002059 setenv_long("SERVER_PORT", tcp_port);
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002060 }
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002061#endif
2062
Denis Vlasenko55a99402006-09-30 20:41:44 +00002063#if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002064 sighup_handler(0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002065#else
Denis Vlasenko8b8c75e2006-09-26 10:07:41 +00002066 parse_conf(default_path_httpd_conf, FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002067#endif
2068
Denis Vlasenko0871bc82006-11-16 16:17:02 +00002069 if (opt & OPT_INETD)
2070 return miniHttpd_inetd();
2071
2072 if (!(opt & OPT_FOREGROUND))
Denis Vlasenko5a142022007-03-26 13:20:54 +00002073 bb_daemonize(0); /* don't change current directory */
Denis Vlasenko77e44d62007-06-09 23:49:05 +00002074 return miniHttpd(server_socket);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002075}