blob: df280ccf554e2f6f88cb70004cde94ed06b3b373 [file] [log] [blame]
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002/*
3 * httpd implementation for busybox
4 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00005 * Copyright (C) 2002,2003 Glenn Engel <glenne@engel.org>
"Vladimir N. Oleynik"79af7d52006-01-26 10:58:12 +00006 * Copyright (C) 2003-2006 Vladimir Oleynik <dzo@simtreas.ru>
Glenn L McGrath58c708a2003-01-05 04:01:56 +00007 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00008 * simplify patch stolen from libbb without using strdup
Glenn L McGrath58c708a2003-01-05 04:01:56 +00009 *
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +000010 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000011 *
12 *****************************************************************************
13 *
Glenn L McGrath06e95652003-02-09 06:51:14 +000014 * Typical usage:
15 * for non root user
16 * httpd -p 8080 -h $HOME/public_html
17 * or for daemon start from rc script with uid=0:
18 * httpd -u www
19 * This is equivalent if www user have uid=80 to
20 * httpd -p 80 -u 80 -h /www -c /etc/httpd.conf -r "Web Server Authentication"
21 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +000022 *
23 * When a url contains "cgi-bin" it is assumed to be a cgi script. The
24 * server changes directory to the location of the script and executes it
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +000025 * after setting QUERY_STRING and other environment variables.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000026 *
27 * The server can also be invoked as a url arg decoder and html text encoder
28 * as follows:
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000029 * foo=`httpd -d $foo` # decode "Hello%20World" as "Hello World"
Glenn L McGrathc9163fe2003-05-13 16:20:11 +000030 * bar=`httpd -e "<Hello World>"` # encode as "&#60Hello&#32World&#62"
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000031 * Note that url encoding for arguments is not the same as html encoding for
Eric Andersenaff114c2004-04-14 17:51:38 +000032 * presentation. -d decodes a url-encoded argument while -e encodes in html
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000033 * for page display.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000034 *
35 * httpd.conf has the following format:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000036 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +000037 * A:172.20. # Allow address from 172.20.0.0/16
38 * A:10.0.0.0/25 # Allow any address from 10.0.0.0-10.0.0.127
39 * A:10.0.0.0/255.255.255.128 # Allow any address that previous set
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000040 * A:127.0.0.1 # Allow local loopback connections
41 * D:* # Deny from other IP connections
42 * /cgi-bin:foo:bar # Require user foo, pwd bar on urls starting with /cgi-bin/
43 * /adm:admin:setup # Require user admin, pwd setup on urls starting with /adm/
44 * /adm:toor:PaSsWd # or user toor, pwd PaSsWd on urls starting with /adm/
45 * .au:audio/basic # additional mime type for audio.au files
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +000046 * *.php:/path/php # running cgi.php scripts through an interpreter
Eric Andersenc7bda1c2004-03-15 08:29:22 +000047 *
Eric Andersenaff114c2004-04-14 17:51:38 +000048 * A/D may be as a/d or allow/deny - first char case insensitive
Glenn L McGrath393183d2003-05-26 14:07:50 +000049 * Deny IP rules take precedence over allow rules.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000050 *
51 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000052 * The Deny/Allow IP logic:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000053 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000054 * - Default is to allow all. No addresses are denied unless
Eric Andersen97a1de12004-08-26 22:22:50 +000055 * denied with a D: rule.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000056 * - Order of Deny/Allow rules is significant
57 * - Deny rules take precedence over allow rules.
58 * - If a deny all rule (D:*) is used it acts as a catch-all for unmatched
Eric Andersen97a1de12004-08-26 22:22:50 +000059 * addresses.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000060 * - Specification of Allow all (A:*) is a no-op
Eric Andersenc7bda1c2004-03-15 08:29:22 +000061 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000062 * Example:
63 * 1. Allow only specified addresses
Glenn L McGrathb65422c2003-09-08 10:59:27 +000064 * A:172.20 # Allow any address that begins with 172.20.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000065 * A:10.10. # Allow any address that begins with 10.10.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000066 * A:127.0.0.1 # Allow local loopback connections
67 * D:* # Deny from other IP connections
Eric Andersenc7bda1c2004-03-15 08:29:22 +000068 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000069 * 2. Only deny specified addresses
70 * D:1.2.3. # deny from 1.2.3.0 - 1.2.3.255
71 * D:2.3.4. # deny from 2.3.4.0 - 2.3.4.255
72 * A:* # (optional line added for clarity)
Eric Andersenc7bda1c2004-03-15 08:29:22 +000073 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000074 * If a sub directory contains a config file it is parsed and merged with
Glenn L McGrathb65422c2003-09-08 10:59:27 +000075 * any existing settings as if it was appended to the original configuration.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000076 *
77 * subdir paths are relative to the containing subdir and thus cannot
78 * affect the parent rules.
79 *
80 * Note that since the sub dir is parsed in the forked thread servicing the
81 * subdir http request, any merge is discarded when the process exits. As a
82 * result, the subdir settings only have a lifetime of a single request.
83 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000084 *
85 * If -c is not set, an attempt will be made to open the default
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000086 * root configuration file. If -c is set and the file is not found, the
87 * server exits with an error.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000088 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000089*/
Glenn L McGrath58c708a2003-01-05 04:01:56 +000090
Glenn L McGrath06e95652003-02-09 06:51:14 +000091
Glenn L McGrath58c708a2003-01-05 04:01:56 +000092#include <stdio.h>
93#include <ctype.h> /* for isspace */
Glenn L McGrath06e95652003-02-09 06:51:14 +000094#include <string.h>
Glenn L McGrath58c708a2003-01-05 04:01:56 +000095#include <stdlib.h> /* for malloc */
96#include <time.h>
Glenn L McGrath58c708a2003-01-05 04:01:56 +000097#include <unistd.h> /* for close */
98#include <signal.h>
99#include <sys/types.h>
100#include <sys/socket.h> /* for connect and socket*/
101#include <netinet/in.h> /* for sockaddr_in */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000102#include <sys/time.h>
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000103#include <sys/stat.h>
104#include <sys/wait.h>
Glenn L McGrath06e95652003-02-09 06:51:14 +0000105#include <fcntl.h> /* for open modes */
106#include "busybox.h"
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000107
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000108
Eric Andersen07f2fea2004-10-08 08:03:29 +0000109static const char httpdVersion[] = "busybox httpd/1.35 6-Oct-2004";
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000110static const char default_path_httpd_conf[] = "/etc";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000111static const char httpd_conf[] = "httpd.conf";
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000112static const char home[] = "./";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000113
Glenn L McGrath5cd64612003-08-29 15:53:23 +0000114#ifdef CONFIG_LFS
115# define cont_l_fmt "%lld"
Eric Andersen0cb6f352006-01-30 22:30:41 +0000116# define cont_l_type (long long)
Glenn L McGrath5cd64612003-08-29 15:53:23 +0000117#else
118# define cont_l_fmt "%ld"
Eric Andersen0cb6f352006-01-30 22:30:41 +0000119# define cont_l_type (long)
Glenn L McGrath5cd64612003-08-29 15:53:23 +0000120#endif
121
Eric Andersen07f2fea2004-10-08 08:03:29 +0000122#define TIMEOUT 60
123
Eric Andersenaff114c2004-04-14 17:51:38 +0000124// Note: busybox xfuncs are not used because we want the server to keep running
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000125// if something bad happens due to a malformed user request.
Glenn L McGrath06e95652003-02-09 06:51:14 +0000126// As a result, all memory allocation after daemonize
127// is checked rigorously
128
129//#define DEBUG 1
130
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000131/* Configure options, disabled by default as custom httpd feature */
132
133/* disabled as optional features */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000134//#define CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath06e95652003-02-09 06:51:14 +0000135//#define CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
136//#define CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
137//#define CONFIG_FEATURE_HTTPD_SETUID
138//#define CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
139
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000140/* If set, use this server from internet superserver only */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000141//#define CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
142
143/* You can use this server as standalone, require libbb.a for linking */
144//#define HTTPD_STANDALONE
145
146/* Config options, disable this for do very small module */
147//#define CONFIG_FEATURE_HTTPD_CGI
148//#define CONFIG_FEATURE_HTTPD_BASIC_AUTH
Eric Andersen35e643b2003-07-28 07:40:39 +0000149//#define CONFIG_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath06e95652003-02-09 06:51:14 +0000150
151#ifdef HTTPD_STANDALONE
152/* standalone, enable all features */
153#undef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
154/* unset config option for remove warning as redefined */
155#undef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Eric Andersen35e643b2003-07-28 07:40:39 +0000156#undef CONFIG_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath06e95652003-02-09 06:51:14 +0000157#undef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath06e95652003-02-09 06:51:14 +0000158#undef CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
159#undef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
160#undef CONFIG_FEATURE_HTTPD_CGI
161#undef CONFIG_FEATURE_HTTPD_SETUID
162#undef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000163#undef ENABLE_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
164#undef ENABLE_FEATURE_HTTPD_BASIC_AUTH
165#undef ENABLE_FEATURE_HTTPD_AUTH_MD5
166#undef ENABLE_FEATURE_HTTPD_ENCODE_URL_STR
167#undef ENABLE_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
168#undef ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
169#undef ENABLE_FEATURE_HTTPD_CGI
170#undef ENABLE_FEATURE_HTTPD_SETUID
171#undef ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
Glenn L McGrath06e95652003-02-09 06:51:14 +0000172/* enable all features now */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000173#define CONFIG_FEATURE_HTTPD_BASIC_AUTH
Eric Andersen35e643b2003-07-28 07:40:39 +0000174#define CONFIG_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath06e95652003-02-09 06:51:14 +0000175#define CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath06e95652003-02-09 06:51:14 +0000176#define CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
177#define CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
178#define CONFIG_FEATURE_HTTPD_CGI
179#define CONFIG_FEATURE_HTTPD_SETUID
180#define CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000181#define ENABLE_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY 0
182#define ENABLE_FEATURE_HTTPD_BASIC_AUTH 1
183#define ENABLE_FEATURE_HTTPD_AUTH_MD5 1
184#define ENABLE_FEATURE_HTTPD_ENCODE_URL_STR 1
185#define ENABLE_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV 1
186#define ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES 1
187#define ENABLE_FEATURE_HTTPD_CGI 1
188#define ENABLE_FEATURE_HTTPD_SETUID 1
189#define ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP 1
Glenn L McGrath06e95652003-02-09 06:51:14 +0000190
191/* require from libbb.a for linking */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000192const char *bb_applet_name = "httpd";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000193
Manuel Novoa III cad53642003-03-19 09:13:01 +0000194void bb_show_usage(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000195{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000196 fprintf(stderr, "Usage: %s [-p <port>] [-c configFile] [-d/-e <string>] "
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000197 "[-r realm] [-u user] [-h homedir]\n", bb_applet_name);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000198 exit(1);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000199}
200#endif
201
Glenn L McGrath06e95652003-02-09 06:51:14 +0000202#ifdef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
203#undef CONFIG_FEATURE_HTTPD_SETUID /* use inetd user.group config settings */
204#undef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP /* so is not daemon */
205/* inetd set stderr to accepted socket and we can`t true see debug messages */
206#undef DEBUG
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000207#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000208
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000209#ifndef DEBUG
210# define DEBUG 0
211#endif
212
Glenn L McGrath06e95652003-02-09 06:51:14 +0000213#define MAX_MEMORY_BUFF 8192 /* IO buffer */
214
215typedef struct HT_ACCESS {
216 char *after_colon;
217 struct HT_ACCESS *next;
218 char before_colon[1]; /* really bigger, must last */
219} Htaccess;
220
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000221typedef struct HT_ACCESS_IP {
222 unsigned int ip;
223 unsigned int mask;
224 int allow_deny;
225 struct HT_ACCESS_IP *next;
226} Htaccess_IP;
227
Glenn L McGrath06e95652003-02-09 06:51:14 +0000228typedef struct
229{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000230 char buf[MAX_MEMORY_BUFF];
231
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +0000232 USE_FEATURE_HTTPD_BASIC_AUTH(const char *realm;)
233 USE_FEATURE_HTTPD_BASIC_AUTH(char *remoteuser;)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000234
Eric Andersen07f2fea2004-10-08 08:03:29 +0000235 const char *query;
236
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +0000237 USE_FEATURE_HTTPD_CGI(char *referer;)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000238
Glenn L McGrath06e95652003-02-09 06:51:14 +0000239 const char *configFile;
240
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000241 unsigned int rmt_ip;
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000242#if ENABLE_FEATURE_HTTPD_CGI || DEBUG
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000243 char rmt_ip_str[16]; /* for set env REMOTE_ADDR */
244#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000245 unsigned port; /* server initial port and for
246 set env REMOTE_PORT */
Eric Andersen07f2fea2004-10-08 08:03:29 +0000247 union HTTPD_FOUND {
248 const char *found_mime_type;
249 const char *found_moved_temporarily;
250 } httpd_found;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000251
Glenn L McGrath06e95652003-02-09 06:51:14 +0000252 off_t ContentLength; /* -1 - unknown */
253 time_t last_mod;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000254
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000255 Htaccess_IP *ip_a_d; /* config allow/deny lines */
Glenn L McGrath393183d2003-05-26 14:07:50 +0000256 int flg_deny_all;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000257#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
258 Htaccess *auth; /* config user:password lines */
259#endif
260#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
261 Htaccess *mime_a; /* config mime types */
262#endif
263
Glenn L McGrath06e95652003-02-09 06:51:14 +0000264#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
265 int accepted_socket;
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000266# define a_c_r config->accepted_socket
267# define a_c_w config->accepted_socket
Glenn L McGrath06e95652003-02-09 06:51:14 +0000268#else
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000269# define a_c_r 0
270# define a_c_w 1
Glenn L McGrath06e95652003-02-09 06:51:14 +0000271#endif
Eric Andersen07f2fea2004-10-08 08:03:29 +0000272 volatile int alarm_signaled;
273
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000274#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
275 Htaccess *script_i; /* config script interpreters */
276#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000277} HttpdConfig;
278
279static HttpdConfig *config;
280
281static const char request_GET[] = "GET"; /* size algorithic optimize */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000282
283static const char* const suffixTable [] = {
Eric Andersenaff114c2004-04-14 17:51:38 +0000284/* Warning: shorted equivalent suffix in one line must be first */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000285 ".htm.html", "text/html",
286 ".jpg.jpeg", "image/jpeg",
287 ".gif", "image/gif",
288 ".png", "image/png",
289 ".txt.h.c.cc.cpp", "text/plain",
Glenn L McGrath06e95652003-02-09 06:51:14 +0000290 ".css", "text/css",
291 ".wav", "audio/wav",
292 ".avi", "video/x-msvideo",
293 ".qt.mov", "video/quicktime",
294 ".mpe.mpeg", "video/mpeg",
295 ".mid.midi", "audio/midi",
296 ".mp3", "audio/mpeg",
297#if 0 /* unpopular */
298 ".au", "audio/basic",
299 ".pac", "application/x-ns-proxy-autoconfig",
300 ".vrml.wrl", "model/vrml",
301#endif
302 0, "application/octet-stream" /* default */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000303 };
304
305typedef enum
306{
307 HTTP_OK = 200,
Eric Andersen07f2fea2004-10-08 08:03:29 +0000308 HTTP_MOVED_TEMPORARILY = 302,
309 HTTP_BAD_REQUEST = 400, /* malformed syntax */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000310 HTTP_UNAUTHORIZED = 401, /* authentication needed, respond with auth hdr */
311 HTTP_NOT_FOUND = 404,
Glenn L McGrath06e95652003-02-09 06:51:14 +0000312 HTTP_FORBIDDEN = 403,
Eric Andersen07f2fea2004-10-08 08:03:29 +0000313 HTTP_REQUEST_TIMEOUT = 408,
314 HTTP_NOT_IMPLEMENTED = 501, /* used for unrecognized requests */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000315 HTTP_INTERNAL_SERVER_ERROR = 500,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000316#if 0 /* future use */
317 HTTP_CONTINUE = 100,
318 HTTP_SWITCHING_PROTOCOLS = 101,
319 HTTP_CREATED = 201,
320 HTTP_ACCEPTED = 202,
321 HTTP_NON_AUTHORITATIVE_INFO = 203,
322 HTTP_NO_CONTENT = 204,
323 HTTP_MULTIPLE_CHOICES = 300,
324 HTTP_MOVED_PERMANENTLY = 301,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000325 HTTP_NOT_MODIFIED = 304,
326 HTTP_PAYMENT_REQUIRED = 402,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000327 HTTP_BAD_GATEWAY = 502,
328 HTTP_SERVICE_UNAVAILABLE = 503, /* overload, maintenance */
329 HTTP_RESPONSE_SETSIZE=0xffffffff
330#endif
331} HttpResponseNum;
332
333typedef struct
334{
335 HttpResponseNum type;
336 const char *name;
337 const char *info;
338} HttpEnumString;
339
340static const HttpEnumString httpResponseNames[] = {
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +0000341 { HTTP_OK, "OK", NULL },
Eric Andersen07f2fea2004-10-08 08:03:29 +0000342 { HTTP_MOVED_TEMPORARILY, "Found", "Directories must end with a slash." },
343 { HTTP_REQUEST_TIMEOUT, "Request Timeout",
344 "No request appeared within a reasonable time period." },
Glenn L McGrath06e95652003-02-09 06:51:14 +0000345 { HTTP_NOT_IMPLEMENTED, "Not Implemented",
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000346 "The requested method is not recognized by this server." },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000347#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000348 { HTTP_UNAUTHORIZED, "Unauthorized", "" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000349#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000350 { HTTP_NOT_FOUND, "Not Found",
351 "The requested URL was not found on this server." },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000352 { HTTP_BAD_REQUEST, "Bad Request", "Unsupported method." },
Glenn L McGrath06e95652003-02-09 06:51:14 +0000353 { HTTP_FORBIDDEN, "Forbidden", "" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000354 { HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error",
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000355 "Internal Server Error" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000356#if 0 /* not implemented */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000357 { HTTP_CREATED, "Created" },
358 { HTTP_ACCEPTED, "Accepted" },
359 { HTTP_NO_CONTENT, "No Content" },
360 { HTTP_MULTIPLE_CHOICES, "Multiple Choices" },
361 { HTTP_MOVED_PERMANENTLY, "Moved Permanently" },
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000362 { HTTP_NOT_MODIFIED, "Not Modified" },
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000363 { HTTP_BAD_GATEWAY, "Bad Gateway", "" },
364 { HTTP_SERVICE_UNAVAILABLE, "Service Unavailable", "" },
365#endif
366};
367
Glenn L McGrath06e95652003-02-09 06:51:14 +0000368
369static const char RFC1123FMT[] = "%a, %d %b %Y %H:%M:%S GMT";
370static const char Content_length[] = "Content-length:";
371
372
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000373static int
374scan_ip (const char **ep, unsigned int *ip, unsigned char endc)
375{
376 const char *p = *ep;
377 int auto_mask = 8;
378 int j;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000379
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000380 *ip = 0;
381 for (j = 0; j < 4; j++) {
382 unsigned int octet;
383
384 if ((*p < '0' || *p > '9') && (*p != '/' || j == 0) && *p != 0)
385 return -auto_mask;
386 octet = 0;
387 while (*p >= '0' && *p <= '9') {
388 octet *= 10;
389 octet += *p - '0';
390 if (octet > 255)
391 return -auto_mask;
392 p++;
393 }
394 if (*p == '.')
395 p++;
396 if (*p != '/' && *p != 0)
397 auto_mask += 8;
398 *ip = ((*ip) << 8) | octet;
399 }
400 if (*p != 0) {
401 if (*p != endc)
402 return -auto_mask;
403 p++;
404 if(*p == 0)
405 return -auto_mask;
406 }
407 *ep = p;
408 return auto_mask;
409}
410
411static int
412scan_ip_mask (const char *ipm, unsigned int *ip, unsigned int *mask)
413{
414 int i;
415 unsigned int msk;
416
417 i = scan_ip(&ipm, ip, '/');
418 if(i < 0)
419 return i;
420 if(*ipm) {
421 const char *p = ipm;
422
423 i = 0;
424 while (*p) {
425 if (*p < '0' || *p > '9') {
426 if (*p == '.') {
427 i = scan_ip (&ipm, mask, 0);
428 return i != 32;
429 }
430 return -1;
431 }
432 i *= 10;
433 i += *p - '0';
434 p++;
435 }
436 }
437 if (i > 32 || i < 0)
438 return -1;
439 msk = 0x80000000;
440 *mask = 0;
441 while (i > 0) {
442 *mask |= msk;
443 msk >>= 1;
444 i--;
445 }
446 return 0;
447}
448
449#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000450static void free_config_lines(Htaccess **pprev)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000451{
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000452 Htaccess *prev = *pprev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000453
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000454 while( prev ) {
455 Htaccess *cur = prev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000456
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000457 prev = cur->next;
458 free(cur);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000459 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000460 *pprev = NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000461}
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000462#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000463
Glenn L McGrath06e95652003-02-09 06:51:14 +0000464/* flag */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000465#define FIRST_PARSE 0
466#define SUBDIR_PARSE 1
467#define SIGNALED_PARSE 2
468#define FIND_FROM_HTTPD_ROOT 3
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000469/****************************************************************************
470 *
471 > $Function: parse_conf()
472 *
473 * $Description: parse configuration file into in-memory linked list.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000474 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000475 * The first non-white character is examined to determine if the config line
476 * is one of the following:
477 * .ext:mime/type # new mime type not compiled into httpd
478 * [adAD]:from # ip address allow/deny, * for wildcard
479 * /path:user:pass # username/password
480 *
481 * Any previous IP rules are discarded.
482 * If the flag argument is not SUBDIR_PARSE then all /path and mime rules
483 * are also discarded. That is, previous settings are retained if flag is
484 * SUBDIR_PARSE.
485 *
486 * $Parameters:
487 * (const char *) path . . null for ip address checks, path for password
488 * checks.
489 * (int) flag . . . . . . the source of the parse request.
490 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000491 * $Return: (None)
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000492 *
493 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000494static void parse_conf(const char *path, int flag)
495{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000496 FILE *f;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000497#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000498 Htaccess *prev, *cur;
499#elif CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
500 Htaccess *cur;
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000501#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000502
Glenn L McGrath06e95652003-02-09 06:51:14 +0000503 const char *cf = config->configFile;
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000504 char buf[160];
Glenn L McGrath06e95652003-02-09 06:51:14 +0000505 char *p0 = NULL;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000506 char *c, *p;
507
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000508 /* free previous ip setup if present */
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000509 Htaccess_IP *pip = config->ip_a_d;
510
511 while( pip ) {
512 Htaccess_IP *cur_ipl = pip;
513
514 pip = cur_ipl->next;
515 free(cur_ipl);
516 }
517 config->ip_a_d = NULL;
518
Glenn L McGrath24833432003-06-10 17:22:49 +0000519 config->flg_deny_all = 0;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000520
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000521#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR)
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000522 /* retain previous auth and mime config only for subdir parse */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000523 if(flag != SUBDIR_PARSE) {
524#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000525 free_config_lines(&config->auth);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000526#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000527#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
528 free_config_lines(&config->mime_a);
529#endif
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000530#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
531 free_config_lines(&config->script_i);
532#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000533 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000534#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000535
536 if(flag == SUBDIR_PARSE || cf == NULL) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000537 cf = alloca(strlen(path) + sizeof(httpd_conf) + 2);
538 if(cf == NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000539 if(flag == FIRST_PARSE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000540 bb_error_msg_and_die(bb_msg_memory_exhausted);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000541 return;
542 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000543 sprintf((char *)cf, "%s/%s", path, httpd_conf);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000544 }
545
546 while((f = fopen(cf, "r")) == NULL) {
Eric Andersen35e643b2003-07-28 07:40:39 +0000547 if(flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) {
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000548 /* config file not found, no changes to config */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000549 return;
550 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000551 if(config->configFile && flag == FIRST_PARSE) /* if -c option given */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000552 bb_perror_msg_and_die("%s", cf);
553 flag = FIND_FROM_HTTPD_ROOT;
554 cf = httpd_conf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000555 }
556
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000557#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
558 prev = config->auth;
559#endif
560 /* This could stand some work */
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000561 while ( (p0 = fgets(buf, sizeof(buf), f)) != NULL) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000562 c = NULL;
563 for(p = p0; *p0 != 0 && *p0 != '#'; p0++) {
564 if(!isspace(*p0)) {
565 *p++ = *p0;
566 if(*p0 == ':' && c == NULL)
567 c = p;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000568 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000569 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000570 *p = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000571
572 /* test for empty or strange line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000573 if (c == NULL || *c == 0)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000574 continue;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000575 p0 = buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000576 if(*p0 == 'd')
577 *p0 = 'D';
Glenn L McGrath393183d2003-05-26 14:07:50 +0000578 if(*c == '*') {
579 if(*p0 == 'D') {
580 /* memorize deny all */
581 config->flg_deny_all++;
582 }
583 /* skip default other "word:*" config lines */
584 continue;
585 }
586
587 if(*p0 == 'a')
588 *p0 = 'A';
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000589 else if(*p0 != 'D' && *p0 != 'A'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000590#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000591 && *p0 != '/'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000592#endif
593#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000594 && *p0 != '.'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000595#endif
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000596#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
597 && *p0 != '*'
598#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000599 )
Glenn L McGrath06e95652003-02-09 06:51:14 +0000600 continue;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000601 if(*p0 == 'A' || *p0 == 'D') {
602 /* storing current config IP line */
603 pip = calloc(1, sizeof(Htaccess_IP));
604 if(pip) {
605 if(scan_ip_mask (c, &(pip->ip), &(pip->mask))) {
606 /* syntax IP{/mask} error detected, protect all */
607 *p0 = 'D';
608 pip->mask = 0;
609 }
610 pip->allow_deny = *p0;
611 if(*p0 == 'D') {
612 /* Deny:form_IP move top */
613 pip->next = config->ip_a_d;
614 config->ip_a_d = pip;
615 } else {
616 /* add to bottom A:form_IP config line */
617 Htaccess_IP *prev_IP = config->ip_a_d;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000618
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000619 if(prev_IP == NULL) {
620 config->ip_a_d = pip;
621 } else {
622 while(prev_IP->next)
623 prev_IP = prev_IP->next;
624 prev_IP->next = pip;
625 }
626 }
627 }
628 continue;
629 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000630#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
631 if(*p0 == '/') {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000632 /* make full path from httpd root / curent_path / config_line_path */
633 cf = flag == SUBDIR_PARSE ? path : "";
634 p0 = malloc(strlen(cf) + (c - buf) + 2 + strlen(c));
635 if(p0 == NULL)
636 continue;
637 c[-1] = 0;
638 sprintf(p0, "/%s%s", cf, buf);
639
640 /* another call bb_simplify_path */
641 cf = p = p0;
642
643 do {
644 if (*p == '/') {
645 if (*cf == '/') { /* skip duplicate (or initial) slash */
646 continue;
647 } else if (*cf == '.') {
648 if (cf[1] == '/' || cf[1] == 0) { /* remove extra '.' */
649 continue;
650 } else if ((cf[1] == '.') && (cf[2] == '/' || cf[2] == 0)) {
651 ++cf;
652 if (p > p0) {
653 while (*--p != '/'); /* omit previous dir */
654 }
655 continue;
656 }
657 }
658 }
659 *++p = *cf;
660 } while (*++cf);
661
662 if ((p == p0) || (*p != '/')) { /* not a trailing slash */
663 ++p; /* so keep last character */
664 }
665 *p = 0;
666 sprintf(p0, "%s:%s", p0, c);
667 }
668#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000669
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000670#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR)
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000671 /* storing current config line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000672 cur = calloc(1, sizeof(Htaccess) + strlen(p0));
673 if(cur) {
674 cf = strcpy(cur->before_colon, p0);
675 c = strchr(cf, ':');
676 *c++ = 0;
677 cur->after_colon = c;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000678#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Glenn L McGrath5875be42003-09-08 15:39:09 +0000679 if(*cf == '.') {
Glenn L McGrath874e3382003-05-14 12:11:36 +0000680 /* config .mime line move top for overwrite previous */
681 cur->next = config->mime_a;
682 config->mime_a = cur;
Glenn L McGrath5875be42003-09-08 15:39:09 +0000683 continue;
Glenn L McGrath874e3382003-05-14 12:11:36 +0000684 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000685#endif
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000686#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
687 if(*cf == '*' && cf[1] == '.') {
688 /* config script interpreter line move top for overwrite previous */
689 cur->next = config->script_i;
690 config->script_i = cur;
691 continue;
692 }
693#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000694#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath5875be42003-09-08 15:39:09 +0000695 free(p0);
696 if(prev == NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000697 /* first line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000698 config->auth = prev = cur;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000699 } else {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000700 /* sort path, if current lenght eq or bigger then move up */
701 Htaccess *prev_hti = config->auth;
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +0000702 size_t l = strlen(cf);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000703 Htaccess *hti;
704
705 for(hti = prev_hti; hti; hti = hti->next) {
706 if(l >= strlen(hti->before_colon)) {
707 /* insert before hti */
708 cur->next = hti;
709 if(prev_hti != hti) {
710 prev_hti->next = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000711 } else {
712 /* insert as top */
713 config->auth = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000714 }
Glenn L McGrath393183d2003-05-26 14:07:50 +0000715 break;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000716 }
717 if(prev_hti != hti)
718 prev_hti = prev_hti->next;
719 }
720 if(!hti) { /* not inserted, add to bottom */
721 prev->next = cur;
722 prev = cur;
723 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000724 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000725#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000726 }
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000727#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000728 }
729 fclose(f);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000730}
731
732#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000733/****************************************************************************
734 *
735 > $Function: encodeString()
736 *
737 * $Description: Given a string, html encode special characters.
738 * This is used for the -e command line option to provide an easy way
739 * for scripts to encode result data without confusing browsers. The
740 * returned string pointer is memory allocated by malloc().
741 *
742 * $Parameters:
743 * (const char *) string . . The first string to encode.
744 *
745 * $Return: (char *) . . . .. . . A pointer to the encoded string.
746 *
747 * $Errors: Returns a null string ("") if memory is not available.
748 *
749 ****************************************************************************/
750static char *encodeString(const char *string)
751{
752 /* take the simple route and encode everything */
753 /* could possibly scan once to get length. */
754 int len = strlen(string);
"Vladimir N. Oleynik"0bf67e82005-12-26 17:26:59 +0000755 char *out = malloc(len * 6 + 1);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000756 char *p=out;
757 char ch;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000758
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000759 if (!out) return "";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000760 while ((ch = *string++)) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000761 // very simple check for what to encode
762 if (isalnum(ch)) *p++ = ch;
Eric Andersen3efa51d2005-06-23 05:51:48 +0000763 else p += sprintf(p, "&#%d;", (unsigned char) ch);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000764 }
765 *p=0;
766 return out;
767}
Glenn L McGrath06e95652003-02-09 06:51:14 +0000768#endif /* CONFIG_FEATURE_HTTPD_ENCODE_URL_STR */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000769
770/****************************************************************************
771 *
772 > $Function: decodeString()
773 *
774 * $Description: Given a URL encoded string, convert it to plain ascii.
775 * Since decoding always makes strings smaller, the decode is done in-place.
776 * Thus, callers should strdup() the argument if they do not want the
777 * argument modified. The return is the original pointer, allowing this
778 * function to be easily used as arguments to other functions.
779 *
780 * $Parameters:
781 * (char *) string . . . The first string to decode.
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000782 * (int) flag . . . 1 if require decode '+' as ' ' for CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000783 *
784 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
785 *
786 * $Errors: None
787 *
788 ****************************************************************************/
Eric Andersena3bb3e62003-06-26 09:05:32 +0000789static char *decodeString(char *orig, int flag_plus_to_space)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000790{
791 /* note that decoded string is always shorter than original */
Eric Andersena3bb3e62003-06-26 09:05:32 +0000792 char *string = orig;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000793 char *ptr = string;
Eric Andersena3bb3e62003-06-26 09:05:32 +0000794
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000795 while (*ptr)
796 {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000797 if (*ptr == '+' && flag_plus_to_space) { *string++ = ' '; ptr++; }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000798 else if (*ptr != '%') *string++ = *ptr++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000799 else {
"Vladimir N. Oleynik"0bf67e82005-12-26 17:26:59 +0000800 unsigned int value1, value2;
801
802 ptr++;
803 if(sscanf(ptr, "%1X", &value1) != 1 ||
804 sscanf(ptr+1, "%1X", &value2) != 1) {
805 if(!flag_plus_to_space)
806 return NULL;
807 *string++ = '%';
808 } else {
809 value1 = value1 * 16 + value2;
810 if(value1 == '/' || value1 == 0)
811 return orig+1;
812 *string++ = value1;
813 ptr += 2;
814 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000815 }
816 }
817 *string = '\0';
818 return orig;
819}
820
821
Glenn L McGrath06e95652003-02-09 06:51:14 +0000822#ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000823/****************************************************************************
824 *
825 > $Function: addEnv()
826 *
Eric Andersenaff114c2004-04-14 17:51:38 +0000827 * $Description: Add an environment variable setting to the global list.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000828 * A NAME=VALUE string is allocated, filled, and added to the list of
829 * environment settings passed to the cgi execution script.
830 *
831 * $Parameters:
Glenn L McGrath06e95652003-02-09 06:51:14 +0000832 * (char *) name_before_underline - The first part environment variable name.
833 * (char *) name_after_underline - The second part environment variable name.
834 * (char *) value . . The value to which the env variable is set.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000835 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000836 * $Return: (void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000837 *
838 * $Errors: Silently returns if the env runs out of space to hold the new item
839 *
840 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000841static void addEnv(const char *name_before_underline,
842 const char *name_after_underline, const char *value)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000843{
Glenn L McGrath5875be42003-09-08 15:39:09 +0000844 char *s = NULL;
Glenn L McGrath393183d2003-05-26 14:07:50 +0000845 const char *underline;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000846
Glenn L McGrath06e95652003-02-09 06:51:14 +0000847 if (!value)
848 value = "";
Glenn L McGrath393183d2003-05-26 14:07:50 +0000849 underline = *name_after_underline ? "_" : "";
850 asprintf(&s, "%s%s%s=%s", name_before_underline, underline,
Glenn L McGrath06e95652003-02-09 06:51:14 +0000851 name_after_underline, value);
Glenn L McGrath393183d2003-05-26 14:07:50 +0000852 if(s) {
Glenn L McGrath5875be42003-09-08 15:39:09 +0000853 putenv(s);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000854 }
855}
856
Eric Andersena3bb3e62003-06-26 09:05:32 +0000857#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 +0000858/* set environs SERVER_PORT and REMOTE_PORT */
859static void addEnvPort(const char *port_name)
860{
861 char buf[16];
862
863 sprintf(buf, "%u", config->port);
864 addEnv(port_name, "PORT", buf);
865}
Eric Andersena3bb3e62003-06-26 09:05:32 +0000866#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000867#endif /* CONFIG_FEATURE_HTTPD_CGI */
868
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000869
870#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000871/****************************************************************************
872 *
873 > $Function: decodeBase64()
874 *
875 > $Description: Decode a base 64 data stream as per rfc1521.
876 * Note that the rfc states that none base64 chars are to be ignored.
877 * Since the decode always results in a shorter size than the input, it is
878 * OK to pass the input arg as an output arg.
879 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000880 * $Parameter:
881 * (char *) Data . . . . A pointer to a base64 encoded string.
882 * Where to place the decoded data.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000883 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000884 * $Return: void
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000885 *
886 * $Errors: None
887 *
888 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000889static void decodeBase64(char *Data)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000890{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000891
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +0000892 const unsigned char *in = (const unsigned char *)Data;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000893 // The decoded size will be at most 3/4 the size of the encoded
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000894 unsigned long ch = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000895 int i = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000896
897 while (*in) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000898 int t = *in++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000899
Glenn L McGrath874e3382003-05-14 12:11:36 +0000900 if(t >= '0' && t <= '9')
901 t = t - '0' + 52;
902 else if(t >= 'A' && t <= 'Z')
903 t = t - 'A';
904 else if(t >= 'a' && t <= 'z')
905 t = t - 'a' + 26;
906 else if(t == '+')
907 t = 62;
908 else if(t == '/')
909 t = 63;
910 else if(t == '=')
911 t = 0;
912 else
913 continue;
914
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000915 ch = (ch << 6) | t;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000916 i++;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000917 if (i == 4) {
918 *Data++ = (char) (ch >> 16);
919 *Data++ = (char) (ch >> 8);
920 *Data++ = (char) ch;
921 i = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000922 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000923 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000924 *Data = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000925}
926#endif
927
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000928
Glenn L McGrath06e95652003-02-09 06:51:14 +0000929#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000930/****************************************************************************
931 *
932 > $Function: openServer()
933 *
934 * $Description: create a listen server socket on the designated port.
935 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000936 * $Return: (int) . . . A connection socket. -1 for errors.
937 *
938 * $Errors: None
939 *
940 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000941static int openServer(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000942{
943 struct sockaddr_in lsocket;
944 int fd;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000945
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000946 /* create the socket right now */
947 /* inet_addr() returns a value that is already in network order */
948 memset(&lsocket, 0, sizeof(lsocket));
949 lsocket.sin_family = AF_INET;
950 lsocket.sin_addr.s_addr = INADDR_ANY;
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +0000951 lsocket.sin_port = htons(config->port);
952 fd = bb_xsocket(AF_INET, SOCK_STREAM, 0);
953 /* tell the OS it's OK to reuse a previous address even though */
954 /* it may still be in a close down state. Allows bind to succeed. */
955 int on = 1;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000956#ifdef SO_REUSEPORT
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +0000957 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *)&on, sizeof(on)) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000958#else
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +0000959 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000960#endif
Bernhard Reutner-Fischer67f641e2006-04-12 18:24:37 +0000961 bb_xbind(fd, (struct sockaddr *)&lsocket, sizeof(lsocket));
962 listen(fd, 9); /* bb_xlisten? */
963 signal(SIGCHLD, SIG_IGN); /* prevent zombie (defunct) processes */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000964 return fd;
965}
Glenn L McGrath06e95652003-02-09 06:51:14 +0000966#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000967
968/****************************************************************************
969 *
970 > $Function: sendHeaders()
971 *
972 * $Description: Create and send HTTP response headers.
973 * The arguments are combined and sent as one write operation. Note that
974 * IE will puke big-time if the headers are not sent in one packet and the
Glenn L McGrath06e95652003-02-09 06:51:14 +0000975 * second packet is delayed for any reason.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000976 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000977 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000978 * (HttpResponseNum) responseNum . . . The result code to send.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000979 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000980 * $Return: (int) . . . . writing errors
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000981 *
982 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000983static int sendHeaders(HttpResponseNum responseNum)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000984{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000985 char *buf = config->buf;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000986 const char *responseString = "";
987 const char *infoString = 0;
Eric Andersen07f2fea2004-10-08 08:03:29 +0000988 const char *mime_type;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000989 unsigned int i;
990 time_t timer = time(0);
991 char timeStr[80];
Glenn L McGrath06e95652003-02-09 06:51:14 +0000992 int len;
993
994 for (i = 0;
995 i < (sizeof(httpResponseNames)/sizeof(httpResponseNames[0])); i++) {
996 if (httpResponseNames[i].type == responseNum) {
997 responseString = httpResponseNames[i].name;
998 infoString = httpResponseNames[i].info;
999 break;
1000 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001001 }
Eric Andersen07f2fea2004-10-08 08:03:29 +00001002 /* error message is HTML */
1003 mime_type = responseNum == HTTP_OK ?
1004 config->httpd_found.found_mime_type : "text/html";
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001005
1006 /* emit the current date */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001007 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&timer));
1008 len = sprintf(buf,
"Vladimir N. Oleynik"2e33daa2006-01-26 10:46:14 +00001009 "HTTP/1.0 %d %s\r\nContent-type: %s\r\n"
Glenn L McGrath06e95652003-02-09 06:51:14 +00001010 "Date: %s\r\nConnection: close\r\n",
Eric Andersen07f2fea2004-10-08 08:03:29 +00001011 responseNum, responseString, mime_type, timeStr);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001012
Glenn L McGrath3d2405c2003-02-10 22:28:21 +00001013#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath06e95652003-02-09 06:51:14 +00001014 if (responseNum == HTTP_UNAUTHORIZED) {
1015 len += sprintf(buf+len, "WWW-Authenticate: Basic realm=\"%s\"\r\n",
1016 config->realm);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001017 }
Glenn L McGrath3d2405c2003-02-10 22:28:21 +00001018#endif
Eric Andersen07f2fea2004-10-08 08:03:29 +00001019 if(responseNum == HTTP_MOVED_TEMPORARILY) {
1020 len += sprintf(buf+len, "Location: %s/%s%s\r\n",
1021 config->httpd_found.found_moved_temporarily,
1022 (config->query ? "?" : ""),
1023 (config->query ? config->query : ""));
1024 }
1025
Glenn L McGrath06e95652003-02-09 06:51:14 +00001026 if (config->ContentLength != -1) { /* file */
1027 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&config->last_mod));
Glenn L McGrath5cd64612003-08-29 15:53:23 +00001028 len += sprintf(buf+len, "Last-Modified: %s\r\n%s " cont_l_fmt "\r\n",
Eric Andersen0cb6f352006-01-30 22:30:41 +00001029 timeStr, Content_length, cont_l_type config->ContentLength);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001030 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001031 strcat(buf, "\r\n");
1032 len += 2;
1033 if (infoString) {
1034 len += sprintf(buf+len,
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001035 "<HEAD><TITLE>%d %s</TITLE></HEAD>\n"
1036 "<BODY><H1>%d %s</H1>\n%s\n</BODY>\n",
1037 responseNum, responseString,
Glenn L McGrath06e95652003-02-09 06:51:14 +00001038 responseNum, responseString, infoString);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001039 }
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001040#if DEBUG
1041 fprintf(stderr, "Headers: '%s'", buf);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001042#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +00001043 return bb_full_write(a_c_w, buf, len);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001044}
1045
1046/****************************************************************************
1047 *
1048 > $Function: getLine()
1049 *
1050 * $Description: Read from the socket until an end of line char found.
1051 *
1052 * Characters are read one at a time until an eol sequence is found.
1053 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001054 * $Return: (int) . . . . number of characters read. -1 if error.
1055 *
1056 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001057static int getLine(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001058{
1059 int count = 0;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001060 char *buf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001061
1062 while (read(a_c_r, buf + count, 1) == 1) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001063 if (buf[count] == '\r') continue;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001064 if (buf[count] == '\n') {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001065 buf[count] = 0;
1066 return count;
1067 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001068 if(count < (MAX_MEMORY_BUFF-1)) /* check owerflow */
1069 count++;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001070 }
1071 if (count) return count;
1072 else return -1;
1073}
1074
Glenn L McGrath06e95652003-02-09 06:51:14 +00001075#ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001076/****************************************************************************
1077 *
1078 > $Function: sendCgi()
1079 *
1080 * $Description: Execute a CGI script and send it's stdout back
1081 *
1082 * Environment variables are set up and the script is invoked with pipes
1083 * for stdin/stdout. If a post is being done the script is fed the POST
1084 * data in addition to setting the QUERY_STRING variable (for GETs or POSTs).
1085 *
1086 * $Parameters:
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001087 * (const char *) url . . . . . . The requested URL (with leading /).
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001088 * (int bodyLen) . . . . . . . . Length of the post body.
1089 * (const char *cookie) . . . . . For set HTTP_COOKIE.
1090 * (const char *content_type) . . For set CONTENT_TYPE.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001091
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001092 *
1093 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
1094 *
1095 * $Errors: None
1096 *
1097 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001098static int sendCgi(const char *url,
Eric Andersen07f2fea2004-10-08 08:03:29 +00001099 const char *request, int bodyLen, const char *cookie,
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001100 const char *content_type)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001101{
1102 int fromCgi[2]; /* pipe for reading data from CGI */
1103 int toCgi[2]; /* pipe for sending data to CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001104
Glenn L McGrath06e95652003-02-09 06:51:14 +00001105 static char * argp[] = { 0, 0 };
1106 int pid = 0;
1107 int inFd;
1108 int outFd;
1109 int firstLine = 1;
1110
1111 do {
1112 if (pipe(fromCgi) != 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001113 break;
1114 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001115 if (pipe(toCgi) != 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001116 break;
1117 }
1118
1119 pid = fork();
Glenn L McGrath06e95652003-02-09 06:51:14 +00001120 if (pid < 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001121 pid = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001122 break;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001123 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001124
1125 if (!pid) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001126 /* child process */
1127 char *script;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001128 char *purl = strdup( url );
1129 char realpath_buff[MAXPATHLEN];
1130
1131 if(purl == NULL)
1132 _exit(242);
1133
1134 inFd = toCgi[0];
1135 outFd = fromCgi[1];
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001136
1137 dup2(inFd, 0); // replace stdin with the pipe
1138 dup2(outFd, 1); // replace stdout with the pipe
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001139 if(!DEBUG)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001140 dup2(outFd, 2); // replace stderr with the pipe
1141
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001142 close(toCgi[0]);
1143 close(toCgi[1]);
1144 close(fromCgi[0]);
1145 close(fromCgi[1]);
1146
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001147 /*
Glenn L McGrath06e95652003-02-09 06:51:14 +00001148 * Find PATH_INFO.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001149 */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001150 script = purl;
1151 while((script = strchr( script + 1, '/' )) != NULL) {
1152 /* have script.cgi/PATH_INFO or dirs/script.cgi[/PATH_INFO] */
1153 struct stat sb;
1154
1155 *script = '\0';
1156 if(is_directory(purl + 1, 1, &sb) == 0) {
1157 /* not directory, found script.cgi/PATH_INFO */
1158 *script = '/';
1159 break;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001160 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001161 *script = '/'; /* is directory, find next '/' */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001162 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001163 addEnv("PATH", "INFO", script); /* set /PATH_INFO or NULL */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001164 addEnv("PATH", "", getenv("PATH"));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001165 addEnv("REQUEST", "METHOD", request);
Eric Andersen07f2fea2004-10-08 08:03:29 +00001166 if(config->query) {
1167 char *uri = alloca(strlen(purl) + 2 + strlen(config->query));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001168 if(uri)
Eric Andersen07f2fea2004-10-08 08:03:29 +00001169 sprintf(uri, "%s?%s", purl, config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001170 addEnv("REQUEST", "URI", uri);
1171 } else {
1172 addEnv("REQUEST", "URI", purl);
1173 }
1174 if(script != NULL)
1175 *script = '\0'; /* reduce /PATH_INFO */
Rob Landley344ea472005-09-01 09:38:32 +00001176 /* SCRIPT_FILENAME required by PHP in CGI mode */
1177 if(realpath(purl + 1, realpath_buff))
"Vladimir N. Oleynik"54deebf2005-09-19 10:46:44 +00001178 addEnv("SCRIPT", "FILENAME", realpath_buff);
Rob Landley344ea472005-09-01 09:38:32 +00001179 else
"Vladimir N. Oleynik"54deebf2005-09-19 10:46:44 +00001180 *realpath_buff = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001181 /* set SCRIPT_NAME as full path: /cgi-bin/dirs/script.cgi */
1182 addEnv("SCRIPT_NAME", "", purl);
Eric Andersen07f2fea2004-10-08 08:03:29 +00001183 addEnv("QUERY_STRING", "", config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001184 addEnv("SERVER", "SOFTWARE", httpdVersion);
1185 addEnv("SERVER", "PROTOCOL", "HTTP/1.0");
1186 addEnv("GATEWAY_INTERFACE", "", "CGI/1.1");
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001187 addEnv("REMOTE", "ADDR", config->rmt_ip_str);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001188#ifdef CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
Glenn L McGrath06e95652003-02-09 06:51:14 +00001189 addEnvPort("REMOTE");
Glenn L McGrath06e95652003-02-09 06:51:14 +00001190#endif
1191 if(bodyLen) {
1192 char sbl[32];
1193
1194 sprintf(sbl, "%d", bodyLen);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001195 addEnv("CONTENT", "LENGTH", sbl);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001196 }
1197 if(cookie)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001198 addEnv("HTTP", "COOKIE", cookie);
1199 if(content_type)
1200 addEnv("CONTENT", "TYPE", content_type);
Eric Andersen769a3ef2003-12-19 11:23:47 +00001201#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001202 if(config->remoteuser) {
1203 addEnv("REMOTE", "USER", config->remoteuser);
1204 addEnv("AUTH_TYPE", "", "Basic");
Glenn L McGrath06e95652003-02-09 06:51:14 +00001205 }
Eric Andersen769a3ef2003-12-19 11:23:47 +00001206#endif
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001207 if(config->referer)
1208 addEnv("HTTP", "REFERER", config->referer);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001209
1210 /* set execve argp[0] without path */
1211 argp[0] = strrchr( purl, '/' ) + 1;
1212 /* but script argp[0] must have absolute path and chdiring to this */
Rob Landley344ea472005-09-01 09:38:32 +00001213 if(*realpath_buff) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001214 script = strrchr(realpath_buff, '/');
1215 if(script) {
1216 *script = '\0';
1217 if(chdir(realpath_buff) == 0) {
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00001218 // now run the program. If it fails,
1219 // use _exit() so no destructors
1220 // get called and make a mess.
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +00001221#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
1222 char *interpr = NULL;
1223 char *suffix = strrchr(purl, '.');
1224
1225 if(suffix) {
1226 Htaccess * cur;
1227 for (cur = config->script_i; cur; cur = cur->next)
1228 if(strcmp(cur->before_colon + 1, suffix) == 0) {
1229 interpr = cur->after_colon;
1230 break;
1231 }
1232 }
1233#endif
1234 *script = '/';
1235#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
1236 if (interpr)
1237 execv(interpr, argp);
1238 else
1239#endif
1240 execv(realpath_buff, argp);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001241 }
1242 }
1243 }
1244#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1245 config->accepted_socket = 1; /* send to stdout */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001246#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001247 sendHeaders(HTTP_NOT_FOUND);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001248 _exit(242);
1249 } /* end child */
1250
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001251 } while (0);
1252
Glenn L McGrath06e95652003-02-09 06:51:14 +00001253 if (pid) {
1254 /* parent process */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001255 int status;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001256 size_t post_readed_size = 0, post_readed_idx = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001257
1258 inFd = fromCgi[0];
1259 outFd = toCgi[1];
1260 close(fromCgi[1]);
1261 close(toCgi[0]);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001262 signal(SIGPIPE, SIG_IGN);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001263
1264 while (1) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001265 fd_set readSet;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001266 fd_set writeSet;
1267 char wbuf[128];
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001268 int nfound;
1269 int count;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001270
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001271 FD_ZERO(&readSet);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001272 FD_ZERO(&writeSet);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001273 FD_SET(inFd, &readSet);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001274 if(bodyLen > 0 || post_readed_size > 0) {
1275 FD_SET(outFd, &writeSet);
1276 nfound = outFd > inFd ? outFd : inFd;
1277 if(post_readed_size == 0) {
1278 FD_SET(a_c_r, &readSet);
1279 if(nfound < a_c_r)
1280 nfound = a_c_r;
1281 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001282 /* Now wait on the set of sockets! */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001283 nfound = select(nfound + 1, &readSet, &writeSet, 0, NULL);
1284 } else {
1285 if(!bodyLen) {
1286 close(outFd);
1287 bodyLen = -1;
1288 }
1289 nfound = select(inFd + 1, &readSet, 0, 0, NULL);
1290 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001291
Glenn L McGrath06e95652003-02-09 06:51:14 +00001292 if (nfound <= 0) {
1293 if (waitpid(pid, &status, WNOHANG) > 0) {
1294 close(inFd);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001295#if DEBUG
1296 if (WIFEXITED(status))
Manuel Novoa III cad53642003-03-19 09:13:01 +00001297 bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status));
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001298 if (WIFSIGNALED(status))
Manuel Novoa III cad53642003-03-19 09:13:01 +00001299 bb_error_msg("piped has exited with signal=%d", WTERMSIG(status));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001300#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001301 break;
1302 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001303 } else if(post_readed_size > 0 && FD_ISSET(outFd, &writeSet)) {
1304 count = bb_full_write(outFd, wbuf + post_readed_idx, post_readed_size);
1305 if(count > 0) {
1306 post_readed_size -= count;
1307 post_readed_idx += count;
1308 if(post_readed_size == 0)
1309 post_readed_idx = 0;
Paul Fox77ee5232005-07-20 18:42:52 +00001310 } else {
1311 post_readed_size = post_readed_idx = bodyLen = 0; /* broken pipe to CGI */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001312 }
1313 } else if(bodyLen > 0 && post_readed_size == 0 && FD_ISSET(a_c_r, &readSet)) {
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001314 count = bodyLen > (int)sizeof(wbuf) ? (int)sizeof(wbuf) : bodyLen;
Eric Andersen97a1de12004-08-26 22:22:50 +00001315 count = safe_read(a_c_r, wbuf, count);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001316 if(count > 0) {
1317 post_readed_size += count;
1318 bodyLen -= count;
Paul Fox77ee5232005-07-20 18:42:52 +00001319 } else {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001320 bodyLen = 0; /* closed */
1321 }
Eric Andersen97a1de12004-08-26 22:22:50 +00001322 }
1323 if(FD_ISSET(inFd, &readSet)) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001324 int s = a_c_w;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001325 char *rbuf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001326
Eric Andersen97a1de12004-08-26 22:22:50 +00001327#ifndef PIPE_BUF
1328# define PIPESIZE 4096 /* amount of buffering in a pipe */
1329#else
1330# define PIPESIZE PIPE_BUF
1331#endif
1332#if PIPESIZE >= MAX_MEMORY_BUFF
1333# error "PIPESIZE >= MAX_MEMORY_BUFF"
1334#endif
1335
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001336 // There is something to read
Eric Andersen97a1de12004-08-26 22:22:50 +00001337 count = safe_read(inFd, rbuf, PIPESIZE);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001338 if (count == 0)
1339 break; /* closed */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001340 if (count > 0) {
1341 if (firstLine) {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001342 rbuf[count] = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001343 /* check to see if the user script added headers */
"Vladimir N. Oleynik"2e33daa2006-01-26 10:46:14 +00001344 if(strncmp(rbuf, "HTTP/1.0 200 OK\r\n", 4) != 0) {
1345 bb_full_write(s, "HTTP/1.0 200 OK\r\n", 17);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001346 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001347 if (strstr(rbuf, "ontent-") == 0) {
"Vladimir N. Oleynik"2e33daa2006-01-26 10:46:14 +00001348 bb_full_write(s, "Content-type: text/plain\r\n\r\n", 28);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001349 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001350 firstLine = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001351 }
Eric Andersenef437492004-02-04 11:10:28 +00001352 if (bb_full_write(s, rbuf, count) != count)
1353 break;
1354
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001355#if DEBUG
1356 fprintf(stderr, "cgi read %d bytes\n", count);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001357#endif
1358 }
1359 }
1360 }
1361 }
1362 return 0;
1363}
Glenn L McGrath06e95652003-02-09 06:51:14 +00001364#endif /* CONFIG_FEATURE_HTTPD_CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001365
1366/****************************************************************************
1367 *
1368 > $Function: sendFile()
1369 *
1370 * $Description: Send a file response to an HTTP request
1371 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00001372 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001373 * (const char *) url . . The URL requested.
1374 *
1375 * $Return: (int) . . . . . . Always 0.
1376 *
1377 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001378static int sendFile(const char *url)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001379{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001380 char * suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001381 int f;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001382 const char * const * table;
1383 const char * try_suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001384
Glenn L McGrath06e95652003-02-09 06:51:14 +00001385 suffix = strrchr(url, '.');
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001386
Glenn L McGrath06e95652003-02-09 06:51:14 +00001387 for (table = suffixTable; *table; table += 2)
1388 if(suffix != NULL && (try_suffix = strstr(*table, suffix)) != 0) {
1389 try_suffix += strlen(suffix);
1390 if(*try_suffix == 0 || *try_suffix == '.')
1391 break;
1392 }
1393 /* also, if not found, set default as "application/octet-stream"; */
Eric Andersen07f2fea2004-10-08 08:03:29 +00001394 config->httpd_found.found_mime_type = *(table+1);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001395#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
1396 if (suffix) {
1397 Htaccess * cur;
1398
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001399 for (cur = config->mime_a; cur; cur = cur->next) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001400 if(strcmp(cur->before_colon, suffix) == 0) {
Eric Andersen07f2fea2004-10-08 08:03:29 +00001401 config->httpd_found.found_mime_type = cur->after_colon;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001402 break;
1403 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001404 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001405 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001406#endif /* CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES */
1407
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001408#if DEBUG
1409 fprintf(stderr, "Sending file '%s' Content-type: %s\n",
1410 url, config->httpd_found.found_mime_type);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001411#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001412
1413 f = open(url, O_RDONLY);
1414 if (f >= 0) {
1415 int count;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001416 char *buf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001417
1418 sendHeaders(HTTP_OK);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001419 while ((count = bb_full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
Glenn L McGrath9adcf732003-12-08 20:21:53 +00001420 if (bb_full_write(a_c_w, buf, count) != count)
1421 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001422 }
1423 close(f);
1424 } else {
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001425#if DEBUG
1426 bb_perror_msg("Unable to open '%s'", url);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001427#endif
1428 sendHeaders(HTTP_NOT_FOUND);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001429 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001430
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001431 return 0;
1432}
1433
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001434static int checkPermIP(void)
1435{
1436 Htaccess_IP * cur;
1437
1438 /* This could stand some work */
1439 for (cur = config->ip_a_d; cur; cur = cur->next) {
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001440#if DEBUG
1441 fprintf(stderr, "checkPermIP: '%s' ? ", config->rmt_ip_str);
1442 fprintf(stderr, "'%u.%u.%u.%u/%u.%u.%u.%u'\n",
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001443 (unsigned char)(cur->ip >> 24),
1444 (unsigned char)(cur->ip >> 16),
1445 (unsigned char)(cur->ip >> 8),
1446 cur->ip & 0xff,
1447 (unsigned char)(cur->mask >> 24),
1448 (unsigned char)(cur->mask >> 16),
1449 (unsigned char)(cur->mask >> 8),
1450 cur->mask & 0xff);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001451#endif
1452 if((config->rmt_ip & cur->mask) == cur->ip)
1453 return cur->allow_deny == 'A'; /* Allow/Deny */
1454 }
1455
Eric Andersenaff114c2004-04-14 17:51:38 +00001456 /* if unconfigured, return 1 - access from all */
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001457 return !config->flg_deny_all;
1458}
1459
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001460/****************************************************************************
1461 *
1462 > $Function: checkPerm()
1463 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001464 * $Description: Check the permission file for access password protected.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001465 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001466 * If config file isn't present, everything is allowed.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001467 * Entries are of the form you can see example from header source
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001468 *
1469 * $Parameters:
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001470 * (const char *) path . . . . The file path.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001471 * (const char *) request . . . User information to validate.
1472 *
1473 * $Return: (int) . . . . . . . . . 1 if request OK, 0 otherwise.
1474 *
1475 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001476
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001477#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001478static int checkPerm(const char *path, const char *request)
1479{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001480 Htaccess * cur;
1481 const char *p;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001482 const char *p0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001483
Glenn L McGrath06e95652003-02-09 06:51:14 +00001484 const char *prev = NULL;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001485
1486 /* This could stand some work */
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001487 for (cur = config->auth; cur; cur = cur->next) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001488 p0 = cur->before_colon;
1489 if(prev != NULL && strcmp(prev, p0) != 0)
1490 continue; /* find next identical */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001491 p = cur->after_colon;
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001492#if DEBUG
1493 fprintf(stderr,"checkPerm: '%s' ? '%s'\n", p0, request);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001494#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001495 {
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001496 size_t l = strlen(p0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001497
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001498 if(strncmp(p0, path, l) == 0 &&
1499 (l == 1 || path[l] == '/' || path[l] == 0)) {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001500 char *u;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001501 /* path match found. Check request */
Eric Andersen35e643b2003-07-28 07:40:39 +00001502 /* for check next /path:user:password */
1503 prev = p0;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001504 u = strchr(request, ':');
1505 if(u == NULL) {
1506 /* bad request, ':' required */
1507 break;
1508 }
1509
Eric Andersen35e643b2003-07-28 07:40:39 +00001510#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
1511 {
1512 char *cipher;
1513 char *pp;
Eric Andersen35e643b2003-07-28 07:40:39 +00001514
Eric Andersen35e643b2003-07-28 07:40:39 +00001515 if(strncmp(p, request, u-request) != 0) {
1516 /* user uncompared */
1517 continue;
1518 }
1519 pp = strchr(p, ':');
1520 if(pp && pp[1] == '$' && pp[2] == '1' &&
1521 pp[3] == '$' && pp[4]) {
1522 pp++;
1523 cipher = pw_encrypt(u+1, pp);
1524 if (strcmp(cipher, pp) == 0)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001525 goto set_remoteuser_var; /* Ok */
Eric Andersen35e643b2003-07-28 07:40:39 +00001526 /* unauthorized */
1527 continue;
1528 }
1529 }
1530#endif
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001531 if (strcmp(p, request) == 0) {
Glenn L McGrath9d1a33c2003-10-06 13:23:06 +00001532#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001533set_remoteuser_var:
Glenn L McGrath9d1a33c2003-10-06 13:23:06 +00001534#endif
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001535 config->remoteuser = strdup(request);
1536 if(config->remoteuser)
1537 config->remoteuser[(u - request)] = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001538 return 1; /* Ok */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001539 }
Eric Andersen35e643b2003-07-28 07:40:39 +00001540 /* unauthorized */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001541 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001542 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001543 } /* for */
1544
Glenn L McGrath06e95652003-02-09 06:51:14 +00001545 return prev == NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001546}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001547
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001548#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1549
Eric Andersen07f2fea2004-10-08 08:03:29 +00001550/****************************************************************************
1551 *
Mike Frysingerbb12d6f2006-01-03 23:59:01 +00001552 > $Function: handle_sigalrm()
Eric Andersen07f2fea2004-10-08 08:03:29 +00001553 *
Mike Frysingerbb12d6f2006-01-03 23:59:01 +00001554 * $Description: Handle timeouts
Eric Andersen07f2fea2004-10-08 08:03:29 +00001555 *
1556 ****************************************************************************/
1557
1558static void
1559handle_sigalrm( int sig )
1560{
1561 sendHeaders(HTTP_REQUEST_TIMEOUT);
1562 config->alarm_signaled = sig;
1563}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001564
1565/****************************************************************************
1566 *
1567 > $Function: handleIncoming()
1568 *
1569 * $Description: Handle an incoming http request.
1570 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001571 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001572static void handleIncoming(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001573{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001574 char *buf = config->buf;
1575 char *url;
1576 char *purl;
1577 int blank = -1;
Glenn L McGrath3d752f72004-03-05 09:38:16 +00001578 char *test;
1579 struct stat sb;
1580 int ip_allowed;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001581#ifdef CONFIG_FEATURE_HTTPD_CGI
1582 const char *prequest = request_GET;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001583 long length=0;
1584 char *cookie = 0;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001585 char *content_type = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001586#endif
Glenn L McGrath3d752f72004-03-05 09:38:16 +00001587 fd_set s_fd;
1588 struct timeval tv;
Eric Andersend8746cd2004-02-24 07:28:38 +00001589 int retval;
Eric Andersen07f2fea2004-10-08 08:03:29 +00001590 struct sigaction sa;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001591
1592#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1593 int credentials = -1; /* if not requred this is Ok */
1594#endif
1595
Eric Andersen07f2fea2004-10-08 08:03:29 +00001596 sa.sa_handler = handle_sigalrm;
1597 sigemptyset(&sa.sa_mask);
1598 sa.sa_flags = 0; /* no SA_RESTART */
1599 sigaction(SIGALRM, &sa, NULL);
1600
Glenn L McGrath06e95652003-02-09 06:51:14 +00001601 do {
1602 int count;
1603
Eric Andersen07f2fea2004-10-08 08:03:29 +00001604 (void) alarm( TIMEOUT );
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001605 if (getLine() <= 0)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001606 break; /* closed */
1607
1608 purl = strpbrk(buf, " \t");
1609 if(purl == NULL) {
1610BAD_REQUEST:
1611 sendHeaders(HTTP_BAD_REQUEST);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001612 break;
1613 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001614 *purl = 0;
1615#ifdef CONFIG_FEATURE_HTTPD_CGI
1616 if(strcasecmp(buf, prequest) != 0) {
1617 prequest = "POST";
1618 if(strcasecmp(buf, prequest) != 0) {
1619 sendHeaders(HTTP_NOT_IMPLEMENTED);
1620 break;
1621 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001622 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001623#else
1624 if(strcasecmp(buf, request_GET) != 0) {
1625 sendHeaders(HTTP_NOT_IMPLEMENTED);
1626 break;
1627 }
1628#endif
1629 *purl = ' ';
1630 count = sscanf(purl, " %[^ ] HTTP/%d.%*d", buf, &blank);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001631
Glenn L McGrath06e95652003-02-09 06:51:14 +00001632 if (count < 1 || buf[0] != '/') {
1633 /* Garbled request/URL */
1634 goto BAD_REQUEST;
1635 }
1636 url = alloca(strlen(buf) + 12); /* + sizeof("/index.html\0") */
1637 if(url == NULL) {
1638 sendHeaders(HTTP_INTERNAL_SERVER_ERROR);
1639 break;
1640 }
1641 strcpy(url, buf);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001642 /* extract url args if present */
Eric Andersen07f2fea2004-10-08 08:03:29 +00001643 test = strchr(url, '?');
1644 if (test) {
1645 *test++ = 0;
1646 config->query = test;
1647 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001648
"Vladimir N. Oleynik"ab90b9f2006-01-24 12:02:27 +00001649 test = decodeString(url, 0);
1650 if(test == NULL)
1651 goto BAD_REQUEST;
1652 if(test == (buf+1)) {
1653 sendHeaders(HTTP_NOT_FOUND);
1654 break;
1655 }
Manuel Novoa III cad53642003-03-19 09:13:01 +00001656 /* algorithm stolen from libbb bb_simplify_path(),
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001657 but don`t strdup and reducing trailing slash and protect out root */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001658 purl = test = url;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001659
Glenn L McGrath06e95652003-02-09 06:51:14 +00001660 do {
1661 if (*purl == '/') {
1662 if (*test == '/') { /* skip duplicate (or initial) slash */
1663 continue;
1664 } else if (*test == '.') {
1665 if (test[1] == '/' || test[1] == 0) { /* skip extra '.' */
1666 continue;
1667 } else if ((test[1] == '.') && (test[2] == '/' || test[2] == 0)) {
1668 ++test;
1669 if (purl == url) {
1670 /* protect out root */
1671 goto BAD_REQUEST;
1672 }
1673 while (*--purl != '/'); /* omit previous dir */
1674 continue;
1675 }
1676 }
1677 }
1678 *++purl = *test;
1679 } while (*++test);
1680
1681 *++purl = 0; /* so keep last character */
1682 test = purl; /* end ptr */
1683
1684 /* If URL is directory, adding '/' */
1685 if(test[-1] != '/') {
1686 if ( is_directory(url + 1, 1, &sb) ) {
Eric Andersen07f2fea2004-10-08 08:03:29 +00001687 config->httpd_found.found_moved_temporarily = url;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001688 }
1689 }
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001690#if DEBUG
1691 fprintf(stderr, "url='%s', args=%s\n", url, config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001692#endif
1693
1694 test = url;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001695 ip_allowed = checkPermIP();
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001696 while(ip_allowed && (test = strchr( test + 1, '/' )) != NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001697 /* have path1/path2 */
1698 *test = '\0';
1699 if( is_directory(url + 1, 1, &sb) ) {
1700 /* may be having subdir config */
1701 parse_conf(url + 1, SUBDIR_PARSE);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001702 ip_allowed = checkPermIP();
Glenn L McGrath06e95652003-02-09 06:51:14 +00001703 }
1704 *test = '/';
1705 }
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001706 if(blank >= 0) {
1707 // read until blank line for HTTP version specified, else parse immediate
1708 while(1) {
1709 alarm(TIMEOUT);
1710 count = getLine();
1711 if(count <= 0)
1712 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001713
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001714#if DEBUG
1715 fprintf(stderr, "Header: '%s'\n", buf);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001716#endif
1717
1718#ifdef CONFIG_FEATURE_HTTPD_CGI
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001719 /* try and do our best to parse more lines */
1720 if ((strncasecmp(buf, Content_length, 15) == 0)) {
1721 if(prequest != request_GET)
1722 length = strtol(buf + 15, 0, 0); // extra read only for POST
1723 } else if ((strncasecmp(buf, "Cookie:", 7) == 0)) {
1724 for(test = buf + 7; isspace(*test); test++)
1725 ;
1726 cookie = strdup(test);
1727 } else if ((strncasecmp(buf, "Content-Type:", 13) == 0)) {
1728 for(test = buf + 13; isspace(*test); test++)
1729 ;
1730 content_type = strdup(test);
1731 } else if ((strncasecmp(buf, "Referer:", 8) == 0)) {
1732 for(test = buf + 8; isspace(*test); test++)
1733 ;
1734 config->referer = strdup(test);
1735 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001736#endif
1737
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001738#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001739 if (strncasecmp(buf, "Authorization:", 14) == 0) {
1740 /* We only allow Basic credentials.
1741 * It shows up as "Authorization: Basic <userid:password>" where
1742 * the userid:password is base64 encoded.
1743 */
1744 for(test = buf + 14; isspace(*test); test++)
1745 ;
1746 if (strncasecmp(test, "Basic", 5) != 0)
1747 continue;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001748
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001749 test += 5; /* decodeBase64() skiping space self */
1750 decodeBase64(test);
1751 credentials = checkPerm(url, test);
1752 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001753#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1754
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001755 } /* while extra header reading */
1756 }
Eric Andersen07f2fea2004-10-08 08:03:29 +00001757 (void) alarm( 0 );
1758 if(config->alarm_signaled)
1759 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001760
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001761 if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001762 /* protect listing [/path]/httpd_conf or IP deny */
1763#ifdef CONFIG_FEATURE_HTTPD_CGI
1764FORBIDDEN: /* protect listing /cgi-bin */
1765#endif
1766 sendHeaders(HTTP_FORBIDDEN);
1767 break;
1768 }
1769
1770#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1771 if (credentials <= 0 && checkPerm(url, ":") == 0) {
1772 sendHeaders(HTTP_UNAUTHORIZED);
1773 break;
1774 }
1775#endif
1776
Eric Andersen07f2fea2004-10-08 08:03:29 +00001777 if(config->httpd_found.found_moved_temporarily) {
1778 sendHeaders(HTTP_MOVED_TEMPORARILY);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001779#if DEBUG
Eric Andersen07f2fea2004-10-08 08:03:29 +00001780 /* clear unforked memory flag */
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001781 config->httpd_found.found_moved_temporarily = NULL;
Eric Andersen07f2fea2004-10-08 08:03:29 +00001782#endif
1783 break;
1784 }
1785
Glenn L McGrath06e95652003-02-09 06:51:14 +00001786 test = url + 1; /* skip first '/' */
1787
1788#ifdef CONFIG_FEATURE_HTTPD_CGI
1789 /* if strange Content-Length */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001790 if (length < 0)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001791 break;
1792
Glenn L McGrath06e95652003-02-09 06:51:14 +00001793 if (strncmp(test, "cgi-bin", 7) == 0) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001794 if(test[7] == '/' && test[8] == 0)
1795 goto FORBIDDEN; // protect listing cgi-bin/
Eric Andersen07f2fea2004-10-08 08:03:29 +00001796 sendCgi(url, prequest, length, cookie, content_type);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001797 } else {
1798 if (prequest != request_GET)
1799 sendHeaders(HTTP_NOT_IMPLEMENTED);
1800 else {
1801#endif /* CONFIG_FEATURE_HTTPD_CGI */
1802 if(purl[-1] == '/')
1803 strcpy(purl, "index.html");
1804 if ( stat(test, &sb ) == 0 ) {
1805 config->ContentLength = sb.st_size;
1806 config->last_mod = sb.st_mtime;
1807 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001808 sendFile(test);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001809#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1810 /* unset if non inetd looped */
1811 config->ContentLength = -1;
1812#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001813
Glenn L McGrath06e95652003-02-09 06:51:14 +00001814#ifdef CONFIG_FEATURE_HTTPD_CGI
1815 }
1816 }
1817#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001818
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001819 } while (0);
1820
Glenn L McGrath06e95652003-02-09 06:51:14 +00001821
1822#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1823/* from inetd don`t looping: freeing, closing automatic from exit always */
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001824# if DEBUG
1825 fprintf(stderr, "closing socket\n");
Glenn L McGrath06e95652003-02-09 06:51:14 +00001826# endif
1827# ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath06e95652003-02-09 06:51:14 +00001828 free(cookie);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001829 free(content_type);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001830 free(config->referer);
Eric Andersen769a3ef2003-12-19 11:23:47 +00001831#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1832 free(config->remoteuser);
1833#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001834# endif
Bernhard Reutner-Fischere7f8a322006-04-11 13:51:50 +00001835#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001836 shutdown(a_c_w, SHUT_WR);
Eric Andersend8746cd2004-02-24 07:28:38 +00001837
1838 /* Properly wait for remote to closed */
1839 FD_ZERO (&s_fd) ;
Bernhard Reutner-Fischere7f8a322006-04-11 13:51:50 +00001840 FD_SET (a_c_r, &s_fd) ;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001841
Eric Andersend8746cd2004-02-24 07:28:38 +00001842 do {
1843 tv.tv_sec = 2 ;
1844 tv.tv_usec = 0 ;
Bernhard Reutner-Fischere7f8a322006-04-11 13:51:50 +00001845 retval = select (a_c_r + 1, &s_fd, NULL, NULL, &tv);
1846 } while (retval > 0 && (read (a_c_r, buf, sizeof (config->buf)) > 0));
Eric Andersend8746cd2004-02-24 07:28:38 +00001847
Glenn L McGrath06e95652003-02-09 06:51:14 +00001848 shutdown(a_c_r, SHUT_RD);
Bernhard Reutner-Fischere7f8a322006-04-11 13:51:50 +00001849#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
Glenn L McGrath06e95652003-02-09 06:51:14 +00001850 close(config->accepted_socket);
1851#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001852}
1853
1854/****************************************************************************
1855 *
1856 > $Function: miniHttpd()
1857 *
1858 * $Description: The main http server function.
1859 *
1860 * Given an open socket fildes, listen for new connections and farm out
1861 * the processing as a forked process.
1862 *
1863 * $Parameters:
1864 * (int) server. . . The server socket fildes.
1865 *
1866 * $Return: (int) . . . . Always 0.
1867 *
1868 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001869#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001870static int miniHttpd(int server)
1871{
1872 fd_set readfd, portfd;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001873
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001874 FD_ZERO(&portfd);
1875 FD_SET(server, &portfd);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001876
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001877 /* copy the ports we are watching to the readfd set */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001878 while (1) {
1879 readfd = portfd;
1880
Eric Andersenaff114c2004-04-14 17:51:38 +00001881 /* Now wait INDEFINITELY on the set of sockets! */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001882 if (select(server + 1, &readfd, 0, 0, 0) > 0) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001883 if (FD_ISSET(server, &readfd)) {
1884 int on;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001885 struct sockaddr_in fromAddr;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001886
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001887 socklen_t fromAddrLen = sizeof(fromAddr);
1888 int s = accept(server,
Glenn L McGrath06e95652003-02-09 06:51:14 +00001889 (struct sockaddr *)&fromAddr, &fromAddrLen);
1890
1891 if (s < 0) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001892 continue;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001893 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001894 config->accepted_socket = s;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001895 config->rmt_ip = ntohl(fromAddr.sin_addr.s_addr);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001896#if ENABLE_FEATURE_HTTPD_CGI || DEBUG
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001897 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1898 (unsigned char)(config->rmt_ip >> 24),
1899 (unsigned char)(config->rmt_ip >> 16),
1900 (unsigned char)(config->rmt_ip >> 8),
1901 config->rmt_ip & 0xff);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001902 config->port = ntohs(fromAddr.sin_port);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001903#if DEBUG
1904 bb_error_msg("connection from IP=%s, port %u\n",
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001905 config->rmt_ip_str, config->port);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001906#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001907#endif /* CONFIG_FEATURE_HTTPD_CGI */
1908
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001909 /* set the KEEPALIVE option to cull dead connections */
1910 on = 1;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001911 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof (on));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001912
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001913#if !DEBUG
1914 if (fork() == 0)
1915#endif
1916 {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001917 /* This is the spawned thread */
1918#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
1919 /* protect reload config, may be confuse checking */
1920 signal(SIGHUP, SIG_IGN);
1921#endif
1922 handleIncoming();
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001923#if !DEBUG
1924 exit(0);
1925#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001926 }
1927 close(s);
1928 }
1929 }
1930 } // while (1)
1931 return 0;
1932}
1933
Glenn L McGrath06e95652003-02-09 06:51:14 +00001934#else
1935 /* from inetd */
1936
1937static int miniHttpd(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001938{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001939 struct sockaddr_in fromAddrLen;
1940 socklen_t sinlen = sizeof (struct sockaddr_in);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001941
1942 getpeername (0, (struct sockaddr *)&fromAddrLen, &sinlen);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001943 config->rmt_ip = ntohl(fromAddrLen.sin_addr.s_addr);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001944#if ENABLE_FEATURE_HTTPD_CGI
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001945 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1946 (unsigned char)(config->rmt_ip >> 24),
1947 (unsigned char)(config->rmt_ip >> 16),
1948 (unsigned char)(config->rmt_ip >> 8),
1949 config->rmt_ip & 0xff);
1950#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001951 config->port = ntohs(fromAddrLen.sin_port);
1952 handleIncoming();
1953 return 0;
1954}
1955#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
1956
1957#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
1958static void sighup_handler(int sig)
1959{
1960 /* set and reset */
1961 struct sigaction sa;
1962
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001963 parse_conf(default_path_httpd_conf,
1964 sig == SIGHUP ? SIGNALED_PARSE : FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001965 sa.sa_handler = sighup_handler;
1966 sigemptyset(&sa.sa_mask);
1967 sa.sa_flags = SA_RESTART;
1968 sigaction(SIGHUP, &sa, NULL);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001969}
1970#endif
1971
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001972enum httpd_opts_nums {
1973 c_opt_config_file = 0,
1974 d_opt_decode_url,
1975 h_opt_home_httpd,
1976 USE_FEATURE_HTTPD_ENCODE_URL_STR(e_opt_encode_url,)
1977 USE_FEATURE_HTTPD_BASIC_AUTH(r_opt_realm,)
1978 USE_FEATURE_HTTPD_AUTH_MD5(m_opt_md5,)
1979 USE_FEATURE_HTTPD_SETUID(u_opt_setuid,)
Rob Landley0d8766a2006-02-20 23:05:06 +00001980 SKIP_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY(p_opt_port,)
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001981};
Eric Andersena3bb3e62003-06-26 09:05:32 +00001982
1983static const char httpd_opts[]="c:d:h:"
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001984 USE_FEATURE_HTTPD_ENCODE_URL_STR("e:")
1985 USE_FEATURE_HTTPD_BASIC_AUTH("r:")
1986 USE_FEATURE_HTTPD_AUTH_MD5("m:")
1987 USE_FEATURE_HTTPD_SETUID("u:")
Rob Landley0d8766a2006-02-20 23:05:06 +00001988 SKIP_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY("p:");
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001989
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001990#define OPT_CONFIG_FILE (1<<c_opt_config_file)
1991#define OPT_DECODE_URL (1<<d_opt_decode_url)
1992#define OPT_HOME_HTTPD (1<<h_opt_home_httpd)
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001993
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001994#define OPT_ENCODE_URL USE_FEATURE_HTTPD_ENCODE_URL_STR((1<<e_opt_encode_url)) \
Rob Landley0d8766a2006-02-20 23:05:06 +00001995 SKIP_FEATURE_HTTPD_ENCODE_URL_STR(0)
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001996
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001997#define OPT_REALM USE_FEATURE_HTTPD_BASIC_AUTH((1<<r_opt_realm)) \
Rob Landley0d8766a2006-02-20 23:05:06 +00001998 SKIP_FEATURE_HTTPD_BASIC_AUTH(0)
Eric Andersena3bb3e62003-06-26 09:05:32 +00001999
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00002000#define OPT_MD5 USE_FEATURE_HTTPD_AUTH_MD5((1<<m_opt_md5)) \
Rob Landley0d8766a2006-02-20 23:05:06 +00002001 SKIP_FEATURE_HTTPD_AUTH_MD5(0)
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00002002
2003#define OPT_SETUID USE_FEATURE_HTTPD_SETUID((1<<u_opt_setuid)) \
Rob Landley0d8766a2006-02-20 23:05:06 +00002004 SKIP_FEATURE_HTTPD_SETUID(0)
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00002005
Rob Landley0d8766a2006-02-20 23:05:06 +00002006#define OPT_PORT SKIP_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY((1<<p_opt_port)) \
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00002007 USE_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY(0)
Eric Andersena3bb3e62003-06-26 09:05:32 +00002008
2009
Glenn L McGrath06e95652003-02-09 06:51:14 +00002010#ifdef HTTPD_STANDALONE
2011int main(int argc, char *argv[])
2012#else
2013int httpd_main(int argc, char *argv[])
2014#endif
2015{
Eric Andersena3bb3e62003-06-26 09:05:32 +00002016 unsigned long opt;
Glenn L McGrath06e95652003-02-09 06:51:14 +00002017 const char *home_httpd = home;
Eric Andersena3bb3e62003-06-26 09:05:32 +00002018 char *url_for_decode;
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00002019 USE_FEATURE_HTTPD_ENCODE_URL_STR(const char *url_for_encode;)
Rob Landley0d8766a2006-02-20 23:05:06 +00002020 SKIP_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY(const char *s_port;)
2021 SKIP_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY(int server;)
Glenn L McGrath06e95652003-02-09 06:51:14 +00002022
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00002023 USE_FEATURE_HTTPD_SETUID(const char *s_uid;)
2024 USE_FEATURE_HTTPD_SETUID(long uid = -1;)
Glenn L McGrath06e95652003-02-09 06:51:14 +00002025
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00002026 USE_FEATURE_HTTPD_AUTH_MD5(const char *pass;)
Eric Andersen35e643b2003-07-28 07:40:39 +00002027
Glenn L McGrath06e95652003-02-09 06:51:14 +00002028 config = xcalloc(1, sizeof(*config));
2029#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
2030 config->realm = "Web Server Authentication";
2031#endif
2032
2033#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2034 config->port = 80;
2035#endif
2036
2037 config->ContentLength = -1;
2038
Eric Andersena3bb3e62003-06-26 09:05:32 +00002039 opt = bb_getopt_ulflags(argc, argv, httpd_opts,
2040 &(config->configFile), &url_for_decode, &home_httpd
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00002041 USE_FEATURE_HTTPD_ENCODE_URL_STR(, &url_for_encode)
2042 USE_FEATURE_HTTPD_BASIC_AUTH(, &(config->realm))
2043 USE_FEATURE_HTTPD_AUTH_MD5(, &pass)
2044 USE_FEATURE_HTTPD_SETUID(, &s_uid)
Rob Landley0d8766a2006-02-20 23:05:06 +00002045 SKIP_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY(, &s_port)
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00002046 );
Eric Andersena3bb3e62003-06-26 09:05:32 +00002047
2048 if(opt & OPT_DECODE_URL) {
2049 printf("%s", decodeString(url_for_decode, 1));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002050 return 0;
Eric Andersena3bb3e62003-06-26 09:05:32 +00002051 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002052#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Eric Andersena3bb3e62003-06-26 09:05:32 +00002053 if(opt & OPT_ENCODE_URL) {
2054 printf("%s", encodeString(url_for_encode));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002055 return 0;
Eric Andersena3bb3e62003-06-26 09:05:32 +00002056 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002057#endif
Eric Andersen35e643b2003-07-28 07:40:39 +00002058#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
2059 if(opt & OPT_MD5) {
2060 printf("%s\n", pw_encrypt(pass, "$1$"));
2061 return 0;
2062 }
2063#endif
Eric Andersena3bb3e62003-06-26 09:05:32 +00002064#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2065 if(opt & OPT_PORT)
2066 config->port = bb_xgetlarg(s_port, 10, 1, 0xffff);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002067#ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersena3bb3e62003-06-26 09:05:32 +00002068 if(opt & OPT_SETUID) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00002069 char *e;
2070
Eric Andersena3bb3e62003-06-26 09:05:32 +00002071 uid = strtol(s_uid, &e, 0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002072 if(*e != '\0') {
2073 /* not integer */
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +00002074 uid = bb_xgetpwnam(s_uid);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002075 }
2076 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002077#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00002078#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002079
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +00002080 bb_xchdir(home_httpd);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002081#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2082 server = openServer();
2083# ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersenaff114c2004-04-14 17:51:38 +00002084 /* drop privileges */
Glenn L McGrath06e95652003-02-09 06:51:14 +00002085 if(uid > 0)
2086 setuid(uid);
2087# endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002088#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00002089
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002090#ifdef CONFIG_FEATURE_HTTPD_CGI
2091 {
2092 char *p = getenv("PATH");
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002093 if(p) {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00002094 p = bb_xstrdup(p);
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002095 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00002096 clearenv();
2097 if(p)
2098 setenv("PATH", p, 1);
Glenn L McGrath14092a12003-09-12 00:44:50 +00002099# ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2100 addEnvPort("SERVER");
2101# endif
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002102 }
2103#endif
2104
Glenn L McGrath06e95652003-02-09 06:51:14 +00002105#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
2106 sighup_handler(0);
2107#else
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00002108 parse_conf(default_path_httpd_conf, FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002109#endif
2110
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00002111#if !ENABLE_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2112# if !DEBUG
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +00002113 bb_xdaemon(1, 0); /* don`t change curent directory */
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00002114# endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00002115 return miniHttpd(server);
2116#else
2117 return miniHttpd();
2118#endif
2119}