blob: c00f3e46d10cc158b6f97e5b388ece9ac2109958 [file] [log] [blame]
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001/*
2 * httpd implementation for busybox
3 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00004 * Copyright (C) 2002,2003 Glenn Engel <glenne@engel.org>
"Vladimir N. Oleynik"79af7d52006-01-26 10:58:12 +00005 * Copyright (C) 2003-2006 Vladimir Oleynik <dzo@simtreas.ru>
Glenn L McGrath58c708a2003-01-05 04:01:56 +00006 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00007 * simplify patch stolen from libbb without using strdup
Glenn L McGrath58c708a2003-01-05 04:01:56 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 *****************************************************************************
24 *
Glenn L McGrath06e95652003-02-09 06:51:14 +000025 * Typical usage:
26 * for non root user
27 * httpd -p 8080 -h $HOME/public_html
28 * or for daemon start from rc script with uid=0:
29 * httpd -u www
30 * This is equivalent if www user have uid=80 to
31 * httpd -p 80 -u 80 -h /www -c /etc/httpd.conf -r "Web Server Authentication"
32 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +000033 *
34 * When a url contains "cgi-bin" it is assumed to be a cgi script. The
35 * server changes directory to the location of the script and executes it
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +000036 * after setting QUERY_STRING and other environment variables.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000037 *
38 * The server can also be invoked as a url arg decoder and html text encoder
39 * as follows:
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000040 * foo=`httpd -d $foo` # decode "Hello%20World" as "Hello World"
Glenn L McGrathc9163fe2003-05-13 16:20:11 +000041 * bar=`httpd -e "<Hello World>"` # encode as "&#60Hello&#32World&#62"
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000042 * Note that url encoding for arguments is not the same as html encoding for
Eric Andersenaff114c2004-04-14 17:51:38 +000043 * presentation. -d decodes a url-encoded argument while -e encodes in html
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000044 * for page display.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000045 *
46 * httpd.conf has the following format:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000047 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +000048 * A:172.20. # Allow address from 172.20.0.0/16
49 * A:10.0.0.0/25 # Allow any address from 10.0.0.0-10.0.0.127
50 * A:10.0.0.0/255.255.255.128 # Allow any address that previous set
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000051 * A:127.0.0.1 # Allow local loopback connections
52 * D:* # Deny from other IP connections
53 * /cgi-bin:foo:bar # Require user foo, pwd bar on urls starting with /cgi-bin/
54 * /adm:admin:setup # Require user admin, pwd setup on urls starting with /adm/
55 * /adm:toor:PaSsWd # or user toor, pwd PaSsWd on urls starting with /adm/
56 * .au:audio/basic # additional mime type for audio.au files
Eric Andersenc7bda1c2004-03-15 08:29:22 +000057 *
Eric Andersenaff114c2004-04-14 17:51:38 +000058 * A/D may be as a/d or allow/deny - first char case insensitive
Glenn L McGrath393183d2003-05-26 14:07:50 +000059 * Deny IP rules take precedence over allow rules.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000060 *
61 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000062 * The Deny/Allow IP logic:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000063 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000064 * - Default is to allow all. No addresses are denied unless
Eric Andersen97a1de12004-08-26 22:22:50 +000065 * denied with a D: rule.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000066 * - Order of Deny/Allow rules is significant
67 * - Deny rules take precedence over allow rules.
68 * - If a deny all rule (D:*) is used it acts as a catch-all for unmatched
Eric Andersen97a1de12004-08-26 22:22:50 +000069 * addresses.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000070 * - Specification of Allow all (A:*) is a no-op
Eric Andersenc7bda1c2004-03-15 08:29:22 +000071 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000072 * Example:
73 * 1. Allow only specified addresses
Glenn L McGrathb65422c2003-09-08 10:59:27 +000074 * A:172.20 # Allow any address that begins with 172.20.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000075 * A:10.10. # Allow any address that begins with 10.10.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000076 * A:127.0.0.1 # Allow local loopback connections
77 * D:* # Deny from other IP connections
Eric Andersenc7bda1c2004-03-15 08:29:22 +000078 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000079 * 2. Only deny specified addresses
80 * D:1.2.3. # deny from 1.2.3.0 - 1.2.3.255
81 * D:2.3.4. # deny from 2.3.4.0 - 2.3.4.255
82 * A:* # (optional line added for clarity)
Eric Andersenc7bda1c2004-03-15 08:29:22 +000083 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000084 * If a sub directory contains a config file it is parsed and merged with
Glenn L McGrathb65422c2003-09-08 10:59:27 +000085 * any existing settings as if it was appended to the original configuration.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000086 *
87 * subdir paths are relative to the containing subdir and thus cannot
88 * affect the parent rules.
89 *
90 * Note that since the sub dir is parsed in the forked thread servicing the
91 * subdir http request, any merge is discarded when the process exits. As a
92 * result, the subdir settings only have a lifetime of a single request.
93 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000094 *
95 * If -c is not set, an attempt will be made to open the default
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000096 * root configuration file. If -c is set and the file is not found, the
97 * server exits with an error.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000098 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000099*/
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000100
Glenn L McGrath06e95652003-02-09 06:51:14 +0000101
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000102#include <stdio.h>
103#include <ctype.h> /* for isspace */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000104#include <string.h>
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000105#include <stdlib.h> /* for malloc */
106#include <time.h>
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000107#include <unistd.h> /* for close */
108#include <signal.h>
109#include <sys/types.h>
110#include <sys/socket.h> /* for connect and socket*/
111#include <netinet/in.h> /* for sockaddr_in */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000112#include <sys/time.h>
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000113#include <sys/stat.h>
114#include <sys/wait.h>
Glenn L McGrath06e95652003-02-09 06:51:14 +0000115#include <fcntl.h> /* for open modes */
116#include "busybox.h"
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000117
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000118
Eric Andersen07f2fea2004-10-08 08:03:29 +0000119static const char httpdVersion[] = "busybox httpd/1.35 6-Oct-2004";
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000120static const char default_path_httpd_conf[] = "/etc";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000121static const char httpd_conf[] = "httpd.conf";
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000122static const char home[] = "./";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000123
Glenn L McGrath5cd64612003-08-29 15:53:23 +0000124#ifdef CONFIG_LFS
125# define cont_l_fmt "%lld"
126#else
127# define cont_l_fmt "%ld"
128#endif
129
Eric Andersen07f2fea2004-10-08 08:03:29 +0000130#define TIMEOUT 60
131
Eric Andersenaff114c2004-04-14 17:51:38 +0000132// Note: busybox xfuncs are not used because we want the server to keep running
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000133// if something bad happens due to a malformed user request.
Glenn L McGrath06e95652003-02-09 06:51:14 +0000134// As a result, all memory allocation after daemonize
135// is checked rigorously
136
137//#define DEBUG 1
138
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000139/* Configure options, disabled by default as custom httpd feature */
140
141/* disabled as optional features */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000142//#define CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath06e95652003-02-09 06:51:14 +0000143//#define CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
144//#define CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
145//#define CONFIG_FEATURE_HTTPD_SETUID
146//#define CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
147
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000148/* If set, use this server from internet superserver only */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000149//#define CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
150
151/* You can use this server as standalone, require libbb.a for linking */
152//#define HTTPD_STANDALONE
153
154/* Config options, disable this for do very small module */
155//#define CONFIG_FEATURE_HTTPD_CGI
156//#define CONFIG_FEATURE_HTTPD_BASIC_AUTH
Eric Andersen35e643b2003-07-28 07:40:39 +0000157//#define CONFIG_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath06e95652003-02-09 06:51:14 +0000158
159#ifdef HTTPD_STANDALONE
160/* standalone, enable all features */
161#undef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
162/* unset config option for remove warning as redefined */
163#undef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Eric Andersen35e643b2003-07-28 07:40:39 +0000164#undef CONFIG_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath06e95652003-02-09 06:51:14 +0000165#undef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath06e95652003-02-09 06:51:14 +0000166#undef CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
167#undef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
168#undef CONFIG_FEATURE_HTTPD_CGI
169#undef CONFIG_FEATURE_HTTPD_SETUID
170#undef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000171#undef ENABLE_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
172#undef ENABLE_FEATURE_HTTPD_BASIC_AUTH
173#undef ENABLE_FEATURE_HTTPD_AUTH_MD5
174#undef ENABLE_FEATURE_HTTPD_ENCODE_URL_STR
175#undef ENABLE_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
176#undef ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
177#undef ENABLE_FEATURE_HTTPD_CGI
178#undef ENABLE_FEATURE_HTTPD_SETUID
179#undef ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
Glenn L McGrath06e95652003-02-09 06:51:14 +0000180/* enable all features now */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000181#define CONFIG_FEATURE_HTTPD_BASIC_AUTH
Eric Andersen35e643b2003-07-28 07:40:39 +0000182#define CONFIG_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath06e95652003-02-09 06:51:14 +0000183#define CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath06e95652003-02-09 06:51:14 +0000184#define CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
185#define CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
186#define CONFIG_FEATURE_HTTPD_CGI
187#define CONFIG_FEATURE_HTTPD_SETUID
188#define CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000189#define ENABLE_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY 0
190#define ENABLE_FEATURE_HTTPD_BASIC_AUTH 1
191#define ENABLE_FEATURE_HTTPD_AUTH_MD5 1
192#define ENABLE_FEATURE_HTTPD_ENCODE_URL_STR 1
193#define ENABLE_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV 1
194#define ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES 1
195#define ENABLE_FEATURE_HTTPD_CGI 1
196#define ENABLE_FEATURE_HTTPD_SETUID 1
197#define ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP 1
Glenn L McGrath06e95652003-02-09 06:51:14 +0000198
199/* require from libbb.a for linking */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000200const char *bb_applet_name = "httpd";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000201
Manuel Novoa III cad53642003-03-19 09:13:01 +0000202void bb_show_usage(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000203{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000204 fprintf(stderr, "Usage: %s [-p <port>] [-c configFile] [-d/-e <string>] "
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000205 "[-r realm] [-u user] [-h homedir]\n", bb_applet_name);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000206 exit(1);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000207}
208#endif
209
Glenn L McGrath06e95652003-02-09 06:51:14 +0000210#ifdef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
211#undef CONFIG_FEATURE_HTTPD_SETUID /* use inetd user.group config settings */
212#undef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP /* so is not daemon */
213/* inetd set stderr to accepted socket and we can`t true see debug messages */
214#undef DEBUG
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000215#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000216
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000217#ifndef DEBUG
218# define DEBUG 0
219#endif
220
Glenn L McGrath06e95652003-02-09 06:51:14 +0000221#define MAX_MEMORY_BUFF 8192 /* IO buffer */
222
223typedef struct HT_ACCESS {
224 char *after_colon;
225 struct HT_ACCESS *next;
226 char before_colon[1]; /* really bigger, must last */
227} Htaccess;
228
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000229typedef struct HT_ACCESS_IP {
230 unsigned int ip;
231 unsigned int mask;
232 int allow_deny;
233 struct HT_ACCESS_IP *next;
234} Htaccess_IP;
235
Glenn L McGrath06e95652003-02-09 06:51:14 +0000236typedef struct
237{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000238 char buf[MAX_MEMORY_BUFF];
239
240#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
241 const char *realm;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000242 char *remoteuser;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000243#endif
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000244
Eric Andersen07f2fea2004-10-08 08:03:29 +0000245 const char *query;
246
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000247#ifdef CONFIG_FEATURE_HTTPD_CGI
248 char *referer;
249#endif
250
Glenn L McGrath06e95652003-02-09 06:51:14 +0000251 const char *configFile;
252
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000253 unsigned int rmt_ip;
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000254#if ENABLE_FEATURE_HTTPD_CGI || DEBUG
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000255 char rmt_ip_str[16]; /* for set env REMOTE_ADDR */
256#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000257 unsigned port; /* server initial port and for
258 set env REMOTE_PORT */
Eric Andersen07f2fea2004-10-08 08:03:29 +0000259 union HTTPD_FOUND {
260 const char *found_mime_type;
261 const char *found_moved_temporarily;
262 } httpd_found;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000263
Glenn L McGrath06e95652003-02-09 06:51:14 +0000264 off_t ContentLength; /* -1 - unknown */
265 time_t last_mod;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000266
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000267 Htaccess_IP *ip_a_d; /* config allow/deny lines */
Glenn L McGrath393183d2003-05-26 14:07:50 +0000268 int flg_deny_all;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000269#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
270 Htaccess *auth; /* config user:password lines */
271#endif
272#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
273 Htaccess *mime_a; /* config mime types */
274#endif
275
Glenn L McGrath06e95652003-02-09 06:51:14 +0000276#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
277 int accepted_socket;
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000278# define a_c_r config->accepted_socket
279# define a_c_w config->accepted_socket
Glenn L McGrath06e95652003-02-09 06:51:14 +0000280#else
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000281# define a_c_r 0
282# define a_c_w 1
Glenn L McGrath06e95652003-02-09 06:51:14 +0000283#endif
Eric Andersen07f2fea2004-10-08 08:03:29 +0000284 volatile int alarm_signaled;
285
Glenn L McGrath06e95652003-02-09 06:51:14 +0000286} HttpdConfig;
287
288static HttpdConfig *config;
289
290static const char request_GET[] = "GET"; /* size algorithic optimize */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000291
292static const char* const suffixTable [] = {
Eric Andersenaff114c2004-04-14 17:51:38 +0000293/* Warning: shorted equivalent suffix in one line must be first */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000294 ".htm.html", "text/html",
295 ".jpg.jpeg", "image/jpeg",
296 ".gif", "image/gif",
297 ".png", "image/png",
298 ".txt.h.c.cc.cpp", "text/plain",
Glenn L McGrath06e95652003-02-09 06:51:14 +0000299 ".css", "text/css",
300 ".wav", "audio/wav",
301 ".avi", "video/x-msvideo",
302 ".qt.mov", "video/quicktime",
303 ".mpe.mpeg", "video/mpeg",
304 ".mid.midi", "audio/midi",
305 ".mp3", "audio/mpeg",
306#if 0 /* unpopular */
307 ".au", "audio/basic",
308 ".pac", "application/x-ns-proxy-autoconfig",
309 ".vrml.wrl", "model/vrml",
310#endif
311 0, "application/octet-stream" /* default */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000312 };
313
314typedef enum
315{
316 HTTP_OK = 200,
Eric Andersen07f2fea2004-10-08 08:03:29 +0000317 HTTP_MOVED_TEMPORARILY = 302,
318 HTTP_BAD_REQUEST = 400, /* malformed syntax */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000319 HTTP_UNAUTHORIZED = 401, /* authentication needed, respond with auth hdr */
320 HTTP_NOT_FOUND = 404,
Glenn L McGrath06e95652003-02-09 06:51:14 +0000321 HTTP_FORBIDDEN = 403,
Eric Andersen07f2fea2004-10-08 08:03:29 +0000322 HTTP_REQUEST_TIMEOUT = 408,
323 HTTP_NOT_IMPLEMENTED = 501, /* used for unrecognized requests */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000324 HTTP_INTERNAL_SERVER_ERROR = 500,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000325#if 0 /* future use */
326 HTTP_CONTINUE = 100,
327 HTTP_SWITCHING_PROTOCOLS = 101,
328 HTTP_CREATED = 201,
329 HTTP_ACCEPTED = 202,
330 HTTP_NON_AUTHORITATIVE_INFO = 203,
331 HTTP_NO_CONTENT = 204,
332 HTTP_MULTIPLE_CHOICES = 300,
333 HTTP_MOVED_PERMANENTLY = 301,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000334 HTTP_NOT_MODIFIED = 304,
335 HTTP_PAYMENT_REQUIRED = 402,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000336 HTTP_BAD_GATEWAY = 502,
337 HTTP_SERVICE_UNAVAILABLE = 503, /* overload, maintenance */
338 HTTP_RESPONSE_SETSIZE=0xffffffff
339#endif
340} HttpResponseNum;
341
342typedef struct
343{
344 HttpResponseNum type;
345 const char *name;
346 const char *info;
347} HttpEnumString;
348
349static const HttpEnumString httpResponseNames[] = {
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +0000350 { HTTP_OK, "OK", NULL },
Eric Andersen07f2fea2004-10-08 08:03:29 +0000351 { HTTP_MOVED_TEMPORARILY, "Found", "Directories must end with a slash." },
352 { HTTP_REQUEST_TIMEOUT, "Request Timeout",
353 "No request appeared within a reasonable time period." },
Glenn L McGrath06e95652003-02-09 06:51:14 +0000354 { HTTP_NOT_IMPLEMENTED, "Not Implemented",
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000355 "The requested method is not recognized by this server." },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000356#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000357 { HTTP_UNAUTHORIZED, "Unauthorized", "" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000358#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000359 { HTTP_NOT_FOUND, "Not Found",
360 "The requested URL was not found on this server." },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000361 { HTTP_BAD_REQUEST, "Bad Request", "Unsupported method." },
Glenn L McGrath06e95652003-02-09 06:51:14 +0000362 { HTTP_FORBIDDEN, "Forbidden", "" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000363 { HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error",
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000364 "Internal Server Error" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000365#if 0 /* not implemented */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000366 { HTTP_CREATED, "Created" },
367 { HTTP_ACCEPTED, "Accepted" },
368 { HTTP_NO_CONTENT, "No Content" },
369 { HTTP_MULTIPLE_CHOICES, "Multiple Choices" },
370 { HTTP_MOVED_PERMANENTLY, "Moved Permanently" },
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000371 { HTTP_NOT_MODIFIED, "Not Modified" },
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000372 { HTTP_BAD_GATEWAY, "Bad Gateway", "" },
373 { HTTP_SERVICE_UNAVAILABLE, "Service Unavailable", "" },
374#endif
375};
376
Glenn L McGrath06e95652003-02-09 06:51:14 +0000377
378static const char RFC1123FMT[] = "%a, %d %b %Y %H:%M:%S GMT";
379static const char Content_length[] = "Content-length:";
380
381
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000382static int
383scan_ip (const char **ep, unsigned int *ip, unsigned char endc)
384{
385 const char *p = *ep;
386 int auto_mask = 8;
387 int j;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000388
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000389 *ip = 0;
390 for (j = 0; j < 4; j++) {
391 unsigned int octet;
392
393 if ((*p < '0' || *p > '9') && (*p != '/' || j == 0) && *p != 0)
394 return -auto_mask;
395 octet = 0;
396 while (*p >= '0' && *p <= '9') {
397 octet *= 10;
398 octet += *p - '0';
399 if (octet > 255)
400 return -auto_mask;
401 p++;
402 }
403 if (*p == '.')
404 p++;
405 if (*p != '/' && *p != 0)
406 auto_mask += 8;
407 *ip = ((*ip) << 8) | octet;
408 }
409 if (*p != 0) {
410 if (*p != endc)
411 return -auto_mask;
412 p++;
413 if(*p == 0)
414 return -auto_mask;
415 }
416 *ep = p;
417 return auto_mask;
418}
419
420static int
421scan_ip_mask (const char *ipm, unsigned int *ip, unsigned int *mask)
422{
423 int i;
424 unsigned int msk;
425
426 i = scan_ip(&ipm, ip, '/');
427 if(i < 0)
428 return i;
429 if(*ipm) {
430 const char *p = ipm;
431
432 i = 0;
433 while (*p) {
434 if (*p < '0' || *p > '9') {
435 if (*p == '.') {
436 i = scan_ip (&ipm, mask, 0);
437 return i != 32;
438 }
439 return -1;
440 }
441 i *= 10;
442 i += *p - '0';
443 p++;
444 }
445 }
446 if (i > 32 || i < 0)
447 return -1;
448 msk = 0x80000000;
449 *mask = 0;
450 while (i > 0) {
451 *mask |= msk;
452 msk >>= 1;
453 i--;
454 }
455 return 0;
456}
457
458#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000459static void free_config_lines(Htaccess **pprev)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000460{
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000461 Htaccess *prev = *pprev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000462
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000463 while( prev ) {
464 Htaccess *cur = prev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000465
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000466 prev = cur->next;
467 free(cur);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000468 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000469 *pprev = NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000470}
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000471#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000472
Glenn L McGrath06e95652003-02-09 06:51:14 +0000473/* flag */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000474#define FIRST_PARSE 0
475#define SUBDIR_PARSE 1
476#define SIGNALED_PARSE 2
477#define FIND_FROM_HTTPD_ROOT 3
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000478/****************************************************************************
479 *
480 > $Function: parse_conf()
481 *
482 * $Description: parse configuration file into in-memory linked list.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000483 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000484 * The first non-white character is examined to determine if the config line
485 * is one of the following:
486 * .ext:mime/type # new mime type not compiled into httpd
487 * [adAD]:from # ip address allow/deny, * for wildcard
488 * /path:user:pass # username/password
489 *
490 * Any previous IP rules are discarded.
491 * If the flag argument is not SUBDIR_PARSE then all /path and mime rules
492 * are also discarded. That is, previous settings are retained if flag is
493 * SUBDIR_PARSE.
494 *
495 * $Parameters:
496 * (const char *) path . . null for ip address checks, path for password
497 * checks.
498 * (int) flag . . . . . . the source of the parse request.
499 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000500 * $Return: (None)
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000501 *
502 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000503static void parse_conf(const char *path, int flag)
504{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000505 FILE *f;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000506#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000507 Htaccess *prev, *cur;
508#elif CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
509 Htaccess *cur;
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000510#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000511
Glenn L McGrath06e95652003-02-09 06:51:14 +0000512 const char *cf = config->configFile;
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000513 char buf[160];
Glenn L McGrath06e95652003-02-09 06:51:14 +0000514 char *p0 = NULL;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000515 char *c, *p;
516
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000517 /* free previous ip setup if present */
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000518 Htaccess_IP *pip = config->ip_a_d;
519
520 while( pip ) {
521 Htaccess_IP *cur_ipl = pip;
522
523 pip = cur_ipl->next;
524 free(cur_ipl);
525 }
526 config->ip_a_d = NULL;
527
Glenn L McGrath24833432003-06-10 17:22:49 +0000528 config->flg_deny_all = 0;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000529
530#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000531 /* retain previous auth and mime config only for subdir parse */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000532 if(flag != SUBDIR_PARSE) {
533#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000534 free_config_lines(&config->auth);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000535#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000536#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
537 free_config_lines(&config->mime_a);
538#endif
539 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000540#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000541
542 if(flag == SUBDIR_PARSE || cf == NULL) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000543 cf = alloca(strlen(path) + sizeof(httpd_conf) + 2);
544 if(cf == NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000545 if(flag == FIRST_PARSE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000546 bb_error_msg_and_die(bb_msg_memory_exhausted);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000547 return;
548 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000549 sprintf((char *)cf, "%s/%s", path, httpd_conf);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000550 }
551
552 while((f = fopen(cf, "r")) == NULL) {
Eric Andersen35e643b2003-07-28 07:40:39 +0000553 if(flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) {
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000554 /* config file not found, no changes to config */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000555 return;
556 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000557 if(config->configFile && flag == FIRST_PARSE) /* if -c option given */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000558 bb_perror_msg_and_die("%s", cf);
559 flag = FIND_FROM_HTTPD_ROOT;
560 cf = httpd_conf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000561 }
562
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000563#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
564 prev = config->auth;
565#endif
566 /* This could stand some work */
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000567 while ( (p0 = fgets(buf, sizeof(buf), f)) != NULL) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000568 c = NULL;
569 for(p = p0; *p0 != 0 && *p0 != '#'; p0++) {
570 if(!isspace(*p0)) {
571 *p++ = *p0;
572 if(*p0 == ':' && c == NULL)
573 c = p;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000574 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000575 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000576 *p = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000577
578 /* test for empty or strange line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000579 if (c == NULL || *c == 0)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000580 continue;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000581 p0 = buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000582 if(*p0 == 'd')
583 *p0 = 'D';
Glenn L McGrath393183d2003-05-26 14:07:50 +0000584 if(*c == '*') {
585 if(*p0 == 'D') {
586 /* memorize deny all */
587 config->flg_deny_all++;
588 }
589 /* skip default other "word:*" config lines */
590 continue;
591 }
592
593 if(*p0 == 'a')
594 *p0 = 'A';
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000595 else if(*p0 != 'D' && *p0 != 'A'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000596#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000597 && *p0 != '/'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000598#endif
599#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000600 && *p0 != '.'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000601#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000602 )
Glenn L McGrath06e95652003-02-09 06:51:14 +0000603 continue;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000604 if(*p0 == 'A' || *p0 == 'D') {
605 /* storing current config IP line */
606 pip = calloc(1, sizeof(Htaccess_IP));
607 if(pip) {
608 if(scan_ip_mask (c, &(pip->ip), &(pip->mask))) {
609 /* syntax IP{/mask} error detected, protect all */
610 *p0 = 'D';
611 pip->mask = 0;
612 }
613 pip->allow_deny = *p0;
614 if(*p0 == 'D') {
615 /* Deny:form_IP move top */
616 pip->next = config->ip_a_d;
617 config->ip_a_d = pip;
618 } else {
619 /* add to bottom A:form_IP config line */
620 Htaccess_IP *prev_IP = config->ip_a_d;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000621
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000622 if(prev_IP == NULL) {
623 config->ip_a_d = pip;
624 } else {
625 while(prev_IP->next)
626 prev_IP = prev_IP->next;
627 prev_IP->next = pip;
628 }
629 }
630 }
631 continue;
632 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000633#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
634 if(*p0 == '/') {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000635 /* make full path from httpd root / curent_path / config_line_path */
636 cf = flag == SUBDIR_PARSE ? path : "";
637 p0 = malloc(strlen(cf) + (c - buf) + 2 + strlen(c));
638 if(p0 == NULL)
639 continue;
640 c[-1] = 0;
641 sprintf(p0, "/%s%s", cf, buf);
642
643 /* another call bb_simplify_path */
644 cf = p = p0;
645
646 do {
647 if (*p == '/') {
648 if (*cf == '/') { /* skip duplicate (or initial) slash */
649 continue;
650 } else if (*cf == '.') {
651 if (cf[1] == '/' || cf[1] == 0) { /* remove extra '.' */
652 continue;
653 } else if ((cf[1] == '.') && (cf[2] == '/' || cf[2] == 0)) {
654 ++cf;
655 if (p > p0) {
656 while (*--p != '/'); /* omit previous dir */
657 }
658 continue;
659 }
660 }
661 }
662 *++p = *cf;
663 } while (*++cf);
664
665 if ((p == p0) || (*p != '/')) { /* not a trailing slash */
666 ++p; /* so keep last character */
667 }
668 *p = 0;
669 sprintf(p0, "%s:%s", p0, c);
670 }
671#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000672
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000673#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
674 /* storing current config line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000675 cur = calloc(1, sizeof(Htaccess) + strlen(p0));
676 if(cur) {
677 cf = strcpy(cur->before_colon, p0);
678 c = strchr(cf, ':');
679 *c++ = 0;
680 cur->after_colon = c;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000681#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Glenn L McGrath5875be42003-09-08 15:39:09 +0000682 if(*cf == '.') {
Glenn L McGrath874e3382003-05-14 12:11:36 +0000683 /* config .mime line move top for overwrite previous */
684 cur->next = config->mime_a;
685 config->mime_a = cur;
Glenn L McGrath5875be42003-09-08 15:39:09 +0000686 continue;
Glenn L McGrath874e3382003-05-14 12:11:36 +0000687 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000688#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000689#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath5875be42003-09-08 15:39:09 +0000690 free(p0);
691 if(prev == NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000692 /* first line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000693 config->auth = prev = cur;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000694 } else {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000695 /* sort path, if current lenght eq or bigger then move up */
696 Htaccess *prev_hti = config->auth;
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +0000697 size_t l = strlen(cf);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000698 Htaccess *hti;
699
700 for(hti = prev_hti; hti; hti = hti->next) {
701 if(l >= strlen(hti->before_colon)) {
702 /* insert before hti */
703 cur->next = hti;
704 if(prev_hti != hti) {
705 prev_hti->next = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000706 } else {
707 /* insert as top */
708 config->auth = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000709 }
Glenn L McGrath393183d2003-05-26 14:07:50 +0000710 break;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000711 }
712 if(prev_hti != hti)
713 prev_hti = prev_hti->next;
714 }
715 if(!hti) { /* not inserted, add to bottom */
716 prev->next = cur;
717 prev = cur;
718 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000719 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000720#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000721 }
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000722#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000723 }
724 fclose(f);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000725}
726
727#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000728/****************************************************************************
729 *
730 > $Function: encodeString()
731 *
732 * $Description: Given a string, html encode special characters.
733 * This is used for the -e command line option to provide an easy way
734 * for scripts to encode result data without confusing browsers. The
735 * returned string pointer is memory allocated by malloc().
736 *
737 * $Parameters:
738 * (const char *) string . . The first string to encode.
739 *
740 * $Return: (char *) . . . .. . . A pointer to the encoded string.
741 *
742 * $Errors: Returns a null string ("") if memory is not available.
743 *
744 ****************************************************************************/
745static char *encodeString(const char *string)
746{
747 /* take the simple route and encode everything */
748 /* could possibly scan once to get length. */
749 int len = strlen(string);
"Vladimir N. Oleynik"0bf67e82005-12-26 17:26:59 +0000750 char *out = malloc(len * 6 + 1);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000751 char *p=out;
752 char ch;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000753
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000754 if (!out) return "";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000755 while ((ch = *string++)) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000756 // very simple check for what to encode
757 if (isalnum(ch)) *p++ = ch;
Eric Andersen3efa51d2005-06-23 05:51:48 +0000758 else p += sprintf(p, "&#%d;", (unsigned char) ch);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000759 }
760 *p=0;
761 return out;
762}
Glenn L McGrath06e95652003-02-09 06:51:14 +0000763#endif /* CONFIG_FEATURE_HTTPD_ENCODE_URL_STR */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000764
765/****************************************************************************
766 *
767 > $Function: decodeString()
768 *
769 * $Description: Given a URL encoded string, convert it to plain ascii.
770 * Since decoding always makes strings smaller, the decode is done in-place.
771 * Thus, callers should strdup() the argument if they do not want the
772 * argument modified. The return is the original pointer, allowing this
773 * function to be easily used as arguments to other functions.
774 *
775 * $Parameters:
776 * (char *) string . . . The first string to decode.
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000777 * (int) flag . . . 1 if require decode '+' as ' ' for CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000778 *
779 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
780 *
781 * $Errors: None
782 *
783 ****************************************************************************/
Eric Andersena3bb3e62003-06-26 09:05:32 +0000784static char *decodeString(char *orig, int flag_plus_to_space)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000785{
786 /* note that decoded string is always shorter than original */
Eric Andersena3bb3e62003-06-26 09:05:32 +0000787 char *string = orig;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000788 char *ptr = string;
Eric Andersena3bb3e62003-06-26 09:05:32 +0000789
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000790 while (*ptr)
791 {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000792 if (*ptr == '+' && flag_plus_to_space) { *string++ = ' '; ptr++; }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000793 else if (*ptr != '%') *string++ = *ptr++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000794 else {
"Vladimir N. Oleynik"0bf67e82005-12-26 17:26:59 +0000795 unsigned int value1, value2;
796
797 ptr++;
798 if(sscanf(ptr, "%1X", &value1) != 1 ||
799 sscanf(ptr+1, "%1X", &value2) != 1) {
800 if(!flag_plus_to_space)
801 return NULL;
802 *string++ = '%';
803 } else {
804 value1 = value1 * 16 + value2;
805 if(value1 == '/' || value1 == 0)
806 return orig+1;
807 *string++ = value1;
808 ptr += 2;
809 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000810 }
811 }
812 *string = '\0';
813 return orig;
814}
815
816
Glenn L McGrath06e95652003-02-09 06:51:14 +0000817#ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000818/****************************************************************************
819 *
820 > $Function: addEnv()
821 *
Eric Andersenaff114c2004-04-14 17:51:38 +0000822 * $Description: Add an environment variable setting to the global list.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000823 * A NAME=VALUE string is allocated, filled, and added to the list of
824 * environment settings passed to the cgi execution script.
825 *
826 * $Parameters:
Glenn L McGrath06e95652003-02-09 06:51:14 +0000827 * (char *) name_before_underline - The first part environment variable name.
828 * (char *) name_after_underline - The second part environment variable name.
829 * (char *) value . . The value to which the env variable is set.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000830 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000831 * $Return: (void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000832 *
833 * $Errors: Silently returns if the env runs out of space to hold the new item
834 *
835 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000836static void addEnv(const char *name_before_underline,
837 const char *name_after_underline, const char *value)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000838{
Glenn L McGrath5875be42003-09-08 15:39:09 +0000839 char *s = NULL;
Glenn L McGrath393183d2003-05-26 14:07:50 +0000840 const char *underline;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000841
Glenn L McGrath06e95652003-02-09 06:51:14 +0000842 if (!value)
843 value = "";
Glenn L McGrath393183d2003-05-26 14:07:50 +0000844 underline = *name_after_underline ? "_" : "";
845 asprintf(&s, "%s%s%s=%s", name_before_underline, underline,
Glenn L McGrath06e95652003-02-09 06:51:14 +0000846 name_after_underline, value);
Glenn L McGrath393183d2003-05-26 14:07:50 +0000847 if(s) {
Glenn L McGrath5875be42003-09-08 15:39:09 +0000848 putenv(s);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000849 }
850}
851
Eric Andersena3bb3e62003-06-26 09:05:32 +0000852#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 +0000853/* set environs SERVER_PORT and REMOTE_PORT */
854static void addEnvPort(const char *port_name)
855{
856 char buf[16];
857
858 sprintf(buf, "%u", config->port);
859 addEnv(port_name, "PORT", buf);
860}
Eric Andersena3bb3e62003-06-26 09:05:32 +0000861#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000862#endif /* CONFIG_FEATURE_HTTPD_CGI */
863
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000864
865#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000866/****************************************************************************
867 *
868 > $Function: decodeBase64()
869 *
870 > $Description: Decode a base 64 data stream as per rfc1521.
871 * Note that the rfc states that none base64 chars are to be ignored.
872 * Since the decode always results in a shorter size than the input, it is
873 * OK to pass the input arg as an output arg.
874 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000875 * $Parameter:
876 * (char *) Data . . . . A pointer to a base64 encoded string.
877 * Where to place the decoded data.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000878 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000879 * $Return: void
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000880 *
881 * $Errors: None
882 *
883 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000884static void decodeBase64(char *Data)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000885{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000886
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +0000887 const unsigned char *in = (const unsigned char *)Data;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000888 // The decoded size will be at most 3/4 the size of the encoded
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000889 unsigned long ch = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000890 int i = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000891
892 while (*in) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000893 int t = *in++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000894
Glenn L McGrath874e3382003-05-14 12:11:36 +0000895 if(t >= '0' && t <= '9')
896 t = t - '0' + 52;
897 else if(t >= 'A' && t <= 'Z')
898 t = t - 'A';
899 else if(t >= 'a' && t <= 'z')
900 t = t - 'a' + 26;
901 else if(t == '+')
902 t = 62;
903 else if(t == '/')
904 t = 63;
905 else if(t == '=')
906 t = 0;
907 else
908 continue;
909
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000910 ch = (ch << 6) | t;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000911 i++;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000912 if (i == 4) {
913 *Data++ = (char) (ch >> 16);
914 *Data++ = (char) (ch >> 8);
915 *Data++ = (char) ch;
916 i = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000917 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000918 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000919 *Data = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000920}
921#endif
922
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000923
Glenn L McGrath06e95652003-02-09 06:51:14 +0000924#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000925/****************************************************************************
926 *
927 > $Function: openServer()
928 *
929 * $Description: create a listen server socket on the designated port.
930 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000931 * $Return: (int) . . . A connection socket. -1 for errors.
932 *
933 * $Errors: None
934 *
935 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000936static int openServer(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000937{
938 struct sockaddr_in lsocket;
939 int fd;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000940
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000941 /* create the socket right now */
942 /* inet_addr() returns a value that is already in network order */
943 memset(&lsocket, 0, sizeof(lsocket));
944 lsocket.sin_family = AF_INET;
945 lsocket.sin_addr.s_addr = INADDR_ANY;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000946 lsocket.sin_port = htons(config->port) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000947 fd = socket(AF_INET, SOCK_STREAM, 0);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000948 if (fd >= 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000949 /* tell the OS it's OK to reuse a previous address even though */
950 /* it may still be in a close down state. Allows bind to succeed. */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000951 int on = 1;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000952#ifdef SO_REUSEPORT
Glenn L McGrath06e95652003-02-09 06:51:14 +0000953 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *)&on, sizeof(on)) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000954#else
Glenn L McGrath06e95652003-02-09 06:51:14 +0000955 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000956#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000957 if (bind(fd, (struct sockaddr *)&lsocket, sizeof(lsocket)) == 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000958 listen(fd, 9);
959 signal(SIGCHLD, SIG_IGN); /* prevent zombie (defunct) processes */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000960 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000961 bb_perror_msg_and_die("bind");
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000962 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000963 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000964 bb_perror_msg_and_die("create socket");
Glenn L McGrath06e95652003-02-09 06:51:14 +0000965 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000966 return fd;
967}
Glenn L McGrath06e95652003-02-09 06:51:14 +0000968#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000969
970/****************************************************************************
971 *
972 > $Function: sendHeaders()
973 *
974 * $Description: Create and send HTTP response headers.
975 * The arguments are combined and sent as one write operation. Note that
976 * IE will puke big-time if the headers are not sent in one packet and the
Glenn L McGrath06e95652003-02-09 06:51:14 +0000977 * second packet is delayed for any reason.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000978 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000979 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000980 * (HttpResponseNum) responseNum . . . The result code to send.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000981 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000982 * $Return: (int) . . . . writing errors
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000983 *
984 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000985static int sendHeaders(HttpResponseNum responseNum)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000986{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000987 char *buf = config->buf;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000988 const char *responseString = "";
989 const char *infoString = 0;
Eric Andersen07f2fea2004-10-08 08:03:29 +0000990 const char *mime_type;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000991 unsigned int i;
992 time_t timer = time(0);
993 char timeStr[80];
Glenn L McGrath06e95652003-02-09 06:51:14 +0000994 int len;
995
996 for (i = 0;
997 i < (sizeof(httpResponseNames)/sizeof(httpResponseNames[0])); i++) {
998 if (httpResponseNames[i].type == responseNum) {
999 responseString = httpResponseNames[i].name;
1000 infoString = httpResponseNames[i].info;
1001 break;
1002 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001003 }
Eric Andersen07f2fea2004-10-08 08:03:29 +00001004 /* error message is HTML */
1005 mime_type = responseNum == HTTP_OK ?
1006 config->httpd_found.found_mime_type : "text/html";
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001007
1008 /* emit the current date */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001009 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&timer));
1010 len = sprintf(buf,
"Vladimir N. Oleynik"2e33daa2006-01-26 10:46:14 +00001011 "HTTP/1.0 %d %s\r\nContent-type: %s\r\n"
Glenn L McGrath06e95652003-02-09 06:51:14 +00001012 "Date: %s\r\nConnection: close\r\n",
Eric Andersen07f2fea2004-10-08 08:03:29 +00001013 responseNum, responseString, mime_type, timeStr);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001014
Glenn L McGrath3d2405c2003-02-10 22:28:21 +00001015#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath06e95652003-02-09 06:51:14 +00001016 if (responseNum == HTTP_UNAUTHORIZED) {
1017 len += sprintf(buf+len, "WWW-Authenticate: Basic realm=\"%s\"\r\n",
1018 config->realm);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001019 }
Glenn L McGrath3d2405c2003-02-10 22:28:21 +00001020#endif
Eric Andersen07f2fea2004-10-08 08:03:29 +00001021 if(responseNum == HTTP_MOVED_TEMPORARILY) {
1022 len += sprintf(buf+len, "Location: %s/%s%s\r\n",
1023 config->httpd_found.found_moved_temporarily,
1024 (config->query ? "?" : ""),
1025 (config->query ? config->query : ""));
1026 }
1027
Glenn L McGrath06e95652003-02-09 06:51:14 +00001028 if (config->ContentLength != -1) { /* file */
1029 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&config->last_mod));
Glenn L McGrath5cd64612003-08-29 15:53:23 +00001030 len += sprintf(buf+len, "Last-Modified: %s\r\n%s " cont_l_fmt "\r\n",
Glenn L McGrath06e95652003-02-09 06:51:14 +00001031 timeStr, Content_length, config->ContentLength);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001032 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001033 strcat(buf, "\r\n");
1034 len += 2;
1035 if (infoString) {
1036 len += sprintf(buf+len,
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001037 "<HEAD><TITLE>%d %s</TITLE></HEAD>\n"
1038 "<BODY><H1>%d %s</H1>\n%s\n</BODY>\n",
1039 responseNum, responseString,
Glenn L McGrath06e95652003-02-09 06:51:14 +00001040 responseNum, responseString, infoString);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001041 }
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001042#if DEBUG
1043 fprintf(stderr, "Headers: '%s'", buf);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001044#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +00001045 return bb_full_write(a_c_w, buf, len);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001046}
1047
1048/****************************************************************************
1049 *
1050 > $Function: getLine()
1051 *
1052 * $Description: Read from the socket until an end of line char found.
1053 *
1054 * Characters are read one at a time until an eol sequence is found.
1055 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001056 * $Return: (int) . . . . number of characters read. -1 if error.
1057 *
1058 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001059static int getLine(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001060{
1061 int count = 0;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001062 char *buf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001063
1064 while (read(a_c_r, buf + count, 1) == 1) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001065 if (buf[count] == '\r') continue;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001066 if (buf[count] == '\n') {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001067 buf[count] = 0;
1068 return count;
1069 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001070 if(count < (MAX_MEMORY_BUFF-1)) /* check owerflow */
1071 count++;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001072 }
1073 if (count) return count;
1074 else return -1;
1075}
1076
Glenn L McGrath06e95652003-02-09 06:51:14 +00001077#ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001078/****************************************************************************
1079 *
1080 > $Function: sendCgi()
1081 *
1082 * $Description: Execute a CGI script and send it's stdout back
1083 *
1084 * Environment variables are set up and the script is invoked with pipes
1085 * for stdin/stdout. If a post is being done the script is fed the POST
1086 * data in addition to setting the QUERY_STRING variable (for GETs or POSTs).
1087 *
1088 * $Parameters:
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001089 * (const char *) url . . . . . . The requested URL (with leading /).
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001090 * (int bodyLen) . . . . . . . . Length of the post body.
1091 * (const char *cookie) . . . . . For set HTTP_COOKIE.
1092 * (const char *content_type) . . For set CONTENT_TYPE.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001093
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001094 *
1095 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
1096 *
1097 * $Errors: None
1098 *
1099 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001100static int sendCgi(const char *url,
Eric Andersen07f2fea2004-10-08 08:03:29 +00001101 const char *request, int bodyLen, const char *cookie,
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001102 const char *content_type)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001103{
1104 int fromCgi[2]; /* pipe for reading data from CGI */
1105 int toCgi[2]; /* pipe for sending data to CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001106
Glenn L McGrath06e95652003-02-09 06:51:14 +00001107 static char * argp[] = { 0, 0 };
1108 int pid = 0;
1109 int inFd;
1110 int outFd;
1111 int firstLine = 1;
1112
1113 do {
1114 if (pipe(fromCgi) != 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001115 break;
1116 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001117 if (pipe(toCgi) != 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001118 break;
1119 }
1120
1121 pid = fork();
Glenn L McGrath06e95652003-02-09 06:51:14 +00001122 if (pid < 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001123 pid = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001124 break;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001125 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001126
1127 if (!pid) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001128 /* child process */
1129 char *script;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001130 char *purl = strdup( url );
1131 char realpath_buff[MAXPATHLEN];
1132
1133 if(purl == NULL)
1134 _exit(242);
1135
1136 inFd = toCgi[0];
1137 outFd = fromCgi[1];
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001138
1139 dup2(inFd, 0); // replace stdin with the pipe
1140 dup2(outFd, 1); // replace stdout with the pipe
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001141 if(!DEBUG)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001142 dup2(outFd, 2); // replace stderr with the pipe
1143
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001144 close(toCgi[0]);
1145 close(toCgi[1]);
1146 close(fromCgi[0]);
1147 close(fromCgi[1]);
1148
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001149 /*
Glenn L McGrath06e95652003-02-09 06:51:14 +00001150 * Find PATH_INFO.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001151 */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001152 script = purl;
1153 while((script = strchr( script + 1, '/' )) != NULL) {
1154 /* have script.cgi/PATH_INFO or dirs/script.cgi[/PATH_INFO] */
1155 struct stat sb;
1156
1157 *script = '\0';
1158 if(is_directory(purl + 1, 1, &sb) == 0) {
1159 /* not directory, found script.cgi/PATH_INFO */
1160 *script = '/';
1161 break;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001162 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001163 *script = '/'; /* is directory, find next '/' */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001164 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001165 addEnv("PATH", "INFO", script); /* set /PATH_INFO or NULL */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001166 addEnv("PATH", "", getenv("PATH"));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001167 addEnv("REQUEST", "METHOD", request);
Eric Andersen07f2fea2004-10-08 08:03:29 +00001168 if(config->query) {
1169 char *uri = alloca(strlen(purl) + 2 + strlen(config->query));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001170 if(uri)
Eric Andersen07f2fea2004-10-08 08:03:29 +00001171 sprintf(uri, "%s?%s", purl, config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001172 addEnv("REQUEST", "URI", uri);
1173 } else {
1174 addEnv("REQUEST", "URI", purl);
1175 }
1176 if(script != NULL)
1177 *script = '\0'; /* reduce /PATH_INFO */
Rob Landley344ea472005-09-01 09:38:32 +00001178 /* SCRIPT_FILENAME required by PHP in CGI mode */
1179 if(realpath(purl + 1, realpath_buff))
"Vladimir N. Oleynik"54deebf2005-09-19 10:46:44 +00001180 addEnv("SCRIPT", "FILENAME", realpath_buff);
Rob Landley344ea472005-09-01 09:38:32 +00001181 else
"Vladimir N. Oleynik"54deebf2005-09-19 10:46:44 +00001182 *realpath_buff = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001183 /* set SCRIPT_NAME as full path: /cgi-bin/dirs/script.cgi */
1184 addEnv("SCRIPT_NAME", "", purl);
Eric Andersen07f2fea2004-10-08 08:03:29 +00001185 addEnv("QUERY_STRING", "", config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001186 addEnv("SERVER", "SOFTWARE", httpdVersion);
1187 addEnv("SERVER", "PROTOCOL", "HTTP/1.0");
1188 addEnv("GATEWAY_INTERFACE", "", "CGI/1.1");
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001189 addEnv("REMOTE", "ADDR", config->rmt_ip_str);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001190#ifdef CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
Glenn L McGrath06e95652003-02-09 06:51:14 +00001191 addEnvPort("REMOTE");
Glenn L McGrath06e95652003-02-09 06:51:14 +00001192#endif
1193 if(bodyLen) {
1194 char sbl[32];
1195
1196 sprintf(sbl, "%d", bodyLen);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001197 addEnv("CONTENT", "LENGTH", sbl);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001198 }
1199 if(cookie)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001200 addEnv("HTTP", "COOKIE", cookie);
1201 if(content_type)
1202 addEnv("CONTENT", "TYPE", content_type);
Eric Andersen769a3ef2003-12-19 11:23:47 +00001203#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001204 if(config->remoteuser) {
1205 addEnv("REMOTE", "USER", config->remoteuser);
1206 addEnv("AUTH_TYPE", "", "Basic");
Glenn L McGrath06e95652003-02-09 06:51:14 +00001207 }
Eric Andersen769a3ef2003-12-19 11:23:47 +00001208#endif
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001209 if(config->referer)
1210 addEnv("HTTP", "REFERER", config->referer);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001211
1212 /* set execve argp[0] without path */
1213 argp[0] = strrchr( purl, '/' ) + 1;
1214 /* but script argp[0] must have absolute path and chdiring to this */
Rob Landley344ea472005-09-01 09:38:32 +00001215 if(*realpath_buff) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001216 script = strrchr(realpath_buff, '/');
1217 if(script) {
1218 *script = '\0';
1219 if(chdir(realpath_buff) == 0) {
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00001220 *script = '/';
1221 // now run the program. If it fails,
1222 // use _exit() so no destructors
1223 // get called and make a mess.
Glenn L McGrath5875be42003-09-08 15:39:09 +00001224 execv(realpath_buff, argp);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001225 }
1226 }
1227 }
1228#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1229 config->accepted_socket = 1; /* send to stdout */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001230#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001231 sendHeaders(HTTP_NOT_FOUND);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001232 _exit(242);
1233 } /* end child */
1234
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001235 } while (0);
1236
Glenn L McGrath06e95652003-02-09 06:51:14 +00001237 if (pid) {
1238 /* parent process */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001239 int status;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001240 size_t post_readed_size = 0, post_readed_idx = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001241
1242 inFd = fromCgi[0];
1243 outFd = toCgi[1];
1244 close(fromCgi[1]);
1245 close(toCgi[0]);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001246 signal(SIGPIPE, SIG_IGN);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001247
1248 while (1) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001249 fd_set readSet;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001250 fd_set writeSet;
1251 char wbuf[128];
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001252 int nfound;
1253 int count;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001254
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001255 FD_ZERO(&readSet);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001256 FD_ZERO(&writeSet);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001257 FD_SET(inFd, &readSet);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001258 if(bodyLen > 0 || post_readed_size > 0) {
1259 FD_SET(outFd, &writeSet);
1260 nfound = outFd > inFd ? outFd : inFd;
1261 if(post_readed_size == 0) {
1262 FD_SET(a_c_r, &readSet);
1263 if(nfound < a_c_r)
1264 nfound = a_c_r;
1265 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001266 /* Now wait on the set of sockets! */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001267 nfound = select(nfound + 1, &readSet, &writeSet, 0, NULL);
1268 } else {
1269 if(!bodyLen) {
1270 close(outFd);
1271 bodyLen = -1;
1272 }
1273 nfound = select(inFd + 1, &readSet, 0, 0, NULL);
1274 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001275
Glenn L McGrath06e95652003-02-09 06:51:14 +00001276 if (nfound <= 0) {
1277 if (waitpid(pid, &status, WNOHANG) > 0) {
1278 close(inFd);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001279#if DEBUG
1280 if (WIFEXITED(status))
Manuel Novoa III cad53642003-03-19 09:13:01 +00001281 bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status));
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001282 if (WIFSIGNALED(status))
Manuel Novoa III cad53642003-03-19 09:13:01 +00001283 bb_error_msg("piped has exited with signal=%d", WTERMSIG(status));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001284#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001285 break;
1286 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001287 } else if(post_readed_size > 0 && FD_ISSET(outFd, &writeSet)) {
1288 count = bb_full_write(outFd, wbuf + post_readed_idx, post_readed_size);
1289 if(count > 0) {
1290 post_readed_size -= count;
1291 post_readed_idx += count;
1292 if(post_readed_size == 0)
1293 post_readed_idx = 0;
Paul Fox77ee5232005-07-20 18:42:52 +00001294 } else {
1295 post_readed_size = post_readed_idx = bodyLen = 0; /* broken pipe to CGI */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001296 }
1297 } else if(bodyLen > 0 && post_readed_size == 0 && FD_ISSET(a_c_r, &readSet)) {
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001298 count = bodyLen > (int)sizeof(wbuf) ? (int)sizeof(wbuf) : bodyLen;
Eric Andersen97a1de12004-08-26 22:22:50 +00001299 count = safe_read(a_c_r, wbuf, count);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001300 if(count > 0) {
1301 post_readed_size += count;
1302 bodyLen -= count;
Paul Fox77ee5232005-07-20 18:42:52 +00001303 } else {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001304 bodyLen = 0; /* closed */
1305 }
Eric Andersen97a1de12004-08-26 22:22:50 +00001306 }
1307 if(FD_ISSET(inFd, &readSet)) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001308 int s = a_c_w;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001309 char *rbuf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001310
Eric Andersen97a1de12004-08-26 22:22:50 +00001311#ifndef PIPE_BUF
1312# define PIPESIZE 4096 /* amount of buffering in a pipe */
1313#else
1314# define PIPESIZE PIPE_BUF
1315#endif
1316#if PIPESIZE >= MAX_MEMORY_BUFF
1317# error "PIPESIZE >= MAX_MEMORY_BUFF"
1318#endif
1319
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001320 // There is something to read
Eric Andersen97a1de12004-08-26 22:22:50 +00001321 count = safe_read(inFd, rbuf, PIPESIZE);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001322 if (count == 0)
1323 break; /* closed */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001324 if (count > 0) {
1325 if (firstLine) {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001326 rbuf[count] = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001327 /* check to see if the user script added headers */
"Vladimir N. Oleynik"2e33daa2006-01-26 10:46:14 +00001328 if(strncmp(rbuf, "HTTP/1.0 200 OK\r\n", 4) != 0) {
1329 bb_full_write(s, "HTTP/1.0 200 OK\r\n", 17);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001330 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001331 if (strstr(rbuf, "ontent-") == 0) {
"Vladimir N. Oleynik"2e33daa2006-01-26 10:46:14 +00001332 bb_full_write(s, "Content-type: text/plain\r\n\r\n", 28);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001333 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001334 firstLine = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001335 }
Eric Andersenef437492004-02-04 11:10:28 +00001336 if (bb_full_write(s, rbuf, count) != count)
1337 break;
1338
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001339#if DEBUG
1340 fprintf(stderr, "cgi read %d bytes\n", count);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001341#endif
1342 }
1343 }
1344 }
1345 }
1346 return 0;
1347}
Glenn L McGrath06e95652003-02-09 06:51:14 +00001348#endif /* CONFIG_FEATURE_HTTPD_CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001349
1350/****************************************************************************
1351 *
1352 > $Function: sendFile()
1353 *
1354 * $Description: Send a file response to an HTTP request
1355 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00001356 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001357 * (const char *) url . . The URL requested.
1358 *
1359 * $Return: (int) . . . . . . Always 0.
1360 *
1361 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001362static int sendFile(const char *url)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001363{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001364 char * suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001365 int f;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001366 const char * const * table;
1367 const char * try_suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001368
Glenn L McGrath06e95652003-02-09 06:51:14 +00001369 suffix = strrchr(url, '.');
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001370
Glenn L McGrath06e95652003-02-09 06:51:14 +00001371 for (table = suffixTable; *table; table += 2)
1372 if(suffix != NULL && (try_suffix = strstr(*table, suffix)) != 0) {
1373 try_suffix += strlen(suffix);
1374 if(*try_suffix == 0 || *try_suffix == '.')
1375 break;
1376 }
1377 /* also, if not found, set default as "application/octet-stream"; */
Eric Andersen07f2fea2004-10-08 08:03:29 +00001378 config->httpd_found.found_mime_type = *(table+1);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001379#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
1380 if (suffix) {
1381 Htaccess * cur;
1382
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001383 for (cur = config->mime_a; cur; cur = cur->next) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001384 if(strcmp(cur->before_colon, suffix) == 0) {
Eric Andersen07f2fea2004-10-08 08:03:29 +00001385 config->httpd_found.found_mime_type = cur->after_colon;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001386 break;
1387 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001388 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001389 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001390#endif /* CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES */
1391
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001392#if DEBUG
1393 fprintf(stderr, "Sending file '%s' Content-type: %s\n",
1394 url, config->httpd_found.found_mime_type);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001395#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001396
1397 f = open(url, O_RDONLY);
1398 if (f >= 0) {
1399 int count;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001400 char *buf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001401
1402 sendHeaders(HTTP_OK);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001403 while ((count = bb_full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
Glenn L McGrath9adcf732003-12-08 20:21:53 +00001404 if (bb_full_write(a_c_w, buf, count) != count)
1405 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001406 }
1407 close(f);
1408 } else {
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001409#if DEBUG
1410 bb_perror_msg("Unable to open '%s'", url);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001411#endif
1412 sendHeaders(HTTP_NOT_FOUND);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001413 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001414
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001415 return 0;
1416}
1417
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001418static int checkPermIP(void)
1419{
1420 Htaccess_IP * cur;
1421
1422 /* This could stand some work */
1423 for (cur = config->ip_a_d; cur; cur = cur->next) {
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001424#if DEBUG
1425 fprintf(stderr, "checkPermIP: '%s' ? ", config->rmt_ip_str);
1426 fprintf(stderr, "'%u.%u.%u.%u/%u.%u.%u.%u'\n",
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001427 (unsigned char)(cur->ip >> 24),
1428 (unsigned char)(cur->ip >> 16),
1429 (unsigned char)(cur->ip >> 8),
1430 cur->ip & 0xff,
1431 (unsigned char)(cur->mask >> 24),
1432 (unsigned char)(cur->mask >> 16),
1433 (unsigned char)(cur->mask >> 8),
1434 cur->mask & 0xff);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001435#endif
1436 if((config->rmt_ip & cur->mask) == cur->ip)
1437 return cur->allow_deny == 'A'; /* Allow/Deny */
1438 }
1439
Eric Andersenaff114c2004-04-14 17:51:38 +00001440 /* if unconfigured, return 1 - access from all */
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001441 return !config->flg_deny_all;
1442}
1443
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001444/****************************************************************************
1445 *
1446 > $Function: checkPerm()
1447 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001448 * $Description: Check the permission file for access password protected.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001449 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001450 * If config file isn't present, everything is allowed.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001451 * Entries are of the form you can see example from header source
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001452 *
1453 * $Parameters:
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001454 * (const char *) path . . . . The file path.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001455 * (const char *) request . . . User information to validate.
1456 *
1457 * $Return: (int) . . . . . . . . . 1 if request OK, 0 otherwise.
1458 *
1459 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001460
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001461#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001462static int checkPerm(const char *path, const char *request)
1463{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001464 Htaccess * cur;
1465 const char *p;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001466 const char *p0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001467
Glenn L McGrath06e95652003-02-09 06:51:14 +00001468 const char *prev = NULL;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001469
1470 /* This could stand some work */
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001471 for (cur = config->auth; cur; cur = cur->next) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001472 p0 = cur->before_colon;
1473 if(prev != NULL && strcmp(prev, p0) != 0)
1474 continue; /* find next identical */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001475 p = cur->after_colon;
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001476#if DEBUG
1477 fprintf(stderr,"checkPerm: '%s' ? '%s'\n", p0, request);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001478#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001479 {
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001480 size_t l = strlen(p0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001481
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001482 if(strncmp(p0, path, l) == 0 &&
1483 (l == 1 || path[l] == '/' || path[l] == 0)) {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001484 char *u;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001485 /* path match found. Check request */
Eric Andersen35e643b2003-07-28 07:40:39 +00001486 /* for check next /path:user:password */
1487 prev = p0;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001488 u = strchr(request, ':');
1489 if(u == NULL) {
1490 /* bad request, ':' required */
1491 break;
1492 }
1493
Eric Andersen35e643b2003-07-28 07:40:39 +00001494#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
1495 {
1496 char *cipher;
1497 char *pp;
Eric Andersen35e643b2003-07-28 07:40:39 +00001498
Eric Andersen35e643b2003-07-28 07:40:39 +00001499 if(strncmp(p, request, u-request) != 0) {
1500 /* user uncompared */
1501 continue;
1502 }
1503 pp = strchr(p, ':');
1504 if(pp && pp[1] == '$' && pp[2] == '1' &&
1505 pp[3] == '$' && pp[4]) {
1506 pp++;
1507 cipher = pw_encrypt(u+1, pp);
1508 if (strcmp(cipher, pp) == 0)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001509 goto set_remoteuser_var; /* Ok */
Eric Andersen35e643b2003-07-28 07:40:39 +00001510 /* unauthorized */
1511 continue;
1512 }
1513 }
1514#endif
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001515 if (strcmp(p, request) == 0) {
Glenn L McGrath9d1a33c2003-10-06 13:23:06 +00001516#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001517set_remoteuser_var:
Glenn L McGrath9d1a33c2003-10-06 13:23:06 +00001518#endif
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001519 config->remoteuser = strdup(request);
1520 if(config->remoteuser)
1521 config->remoteuser[(u - request)] = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001522 return 1; /* Ok */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001523 }
Eric Andersen35e643b2003-07-28 07:40:39 +00001524 /* unauthorized */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001525 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001526 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001527 } /* for */
1528
Glenn L McGrath06e95652003-02-09 06:51:14 +00001529 return prev == NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001530}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001531
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001532#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1533
Eric Andersen07f2fea2004-10-08 08:03:29 +00001534/****************************************************************************
1535 *
Mike Frysingerbb12d6f2006-01-03 23:59:01 +00001536 > $Function: handle_sigalrm()
Eric Andersen07f2fea2004-10-08 08:03:29 +00001537 *
Mike Frysingerbb12d6f2006-01-03 23:59:01 +00001538 * $Description: Handle timeouts
Eric Andersen07f2fea2004-10-08 08:03:29 +00001539 *
1540 ****************************************************************************/
1541
1542static void
1543handle_sigalrm( int sig )
1544{
1545 sendHeaders(HTTP_REQUEST_TIMEOUT);
1546 config->alarm_signaled = sig;
1547}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001548
1549/****************************************************************************
1550 *
1551 > $Function: handleIncoming()
1552 *
1553 * $Description: Handle an incoming http request.
1554 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001555 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001556static void handleIncoming(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001557{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001558 char *buf = config->buf;
1559 char *url;
1560 char *purl;
1561 int blank = -1;
Glenn L McGrath3d752f72004-03-05 09:38:16 +00001562 char *test;
1563 struct stat sb;
1564 int ip_allowed;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001565#ifdef CONFIG_FEATURE_HTTPD_CGI
1566 const char *prequest = request_GET;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001567 long length=0;
1568 char *cookie = 0;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001569 char *content_type = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001570#endif
Glenn L McGrath3d752f72004-03-05 09:38:16 +00001571#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1572 fd_set s_fd;
1573 struct timeval tv;
Eric Andersend8746cd2004-02-24 07:28:38 +00001574 int retval;
Glenn L McGrath3d752f72004-03-05 09:38:16 +00001575#endif
Eric Andersen07f2fea2004-10-08 08:03:29 +00001576 struct sigaction sa;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001577
1578#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1579 int credentials = -1; /* if not requred this is Ok */
1580#endif
1581
Eric Andersen07f2fea2004-10-08 08:03:29 +00001582 sa.sa_handler = handle_sigalrm;
1583 sigemptyset(&sa.sa_mask);
1584 sa.sa_flags = 0; /* no SA_RESTART */
1585 sigaction(SIGALRM, &sa, NULL);
1586
Glenn L McGrath06e95652003-02-09 06:51:14 +00001587 do {
1588 int count;
1589
Eric Andersen07f2fea2004-10-08 08:03:29 +00001590 (void) alarm( TIMEOUT );
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001591 if (getLine() <= 0)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001592 break; /* closed */
1593
1594 purl = strpbrk(buf, " \t");
1595 if(purl == NULL) {
1596BAD_REQUEST:
1597 sendHeaders(HTTP_BAD_REQUEST);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001598 break;
1599 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001600 *purl = 0;
1601#ifdef CONFIG_FEATURE_HTTPD_CGI
1602 if(strcasecmp(buf, prequest) != 0) {
1603 prequest = "POST";
1604 if(strcasecmp(buf, prequest) != 0) {
1605 sendHeaders(HTTP_NOT_IMPLEMENTED);
1606 break;
1607 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001608 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001609#else
1610 if(strcasecmp(buf, request_GET) != 0) {
1611 sendHeaders(HTTP_NOT_IMPLEMENTED);
1612 break;
1613 }
1614#endif
1615 *purl = ' ';
1616 count = sscanf(purl, " %[^ ] HTTP/%d.%*d", buf, &blank);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001617
Glenn L McGrath06e95652003-02-09 06:51:14 +00001618 if (count < 1 || buf[0] != '/') {
1619 /* Garbled request/URL */
1620 goto BAD_REQUEST;
1621 }
1622 url = alloca(strlen(buf) + 12); /* + sizeof("/index.html\0") */
1623 if(url == NULL) {
1624 sendHeaders(HTTP_INTERNAL_SERVER_ERROR);
1625 break;
1626 }
1627 strcpy(url, buf);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001628 /* extract url args if present */
Eric Andersen07f2fea2004-10-08 08:03:29 +00001629 test = strchr(url, '?');
1630 if (test) {
1631 *test++ = 0;
1632 config->query = test;
1633 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001634
"Vladimir N. Oleynik"ab90b9f2006-01-24 12:02:27 +00001635 test = decodeString(url, 0);
1636 if(test == NULL)
1637 goto BAD_REQUEST;
1638 if(test == (buf+1)) {
1639 sendHeaders(HTTP_NOT_FOUND);
1640 break;
1641 }
Manuel Novoa III cad53642003-03-19 09:13:01 +00001642 /* algorithm stolen from libbb bb_simplify_path(),
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001643 but don`t strdup and reducing trailing slash and protect out root */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001644 purl = test = url;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001645
Glenn L McGrath06e95652003-02-09 06:51:14 +00001646 do {
1647 if (*purl == '/') {
1648 if (*test == '/') { /* skip duplicate (or initial) slash */
1649 continue;
1650 } else if (*test == '.') {
1651 if (test[1] == '/' || test[1] == 0) { /* skip extra '.' */
1652 continue;
1653 } else if ((test[1] == '.') && (test[2] == '/' || test[2] == 0)) {
1654 ++test;
1655 if (purl == url) {
1656 /* protect out root */
1657 goto BAD_REQUEST;
1658 }
1659 while (*--purl != '/'); /* omit previous dir */
1660 continue;
1661 }
1662 }
1663 }
1664 *++purl = *test;
1665 } while (*++test);
1666
1667 *++purl = 0; /* so keep last character */
1668 test = purl; /* end ptr */
1669
1670 /* If URL is directory, adding '/' */
1671 if(test[-1] != '/') {
1672 if ( is_directory(url + 1, 1, &sb) ) {
Eric Andersen07f2fea2004-10-08 08:03:29 +00001673 config->httpd_found.found_moved_temporarily = url;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001674 }
1675 }
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001676#if DEBUG
1677 fprintf(stderr, "url='%s', args=%s\n", url, config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001678#endif
1679
1680 test = url;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001681 ip_allowed = checkPermIP();
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001682 while(ip_allowed && (test = strchr( test + 1, '/' )) != NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001683 /* have path1/path2 */
1684 *test = '\0';
1685 if( is_directory(url + 1, 1, &sb) ) {
1686 /* may be having subdir config */
1687 parse_conf(url + 1, SUBDIR_PARSE);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001688 ip_allowed = checkPermIP();
Glenn L McGrath06e95652003-02-09 06:51:14 +00001689 }
1690 *test = '/';
1691 }
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001692 if(blank >= 0) {
1693 // read until blank line for HTTP version specified, else parse immediate
1694 while(1) {
1695 alarm(TIMEOUT);
1696 count = getLine();
1697 if(count <= 0)
1698 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001699
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001700#if DEBUG
1701 fprintf(stderr, "Header: '%s'\n", buf);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001702#endif
1703
1704#ifdef CONFIG_FEATURE_HTTPD_CGI
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001705 /* try and do our best to parse more lines */
1706 if ((strncasecmp(buf, Content_length, 15) == 0)) {
1707 if(prequest != request_GET)
1708 length = strtol(buf + 15, 0, 0); // extra read only for POST
1709 } else if ((strncasecmp(buf, "Cookie:", 7) == 0)) {
1710 for(test = buf + 7; isspace(*test); test++)
1711 ;
1712 cookie = strdup(test);
1713 } else if ((strncasecmp(buf, "Content-Type:", 13) == 0)) {
1714 for(test = buf + 13; isspace(*test); test++)
1715 ;
1716 content_type = strdup(test);
1717 } else if ((strncasecmp(buf, "Referer:", 8) == 0)) {
1718 for(test = buf + 8; isspace(*test); test++)
1719 ;
1720 config->referer = strdup(test);
1721 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001722#endif
1723
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001724#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001725 if (strncasecmp(buf, "Authorization:", 14) == 0) {
1726 /* We only allow Basic credentials.
1727 * It shows up as "Authorization: Basic <userid:password>" where
1728 * the userid:password is base64 encoded.
1729 */
1730 for(test = buf + 14; isspace(*test); test++)
1731 ;
1732 if (strncasecmp(test, "Basic", 5) != 0)
1733 continue;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001734
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001735 test += 5; /* decodeBase64() skiping space self */
1736 decodeBase64(test);
1737 credentials = checkPerm(url, test);
1738 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001739#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1740
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001741 } /* while extra header reading */
1742 }
Eric Andersen07f2fea2004-10-08 08:03:29 +00001743 (void) alarm( 0 );
1744 if(config->alarm_signaled)
1745 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001746
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001747 if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001748 /* protect listing [/path]/httpd_conf or IP deny */
1749#ifdef CONFIG_FEATURE_HTTPD_CGI
1750FORBIDDEN: /* protect listing /cgi-bin */
1751#endif
1752 sendHeaders(HTTP_FORBIDDEN);
1753 break;
1754 }
1755
1756#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1757 if (credentials <= 0 && checkPerm(url, ":") == 0) {
1758 sendHeaders(HTTP_UNAUTHORIZED);
1759 break;
1760 }
1761#endif
1762
Eric Andersen07f2fea2004-10-08 08:03:29 +00001763 if(config->httpd_found.found_moved_temporarily) {
1764 sendHeaders(HTTP_MOVED_TEMPORARILY);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001765#if DEBUG
Eric Andersen07f2fea2004-10-08 08:03:29 +00001766 /* clear unforked memory flag */
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001767 config->httpd_found.found_moved_temporarily = NULL;
Eric Andersen07f2fea2004-10-08 08:03:29 +00001768#endif
1769 break;
1770 }
1771
Glenn L McGrath06e95652003-02-09 06:51:14 +00001772 test = url + 1; /* skip first '/' */
1773
1774#ifdef CONFIG_FEATURE_HTTPD_CGI
1775 /* if strange Content-Length */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001776 if (length < 0)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001777 break;
1778
Glenn L McGrath06e95652003-02-09 06:51:14 +00001779 if (strncmp(test, "cgi-bin", 7) == 0) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001780 if(test[7] == '/' && test[8] == 0)
1781 goto FORBIDDEN; // protect listing cgi-bin/
Eric Andersen07f2fea2004-10-08 08:03:29 +00001782 sendCgi(url, prequest, length, cookie, content_type);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001783 } else {
1784 if (prequest != request_GET)
1785 sendHeaders(HTTP_NOT_IMPLEMENTED);
1786 else {
1787#endif /* CONFIG_FEATURE_HTTPD_CGI */
1788 if(purl[-1] == '/')
1789 strcpy(purl, "index.html");
1790 if ( stat(test, &sb ) == 0 ) {
1791 config->ContentLength = sb.st_size;
1792 config->last_mod = sb.st_mtime;
1793 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001794 sendFile(test);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001795#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1796 /* unset if non inetd looped */
1797 config->ContentLength = -1;
1798#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001799
Glenn L McGrath06e95652003-02-09 06:51:14 +00001800#ifdef CONFIG_FEATURE_HTTPD_CGI
1801 }
1802 }
1803#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001804
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001805 } while (0);
1806
Glenn L McGrath06e95652003-02-09 06:51:14 +00001807
1808#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1809/* from inetd don`t looping: freeing, closing automatic from exit always */
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001810# if DEBUG
1811 fprintf(stderr, "closing socket\n");
Glenn L McGrath06e95652003-02-09 06:51:14 +00001812# endif
1813# ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath06e95652003-02-09 06:51:14 +00001814 free(cookie);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001815 free(content_type);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001816 free(config->referer);
Eric Andersen769a3ef2003-12-19 11:23:47 +00001817#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1818 free(config->remoteuser);
1819#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001820# endif
1821 shutdown(a_c_w, SHUT_WR);
Eric Andersend8746cd2004-02-24 07:28:38 +00001822
1823 /* Properly wait for remote to closed */
1824 FD_ZERO (&s_fd) ;
1825 FD_SET (a_c_w, &s_fd) ;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001826
Eric Andersend8746cd2004-02-24 07:28:38 +00001827 do {
1828 tv.tv_sec = 2 ;
1829 tv.tv_usec = 0 ;
1830 retval = select (a_c_w + 1, &s_fd, NULL, NULL, &tv);
1831 } while (retval > 0 && (read (a_c_w, buf, sizeof (config->buf)) > 0));
1832
Glenn L McGrath06e95652003-02-09 06:51:14 +00001833 shutdown(a_c_r, SHUT_RD);
1834 close(config->accepted_socket);
1835#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001836}
1837
1838/****************************************************************************
1839 *
1840 > $Function: miniHttpd()
1841 *
1842 * $Description: The main http server function.
1843 *
1844 * Given an open socket fildes, listen for new connections and farm out
1845 * the processing as a forked process.
1846 *
1847 * $Parameters:
1848 * (int) server. . . The server socket fildes.
1849 *
1850 * $Return: (int) . . . . Always 0.
1851 *
1852 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001853#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001854static int miniHttpd(int server)
1855{
1856 fd_set readfd, portfd;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001857
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001858 FD_ZERO(&portfd);
1859 FD_SET(server, &portfd);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001860
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001861 /* copy the ports we are watching to the readfd set */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001862 while (1) {
1863 readfd = portfd;
1864
Eric Andersenaff114c2004-04-14 17:51:38 +00001865 /* Now wait INDEFINITELY on the set of sockets! */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001866 if (select(server + 1, &readfd, 0, 0, 0) > 0) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001867 if (FD_ISSET(server, &readfd)) {
1868 int on;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001869 struct sockaddr_in fromAddr;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001870
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001871 socklen_t fromAddrLen = sizeof(fromAddr);
1872 int s = accept(server,
Glenn L McGrath06e95652003-02-09 06:51:14 +00001873 (struct sockaddr *)&fromAddr, &fromAddrLen);
1874
1875 if (s < 0) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001876 continue;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001877 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001878 config->accepted_socket = s;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001879 config->rmt_ip = ntohl(fromAddr.sin_addr.s_addr);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001880#if ENABLE_FEATURE_HTTPD_CGI || DEBUG
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001881 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1882 (unsigned char)(config->rmt_ip >> 24),
1883 (unsigned char)(config->rmt_ip >> 16),
1884 (unsigned char)(config->rmt_ip >> 8),
1885 config->rmt_ip & 0xff);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001886 config->port = ntohs(fromAddr.sin_port);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001887#if DEBUG
1888 bb_error_msg("connection from IP=%s, port %u\n",
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001889 config->rmt_ip_str, config->port);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001890#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001891#endif /* CONFIG_FEATURE_HTTPD_CGI */
1892
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001893 /* set the KEEPALIVE option to cull dead connections */
1894 on = 1;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001895 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof (on));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001896
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001897#if !DEBUG
1898 if (fork() == 0)
1899#endif
1900 {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001901 /* This is the spawned thread */
1902#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
1903 /* protect reload config, may be confuse checking */
1904 signal(SIGHUP, SIG_IGN);
1905#endif
1906 handleIncoming();
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001907#if !DEBUG
1908 exit(0);
1909#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001910 }
1911 close(s);
1912 }
1913 }
1914 } // while (1)
1915 return 0;
1916}
1917
Glenn L McGrath06e95652003-02-09 06:51:14 +00001918#else
1919 /* from inetd */
1920
1921static int miniHttpd(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001922{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001923 struct sockaddr_in fromAddrLen;
1924 socklen_t sinlen = sizeof (struct sockaddr_in);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001925
1926 getpeername (0, (struct sockaddr *)&fromAddrLen, &sinlen);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001927 config->rmt_ip = ntohl(fromAddrLen.sin_addr.s_addr);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001928#if ENABLE_FEATURE_HTTPD_CGI
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001929 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1930 (unsigned char)(config->rmt_ip >> 24),
1931 (unsigned char)(config->rmt_ip >> 16),
1932 (unsigned char)(config->rmt_ip >> 8),
1933 config->rmt_ip & 0xff);
1934#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001935 config->port = ntohs(fromAddrLen.sin_port);
1936 handleIncoming();
1937 return 0;
1938}
1939#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
1940
1941#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
1942static void sighup_handler(int sig)
1943{
1944 /* set and reset */
1945 struct sigaction sa;
1946
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001947 parse_conf(default_path_httpd_conf,
1948 sig == SIGHUP ? SIGNALED_PARSE : FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001949 sa.sa_handler = sighup_handler;
1950 sigemptyset(&sa.sa_mask);
1951 sa.sa_flags = SA_RESTART;
1952 sigaction(SIGHUP, &sa, NULL);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001953}
1954#endif
1955
Eric Andersena3bb3e62003-06-26 09:05:32 +00001956
1957static const char httpd_opts[]="c:d:h:"
1958#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
1959 "e:"
Eric Andersena3bb3e62003-06-26 09:05:32 +00001960#endif
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001961#define OPT_INC_1 ENABLE_FEATURE_HTTPD_ENCODE_URL_STR
1962
Eric Andersena3bb3e62003-06-26 09:05:32 +00001963#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1964 "r:"
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001965#endif
1966#define OPT_INC_2 ENABLE_FEATURE_HTTPD_BASIC_AUTH
1967
1968#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
Eric Andersen35e643b2003-07-28 07:40:39 +00001969 "m:"
Eric Andersen35e643b2003-07-28 07:40:39 +00001970#endif
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001971#define OPT_INC_3 ENABLE_FEATURE_HTTPD_AUTH_MD5
1972
Eric Andersena3bb3e62003-06-26 09:05:32 +00001973#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001974 "p:"
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001975#endif
Eric Andersena3bb3e62003-06-26 09:05:32 +00001976#ifdef CONFIG_FEATURE_HTTPD_SETUID
1977 "u:"
1978#endif
Eric Andersena3bb3e62003-06-26 09:05:32 +00001979 ;
1980
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001981#define OPT_CONFIG_FILE (1<<0) /* c */
1982#define OPT_DECODE_URL (1<<1) /* d */
1983#define OPT_HOME_HTTPD (1<<2) /* h */
1984#define OPT_ENCODE_URL (1<<(2+OPT_INC_1)) /* e */
1985#define OPT_REALM (1<<(2+OPT_INC_1+OPT_INC_2)) /* r */
1986#define OPT_MD5 (1<<(2+OPT_INC_1+OPT_INC_2+OPT_INC_3)) /* m */
1987#define OPT_PORT (1<<(3+OPT_INC_1+OPT_INC_2+OPT_INC_3)) /* p */
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001988#define OPT_SETUID (1<<(4+OPT_INC_1+OPT_INC_2+OPT_INC_3)) /* u */
Eric Andersena3bb3e62003-06-26 09:05:32 +00001989
1990
Glenn L McGrath06e95652003-02-09 06:51:14 +00001991#ifdef HTTPD_STANDALONE
1992int main(int argc, char *argv[])
1993#else
1994int httpd_main(int argc, char *argv[])
1995#endif
1996{
Eric Andersena3bb3e62003-06-26 09:05:32 +00001997 unsigned long opt;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001998 const char *home_httpd = home;
Eric Andersena3bb3e62003-06-26 09:05:32 +00001999 char *url_for_decode;
2000#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
2001 const char *url_for_encode;
2002#endif
2003#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2004 const char *s_port;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002005 int server;
Glenn L McGrath06e95652003-02-09 06:51:14 +00002006#endif
2007
2008#ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersena3bb3e62003-06-26 09:05:32 +00002009 const char *s_uid;
Glenn L McGrath06e95652003-02-09 06:51:14 +00002010 long uid = -1;
2011#endif
2012
Eric Andersen35e643b2003-07-28 07:40:39 +00002013#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
2014 const char *pass;
2015#endif
2016
Glenn L McGrath06e95652003-02-09 06:51:14 +00002017 config = xcalloc(1, sizeof(*config));
2018#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
2019 config->realm = "Web Server Authentication";
2020#endif
2021
2022#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2023 config->port = 80;
2024#endif
2025
2026 config->ContentLength = -1;
2027
Eric Andersena3bb3e62003-06-26 09:05:32 +00002028 opt = bb_getopt_ulflags(argc, argv, httpd_opts,
2029 &(config->configFile), &url_for_decode, &home_httpd
Glenn L McGrath06e95652003-02-09 06:51:14 +00002030#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Eric Andersena3bb3e62003-06-26 09:05:32 +00002031 , &url_for_encode
Glenn L McGrath06e95652003-02-09 06:51:14 +00002032#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002033#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Eric Andersena3bb3e62003-06-26 09:05:32 +00002034 , &(config->realm)
Eric Andersen35e643b2003-07-28 07:40:39 +00002035# ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
2036 , &pass
2037# endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00002038#endif
Eric Andersena3bb3e62003-06-26 09:05:32 +00002039#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2040 , &s_port
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00002041#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00002042#ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersena3bb3e62003-06-26 09:05:32 +00002043 , &s_uid
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002044#endif
2045 );
Eric Andersena3bb3e62003-06-26 09:05:32 +00002046
2047 if(opt & OPT_DECODE_URL) {
2048 printf("%s", decodeString(url_for_decode, 1));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002049 return 0;
Eric Andersena3bb3e62003-06-26 09:05:32 +00002050 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002051#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Eric Andersena3bb3e62003-06-26 09:05:32 +00002052 if(opt & OPT_ENCODE_URL) {
2053 printf("%s", encodeString(url_for_encode));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002054 return 0;
Eric Andersena3bb3e62003-06-26 09:05:32 +00002055 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002056#endif
Eric Andersen35e643b2003-07-28 07:40:39 +00002057#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
2058 if(opt & OPT_MD5) {
2059 printf("%s\n", pw_encrypt(pass, "$1$"));
2060 return 0;
2061 }
2062#endif
Eric Andersena3bb3e62003-06-26 09:05:32 +00002063#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2064 if(opt & OPT_PORT)
2065 config->port = bb_xgetlarg(s_port, 10, 1, 0xffff);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002066#ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersena3bb3e62003-06-26 09:05:32 +00002067 if(opt & OPT_SETUID) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00002068 char *e;
2069
Eric Andersena3bb3e62003-06-26 09:05:32 +00002070 uid = strtol(s_uid, &e, 0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002071 if(*e != '\0') {
2072 /* not integer */
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +00002073 uid = bb_xgetpwnam(s_uid);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002074 }
2075 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002076#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00002077#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002078
Glenn L McGrath06e95652003-02-09 06:51:14 +00002079 if(chdir(home_httpd)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002080 bb_perror_msg_and_die("can`t chdir to %s", home_httpd);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002081 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002082#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2083 server = openServer();
2084# ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersenaff114c2004-04-14 17:51:38 +00002085 /* drop privileges */
Glenn L McGrath06e95652003-02-09 06:51:14 +00002086 if(uid > 0)
2087 setuid(uid);
2088# endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002089#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00002090
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002091#ifdef CONFIG_FEATURE_HTTPD_CGI
2092 {
2093 char *p = getenv("PATH");
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002094 if(p) {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00002095 p = bb_xstrdup(p);
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002096 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00002097 clearenv();
2098 if(p)
2099 setenv("PATH", p, 1);
Glenn L McGrath14092a12003-09-12 00:44:50 +00002100# ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2101 addEnvPort("SERVER");
2102# endif
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002103 }
2104#endif
2105
Glenn L McGrath06e95652003-02-09 06:51:14 +00002106#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
2107 sighup_handler(0);
2108#else
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00002109 parse_conf(default_path_httpd_conf, FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002110#endif
2111
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00002112#if !ENABLE_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2113# if !DEBUG
2114 if (daemon(1, 0) < 0) /* don`t change curent directory */
Manuel Novoa III cad53642003-03-19 09:13:01 +00002115 bb_perror_msg_and_die("daemon");
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00002116# endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00002117 return miniHttpd(server);
2118#else
2119 return miniHttpd();
2120#endif
2121}