blob: b44adf730389f56799cf93938cd04c29aa00dfc3 [file] [log] [blame]
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001/*
2 * httpd implementation for busybox
3 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00004 * Copyright (C) 2002,2003 Glenn Engel <glenne@engel.org>
"Vladimir N. Oleynik"79af7d52006-01-26 10:58:12 +00005 * Copyright (C) 2003-2006 Vladimir Oleynik <dzo@simtreas.ru>
Glenn L McGrath58c708a2003-01-05 04:01:56 +00006 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00007 * simplify patch stolen from libbb without using strdup
Glenn L McGrath58c708a2003-01-05 04:01:56 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 *****************************************************************************
24 *
Glenn L McGrath06e95652003-02-09 06:51:14 +000025 * Typical usage:
26 * for non root user
27 * httpd -p 8080 -h $HOME/public_html
28 * or for daemon start from rc script with uid=0:
29 * httpd -u www
30 * This is equivalent if www user have uid=80 to
31 * httpd -p 80 -u 80 -h /www -c /etc/httpd.conf -r "Web Server Authentication"
32 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +000033 *
34 * When a url contains "cgi-bin" it is assumed to be a cgi script. The
35 * server changes directory to the location of the script and executes it
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +000036 * after setting QUERY_STRING and other environment variables.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000037 *
38 * The server can also be invoked as a url arg decoder and html text encoder
39 * as follows:
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000040 * foo=`httpd -d $foo` # decode "Hello%20World" as "Hello World"
Glenn L McGrathc9163fe2003-05-13 16:20:11 +000041 * bar=`httpd -e "<Hello World>"` # encode as "&#60Hello&#32World&#62"
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000042 * Note that url encoding for arguments is not the same as html encoding for
Eric Andersenaff114c2004-04-14 17:51:38 +000043 * presentation. -d decodes a url-encoded argument while -e encodes in html
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000044 * for page display.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000045 *
46 * httpd.conf has the following format:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000047 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +000048 * A:172.20. # Allow address from 172.20.0.0/16
49 * A:10.0.0.0/25 # Allow any address from 10.0.0.0-10.0.0.127
50 * A:10.0.0.0/255.255.255.128 # Allow any address that previous set
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000051 * A:127.0.0.1 # Allow local loopback connections
52 * D:* # Deny from other IP connections
53 * /cgi-bin:foo:bar # Require user foo, pwd bar on urls starting with /cgi-bin/
54 * /adm:admin:setup # Require user admin, pwd setup on urls starting with /adm/
55 * /adm:toor:PaSsWd # or user toor, pwd PaSsWd on urls starting with /adm/
56 * .au:audio/basic # additional mime type for audio.au files
Eric Andersenc7bda1c2004-03-15 08:29:22 +000057 *
Eric Andersenaff114c2004-04-14 17:51:38 +000058 * A/D may be as a/d or allow/deny - first char case insensitive
Glenn L McGrath393183d2003-05-26 14:07:50 +000059 * Deny IP rules take precedence over allow rules.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000060 *
61 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000062 * The Deny/Allow IP logic:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000063 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000064 * - Default is to allow all. No addresses are denied unless
Eric Andersen97a1de12004-08-26 22:22:50 +000065 * denied with a D: rule.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000066 * - Order of Deny/Allow rules is significant
67 * - Deny rules take precedence over allow rules.
68 * - If a deny all rule (D:*) is used it acts as a catch-all for unmatched
Eric Andersen97a1de12004-08-26 22:22:50 +000069 * addresses.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000070 * - Specification of Allow all (A:*) is a no-op
Eric Andersenc7bda1c2004-03-15 08:29:22 +000071 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000072 * Example:
73 * 1. Allow only specified addresses
Glenn L McGrathb65422c2003-09-08 10:59:27 +000074 * A:172.20 # Allow any address that begins with 172.20.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000075 * A:10.10. # Allow any address that begins with 10.10.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000076 * A:127.0.0.1 # Allow local loopback connections
77 * D:* # Deny from other IP connections
Eric Andersenc7bda1c2004-03-15 08:29:22 +000078 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000079 * 2. Only deny specified addresses
80 * D:1.2.3. # deny from 1.2.3.0 - 1.2.3.255
81 * D:2.3.4. # deny from 2.3.4.0 - 2.3.4.255
82 * A:* # (optional line added for clarity)
Eric Andersenc7bda1c2004-03-15 08:29:22 +000083 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000084 * If a sub directory contains a config file it is parsed and merged with
Glenn L McGrathb65422c2003-09-08 10:59:27 +000085 * any existing settings as if it was appended to the original configuration.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000086 *
87 * subdir paths are relative to the containing subdir and thus cannot
88 * affect the parent rules.
89 *
90 * Note that since the sub dir is parsed in the forked thread servicing the
91 * subdir http request, any merge is discarded when the process exits. As a
92 * result, the subdir settings only have a lifetime of a single request.
93 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000094 *
95 * If -c is not set, an attempt will be made to open the default
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000096 * root configuration file. If -c is set and the file is not found, the
97 * server exits with an error.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000098 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000099*/
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000100
Glenn L McGrath06e95652003-02-09 06:51:14 +0000101
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000102#include <stdio.h>
103#include <ctype.h> /* for isspace */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000104#include <string.h>
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000105#include <stdlib.h> /* for malloc */
106#include <time.h>
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000107#include <unistd.h> /* for close */
108#include <signal.h>
109#include <sys/types.h>
110#include <sys/socket.h> /* for connect and socket*/
111#include <netinet/in.h> /* for sockaddr_in */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000112#include <sys/time.h>
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000113#include <sys/stat.h>
114#include <sys/wait.h>
Glenn L McGrath06e95652003-02-09 06:51:14 +0000115#include <fcntl.h> /* for open modes */
116#include "busybox.h"
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000117
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000118
Eric Andersen07f2fea2004-10-08 08:03:29 +0000119static const char httpdVersion[] = "busybox httpd/1.35 6-Oct-2004";
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000120static const char default_path_httpd_conf[] = "/etc";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000121static const char httpd_conf[] = "httpd.conf";
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000122static const char home[] = "./";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000123
Glenn L McGrath5cd64612003-08-29 15:53:23 +0000124#ifdef CONFIG_LFS
125# define cont_l_fmt "%lld"
Eric Andersen0cb6f352006-01-30 22:30:41 +0000126# define cont_l_type (long long)
Glenn L McGrath5cd64612003-08-29 15:53:23 +0000127#else
128# define cont_l_fmt "%ld"
Eric Andersen0cb6f352006-01-30 22:30:41 +0000129# define cont_l_type (long)
Glenn L McGrath5cd64612003-08-29 15:53:23 +0000130#endif
131
Eric Andersen07f2fea2004-10-08 08:03:29 +0000132#define TIMEOUT 60
133
Eric Andersenaff114c2004-04-14 17:51:38 +0000134// Note: busybox xfuncs are not used because we want the server to keep running
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000135// if something bad happens due to a malformed user request.
Glenn L McGrath06e95652003-02-09 06:51:14 +0000136// As a result, all memory allocation after daemonize
137// is checked rigorously
138
139//#define DEBUG 1
140
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000141/* Configure options, disabled by default as custom httpd feature */
142
143/* disabled as optional features */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000144//#define CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath06e95652003-02-09 06:51:14 +0000145//#define CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
146//#define CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
147//#define CONFIG_FEATURE_HTTPD_SETUID
148//#define CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
149
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000150/* If set, use this server from internet superserver only */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000151//#define CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
152
153/* You can use this server as standalone, require libbb.a for linking */
154//#define HTTPD_STANDALONE
155
156/* Config options, disable this for do very small module */
157//#define CONFIG_FEATURE_HTTPD_CGI
158//#define CONFIG_FEATURE_HTTPD_BASIC_AUTH
Eric Andersen35e643b2003-07-28 07:40:39 +0000159//#define CONFIG_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath06e95652003-02-09 06:51:14 +0000160
161#ifdef HTTPD_STANDALONE
162/* standalone, enable all features */
163#undef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
164/* unset config option for remove warning as redefined */
165#undef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Eric Andersen35e643b2003-07-28 07:40:39 +0000166#undef CONFIG_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath06e95652003-02-09 06:51:14 +0000167#undef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath06e95652003-02-09 06:51:14 +0000168#undef CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
169#undef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
170#undef CONFIG_FEATURE_HTTPD_CGI
171#undef CONFIG_FEATURE_HTTPD_SETUID
172#undef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000173#undef ENABLE_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
174#undef ENABLE_FEATURE_HTTPD_BASIC_AUTH
175#undef ENABLE_FEATURE_HTTPD_AUTH_MD5
176#undef ENABLE_FEATURE_HTTPD_ENCODE_URL_STR
177#undef ENABLE_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
178#undef ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
179#undef ENABLE_FEATURE_HTTPD_CGI
180#undef ENABLE_FEATURE_HTTPD_SETUID
181#undef ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
Glenn L McGrath06e95652003-02-09 06:51:14 +0000182/* enable all features now */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000183#define CONFIG_FEATURE_HTTPD_BASIC_AUTH
Eric Andersen35e643b2003-07-28 07:40:39 +0000184#define CONFIG_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath06e95652003-02-09 06:51:14 +0000185#define CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath06e95652003-02-09 06:51:14 +0000186#define CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
187#define CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
188#define CONFIG_FEATURE_HTTPD_CGI
189#define CONFIG_FEATURE_HTTPD_SETUID
190#define CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000191#define ENABLE_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY 0
192#define ENABLE_FEATURE_HTTPD_BASIC_AUTH 1
193#define ENABLE_FEATURE_HTTPD_AUTH_MD5 1
194#define ENABLE_FEATURE_HTTPD_ENCODE_URL_STR 1
195#define ENABLE_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV 1
196#define ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES 1
197#define ENABLE_FEATURE_HTTPD_CGI 1
198#define ENABLE_FEATURE_HTTPD_SETUID 1
199#define ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP 1
Glenn L McGrath06e95652003-02-09 06:51:14 +0000200
201/* require from libbb.a for linking */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000202const char *bb_applet_name = "httpd";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000203
Manuel Novoa III cad53642003-03-19 09:13:01 +0000204void bb_show_usage(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000205{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000206 fprintf(stderr, "Usage: %s [-p <port>] [-c configFile] [-d/-e <string>] "
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000207 "[-r realm] [-u user] [-h homedir]\n", bb_applet_name);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000208 exit(1);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000209}
210#endif
211
Glenn L McGrath06e95652003-02-09 06:51:14 +0000212#ifdef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
213#undef CONFIG_FEATURE_HTTPD_SETUID /* use inetd user.group config settings */
214#undef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP /* so is not daemon */
215/* inetd set stderr to accepted socket and we can`t true see debug messages */
216#undef DEBUG
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000217#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000218
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000219#ifndef DEBUG
220# define DEBUG 0
221#endif
222
Glenn L McGrath06e95652003-02-09 06:51:14 +0000223#define MAX_MEMORY_BUFF 8192 /* IO buffer */
224
225typedef struct HT_ACCESS {
226 char *after_colon;
227 struct HT_ACCESS *next;
228 char before_colon[1]; /* really bigger, must last */
229} Htaccess;
230
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000231typedef struct HT_ACCESS_IP {
232 unsigned int ip;
233 unsigned int mask;
234 int allow_deny;
235 struct HT_ACCESS_IP *next;
236} Htaccess_IP;
237
Glenn L McGrath06e95652003-02-09 06:51:14 +0000238typedef struct
239{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000240 char buf[MAX_MEMORY_BUFF];
241
242#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
243 const char *realm;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000244 char *remoteuser;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000245#endif
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000246
Eric Andersen07f2fea2004-10-08 08:03:29 +0000247 const char *query;
248
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000249#ifdef CONFIG_FEATURE_HTTPD_CGI
250 char *referer;
251#endif
252
Glenn L McGrath06e95652003-02-09 06:51:14 +0000253 const char *configFile;
254
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000255 unsigned int rmt_ip;
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000256#if ENABLE_FEATURE_HTTPD_CGI || DEBUG
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000257 char rmt_ip_str[16]; /* for set env REMOTE_ADDR */
258#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000259 unsigned port; /* server initial port and for
260 set env REMOTE_PORT */
Eric Andersen07f2fea2004-10-08 08:03:29 +0000261 union HTTPD_FOUND {
262 const char *found_mime_type;
263 const char *found_moved_temporarily;
264 } httpd_found;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000265
Glenn L McGrath06e95652003-02-09 06:51:14 +0000266 off_t ContentLength; /* -1 - unknown */
267 time_t last_mod;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000268
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000269 Htaccess_IP *ip_a_d; /* config allow/deny lines */
Glenn L McGrath393183d2003-05-26 14:07:50 +0000270 int flg_deny_all;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000271#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
272 Htaccess *auth; /* config user:password lines */
273#endif
274#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
275 Htaccess *mime_a; /* config mime types */
276#endif
277
Glenn L McGrath06e95652003-02-09 06:51:14 +0000278#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
279 int accepted_socket;
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000280# define a_c_r config->accepted_socket
281# define a_c_w config->accepted_socket
Glenn L McGrath06e95652003-02-09 06:51:14 +0000282#else
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000283# define a_c_r 0
284# define a_c_w 1
Glenn L McGrath06e95652003-02-09 06:51:14 +0000285#endif
Eric Andersen07f2fea2004-10-08 08:03:29 +0000286 volatile int alarm_signaled;
287
Glenn L McGrath06e95652003-02-09 06:51:14 +0000288} HttpdConfig;
289
290static HttpdConfig *config;
291
292static const char request_GET[] = "GET"; /* size algorithic optimize */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000293
294static const char* const suffixTable [] = {
Eric Andersenaff114c2004-04-14 17:51:38 +0000295/* Warning: shorted equivalent suffix in one line must be first */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000296 ".htm.html", "text/html",
297 ".jpg.jpeg", "image/jpeg",
298 ".gif", "image/gif",
299 ".png", "image/png",
300 ".txt.h.c.cc.cpp", "text/plain",
Glenn L McGrath06e95652003-02-09 06:51:14 +0000301 ".css", "text/css",
302 ".wav", "audio/wav",
303 ".avi", "video/x-msvideo",
304 ".qt.mov", "video/quicktime",
305 ".mpe.mpeg", "video/mpeg",
306 ".mid.midi", "audio/midi",
307 ".mp3", "audio/mpeg",
308#if 0 /* unpopular */
309 ".au", "audio/basic",
310 ".pac", "application/x-ns-proxy-autoconfig",
311 ".vrml.wrl", "model/vrml",
312#endif
313 0, "application/octet-stream" /* default */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000314 };
315
316typedef enum
317{
318 HTTP_OK = 200,
Eric Andersen07f2fea2004-10-08 08:03:29 +0000319 HTTP_MOVED_TEMPORARILY = 302,
320 HTTP_BAD_REQUEST = 400, /* malformed syntax */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000321 HTTP_UNAUTHORIZED = 401, /* authentication needed, respond with auth hdr */
322 HTTP_NOT_FOUND = 404,
Glenn L McGrath06e95652003-02-09 06:51:14 +0000323 HTTP_FORBIDDEN = 403,
Eric Andersen07f2fea2004-10-08 08:03:29 +0000324 HTTP_REQUEST_TIMEOUT = 408,
325 HTTP_NOT_IMPLEMENTED = 501, /* used for unrecognized requests */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000326 HTTP_INTERNAL_SERVER_ERROR = 500,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000327#if 0 /* future use */
328 HTTP_CONTINUE = 100,
329 HTTP_SWITCHING_PROTOCOLS = 101,
330 HTTP_CREATED = 201,
331 HTTP_ACCEPTED = 202,
332 HTTP_NON_AUTHORITATIVE_INFO = 203,
333 HTTP_NO_CONTENT = 204,
334 HTTP_MULTIPLE_CHOICES = 300,
335 HTTP_MOVED_PERMANENTLY = 301,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000336 HTTP_NOT_MODIFIED = 304,
337 HTTP_PAYMENT_REQUIRED = 402,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000338 HTTP_BAD_GATEWAY = 502,
339 HTTP_SERVICE_UNAVAILABLE = 503, /* overload, maintenance */
340 HTTP_RESPONSE_SETSIZE=0xffffffff
341#endif
342} HttpResponseNum;
343
344typedef struct
345{
346 HttpResponseNum type;
347 const char *name;
348 const char *info;
349} HttpEnumString;
350
351static const HttpEnumString httpResponseNames[] = {
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +0000352 { HTTP_OK, "OK", NULL },
Eric Andersen07f2fea2004-10-08 08:03:29 +0000353 { HTTP_MOVED_TEMPORARILY, "Found", "Directories must end with a slash." },
354 { HTTP_REQUEST_TIMEOUT, "Request Timeout",
355 "No request appeared within a reasonable time period." },
Glenn L McGrath06e95652003-02-09 06:51:14 +0000356 { HTTP_NOT_IMPLEMENTED, "Not Implemented",
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000357 "The requested method is not recognized by this server." },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000358#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000359 { HTTP_UNAUTHORIZED, "Unauthorized", "" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000360#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000361 { HTTP_NOT_FOUND, "Not Found",
362 "The requested URL was not found on this server." },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000363 { HTTP_BAD_REQUEST, "Bad Request", "Unsupported method." },
Glenn L McGrath06e95652003-02-09 06:51:14 +0000364 { HTTP_FORBIDDEN, "Forbidden", "" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000365 { HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error",
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000366 "Internal Server Error" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000367#if 0 /* not implemented */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000368 { HTTP_CREATED, "Created" },
369 { HTTP_ACCEPTED, "Accepted" },
370 { HTTP_NO_CONTENT, "No Content" },
371 { HTTP_MULTIPLE_CHOICES, "Multiple Choices" },
372 { HTTP_MOVED_PERMANENTLY, "Moved Permanently" },
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000373 { HTTP_NOT_MODIFIED, "Not Modified" },
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000374 { HTTP_BAD_GATEWAY, "Bad Gateway", "" },
375 { HTTP_SERVICE_UNAVAILABLE, "Service Unavailable", "" },
376#endif
377};
378
Glenn L McGrath06e95652003-02-09 06:51:14 +0000379
380static const char RFC1123FMT[] = "%a, %d %b %Y %H:%M:%S GMT";
381static const char Content_length[] = "Content-length:";
382
383
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000384static int
385scan_ip (const char **ep, unsigned int *ip, unsigned char endc)
386{
387 const char *p = *ep;
388 int auto_mask = 8;
389 int j;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000390
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000391 *ip = 0;
392 for (j = 0; j < 4; j++) {
393 unsigned int octet;
394
395 if ((*p < '0' || *p > '9') && (*p != '/' || j == 0) && *p != 0)
396 return -auto_mask;
397 octet = 0;
398 while (*p >= '0' && *p <= '9') {
399 octet *= 10;
400 octet += *p - '0';
401 if (octet > 255)
402 return -auto_mask;
403 p++;
404 }
405 if (*p == '.')
406 p++;
407 if (*p != '/' && *p != 0)
408 auto_mask += 8;
409 *ip = ((*ip) << 8) | octet;
410 }
411 if (*p != 0) {
412 if (*p != endc)
413 return -auto_mask;
414 p++;
415 if(*p == 0)
416 return -auto_mask;
417 }
418 *ep = p;
419 return auto_mask;
420}
421
422static int
423scan_ip_mask (const char *ipm, unsigned int *ip, unsigned int *mask)
424{
425 int i;
426 unsigned int msk;
427
428 i = scan_ip(&ipm, ip, '/');
429 if(i < 0)
430 return i;
431 if(*ipm) {
432 const char *p = ipm;
433
434 i = 0;
435 while (*p) {
436 if (*p < '0' || *p > '9') {
437 if (*p == '.') {
438 i = scan_ip (&ipm, mask, 0);
439 return i != 32;
440 }
441 return -1;
442 }
443 i *= 10;
444 i += *p - '0';
445 p++;
446 }
447 }
448 if (i > 32 || i < 0)
449 return -1;
450 msk = 0x80000000;
451 *mask = 0;
452 while (i > 0) {
453 *mask |= msk;
454 msk >>= 1;
455 i--;
456 }
457 return 0;
458}
459
460#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000461static void free_config_lines(Htaccess **pprev)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000462{
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000463 Htaccess *prev = *pprev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000464
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000465 while( prev ) {
466 Htaccess *cur = prev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000467
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000468 prev = cur->next;
469 free(cur);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000470 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000471 *pprev = NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000472}
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000473#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000474
Glenn L McGrath06e95652003-02-09 06:51:14 +0000475/* flag */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000476#define FIRST_PARSE 0
477#define SUBDIR_PARSE 1
478#define SIGNALED_PARSE 2
479#define FIND_FROM_HTTPD_ROOT 3
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000480/****************************************************************************
481 *
482 > $Function: parse_conf()
483 *
484 * $Description: parse configuration file into in-memory linked list.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000485 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000486 * The first non-white character is examined to determine if the config line
487 * is one of the following:
488 * .ext:mime/type # new mime type not compiled into httpd
489 * [adAD]:from # ip address allow/deny, * for wildcard
490 * /path:user:pass # username/password
491 *
492 * Any previous IP rules are discarded.
493 * If the flag argument is not SUBDIR_PARSE then all /path and mime rules
494 * are also discarded. That is, previous settings are retained if flag is
495 * SUBDIR_PARSE.
496 *
497 * $Parameters:
498 * (const char *) path . . null for ip address checks, path for password
499 * checks.
500 * (int) flag . . . . . . the source of the parse request.
501 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000502 * $Return: (None)
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000503 *
504 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000505static void parse_conf(const char *path, int flag)
506{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000507 FILE *f;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000508#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000509 Htaccess *prev, *cur;
510#elif CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
511 Htaccess *cur;
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000512#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000513
Glenn L McGrath06e95652003-02-09 06:51:14 +0000514 const char *cf = config->configFile;
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000515 char buf[160];
Glenn L McGrath06e95652003-02-09 06:51:14 +0000516 char *p0 = NULL;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000517 char *c, *p;
518
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000519 /* free previous ip setup if present */
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000520 Htaccess_IP *pip = config->ip_a_d;
521
522 while( pip ) {
523 Htaccess_IP *cur_ipl = pip;
524
525 pip = cur_ipl->next;
526 free(cur_ipl);
527 }
528 config->ip_a_d = NULL;
529
Glenn L McGrath24833432003-06-10 17:22:49 +0000530 config->flg_deny_all = 0;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000531
532#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000533 /* retain previous auth and mime config only for subdir parse */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000534 if(flag != SUBDIR_PARSE) {
535#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000536 free_config_lines(&config->auth);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000537#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000538#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
539 free_config_lines(&config->mime_a);
540#endif
541 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000542#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000543
544 if(flag == SUBDIR_PARSE || cf == NULL) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000545 cf = alloca(strlen(path) + sizeof(httpd_conf) + 2);
546 if(cf == NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000547 if(flag == FIRST_PARSE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000548 bb_error_msg_and_die(bb_msg_memory_exhausted);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000549 return;
550 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000551 sprintf((char *)cf, "%s/%s", path, httpd_conf);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000552 }
553
554 while((f = fopen(cf, "r")) == NULL) {
Eric Andersen35e643b2003-07-28 07:40:39 +0000555 if(flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) {
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000556 /* config file not found, no changes to config */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000557 return;
558 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000559 if(config->configFile && flag == FIRST_PARSE) /* if -c option given */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000560 bb_perror_msg_and_die("%s", cf);
561 flag = FIND_FROM_HTTPD_ROOT;
562 cf = httpd_conf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000563 }
564
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000565#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
566 prev = config->auth;
567#endif
568 /* This could stand some work */
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000569 while ( (p0 = fgets(buf, sizeof(buf), f)) != NULL) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000570 c = NULL;
571 for(p = p0; *p0 != 0 && *p0 != '#'; p0++) {
572 if(!isspace(*p0)) {
573 *p++ = *p0;
574 if(*p0 == ':' && c == NULL)
575 c = p;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000576 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000577 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000578 *p = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000579
580 /* test for empty or strange line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000581 if (c == NULL || *c == 0)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000582 continue;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000583 p0 = buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000584 if(*p0 == 'd')
585 *p0 = 'D';
Glenn L McGrath393183d2003-05-26 14:07:50 +0000586 if(*c == '*') {
587 if(*p0 == 'D') {
588 /* memorize deny all */
589 config->flg_deny_all++;
590 }
591 /* skip default other "word:*" config lines */
592 continue;
593 }
594
595 if(*p0 == 'a')
596 *p0 = 'A';
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000597 else if(*p0 != 'D' && *p0 != 'A'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000598#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000599 && *p0 != '/'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000600#endif
601#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000602 && *p0 != '.'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000603#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000604 )
Glenn L McGrath06e95652003-02-09 06:51:14 +0000605 continue;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000606 if(*p0 == 'A' || *p0 == 'D') {
607 /* storing current config IP line */
608 pip = calloc(1, sizeof(Htaccess_IP));
609 if(pip) {
610 if(scan_ip_mask (c, &(pip->ip), &(pip->mask))) {
611 /* syntax IP{/mask} error detected, protect all */
612 *p0 = 'D';
613 pip->mask = 0;
614 }
615 pip->allow_deny = *p0;
616 if(*p0 == 'D') {
617 /* Deny:form_IP move top */
618 pip->next = config->ip_a_d;
619 config->ip_a_d = pip;
620 } else {
621 /* add to bottom A:form_IP config line */
622 Htaccess_IP *prev_IP = config->ip_a_d;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000623
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000624 if(prev_IP == NULL) {
625 config->ip_a_d = pip;
626 } else {
627 while(prev_IP->next)
628 prev_IP = prev_IP->next;
629 prev_IP->next = pip;
630 }
631 }
632 }
633 continue;
634 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000635#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
636 if(*p0 == '/') {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000637 /* make full path from httpd root / curent_path / config_line_path */
638 cf = flag == SUBDIR_PARSE ? path : "";
639 p0 = malloc(strlen(cf) + (c - buf) + 2 + strlen(c));
640 if(p0 == NULL)
641 continue;
642 c[-1] = 0;
643 sprintf(p0, "/%s%s", cf, buf);
644
645 /* another call bb_simplify_path */
646 cf = p = p0;
647
648 do {
649 if (*p == '/') {
650 if (*cf == '/') { /* skip duplicate (or initial) slash */
651 continue;
652 } else if (*cf == '.') {
653 if (cf[1] == '/' || cf[1] == 0) { /* remove extra '.' */
654 continue;
655 } else if ((cf[1] == '.') && (cf[2] == '/' || cf[2] == 0)) {
656 ++cf;
657 if (p > p0) {
658 while (*--p != '/'); /* omit previous dir */
659 }
660 continue;
661 }
662 }
663 }
664 *++p = *cf;
665 } while (*++cf);
666
667 if ((p == p0) || (*p != '/')) { /* not a trailing slash */
668 ++p; /* so keep last character */
669 }
670 *p = 0;
671 sprintf(p0, "%s:%s", p0, c);
672 }
673#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000674
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000675#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
676 /* storing current config line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000677 cur = calloc(1, sizeof(Htaccess) + strlen(p0));
678 if(cur) {
679 cf = strcpy(cur->before_colon, p0);
680 c = strchr(cf, ':');
681 *c++ = 0;
682 cur->after_colon = c;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000683#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Glenn L McGrath5875be42003-09-08 15:39:09 +0000684 if(*cf == '.') {
Glenn L McGrath874e3382003-05-14 12:11:36 +0000685 /* config .mime line move top for overwrite previous */
686 cur->next = config->mime_a;
687 config->mime_a = cur;
Glenn L McGrath5875be42003-09-08 15:39:09 +0000688 continue;
Glenn L McGrath874e3382003-05-14 12:11:36 +0000689 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000690#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000691#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath5875be42003-09-08 15:39:09 +0000692 free(p0);
693 if(prev == NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000694 /* first line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000695 config->auth = prev = cur;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000696 } else {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000697 /* sort path, if current lenght eq or bigger then move up */
698 Htaccess *prev_hti = config->auth;
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +0000699 size_t l = strlen(cf);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000700 Htaccess *hti;
701
702 for(hti = prev_hti; hti; hti = hti->next) {
703 if(l >= strlen(hti->before_colon)) {
704 /* insert before hti */
705 cur->next = hti;
706 if(prev_hti != hti) {
707 prev_hti->next = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000708 } else {
709 /* insert as top */
710 config->auth = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000711 }
Glenn L McGrath393183d2003-05-26 14:07:50 +0000712 break;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000713 }
714 if(prev_hti != hti)
715 prev_hti = prev_hti->next;
716 }
717 if(!hti) { /* not inserted, add to bottom */
718 prev->next = cur;
719 prev = cur;
720 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000721 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000722#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000723 }
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000724#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000725 }
726 fclose(f);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000727}
728
729#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000730/****************************************************************************
731 *
732 > $Function: encodeString()
733 *
734 * $Description: Given a string, html encode special characters.
735 * This is used for the -e command line option to provide an easy way
736 * for scripts to encode result data without confusing browsers. The
737 * returned string pointer is memory allocated by malloc().
738 *
739 * $Parameters:
740 * (const char *) string . . The first string to encode.
741 *
742 * $Return: (char *) . . . .. . . A pointer to the encoded string.
743 *
744 * $Errors: Returns a null string ("") if memory is not available.
745 *
746 ****************************************************************************/
747static char *encodeString(const char *string)
748{
749 /* take the simple route and encode everything */
750 /* could possibly scan once to get length. */
751 int len = strlen(string);
"Vladimir N. Oleynik"0bf67e82005-12-26 17:26:59 +0000752 char *out = malloc(len * 6 + 1);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000753 char *p=out;
754 char ch;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000755
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000756 if (!out) return "";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000757 while ((ch = *string++)) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000758 // very simple check for what to encode
759 if (isalnum(ch)) *p++ = ch;
Eric Andersen3efa51d2005-06-23 05:51:48 +0000760 else p += sprintf(p, "&#%d;", (unsigned char) ch);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000761 }
762 *p=0;
763 return out;
764}
Glenn L McGrath06e95652003-02-09 06:51:14 +0000765#endif /* CONFIG_FEATURE_HTTPD_ENCODE_URL_STR */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000766
767/****************************************************************************
768 *
769 > $Function: decodeString()
770 *
771 * $Description: Given a URL encoded string, convert it to plain ascii.
772 * Since decoding always makes strings smaller, the decode is done in-place.
773 * Thus, callers should strdup() the argument if they do not want the
774 * argument modified. The return is the original pointer, allowing this
775 * function to be easily used as arguments to other functions.
776 *
777 * $Parameters:
778 * (char *) string . . . The first string to decode.
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000779 * (int) flag . . . 1 if require decode '+' as ' ' for CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000780 *
781 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
782 *
783 * $Errors: None
784 *
785 ****************************************************************************/
Eric Andersena3bb3e62003-06-26 09:05:32 +0000786static char *decodeString(char *orig, int flag_plus_to_space)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000787{
788 /* note that decoded string is always shorter than original */
Eric Andersena3bb3e62003-06-26 09:05:32 +0000789 char *string = orig;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000790 char *ptr = string;
Eric Andersena3bb3e62003-06-26 09:05:32 +0000791
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000792 while (*ptr)
793 {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000794 if (*ptr == '+' && flag_plus_to_space) { *string++ = ' '; ptr++; }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000795 else if (*ptr != '%') *string++ = *ptr++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000796 else {
"Vladimir N. Oleynik"0bf67e82005-12-26 17:26:59 +0000797 unsigned int value1, value2;
798
799 ptr++;
800 if(sscanf(ptr, "%1X", &value1) != 1 ||
801 sscanf(ptr+1, "%1X", &value2) != 1) {
802 if(!flag_plus_to_space)
803 return NULL;
804 *string++ = '%';
805 } else {
806 value1 = value1 * 16 + value2;
807 if(value1 == '/' || value1 == 0)
808 return orig+1;
809 *string++ = value1;
810 ptr += 2;
811 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000812 }
813 }
814 *string = '\0';
815 return orig;
816}
817
818
Glenn L McGrath06e95652003-02-09 06:51:14 +0000819#ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000820/****************************************************************************
821 *
822 > $Function: addEnv()
823 *
Eric Andersenaff114c2004-04-14 17:51:38 +0000824 * $Description: Add an environment variable setting to the global list.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000825 * A NAME=VALUE string is allocated, filled, and added to the list of
826 * environment settings passed to the cgi execution script.
827 *
828 * $Parameters:
Glenn L McGrath06e95652003-02-09 06:51:14 +0000829 * (char *) name_before_underline - The first part environment variable name.
830 * (char *) name_after_underline - The second part environment variable name.
831 * (char *) value . . The value to which the env variable is set.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000832 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000833 * $Return: (void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000834 *
835 * $Errors: Silently returns if the env runs out of space to hold the new item
836 *
837 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000838static void addEnv(const char *name_before_underline,
839 const char *name_after_underline, const char *value)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000840{
Glenn L McGrath5875be42003-09-08 15:39:09 +0000841 char *s = NULL;
Glenn L McGrath393183d2003-05-26 14:07:50 +0000842 const char *underline;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000843
Glenn L McGrath06e95652003-02-09 06:51:14 +0000844 if (!value)
845 value = "";
Glenn L McGrath393183d2003-05-26 14:07:50 +0000846 underline = *name_after_underline ? "_" : "";
847 asprintf(&s, "%s%s%s=%s", name_before_underline, underline,
Glenn L McGrath06e95652003-02-09 06:51:14 +0000848 name_after_underline, value);
Glenn L McGrath393183d2003-05-26 14:07:50 +0000849 if(s) {
Glenn L McGrath5875be42003-09-08 15:39:09 +0000850 putenv(s);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000851 }
852}
853
Eric Andersena3bb3e62003-06-26 09:05:32 +0000854#if defined(CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV) || !defined(CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000855/* set environs SERVER_PORT and REMOTE_PORT */
856static void addEnvPort(const char *port_name)
857{
858 char buf[16];
859
860 sprintf(buf, "%u", config->port);
861 addEnv(port_name, "PORT", buf);
862}
Eric Andersena3bb3e62003-06-26 09:05:32 +0000863#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000864#endif /* CONFIG_FEATURE_HTTPD_CGI */
865
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000866
867#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000868/****************************************************************************
869 *
870 > $Function: decodeBase64()
871 *
872 > $Description: Decode a base 64 data stream as per rfc1521.
873 * Note that the rfc states that none base64 chars are to be ignored.
874 * Since the decode always results in a shorter size than the input, it is
875 * OK to pass the input arg as an output arg.
876 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000877 * $Parameter:
878 * (char *) Data . . . . A pointer to a base64 encoded string.
879 * Where to place the decoded data.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000880 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000881 * $Return: void
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000882 *
883 * $Errors: None
884 *
885 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000886static void decodeBase64(char *Data)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000887{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000888
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +0000889 const unsigned char *in = (const unsigned char *)Data;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000890 // The decoded size will be at most 3/4 the size of the encoded
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000891 unsigned long ch = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000892 int i = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000893
894 while (*in) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000895 int t = *in++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000896
Glenn L McGrath874e3382003-05-14 12:11:36 +0000897 if(t >= '0' && t <= '9')
898 t = t - '0' + 52;
899 else if(t >= 'A' && t <= 'Z')
900 t = t - 'A';
901 else if(t >= 'a' && t <= 'z')
902 t = t - 'a' + 26;
903 else if(t == '+')
904 t = 62;
905 else if(t == '/')
906 t = 63;
907 else if(t == '=')
908 t = 0;
909 else
910 continue;
911
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000912 ch = (ch << 6) | t;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000913 i++;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000914 if (i == 4) {
915 *Data++ = (char) (ch >> 16);
916 *Data++ = (char) (ch >> 8);
917 *Data++ = (char) ch;
918 i = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000919 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000920 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000921 *Data = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000922}
923#endif
924
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000925
Glenn L McGrath06e95652003-02-09 06:51:14 +0000926#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000927/****************************************************************************
928 *
929 > $Function: openServer()
930 *
931 * $Description: create a listen server socket on the designated port.
932 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000933 * $Return: (int) . . . A connection socket. -1 for errors.
934 *
935 * $Errors: None
936 *
937 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000938static int openServer(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000939{
940 struct sockaddr_in lsocket;
941 int fd;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000942
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000943 /* create the socket right now */
944 /* inet_addr() returns a value that is already in network order */
945 memset(&lsocket, 0, sizeof(lsocket));
946 lsocket.sin_family = AF_INET;
947 lsocket.sin_addr.s_addr = INADDR_ANY;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000948 lsocket.sin_port = htons(config->port) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000949 fd = socket(AF_INET, SOCK_STREAM, 0);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000950 if (fd >= 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000951 /* tell the OS it's OK to reuse a previous address even though */
952 /* it may still be in a close down state. Allows bind to succeed. */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000953 int on = 1;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000954#ifdef SO_REUSEPORT
Glenn L McGrath06e95652003-02-09 06:51:14 +0000955 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *)&on, sizeof(on)) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000956#else
Glenn L McGrath06e95652003-02-09 06:51:14 +0000957 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000958#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000959 if (bind(fd, (struct sockaddr *)&lsocket, sizeof(lsocket)) == 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000960 listen(fd, 9);
961 signal(SIGCHLD, SIG_IGN); /* prevent zombie (defunct) processes */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000962 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000963 bb_perror_msg_and_die("bind");
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000964 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000965 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000966 bb_perror_msg_and_die("create socket");
Glenn L McGrath06e95652003-02-09 06:51:14 +0000967 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000968 return fd;
969}
Glenn L McGrath06e95652003-02-09 06:51:14 +0000970#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000971
972/****************************************************************************
973 *
974 > $Function: sendHeaders()
975 *
976 * $Description: Create and send HTTP response headers.
977 * The arguments are combined and sent as one write operation. Note that
978 * IE will puke big-time if the headers are not sent in one packet and the
Glenn L McGrath06e95652003-02-09 06:51:14 +0000979 * second packet is delayed for any reason.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000980 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000981 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000982 * (HttpResponseNum) responseNum . . . The result code to send.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000983 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000984 * $Return: (int) . . . . writing errors
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000985 *
986 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000987static int sendHeaders(HttpResponseNum responseNum)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000988{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000989 char *buf = config->buf;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000990 const char *responseString = "";
991 const char *infoString = 0;
Eric Andersen07f2fea2004-10-08 08:03:29 +0000992 const char *mime_type;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000993 unsigned int i;
994 time_t timer = time(0);
995 char timeStr[80];
Glenn L McGrath06e95652003-02-09 06:51:14 +0000996 int len;
997
998 for (i = 0;
999 i < (sizeof(httpResponseNames)/sizeof(httpResponseNames[0])); i++) {
1000 if (httpResponseNames[i].type == responseNum) {
1001 responseString = httpResponseNames[i].name;
1002 infoString = httpResponseNames[i].info;
1003 break;
1004 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001005 }
Eric Andersen07f2fea2004-10-08 08:03:29 +00001006 /* error message is HTML */
1007 mime_type = responseNum == HTTP_OK ?
1008 config->httpd_found.found_mime_type : "text/html";
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001009
1010 /* emit the current date */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001011 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&timer));
1012 len = sprintf(buf,
"Vladimir N. Oleynik"2e33daa2006-01-26 10:46:14 +00001013 "HTTP/1.0 %d %s\r\nContent-type: %s\r\n"
Glenn L McGrath06e95652003-02-09 06:51:14 +00001014 "Date: %s\r\nConnection: close\r\n",
Eric Andersen07f2fea2004-10-08 08:03:29 +00001015 responseNum, responseString, mime_type, timeStr);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001016
Glenn L McGrath3d2405c2003-02-10 22:28:21 +00001017#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath06e95652003-02-09 06:51:14 +00001018 if (responseNum == HTTP_UNAUTHORIZED) {
1019 len += sprintf(buf+len, "WWW-Authenticate: Basic realm=\"%s\"\r\n",
1020 config->realm);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001021 }
Glenn L McGrath3d2405c2003-02-10 22:28:21 +00001022#endif
Eric Andersen07f2fea2004-10-08 08:03:29 +00001023 if(responseNum == HTTP_MOVED_TEMPORARILY) {
1024 len += sprintf(buf+len, "Location: %s/%s%s\r\n",
1025 config->httpd_found.found_moved_temporarily,
1026 (config->query ? "?" : ""),
1027 (config->query ? config->query : ""));
1028 }
1029
Glenn L McGrath06e95652003-02-09 06:51:14 +00001030 if (config->ContentLength != -1) { /* file */
1031 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&config->last_mod));
Glenn L McGrath5cd64612003-08-29 15:53:23 +00001032 len += sprintf(buf+len, "Last-Modified: %s\r\n%s " cont_l_fmt "\r\n",
Eric Andersen0cb6f352006-01-30 22:30:41 +00001033 timeStr, Content_length, cont_l_type config->ContentLength);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001034 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001035 strcat(buf, "\r\n");
1036 len += 2;
1037 if (infoString) {
1038 len += sprintf(buf+len,
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001039 "<HEAD><TITLE>%d %s</TITLE></HEAD>\n"
1040 "<BODY><H1>%d %s</H1>\n%s\n</BODY>\n",
1041 responseNum, responseString,
Glenn L McGrath06e95652003-02-09 06:51:14 +00001042 responseNum, responseString, infoString);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001043 }
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001044#if DEBUG
1045 fprintf(stderr, "Headers: '%s'", buf);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001046#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +00001047 return bb_full_write(a_c_w, buf, len);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001048}
1049
1050/****************************************************************************
1051 *
1052 > $Function: getLine()
1053 *
1054 * $Description: Read from the socket until an end of line char found.
1055 *
1056 * Characters are read one at a time until an eol sequence is found.
1057 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001058 * $Return: (int) . . . . number of characters read. -1 if error.
1059 *
1060 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001061static int getLine(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001062{
1063 int count = 0;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001064 char *buf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001065
1066 while (read(a_c_r, buf + count, 1) == 1) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001067 if (buf[count] == '\r') continue;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001068 if (buf[count] == '\n') {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001069 buf[count] = 0;
1070 return count;
1071 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001072 if(count < (MAX_MEMORY_BUFF-1)) /* check owerflow */
1073 count++;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001074 }
1075 if (count) return count;
1076 else return -1;
1077}
1078
Glenn L McGrath06e95652003-02-09 06:51:14 +00001079#ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001080/****************************************************************************
1081 *
1082 > $Function: sendCgi()
1083 *
1084 * $Description: Execute a CGI script and send it's stdout back
1085 *
1086 * Environment variables are set up and the script is invoked with pipes
1087 * for stdin/stdout. If a post is being done the script is fed the POST
1088 * data in addition to setting the QUERY_STRING variable (for GETs or POSTs).
1089 *
1090 * $Parameters:
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001091 * (const char *) url . . . . . . The requested URL (with leading /).
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001092 * (int bodyLen) . . . . . . . . Length of the post body.
1093 * (const char *cookie) . . . . . For set HTTP_COOKIE.
1094 * (const char *content_type) . . For set CONTENT_TYPE.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001095
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001096 *
1097 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
1098 *
1099 * $Errors: None
1100 *
1101 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001102static int sendCgi(const char *url,
Eric Andersen07f2fea2004-10-08 08:03:29 +00001103 const char *request, int bodyLen, const char *cookie,
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001104 const char *content_type)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001105{
1106 int fromCgi[2]; /* pipe for reading data from CGI */
1107 int toCgi[2]; /* pipe for sending data to CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001108
Glenn L McGrath06e95652003-02-09 06:51:14 +00001109 static char * argp[] = { 0, 0 };
1110 int pid = 0;
1111 int inFd;
1112 int outFd;
1113 int firstLine = 1;
1114
1115 do {
1116 if (pipe(fromCgi) != 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001117 break;
1118 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001119 if (pipe(toCgi) != 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001120 break;
1121 }
1122
1123 pid = fork();
Glenn L McGrath06e95652003-02-09 06:51:14 +00001124 if (pid < 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001125 pid = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001126 break;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001127 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001128
1129 if (!pid) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001130 /* child process */
1131 char *script;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001132 char *purl = strdup( url );
1133 char realpath_buff[MAXPATHLEN];
1134
1135 if(purl == NULL)
1136 _exit(242);
1137
1138 inFd = toCgi[0];
1139 outFd = fromCgi[1];
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001140
1141 dup2(inFd, 0); // replace stdin with the pipe
1142 dup2(outFd, 1); // replace stdout with the pipe
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001143 if(!DEBUG)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001144 dup2(outFd, 2); // replace stderr with the pipe
1145
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001146 close(toCgi[0]);
1147 close(toCgi[1]);
1148 close(fromCgi[0]);
1149 close(fromCgi[1]);
1150
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001151 /*
Glenn L McGrath06e95652003-02-09 06:51:14 +00001152 * Find PATH_INFO.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001153 */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001154 script = purl;
1155 while((script = strchr( script + 1, '/' )) != NULL) {
1156 /* have script.cgi/PATH_INFO or dirs/script.cgi[/PATH_INFO] */
1157 struct stat sb;
1158
1159 *script = '\0';
1160 if(is_directory(purl + 1, 1, &sb) == 0) {
1161 /* not directory, found script.cgi/PATH_INFO */
1162 *script = '/';
1163 break;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001164 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001165 *script = '/'; /* is directory, find next '/' */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001166 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001167 addEnv("PATH", "INFO", script); /* set /PATH_INFO or NULL */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001168 addEnv("PATH", "", getenv("PATH"));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001169 addEnv("REQUEST", "METHOD", request);
Eric Andersen07f2fea2004-10-08 08:03:29 +00001170 if(config->query) {
1171 char *uri = alloca(strlen(purl) + 2 + strlen(config->query));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001172 if(uri)
Eric Andersen07f2fea2004-10-08 08:03:29 +00001173 sprintf(uri, "%s?%s", purl, config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001174 addEnv("REQUEST", "URI", uri);
1175 } else {
1176 addEnv("REQUEST", "URI", purl);
1177 }
1178 if(script != NULL)
1179 *script = '\0'; /* reduce /PATH_INFO */
Rob Landley344ea472005-09-01 09:38:32 +00001180 /* SCRIPT_FILENAME required by PHP in CGI mode */
1181 if(realpath(purl + 1, realpath_buff))
"Vladimir N. Oleynik"54deebf2005-09-19 10:46:44 +00001182 addEnv("SCRIPT", "FILENAME", realpath_buff);
Rob Landley344ea472005-09-01 09:38:32 +00001183 else
"Vladimir N. Oleynik"54deebf2005-09-19 10:46:44 +00001184 *realpath_buff = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001185 /* set SCRIPT_NAME as full path: /cgi-bin/dirs/script.cgi */
1186 addEnv("SCRIPT_NAME", "", purl);
Eric Andersen07f2fea2004-10-08 08:03:29 +00001187 addEnv("QUERY_STRING", "", config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001188 addEnv("SERVER", "SOFTWARE", httpdVersion);
1189 addEnv("SERVER", "PROTOCOL", "HTTP/1.0");
1190 addEnv("GATEWAY_INTERFACE", "", "CGI/1.1");
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001191 addEnv("REMOTE", "ADDR", config->rmt_ip_str);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001192#ifdef CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
Glenn L McGrath06e95652003-02-09 06:51:14 +00001193 addEnvPort("REMOTE");
Glenn L McGrath06e95652003-02-09 06:51:14 +00001194#endif
1195 if(bodyLen) {
1196 char sbl[32];
1197
1198 sprintf(sbl, "%d", bodyLen);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001199 addEnv("CONTENT", "LENGTH", sbl);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001200 }
1201 if(cookie)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001202 addEnv("HTTP", "COOKIE", cookie);
1203 if(content_type)
1204 addEnv("CONTENT", "TYPE", content_type);
Eric Andersen769a3ef2003-12-19 11:23:47 +00001205#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001206 if(config->remoteuser) {
1207 addEnv("REMOTE", "USER", config->remoteuser);
1208 addEnv("AUTH_TYPE", "", "Basic");
Glenn L McGrath06e95652003-02-09 06:51:14 +00001209 }
Eric Andersen769a3ef2003-12-19 11:23:47 +00001210#endif
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001211 if(config->referer)
1212 addEnv("HTTP", "REFERER", config->referer);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001213
1214 /* set execve argp[0] without path */
1215 argp[0] = strrchr( purl, '/' ) + 1;
1216 /* but script argp[0] must have absolute path and chdiring to this */
Rob Landley344ea472005-09-01 09:38:32 +00001217 if(*realpath_buff) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001218 script = strrchr(realpath_buff, '/');
1219 if(script) {
1220 *script = '\0';
1221 if(chdir(realpath_buff) == 0) {
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00001222 *script = '/';
1223 // now run the program. If it fails,
1224 // use _exit() so no destructors
1225 // get called and make a mess.
Glenn L McGrath5875be42003-09-08 15:39:09 +00001226 execv(realpath_buff, argp);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001227 }
1228 }
1229 }
1230#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1231 config->accepted_socket = 1; /* send to stdout */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001232#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001233 sendHeaders(HTTP_NOT_FOUND);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001234 _exit(242);
1235 } /* end child */
1236
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001237 } while (0);
1238
Glenn L McGrath06e95652003-02-09 06:51:14 +00001239 if (pid) {
1240 /* parent process */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001241 int status;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001242 size_t post_readed_size = 0, post_readed_idx = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001243
1244 inFd = fromCgi[0];
1245 outFd = toCgi[1];
1246 close(fromCgi[1]);
1247 close(toCgi[0]);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001248 signal(SIGPIPE, SIG_IGN);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001249
1250 while (1) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001251 fd_set readSet;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001252 fd_set writeSet;
1253 char wbuf[128];
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001254 int nfound;
1255 int count;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001256
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001257 FD_ZERO(&readSet);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001258 FD_ZERO(&writeSet);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001259 FD_SET(inFd, &readSet);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001260 if(bodyLen > 0 || post_readed_size > 0) {
1261 FD_SET(outFd, &writeSet);
1262 nfound = outFd > inFd ? outFd : inFd;
1263 if(post_readed_size == 0) {
1264 FD_SET(a_c_r, &readSet);
1265 if(nfound < a_c_r)
1266 nfound = a_c_r;
1267 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001268 /* Now wait on the set of sockets! */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001269 nfound = select(nfound + 1, &readSet, &writeSet, 0, NULL);
1270 } else {
1271 if(!bodyLen) {
1272 close(outFd);
1273 bodyLen = -1;
1274 }
1275 nfound = select(inFd + 1, &readSet, 0, 0, NULL);
1276 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001277
Glenn L McGrath06e95652003-02-09 06:51:14 +00001278 if (nfound <= 0) {
1279 if (waitpid(pid, &status, WNOHANG) > 0) {
1280 close(inFd);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001281#if DEBUG
1282 if (WIFEXITED(status))
Manuel Novoa III cad53642003-03-19 09:13:01 +00001283 bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status));
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001284 if (WIFSIGNALED(status))
Manuel Novoa III cad53642003-03-19 09:13:01 +00001285 bb_error_msg("piped has exited with signal=%d", WTERMSIG(status));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001286#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001287 break;
1288 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001289 } else if(post_readed_size > 0 && FD_ISSET(outFd, &writeSet)) {
1290 count = bb_full_write(outFd, wbuf + post_readed_idx, post_readed_size);
1291 if(count > 0) {
1292 post_readed_size -= count;
1293 post_readed_idx += count;
1294 if(post_readed_size == 0)
1295 post_readed_idx = 0;
Paul Fox77ee5232005-07-20 18:42:52 +00001296 } else {
1297 post_readed_size = post_readed_idx = bodyLen = 0; /* broken pipe to CGI */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001298 }
1299 } else if(bodyLen > 0 && post_readed_size == 0 && FD_ISSET(a_c_r, &readSet)) {
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001300 count = bodyLen > (int)sizeof(wbuf) ? (int)sizeof(wbuf) : bodyLen;
Eric Andersen97a1de12004-08-26 22:22:50 +00001301 count = safe_read(a_c_r, wbuf, count);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001302 if(count > 0) {
1303 post_readed_size += count;
1304 bodyLen -= count;
Paul Fox77ee5232005-07-20 18:42:52 +00001305 } else {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001306 bodyLen = 0; /* closed */
1307 }
Eric Andersen97a1de12004-08-26 22:22:50 +00001308 }
1309 if(FD_ISSET(inFd, &readSet)) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001310 int s = a_c_w;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001311 char *rbuf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001312
Eric Andersen97a1de12004-08-26 22:22:50 +00001313#ifndef PIPE_BUF
1314# define PIPESIZE 4096 /* amount of buffering in a pipe */
1315#else
1316# define PIPESIZE PIPE_BUF
1317#endif
1318#if PIPESIZE >= MAX_MEMORY_BUFF
1319# error "PIPESIZE >= MAX_MEMORY_BUFF"
1320#endif
1321
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001322 // There is something to read
Eric Andersen97a1de12004-08-26 22:22:50 +00001323 count = safe_read(inFd, rbuf, PIPESIZE);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001324 if (count == 0)
1325 break; /* closed */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001326 if (count > 0) {
1327 if (firstLine) {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001328 rbuf[count] = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001329 /* check to see if the user script added headers */
"Vladimir N. Oleynik"2e33daa2006-01-26 10:46:14 +00001330 if(strncmp(rbuf, "HTTP/1.0 200 OK\r\n", 4) != 0) {
1331 bb_full_write(s, "HTTP/1.0 200 OK\r\n", 17);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001332 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001333 if (strstr(rbuf, "ontent-") == 0) {
"Vladimir N. Oleynik"2e33daa2006-01-26 10:46:14 +00001334 bb_full_write(s, "Content-type: text/plain\r\n\r\n", 28);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001335 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001336 firstLine = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001337 }
Eric Andersenef437492004-02-04 11:10:28 +00001338 if (bb_full_write(s, rbuf, count) != count)
1339 break;
1340
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001341#if DEBUG
1342 fprintf(stderr, "cgi read %d bytes\n", count);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001343#endif
1344 }
1345 }
1346 }
1347 }
1348 return 0;
1349}
Glenn L McGrath06e95652003-02-09 06:51:14 +00001350#endif /* CONFIG_FEATURE_HTTPD_CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001351
1352/****************************************************************************
1353 *
1354 > $Function: sendFile()
1355 *
1356 * $Description: Send a file response to an HTTP request
1357 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00001358 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001359 * (const char *) url . . The URL requested.
1360 *
1361 * $Return: (int) . . . . . . Always 0.
1362 *
1363 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001364static int sendFile(const char *url)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001365{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001366 char * suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001367 int f;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001368 const char * const * table;
1369 const char * try_suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001370
Glenn L McGrath06e95652003-02-09 06:51:14 +00001371 suffix = strrchr(url, '.');
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001372
Glenn L McGrath06e95652003-02-09 06:51:14 +00001373 for (table = suffixTable; *table; table += 2)
1374 if(suffix != NULL && (try_suffix = strstr(*table, suffix)) != 0) {
1375 try_suffix += strlen(suffix);
1376 if(*try_suffix == 0 || *try_suffix == '.')
1377 break;
1378 }
1379 /* also, if not found, set default as "application/octet-stream"; */
Eric Andersen07f2fea2004-10-08 08:03:29 +00001380 config->httpd_found.found_mime_type = *(table+1);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001381#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
1382 if (suffix) {
1383 Htaccess * cur;
1384
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001385 for (cur = config->mime_a; cur; cur = cur->next) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001386 if(strcmp(cur->before_colon, suffix) == 0) {
Eric Andersen07f2fea2004-10-08 08:03:29 +00001387 config->httpd_found.found_mime_type = cur->after_colon;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001388 break;
1389 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001390 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001391 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001392#endif /* CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES */
1393
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001394#if DEBUG
1395 fprintf(stderr, "Sending file '%s' Content-type: %s\n",
1396 url, config->httpd_found.found_mime_type);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001397#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001398
1399 f = open(url, O_RDONLY);
1400 if (f >= 0) {
1401 int count;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001402 char *buf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001403
1404 sendHeaders(HTTP_OK);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001405 while ((count = bb_full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
Glenn L McGrath9adcf732003-12-08 20:21:53 +00001406 if (bb_full_write(a_c_w, buf, count) != count)
1407 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001408 }
1409 close(f);
1410 } else {
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001411#if DEBUG
1412 bb_perror_msg("Unable to open '%s'", url);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001413#endif
1414 sendHeaders(HTTP_NOT_FOUND);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001415 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001416
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001417 return 0;
1418}
1419
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001420static int checkPermIP(void)
1421{
1422 Htaccess_IP * cur;
1423
1424 /* This could stand some work */
1425 for (cur = config->ip_a_d; cur; cur = cur->next) {
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001426#if DEBUG
1427 fprintf(stderr, "checkPermIP: '%s' ? ", config->rmt_ip_str);
1428 fprintf(stderr, "'%u.%u.%u.%u/%u.%u.%u.%u'\n",
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001429 (unsigned char)(cur->ip >> 24),
1430 (unsigned char)(cur->ip >> 16),
1431 (unsigned char)(cur->ip >> 8),
1432 cur->ip & 0xff,
1433 (unsigned char)(cur->mask >> 24),
1434 (unsigned char)(cur->mask >> 16),
1435 (unsigned char)(cur->mask >> 8),
1436 cur->mask & 0xff);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001437#endif
1438 if((config->rmt_ip & cur->mask) == cur->ip)
1439 return cur->allow_deny == 'A'; /* Allow/Deny */
1440 }
1441
Eric Andersenaff114c2004-04-14 17:51:38 +00001442 /* if unconfigured, return 1 - access from all */
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001443 return !config->flg_deny_all;
1444}
1445
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001446/****************************************************************************
1447 *
1448 > $Function: checkPerm()
1449 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001450 * $Description: Check the permission file for access password protected.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001451 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001452 * If config file isn't present, everything is allowed.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001453 * Entries are of the form you can see example from header source
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001454 *
1455 * $Parameters:
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001456 * (const char *) path . . . . The file path.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001457 * (const char *) request . . . User information to validate.
1458 *
1459 * $Return: (int) . . . . . . . . . 1 if request OK, 0 otherwise.
1460 *
1461 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001462
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001463#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001464static int checkPerm(const char *path, const char *request)
1465{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001466 Htaccess * cur;
1467 const char *p;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001468 const char *p0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001469
Glenn L McGrath06e95652003-02-09 06:51:14 +00001470 const char *prev = NULL;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001471
1472 /* This could stand some work */
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001473 for (cur = config->auth; cur; cur = cur->next) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001474 p0 = cur->before_colon;
1475 if(prev != NULL && strcmp(prev, p0) != 0)
1476 continue; /* find next identical */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001477 p = cur->after_colon;
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001478#if DEBUG
1479 fprintf(stderr,"checkPerm: '%s' ? '%s'\n", p0, request);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001480#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001481 {
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001482 size_t l = strlen(p0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001483
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001484 if(strncmp(p0, path, l) == 0 &&
1485 (l == 1 || path[l] == '/' || path[l] == 0)) {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001486 char *u;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001487 /* path match found. Check request */
Eric Andersen35e643b2003-07-28 07:40:39 +00001488 /* for check next /path:user:password */
1489 prev = p0;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001490 u = strchr(request, ':');
1491 if(u == NULL) {
1492 /* bad request, ':' required */
1493 break;
1494 }
1495
Eric Andersen35e643b2003-07-28 07:40:39 +00001496#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
1497 {
1498 char *cipher;
1499 char *pp;
Eric Andersen35e643b2003-07-28 07:40:39 +00001500
Eric Andersen35e643b2003-07-28 07:40:39 +00001501 if(strncmp(p, request, u-request) != 0) {
1502 /* user uncompared */
1503 continue;
1504 }
1505 pp = strchr(p, ':');
1506 if(pp && pp[1] == '$' && pp[2] == '1' &&
1507 pp[3] == '$' && pp[4]) {
1508 pp++;
1509 cipher = pw_encrypt(u+1, pp);
1510 if (strcmp(cipher, pp) == 0)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001511 goto set_remoteuser_var; /* Ok */
Eric Andersen35e643b2003-07-28 07:40:39 +00001512 /* unauthorized */
1513 continue;
1514 }
1515 }
1516#endif
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001517 if (strcmp(p, request) == 0) {
Glenn L McGrath9d1a33c2003-10-06 13:23:06 +00001518#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001519set_remoteuser_var:
Glenn L McGrath9d1a33c2003-10-06 13:23:06 +00001520#endif
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001521 config->remoteuser = strdup(request);
1522 if(config->remoteuser)
1523 config->remoteuser[(u - request)] = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001524 return 1; /* Ok */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001525 }
Eric Andersen35e643b2003-07-28 07:40:39 +00001526 /* unauthorized */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001527 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001528 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001529 } /* for */
1530
Glenn L McGrath06e95652003-02-09 06:51:14 +00001531 return prev == NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001532}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001533
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001534#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1535
Eric Andersen07f2fea2004-10-08 08:03:29 +00001536/****************************************************************************
1537 *
Mike Frysingerbb12d6f2006-01-03 23:59:01 +00001538 > $Function: handle_sigalrm()
Eric Andersen07f2fea2004-10-08 08:03:29 +00001539 *
Mike Frysingerbb12d6f2006-01-03 23:59:01 +00001540 * $Description: Handle timeouts
Eric Andersen07f2fea2004-10-08 08:03:29 +00001541 *
1542 ****************************************************************************/
1543
1544static void
1545handle_sigalrm( int sig )
1546{
1547 sendHeaders(HTTP_REQUEST_TIMEOUT);
1548 config->alarm_signaled = sig;
1549}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001550
1551/****************************************************************************
1552 *
1553 > $Function: handleIncoming()
1554 *
1555 * $Description: Handle an incoming http request.
1556 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001557 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001558static void handleIncoming(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001559{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001560 char *buf = config->buf;
1561 char *url;
1562 char *purl;
1563 int blank = -1;
Glenn L McGrath3d752f72004-03-05 09:38:16 +00001564 char *test;
1565 struct stat sb;
1566 int ip_allowed;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001567#ifdef CONFIG_FEATURE_HTTPD_CGI
1568 const char *prequest = request_GET;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001569 long length=0;
1570 char *cookie = 0;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001571 char *content_type = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001572#endif
Glenn L McGrath3d752f72004-03-05 09:38:16 +00001573#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1574 fd_set s_fd;
1575 struct timeval tv;
Eric Andersend8746cd2004-02-24 07:28:38 +00001576 int retval;
Glenn L McGrath3d752f72004-03-05 09:38:16 +00001577#endif
Eric Andersen07f2fea2004-10-08 08:03:29 +00001578 struct sigaction sa;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001579
1580#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1581 int credentials = -1; /* if not requred this is Ok */
1582#endif
1583
Eric Andersen07f2fea2004-10-08 08:03:29 +00001584 sa.sa_handler = handle_sigalrm;
1585 sigemptyset(&sa.sa_mask);
1586 sa.sa_flags = 0; /* no SA_RESTART */
1587 sigaction(SIGALRM, &sa, NULL);
1588
Glenn L McGrath06e95652003-02-09 06:51:14 +00001589 do {
1590 int count;
1591
Eric Andersen07f2fea2004-10-08 08:03:29 +00001592 (void) alarm( TIMEOUT );
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001593 if (getLine() <= 0)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001594 break; /* closed */
1595
1596 purl = strpbrk(buf, " \t");
1597 if(purl == NULL) {
1598BAD_REQUEST:
1599 sendHeaders(HTTP_BAD_REQUEST);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001600 break;
1601 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001602 *purl = 0;
1603#ifdef CONFIG_FEATURE_HTTPD_CGI
1604 if(strcasecmp(buf, prequest) != 0) {
1605 prequest = "POST";
1606 if(strcasecmp(buf, prequest) != 0) {
1607 sendHeaders(HTTP_NOT_IMPLEMENTED);
1608 break;
1609 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001610 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001611#else
1612 if(strcasecmp(buf, request_GET) != 0) {
1613 sendHeaders(HTTP_NOT_IMPLEMENTED);
1614 break;
1615 }
1616#endif
1617 *purl = ' ';
1618 count = sscanf(purl, " %[^ ] HTTP/%d.%*d", buf, &blank);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001619
Glenn L McGrath06e95652003-02-09 06:51:14 +00001620 if (count < 1 || buf[0] != '/') {
1621 /* Garbled request/URL */
1622 goto BAD_REQUEST;
1623 }
1624 url = alloca(strlen(buf) + 12); /* + sizeof("/index.html\0") */
1625 if(url == NULL) {
1626 sendHeaders(HTTP_INTERNAL_SERVER_ERROR);
1627 break;
1628 }
1629 strcpy(url, buf);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001630 /* extract url args if present */
Eric Andersen07f2fea2004-10-08 08:03:29 +00001631 test = strchr(url, '?');
1632 if (test) {
1633 *test++ = 0;
1634 config->query = test;
1635 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001636
"Vladimir N. Oleynik"ab90b9f2006-01-24 12:02:27 +00001637 test = decodeString(url, 0);
1638 if(test == NULL)
1639 goto BAD_REQUEST;
1640 if(test == (buf+1)) {
1641 sendHeaders(HTTP_NOT_FOUND);
1642 break;
1643 }
Manuel Novoa III cad53642003-03-19 09:13:01 +00001644 /* algorithm stolen from libbb bb_simplify_path(),
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001645 but don`t strdup and reducing trailing slash and protect out root */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001646 purl = test = url;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001647
Glenn L McGrath06e95652003-02-09 06:51:14 +00001648 do {
1649 if (*purl == '/') {
1650 if (*test == '/') { /* skip duplicate (or initial) slash */
1651 continue;
1652 } else if (*test == '.') {
1653 if (test[1] == '/' || test[1] == 0) { /* skip extra '.' */
1654 continue;
1655 } else if ((test[1] == '.') && (test[2] == '/' || test[2] == 0)) {
1656 ++test;
1657 if (purl == url) {
1658 /* protect out root */
1659 goto BAD_REQUEST;
1660 }
1661 while (*--purl != '/'); /* omit previous dir */
1662 continue;
1663 }
1664 }
1665 }
1666 *++purl = *test;
1667 } while (*++test);
1668
1669 *++purl = 0; /* so keep last character */
1670 test = purl; /* end ptr */
1671
1672 /* If URL is directory, adding '/' */
1673 if(test[-1] != '/') {
1674 if ( is_directory(url + 1, 1, &sb) ) {
Eric Andersen07f2fea2004-10-08 08:03:29 +00001675 config->httpd_found.found_moved_temporarily = url;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001676 }
1677 }
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001678#if DEBUG
1679 fprintf(stderr, "url='%s', args=%s\n", url, config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001680#endif
1681
1682 test = url;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001683 ip_allowed = checkPermIP();
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001684 while(ip_allowed && (test = strchr( test + 1, '/' )) != NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001685 /* have path1/path2 */
1686 *test = '\0';
1687 if( is_directory(url + 1, 1, &sb) ) {
1688 /* may be having subdir config */
1689 parse_conf(url + 1, SUBDIR_PARSE);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001690 ip_allowed = checkPermIP();
Glenn L McGrath06e95652003-02-09 06:51:14 +00001691 }
1692 *test = '/';
1693 }
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001694 if(blank >= 0) {
1695 // read until blank line for HTTP version specified, else parse immediate
1696 while(1) {
1697 alarm(TIMEOUT);
1698 count = getLine();
1699 if(count <= 0)
1700 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001701
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001702#if DEBUG
1703 fprintf(stderr, "Header: '%s'\n", buf);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001704#endif
1705
1706#ifdef CONFIG_FEATURE_HTTPD_CGI
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001707 /* try and do our best to parse more lines */
1708 if ((strncasecmp(buf, Content_length, 15) == 0)) {
1709 if(prequest != request_GET)
1710 length = strtol(buf + 15, 0, 0); // extra read only for POST
1711 } else if ((strncasecmp(buf, "Cookie:", 7) == 0)) {
1712 for(test = buf + 7; isspace(*test); test++)
1713 ;
1714 cookie = strdup(test);
1715 } else if ((strncasecmp(buf, "Content-Type:", 13) == 0)) {
1716 for(test = buf + 13; isspace(*test); test++)
1717 ;
1718 content_type = strdup(test);
1719 } else if ((strncasecmp(buf, "Referer:", 8) == 0)) {
1720 for(test = buf + 8; isspace(*test); test++)
1721 ;
1722 config->referer = strdup(test);
1723 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001724#endif
1725
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001726#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001727 if (strncasecmp(buf, "Authorization:", 14) == 0) {
1728 /* We only allow Basic credentials.
1729 * It shows up as "Authorization: Basic <userid:password>" where
1730 * the userid:password is base64 encoded.
1731 */
1732 for(test = buf + 14; isspace(*test); test++)
1733 ;
1734 if (strncasecmp(test, "Basic", 5) != 0)
1735 continue;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001736
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001737 test += 5; /* decodeBase64() skiping space self */
1738 decodeBase64(test);
1739 credentials = checkPerm(url, test);
1740 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001741#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1742
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001743 } /* while extra header reading */
1744 }
Eric Andersen07f2fea2004-10-08 08:03:29 +00001745 (void) alarm( 0 );
1746 if(config->alarm_signaled)
1747 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001748
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001749 if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001750 /* protect listing [/path]/httpd_conf or IP deny */
1751#ifdef CONFIG_FEATURE_HTTPD_CGI
1752FORBIDDEN: /* protect listing /cgi-bin */
1753#endif
1754 sendHeaders(HTTP_FORBIDDEN);
1755 break;
1756 }
1757
1758#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1759 if (credentials <= 0 && checkPerm(url, ":") == 0) {
1760 sendHeaders(HTTP_UNAUTHORIZED);
1761 break;
1762 }
1763#endif
1764
Eric Andersen07f2fea2004-10-08 08:03:29 +00001765 if(config->httpd_found.found_moved_temporarily) {
1766 sendHeaders(HTTP_MOVED_TEMPORARILY);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001767#if DEBUG
Eric Andersen07f2fea2004-10-08 08:03:29 +00001768 /* clear unforked memory flag */
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001769 config->httpd_found.found_moved_temporarily = NULL;
Eric Andersen07f2fea2004-10-08 08:03:29 +00001770#endif
1771 break;
1772 }
1773
Glenn L McGrath06e95652003-02-09 06:51:14 +00001774 test = url + 1; /* skip first '/' */
1775
1776#ifdef CONFIG_FEATURE_HTTPD_CGI
1777 /* if strange Content-Length */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001778 if (length < 0)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001779 break;
1780
Glenn L McGrath06e95652003-02-09 06:51:14 +00001781 if (strncmp(test, "cgi-bin", 7) == 0) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001782 if(test[7] == '/' && test[8] == 0)
1783 goto FORBIDDEN; // protect listing cgi-bin/
Eric Andersen07f2fea2004-10-08 08:03:29 +00001784 sendCgi(url, prequest, length, cookie, content_type);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001785 } else {
1786 if (prequest != request_GET)
1787 sendHeaders(HTTP_NOT_IMPLEMENTED);
1788 else {
1789#endif /* CONFIG_FEATURE_HTTPD_CGI */
1790 if(purl[-1] == '/')
1791 strcpy(purl, "index.html");
1792 if ( stat(test, &sb ) == 0 ) {
1793 config->ContentLength = sb.st_size;
1794 config->last_mod = sb.st_mtime;
1795 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001796 sendFile(test);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001797#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1798 /* unset if non inetd looped */
1799 config->ContentLength = -1;
1800#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001801
Glenn L McGrath06e95652003-02-09 06:51:14 +00001802#ifdef CONFIG_FEATURE_HTTPD_CGI
1803 }
1804 }
1805#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001806
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001807 } while (0);
1808
Glenn L McGrath06e95652003-02-09 06:51:14 +00001809
1810#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1811/* from inetd don`t looping: freeing, closing automatic from exit always */
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001812# if DEBUG
1813 fprintf(stderr, "closing socket\n");
Glenn L McGrath06e95652003-02-09 06:51:14 +00001814# endif
1815# ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath06e95652003-02-09 06:51:14 +00001816 free(cookie);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001817 free(content_type);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001818 free(config->referer);
Eric Andersen769a3ef2003-12-19 11:23:47 +00001819#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1820 free(config->remoteuser);
1821#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001822# endif
1823 shutdown(a_c_w, SHUT_WR);
Eric Andersend8746cd2004-02-24 07:28:38 +00001824
1825 /* Properly wait for remote to closed */
1826 FD_ZERO (&s_fd) ;
1827 FD_SET (a_c_w, &s_fd) ;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001828
Eric Andersend8746cd2004-02-24 07:28:38 +00001829 do {
1830 tv.tv_sec = 2 ;
1831 tv.tv_usec = 0 ;
1832 retval = select (a_c_w + 1, &s_fd, NULL, NULL, &tv);
1833 } while (retval > 0 && (read (a_c_w, buf, sizeof (config->buf)) > 0));
1834
Glenn L McGrath06e95652003-02-09 06:51:14 +00001835 shutdown(a_c_r, SHUT_RD);
1836 close(config->accepted_socket);
1837#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001838}
1839
1840/****************************************************************************
1841 *
1842 > $Function: miniHttpd()
1843 *
1844 * $Description: The main http server function.
1845 *
1846 * Given an open socket fildes, listen for new connections and farm out
1847 * the processing as a forked process.
1848 *
1849 * $Parameters:
1850 * (int) server. . . The server socket fildes.
1851 *
1852 * $Return: (int) . . . . Always 0.
1853 *
1854 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001855#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001856static int miniHttpd(int server)
1857{
1858 fd_set readfd, portfd;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001859
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001860 FD_ZERO(&portfd);
1861 FD_SET(server, &portfd);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001862
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001863 /* copy the ports we are watching to the readfd set */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001864 while (1) {
1865 readfd = portfd;
1866
Eric Andersenaff114c2004-04-14 17:51:38 +00001867 /* Now wait INDEFINITELY on the set of sockets! */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001868 if (select(server + 1, &readfd, 0, 0, 0) > 0) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001869 if (FD_ISSET(server, &readfd)) {
1870 int on;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001871 struct sockaddr_in fromAddr;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001872
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001873 socklen_t fromAddrLen = sizeof(fromAddr);
1874 int s = accept(server,
Glenn L McGrath06e95652003-02-09 06:51:14 +00001875 (struct sockaddr *)&fromAddr, &fromAddrLen);
1876
1877 if (s < 0) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001878 continue;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001879 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001880 config->accepted_socket = s;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001881 config->rmt_ip = ntohl(fromAddr.sin_addr.s_addr);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001882#if ENABLE_FEATURE_HTTPD_CGI || DEBUG
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001883 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1884 (unsigned char)(config->rmt_ip >> 24),
1885 (unsigned char)(config->rmt_ip >> 16),
1886 (unsigned char)(config->rmt_ip >> 8),
1887 config->rmt_ip & 0xff);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001888 config->port = ntohs(fromAddr.sin_port);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001889#if DEBUG
1890 bb_error_msg("connection from IP=%s, port %u\n",
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001891 config->rmt_ip_str, config->port);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001892#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001893#endif /* CONFIG_FEATURE_HTTPD_CGI */
1894
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001895 /* set the KEEPALIVE option to cull dead connections */
1896 on = 1;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001897 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof (on));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001898
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001899#if !DEBUG
1900 if (fork() == 0)
1901#endif
1902 {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001903 /* This is the spawned thread */
1904#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
1905 /* protect reload config, may be confuse checking */
1906 signal(SIGHUP, SIG_IGN);
1907#endif
1908 handleIncoming();
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001909#if !DEBUG
1910 exit(0);
1911#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001912 }
1913 close(s);
1914 }
1915 }
1916 } // while (1)
1917 return 0;
1918}
1919
Glenn L McGrath06e95652003-02-09 06:51:14 +00001920#else
1921 /* from inetd */
1922
1923static int miniHttpd(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001924{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001925 struct sockaddr_in fromAddrLen;
1926 socklen_t sinlen = sizeof (struct sockaddr_in);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001927
1928 getpeername (0, (struct sockaddr *)&fromAddrLen, &sinlen);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001929 config->rmt_ip = ntohl(fromAddrLen.sin_addr.s_addr);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001930#if ENABLE_FEATURE_HTTPD_CGI
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001931 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1932 (unsigned char)(config->rmt_ip >> 24),
1933 (unsigned char)(config->rmt_ip >> 16),
1934 (unsigned char)(config->rmt_ip >> 8),
1935 config->rmt_ip & 0xff);
1936#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001937 config->port = ntohs(fromAddrLen.sin_port);
1938 handleIncoming();
1939 return 0;
1940}
1941#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
1942
1943#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
1944static void sighup_handler(int sig)
1945{
1946 /* set and reset */
1947 struct sigaction sa;
1948
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001949 parse_conf(default_path_httpd_conf,
1950 sig == SIGHUP ? SIGNALED_PARSE : FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001951 sa.sa_handler = sighup_handler;
1952 sigemptyset(&sa.sa_mask);
1953 sa.sa_flags = SA_RESTART;
1954 sigaction(SIGHUP, &sa, NULL);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001955}
1956#endif
1957
Eric Andersena3bb3e62003-06-26 09:05:32 +00001958
1959static const char httpd_opts[]="c:d:h:"
1960#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
1961 "e:"
Eric Andersena3bb3e62003-06-26 09:05:32 +00001962#endif
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001963#define OPT_INC_1 ENABLE_FEATURE_HTTPD_ENCODE_URL_STR
1964
Eric Andersena3bb3e62003-06-26 09:05:32 +00001965#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1966 "r:"
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001967#endif
1968#define OPT_INC_2 ENABLE_FEATURE_HTTPD_BASIC_AUTH
1969
1970#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
Eric Andersen35e643b2003-07-28 07:40:39 +00001971 "m:"
Eric Andersen35e643b2003-07-28 07:40:39 +00001972#endif
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001973#define OPT_INC_3 ENABLE_FEATURE_HTTPD_AUTH_MD5
1974
Eric Andersena3bb3e62003-06-26 09:05:32 +00001975#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001976 "p:"
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001977#endif
Eric Andersena3bb3e62003-06-26 09:05:32 +00001978#ifdef CONFIG_FEATURE_HTTPD_SETUID
1979 "u:"
1980#endif
Eric Andersena3bb3e62003-06-26 09:05:32 +00001981 ;
1982
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001983#define OPT_CONFIG_FILE (1<<0) /* c */
1984#define OPT_DECODE_URL (1<<1) /* d */
1985#define OPT_HOME_HTTPD (1<<2) /* h */
1986#define OPT_ENCODE_URL (1<<(2+OPT_INC_1)) /* e */
1987#define OPT_REALM (1<<(2+OPT_INC_1+OPT_INC_2)) /* r */
1988#define OPT_MD5 (1<<(2+OPT_INC_1+OPT_INC_2+OPT_INC_3)) /* m */
1989#define OPT_PORT (1<<(3+OPT_INC_1+OPT_INC_2+OPT_INC_3)) /* p */
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001990#define OPT_SETUID (1<<(4+OPT_INC_1+OPT_INC_2+OPT_INC_3)) /* u */
Eric Andersena3bb3e62003-06-26 09:05:32 +00001991
1992
Glenn L McGrath06e95652003-02-09 06:51:14 +00001993#ifdef HTTPD_STANDALONE
1994int main(int argc, char *argv[])
1995#else
1996int httpd_main(int argc, char *argv[])
1997#endif
1998{
Eric Andersena3bb3e62003-06-26 09:05:32 +00001999 unsigned long opt;
Glenn L McGrath06e95652003-02-09 06:51:14 +00002000 const char *home_httpd = home;
Eric Andersena3bb3e62003-06-26 09:05:32 +00002001 char *url_for_decode;
2002#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
2003 const char *url_for_encode;
2004#endif
2005#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2006 const char *s_port;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002007 int server;
Glenn L McGrath06e95652003-02-09 06:51:14 +00002008#endif
2009
2010#ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersena3bb3e62003-06-26 09:05:32 +00002011 const char *s_uid;
Glenn L McGrath06e95652003-02-09 06:51:14 +00002012 long uid = -1;
2013#endif
2014
Eric Andersen35e643b2003-07-28 07:40:39 +00002015#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
2016 const char *pass;
2017#endif
2018
Glenn L McGrath06e95652003-02-09 06:51:14 +00002019 config = xcalloc(1, sizeof(*config));
2020#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
2021 config->realm = "Web Server Authentication";
2022#endif
2023
2024#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2025 config->port = 80;
2026#endif
2027
2028 config->ContentLength = -1;
2029
Eric Andersena3bb3e62003-06-26 09:05:32 +00002030 opt = bb_getopt_ulflags(argc, argv, httpd_opts,
2031 &(config->configFile), &url_for_decode, &home_httpd
Glenn L McGrath06e95652003-02-09 06:51:14 +00002032#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Eric Andersena3bb3e62003-06-26 09:05:32 +00002033 , &url_for_encode
Glenn L McGrath06e95652003-02-09 06:51:14 +00002034#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002035#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Eric Andersena3bb3e62003-06-26 09:05:32 +00002036 , &(config->realm)
Eric Andersen35e643b2003-07-28 07:40:39 +00002037# ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
2038 , &pass
2039# endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00002040#endif
Eric Andersena3bb3e62003-06-26 09:05:32 +00002041#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2042 , &s_port
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00002043#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00002044#ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersena3bb3e62003-06-26 09:05:32 +00002045 , &s_uid
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002046#endif
2047 );
Eric Andersena3bb3e62003-06-26 09:05:32 +00002048
2049 if(opt & OPT_DECODE_URL) {
2050 printf("%s", decodeString(url_for_decode, 1));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002051 return 0;
Eric Andersena3bb3e62003-06-26 09:05:32 +00002052 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002053#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Eric Andersena3bb3e62003-06-26 09:05:32 +00002054 if(opt & OPT_ENCODE_URL) {
2055 printf("%s", encodeString(url_for_encode));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002056 return 0;
Eric Andersena3bb3e62003-06-26 09:05:32 +00002057 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002058#endif
Eric Andersen35e643b2003-07-28 07:40:39 +00002059#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
2060 if(opt & OPT_MD5) {
2061 printf("%s\n", pw_encrypt(pass, "$1$"));
2062 return 0;
2063 }
2064#endif
Eric Andersena3bb3e62003-06-26 09:05:32 +00002065#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2066 if(opt & OPT_PORT)
2067 config->port = bb_xgetlarg(s_port, 10, 1, 0xffff);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002068#ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersena3bb3e62003-06-26 09:05:32 +00002069 if(opt & OPT_SETUID) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00002070 char *e;
2071
Eric Andersena3bb3e62003-06-26 09:05:32 +00002072 uid = strtol(s_uid, &e, 0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002073 if(*e != '\0') {
2074 /* not integer */
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +00002075 uid = bb_xgetpwnam(s_uid);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002076 }
2077 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002078#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00002079#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002080
Glenn L McGrath06e95652003-02-09 06:51:14 +00002081 if(chdir(home_httpd)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002082 bb_perror_msg_and_die("can`t chdir to %s", home_httpd);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002083 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002084#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2085 server = openServer();
2086# ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersenaff114c2004-04-14 17:51:38 +00002087 /* drop privileges */
Glenn L McGrath06e95652003-02-09 06:51:14 +00002088 if(uid > 0)
2089 setuid(uid);
2090# endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002091#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00002092
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002093#ifdef CONFIG_FEATURE_HTTPD_CGI
2094 {
2095 char *p = getenv("PATH");
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002096 if(p) {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00002097 p = bb_xstrdup(p);
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002098 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00002099 clearenv();
2100 if(p)
2101 setenv("PATH", p, 1);
Glenn L McGrath14092a12003-09-12 00:44:50 +00002102# ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2103 addEnvPort("SERVER");
2104# endif
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002105 }
2106#endif
2107
Glenn L McGrath06e95652003-02-09 06:51:14 +00002108#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
2109 sighup_handler(0);
2110#else
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00002111 parse_conf(default_path_httpd_conf, FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002112#endif
2113
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00002114#if !ENABLE_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2115# if !DEBUG
2116 if (daemon(1, 0) < 0) /* don`t change curent directory */
Manuel Novoa III cad53642003-03-19 09:13:01 +00002117 bb_perror_msg_and_die("daemon");
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00002118# endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00002119 return miniHttpd(server);
2120#else
2121 return miniHttpd();
2122#endif
2123}