blob: 83ded53309b2080c0a67829ac293f30aee31f0be [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>
Eric Andersen07f2fea2004-10-08 08:03:29 +00005 * Copyright (C) 2003,2004 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
171/* enable all features now */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000172#define CONFIG_FEATURE_HTTPD_BASIC_AUTH
Eric Andersen35e643b2003-07-28 07:40:39 +0000173#define CONFIG_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath06e95652003-02-09 06:51:14 +0000174#define CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath06e95652003-02-09 06:51:14 +0000175#define CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
176#define CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
177#define CONFIG_FEATURE_HTTPD_CGI
178#define CONFIG_FEATURE_HTTPD_SETUID
179#define CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
180
181/* require from libbb.a for linking */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000182const char *bb_applet_name = "httpd";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000183
Manuel Novoa III cad53642003-03-19 09:13:01 +0000184void bb_show_usage(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000185{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000186 fprintf(stderr, "Usage: %s [-p <port>] [-c configFile] [-d/-e <string>] "
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000187 "[-r realm] [-u user] [-h homedir]\n", bb_applet_name);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000188 exit(1);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000189}
190#endif
191
Glenn L McGrath06e95652003-02-09 06:51:14 +0000192#ifdef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
193#undef CONFIG_FEATURE_HTTPD_SETUID /* use inetd user.group config settings */
194#undef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP /* so is not daemon */
195/* inetd set stderr to accepted socket and we can`t true see debug messages */
196#undef DEBUG
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000197#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000198
Glenn L McGrath06e95652003-02-09 06:51:14 +0000199#define MAX_MEMORY_BUFF 8192 /* IO buffer */
200
201typedef struct HT_ACCESS {
202 char *after_colon;
203 struct HT_ACCESS *next;
204 char before_colon[1]; /* really bigger, must last */
205} Htaccess;
206
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000207typedef struct HT_ACCESS_IP {
208 unsigned int ip;
209 unsigned int mask;
210 int allow_deny;
211 struct HT_ACCESS_IP *next;
212} Htaccess_IP;
213
Glenn L McGrath06e95652003-02-09 06:51:14 +0000214typedef struct
215{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000216 char buf[MAX_MEMORY_BUFF];
217
218#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
219 const char *realm;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000220 char *remoteuser;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000221#endif
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000222
Eric Andersen07f2fea2004-10-08 08:03:29 +0000223 const char *query;
224
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000225#ifdef CONFIG_FEATURE_HTTPD_CGI
226 char *referer;
227#endif
228
Glenn L McGrath06e95652003-02-09 06:51:14 +0000229 const char *configFile;
230
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000231 unsigned int rmt_ip;
232#if defined(CONFIG_FEATURE_HTTPD_CGI) || defined(DEBUG)
233 char rmt_ip_str[16]; /* for set env REMOTE_ADDR */
234#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000235 unsigned port; /* server initial port and for
236 set env REMOTE_PORT */
Eric Andersen07f2fea2004-10-08 08:03:29 +0000237 union HTTPD_FOUND {
238 const char *found_mime_type;
239 const char *found_moved_temporarily;
240 } httpd_found;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000241
Glenn L McGrath06e95652003-02-09 06:51:14 +0000242 off_t ContentLength; /* -1 - unknown */
243 time_t last_mod;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000244
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000245 Htaccess_IP *ip_a_d; /* config allow/deny lines */
Glenn L McGrath393183d2003-05-26 14:07:50 +0000246 int flg_deny_all;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000247#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
248 Htaccess *auth; /* config user:password lines */
249#endif
250#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
251 Htaccess *mime_a; /* config mime types */
252#endif
253
Glenn L McGrath06e95652003-02-09 06:51:14 +0000254#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
255 int accepted_socket;
256#define a_c_r config->accepted_socket
257#define a_c_w config->accepted_socket
258 int debugHttpd; /* if seted, don`t stay daemon */
259#else
260#define a_c_r 0
261#define a_c_w 1
262#endif
Eric Andersen07f2fea2004-10-08 08:03:29 +0000263 volatile int alarm_signaled;
264
Glenn L McGrath06e95652003-02-09 06:51:14 +0000265} HttpdConfig;
266
267static HttpdConfig *config;
268
269static const char request_GET[] = "GET"; /* size algorithic optimize */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000270
271static const char* const suffixTable [] = {
Eric Andersenaff114c2004-04-14 17:51:38 +0000272/* Warning: shorted equivalent suffix in one line must be first */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000273 ".htm.html", "text/html",
274 ".jpg.jpeg", "image/jpeg",
275 ".gif", "image/gif",
276 ".png", "image/png",
277 ".txt.h.c.cc.cpp", "text/plain",
Glenn L McGrath06e95652003-02-09 06:51:14 +0000278 ".css", "text/css",
279 ".wav", "audio/wav",
280 ".avi", "video/x-msvideo",
281 ".qt.mov", "video/quicktime",
282 ".mpe.mpeg", "video/mpeg",
283 ".mid.midi", "audio/midi",
284 ".mp3", "audio/mpeg",
285#if 0 /* unpopular */
286 ".au", "audio/basic",
287 ".pac", "application/x-ns-proxy-autoconfig",
288 ".vrml.wrl", "model/vrml",
289#endif
290 0, "application/octet-stream" /* default */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000291 };
292
293typedef enum
294{
295 HTTP_OK = 200,
Eric Andersen07f2fea2004-10-08 08:03:29 +0000296 HTTP_MOVED_TEMPORARILY = 302,
297 HTTP_BAD_REQUEST = 400, /* malformed syntax */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000298 HTTP_UNAUTHORIZED = 401, /* authentication needed, respond with auth hdr */
299 HTTP_NOT_FOUND = 404,
Glenn L McGrath06e95652003-02-09 06:51:14 +0000300 HTTP_FORBIDDEN = 403,
Eric Andersen07f2fea2004-10-08 08:03:29 +0000301 HTTP_REQUEST_TIMEOUT = 408,
302 HTTP_NOT_IMPLEMENTED = 501, /* used for unrecognized requests */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000303 HTTP_INTERNAL_SERVER_ERROR = 500,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000304#if 0 /* future use */
305 HTTP_CONTINUE = 100,
306 HTTP_SWITCHING_PROTOCOLS = 101,
307 HTTP_CREATED = 201,
308 HTTP_ACCEPTED = 202,
309 HTTP_NON_AUTHORITATIVE_INFO = 203,
310 HTTP_NO_CONTENT = 204,
311 HTTP_MULTIPLE_CHOICES = 300,
312 HTTP_MOVED_PERMANENTLY = 301,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000313 HTTP_NOT_MODIFIED = 304,
314 HTTP_PAYMENT_REQUIRED = 402,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000315 HTTP_BAD_GATEWAY = 502,
316 HTTP_SERVICE_UNAVAILABLE = 503, /* overload, maintenance */
317 HTTP_RESPONSE_SETSIZE=0xffffffff
318#endif
319} HttpResponseNum;
320
321typedef struct
322{
323 HttpResponseNum type;
324 const char *name;
325 const char *info;
326} HttpEnumString;
327
328static const HttpEnumString httpResponseNames[] = {
329 { HTTP_OK, "OK" },
Eric Andersen07f2fea2004-10-08 08:03:29 +0000330 { HTTP_MOVED_TEMPORARILY, "Found", "Directories must end with a slash." },
331 { HTTP_REQUEST_TIMEOUT, "Request Timeout",
332 "No request appeared within a reasonable time period." },
Glenn L McGrath06e95652003-02-09 06:51:14 +0000333 { HTTP_NOT_IMPLEMENTED, "Not Implemented",
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000334 "The requested method is not recognized by this server." },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000335#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000336 { HTTP_UNAUTHORIZED, "Unauthorized", "" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000337#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000338 { HTTP_NOT_FOUND, "Not Found",
339 "The requested URL was not found on this server." },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000340 { HTTP_BAD_REQUEST, "Bad Request", "Unsupported method." },
Glenn L McGrath06e95652003-02-09 06:51:14 +0000341 { HTTP_FORBIDDEN, "Forbidden", "" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000342 { HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error",
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000343 "Internal Server Error" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000344#if 0 /* not implemented */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000345 { HTTP_CREATED, "Created" },
346 { HTTP_ACCEPTED, "Accepted" },
347 { HTTP_NO_CONTENT, "No Content" },
348 { HTTP_MULTIPLE_CHOICES, "Multiple Choices" },
349 { HTTP_MOVED_PERMANENTLY, "Moved Permanently" },
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000350 { HTTP_NOT_MODIFIED, "Not Modified" },
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000351 { HTTP_BAD_GATEWAY, "Bad Gateway", "" },
352 { HTTP_SERVICE_UNAVAILABLE, "Service Unavailable", "" },
353#endif
354};
355
Glenn L McGrath06e95652003-02-09 06:51:14 +0000356
357static const char RFC1123FMT[] = "%a, %d %b %Y %H:%M:%S GMT";
358static const char Content_length[] = "Content-length:";
359
360
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000361static int
362scan_ip (const char **ep, unsigned int *ip, unsigned char endc)
363{
364 const char *p = *ep;
365 int auto_mask = 8;
366 int j;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000367
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000368 *ip = 0;
369 for (j = 0; j < 4; j++) {
370 unsigned int octet;
371
372 if ((*p < '0' || *p > '9') && (*p != '/' || j == 0) && *p != 0)
373 return -auto_mask;
374 octet = 0;
375 while (*p >= '0' && *p <= '9') {
376 octet *= 10;
377 octet += *p - '0';
378 if (octet > 255)
379 return -auto_mask;
380 p++;
381 }
382 if (*p == '.')
383 p++;
384 if (*p != '/' && *p != 0)
385 auto_mask += 8;
386 *ip = ((*ip) << 8) | octet;
387 }
388 if (*p != 0) {
389 if (*p != endc)
390 return -auto_mask;
391 p++;
392 if(*p == 0)
393 return -auto_mask;
394 }
395 *ep = p;
396 return auto_mask;
397}
398
399static int
400scan_ip_mask (const char *ipm, unsigned int *ip, unsigned int *mask)
401{
402 int i;
403 unsigned int msk;
404
405 i = scan_ip(&ipm, ip, '/');
406 if(i < 0)
407 return i;
408 if(*ipm) {
409 const char *p = ipm;
410
411 i = 0;
412 while (*p) {
413 if (*p < '0' || *p > '9') {
414 if (*p == '.') {
415 i = scan_ip (&ipm, mask, 0);
416 return i != 32;
417 }
418 return -1;
419 }
420 i *= 10;
421 i += *p - '0';
422 p++;
423 }
424 }
425 if (i > 32 || i < 0)
426 return -1;
427 msk = 0x80000000;
428 *mask = 0;
429 while (i > 0) {
430 *mask |= msk;
431 msk >>= 1;
432 i--;
433 }
434 return 0;
435}
436
437#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000438static void free_config_lines(Htaccess **pprev)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000439{
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000440 Htaccess *prev = *pprev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000441
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000442 while( prev ) {
443 Htaccess *cur = prev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000444
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000445 prev = cur->next;
446 free(cur);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000447 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000448 *pprev = NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000449}
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000450#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000451
Glenn L McGrath06e95652003-02-09 06:51:14 +0000452/* flag */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000453#define FIRST_PARSE 0
454#define SUBDIR_PARSE 1
455#define SIGNALED_PARSE 2
456#define FIND_FROM_HTTPD_ROOT 3
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000457/****************************************************************************
458 *
459 > $Function: parse_conf()
460 *
461 * $Description: parse configuration file into in-memory linked list.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000462 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000463 * The first non-white character is examined to determine if the config line
464 * is one of the following:
465 * .ext:mime/type # new mime type not compiled into httpd
466 * [adAD]:from # ip address allow/deny, * for wildcard
467 * /path:user:pass # username/password
468 *
469 * Any previous IP rules are discarded.
470 * If the flag argument is not SUBDIR_PARSE then all /path and mime rules
471 * are also discarded. That is, previous settings are retained if flag is
472 * SUBDIR_PARSE.
473 *
474 * $Parameters:
475 * (const char *) path . . null for ip address checks, path for password
476 * checks.
477 * (int) flag . . . . . . the source of the parse request.
478 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000479 * $Return: (None)
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000480 *
481 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000482static void parse_conf(const char *path, int flag)
483{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000484 FILE *f;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000485#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000486 Htaccess *prev, *cur;
487#elif CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
488 Htaccess *cur;
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000489#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000490
Glenn L McGrath06e95652003-02-09 06:51:14 +0000491 const char *cf = config->configFile;
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000492 char buf[160];
Glenn L McGrath06e95652003-02-09 06:51:14 +0000493 char *p0 = NULL;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000494 char *c, *p;
495
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000496 /* free previous ip setup if present */
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000497 Htaccess_IP *pip = config->ip_a_d;
498
499 while( pip ) {
500 Htaccess_IP *cur_ipl = pip;
501
502 pip = cur_ipl->next;
503 free(cur_ipl);
504 }
505 config->ip_a_d = NULL;
506
Glenn L McGrath24833432003-06-10 17:22:49 +0000507 config->flg_deny_all = 0;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000508
509#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000510 /* retain previous auth and mime config only for subdir parse */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000511 if(flag != SUBDIR_PARSE) {
512#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000513 free_config_lines(&config->auth);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000514#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000515#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
516 free_config_lines(&config->mime_a);
517#endif
518 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000519#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000520
521 if(flag == SUBDIR_PARSE || cf == NULL) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000522 cf = alloca(strlen(path) + sizeof(httpd_conf) + 2);
523 if(cf == NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000524 if(flag == FIRST_PARSE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000525 bb_error_msg_and_die(bb_msg_memory_exhausted);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000526 return;
527 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000528 sprintf((char *)cf, "%s/%s", path, httpd_conf);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000529 }
530
531 while((f = fopen(cf, "r")) == NULL) {
Eric Andersen35e643b2003-07-28 07:40:39 +0000532 if(flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) {
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000533 /* config file not found, no changes to config */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000534 return;
535 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000536 if(config->configFile && flag == FIRST_PARSE) /* if -c option given */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000537 bb_perror_msg_and_die("%s", cf);
538 flag = FIND_FROM_HTTPD_ROOT;
539 cf = httpd_conf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000540 }
541
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000542#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
543 prev = config->auth;
544#endif
545 /* This could stand some work */
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000546 while ( (p0 = fgets(buf, sizeof(buf), f)) != NULL) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000547 c = NULL;
548 for(p = p0; *p0 != 0 && *p0 != '#'; p0++) {
549 if(!isspace(*p0)) {
550 *p++ = *p0;
551 if(*p0 == ':' && c == NULL)
552 c = p;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000553 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000554 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000555 *p = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000556
557 /* test for empty or strange line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000558 if (c == NULL || *c == 0)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000559 continue;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000560 p0 = buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000561 if(*p0 == 'd')
562 *p0 = 'D';
Glenn L McGrath393183d2003-05-26 14:07:50 +0000563 if(*c == '*') {
564 if(*p0 == 'D') {
565 /* memorize deny all */
566 config->flg_deny_all++;
567 }
568 /* skip default other "word:*" config lines */
569 continue;
570 }
571
572 if(*p0 == 'a')
573 *p0 = 'A';
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000574 else if(*p0 != 'D' && *p0 != 'A'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000575#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000576 && *p0 != '/'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000577#endif
578#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000579 && *p0 != '.'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000580#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000581 )
Glenn L McGrath06e95652003-02-09 06:51:14 +0000582 continue;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000583 if(*p0 == 'A' || *p0 == 'D') {
584 /* storing current config IP line */
585 pip = calloc(1, sizeof(Htaccess_IP));
586 if(pip) {
587 if(scan_ip_mask (c, &(pip->ip), &(pip->mask))) {
588 /* syntax IP{/mask} error detected, protect all */
589 *p0 = 'D';
590 pip->mask = 0;
591 }
592 pip->allow_deny = *p0;
593 if(*p0 == 'D') {
594 /* Deny:form_IP move top */
595 pip->next = config->ip_a_d;
596 config->ip_a_d = pip;
597 } else {
598 /* add to bottom A:form_IP config line */
599 Htaccess_IP *prev_IP = config->ip_a_d;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000600
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000601 if(prev_IP == NULL) {
602 config->ip_a_d = pip;
603 } else {
604 while(prev_IP->next)
605 prev_IP = prev_IP->next;
606 prev_IP->next = pip;
607 }
608 }
609 }
610 continue;
611 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000612#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
613 if(*p0 == '/') {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000614 /* make full path from httpd root / curent_path / config_line_path */
615 cf = flag == SUBDIR_PARSE ? path : "";
616 p0 = malloc(strlen(cf) + (c - buf) + 2 + strlen(c));
617 if(p0 == NULL)
618 continue;
619 c[-1] = 0;
620 sprintf(p0, "/%s%s", cf, buf);
621
622 /* another call bb_simplify_path */
623 cf = p = p0;
624
625 do {
626 if (*p == '/') {
627 if (*cf == '/') { /* skip duplicate (or initial) slash */
628 continue;
629 } else if (*cf == '.') {
630 if (cf[1] == '/' || cf[1] == 0) { /* remove extra '.' */
631 continue;
632 } else if ((cf[1] == '.') && (cf[2] == '/' || cf[2] == 0)) {
633 ++cf;
634 if (p > p0) {
635 while (*--p != '/'); /* omit previous dir */
636 }
637 continue;
638 }
639 }
640 }
641 *++p = *cf;
642 } while (*++cf);
643
644 if ((p == p0) || (*p != '/')) { /* not a trailing slash */
645 ++p; /* so keep last character */
646 }
647 *p = 0;
648 sprintf(p0, "%s:%s", p0, c);
649 }
650#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000651
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000652#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
653 /* storing current config line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000654 cur = calloc(1, sizeof(Htaccess) + strlen(p0));
655 if(cur) {
656 cf = strcpy(cur->before_colon, p0);
657 c = strchr(cf, ':');
658 *c++ = 0;
659 cur->after_colon = c;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000660#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Glenn L McGrath5875be42003-09-08 15:39:09 +0000661 if(*cf == '.') {
Glenn L McGrath874e3382003-05-14 12:11:36 +0000662 /* config .mime line move top for overwrite previous */
663 cur->next = config->mime_a;
664 config->mime_a = cur;
Glenn L McGrath5875be42003-09-08 15:39:09 +0000665 continue;
Glenn L McGrath874e3382003-05-14 12:11:36 +0000666 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000667#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000668#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath5875be42003-09-08 15:39:09 +0000669 free(p0);
670 if(prev == NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000671 /* first line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000672 config->auth = prev = cur;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000673 } else {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000674 /* sort path, if current lenght eq or bigger then move up */
675 Htaccess *prev_hti = config->auth;
676 int l = strlen(cf);
677 Htaccess *hti;
678
679 for(hti = prev_hti; hti; hti = hti->next) {
680 if(l >= strlen(hti->before_colon)) {
681 /* insert before hti */
682 cur->next = hti;
683 if(prev_hti != hti) {
684 prev_hti->next = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000685 } else {
686 /* insert as top */
687 config->auth = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000688 }
Glenn L McGrath393183d2003-05-26 14:07:50 +0000689 break;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000690 }
691 if(prev_hti != hti)
692 prev_hti = prev_hti->next;
693 }
694 if(!hti) { /* not inserted, add to bottom */
695 prev->next = cur;
696 prev = cur;
697 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000698 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000699#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000700 }
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000701#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000702 }
703 fclose(f);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000704}
705
706#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000707/****************************************************************************
708 *
709 > $Function: encodeString()
710 *
711 * $Description: Given a string, html encode special characters.
712 * This is used for the -e command line option to provide an easy way
713 * for scripts to encode result data without confusing browsers. The
714 * returned string pointer is memory allocated by malloc().
715 *
716 * $Parameters:
717 * (const char *) string . . The first string to encode.
718 *
719 * $Return: (char *) . . . .. . . A pointer to the encoded string.
720 *
721 * $Errors: Returns a null string ("") if memory is not available.
722 *
723 ****************************************************************************/
724static char *encodeString(const char *string)
725{
726 /* take the simple route and encode everything */
727 /* could possibly scan once to get length. */
728 int len = strlen(string);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000729 char *out = malloc(len*5 +1);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000730 char *p=out;
731 char ch;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000732
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000733 if (!out) return "";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000734 while ((ch = *string++)) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000735 // very simple check for what to encode
736 if (isalnum(ch)) *p++ = ch;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000737 else p += sprintf(p, "&#%d", (unsigned char) ch);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000738 }
739 *p=0;
740 return out;
741}
Glenn L McGrath06e95652003-02-09 06:51:14 +0000742#endif /* CONFIG_FEATURE_HTTPD_ENCODE_URL_STR */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000743
744/****************************************************************************
745 *
746 > $Function: decodeString()
747 *
748 * $Description: Given a URL encoded string, convert it to plain ascii.
749 * Since decoding always makes strings smaller, the decode is done in-place.
750 * Thus, callers should strdup() the argument if they do not want the
751 * argument modified. The return is the original pointer, allowing this
752 * function to be easily used as arguments to other functions.
753 *
754 * $Parameters:
755 * (char *) string . . . The first string to decode.
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000756 * (int) flag . . . 1 if require decode '+' as ' ' for CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000757 *
758 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
759 *
760 * $Errors: None
761 *
762 ****************************************************************************/
Eric Andersena3bb3e62003-06-26 09:05:32 +0000763static char *decodeString(char *orig, int flag_plus_to_space)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000764{
765 /* note that decoded string is always shorter than original */
Eric Andersena3bb3e62003-06-26 09:05:32 +0000766 char *string = orig;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000767 char *ptr = string;
Eric Andersena3bb3e62003-06-26 09:05:32 +0000768
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000769 while (*ptr)
770 {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000771 if (*ptr == '+' && flag_plus_to_space) { *string++ = ' '; ptr++; }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000772 else if (*ptr != '%') *string++ = *ptr++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000773 else {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000774 unsigned int value;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000775 sscanf(ptr+1, "%2X", &value);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000776 *string++ = value;
777 ptr += 3;
778 }
779 }
780 *string = '\0';
781 return orig;
782}
783
784
Glenn L McGrath06e95652003-02-09 06:51:14 +0000785#ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000786/****************************************************************************
787 *
788 > $Function: addEnv()
789 *
Eric Andersenaff114c2004-04-14 17:51:38 +0000790 * $Description: Add an environment variable setting to the global list.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000791 * A NAME=VALUE string is allocated, filled, and added to the list of
792 * environment settings passed to the cgi execution script.
793 *
794 * $Parameters:
Glenn L McGrath06e95652003-02-09 06:51:14 +0000795 * (char *) name_before_underline - The first part environment variable name.
796 * (char *) name_after_underline - The second part environment variable name.
797 * (char *) value . . The value to which the env variable is set.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000798 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000799 * $Return: (void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000800 *
801 * $Errors: Silently returns if the env runs out of space to hold the new item
802 *
803 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000804static void addEnv(const char *name_before_underline,
805 const char *name_after_underline, const char *value)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000806{
Glenn L McGrath5875be42003-09-08 15:39:09 +0000807 char *s = NULL;
Glenn L McGrath393183d2003-05-26 14:07:50 +0000808 const char *underline;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000809
Glenn L McGrath06e95652003-02-09 06:51:14 +0000810 if (!value)
811 value = "";
Glenn L McGrath393183d2003-05-26 14:07:50 +0000812 underline = *name_after_underline ? "_" : "";
813 asprintf(&s, "%s%s%s=%s", name_before_underline, underline,
Glenn L McGrath06e95652003-02-09 06:51:14 +0000814 name_after_underline, value);
Glenn L McGrath393183d2003-05-26 14:07:50 +0000815 if(s) {
Glenn L McGrath5875be42003-09-08 15:39:09 +0000816 putenv(s);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000817 }
818}
819
Eric Andersena3bb3e62003-06-26 09:05:32 +0000820#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 +0000821/* set environs SERVER_PORT and REMOTE_PORT */
822static void addEnvPort(const char *port_name)
823{
824 char buf[16];
825
826 sprintf(buf, "%u", config->port);
827 addEnv(port_name, "PORT", buf);
828}
Eric Andersena3bb3e62003-06-26 09:05:32 +0000829#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000830#endif /* CONFIG_FEATURE_HTTPD_CGI */
831
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000832
833#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000834/****************************************************************************
835 *
836 > $Function: decodeBase64()
837 *
838 > $Description: Decode a base 64 data stream as per rfc1521.
839 * Note that the rfc states that none base64 chars are to be ignored.
840 * Since the decode always results in a shorter size than the input, it is
841 * OK to pass the input arg as an output arg.
842 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000843 * $Parameter:
844 * (char *) Data . . . . A pointer to a base64 encoded string.
845 * Where to place the decoded data.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000846 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000847 * $Return: void
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000848 *
849 * $Errors: None
850 *
851 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000852static void decodeBase64(char *Data)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000853{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000854
855 const unsigned char *in = Data;
856 // The decoded size will be at most 3/4 the size of the encoded
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000857 unsigned long ch = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000858 int i = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000859
860 while (*in) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000861 int t = *in++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000862
Glenn L McGrath874e3382003-05-14 12:11:36 +0000863 if(t >= '0' && t <= '9')
864 t = t - '0' + 52;
865 else if(t >= 'A' && t <= 'Z')
866 t = t - 'A';
867 else if(t >= 'a' && t <= 'z')
868 t = t - 'a' + 26;
869 else if(t == '+')
870 t = 62;
871 else if(t == '/')
872 t = 63;
873 else if(t == '=')
874 t = 0;
875 else
876 continue;
877
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000878 ch = (ch << 6) | t;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000879 i++;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000880 if (i == 4) {
881 *Data++ = (char) (ch >> 16);
882 *Data++ = (char) (ch >> 8);
883 *Data++ = (char) ch;
884 i = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000885 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000886 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000887 *Data = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000888}
889#endif
890
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000891
Glenn L McGrath06e95652003-02-09 06:51:14 +0000892#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000893/****************************************************************************
894 *
895 > $Function: openServer()
896 *
897 * $Description: create a listen server socket on the designated port.
898 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000899 * $Return: (int) . . . A connection socket. -1 for errors.
900 *
901 * $Errors: None
902 *
903 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000904static int openServer(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000905{
906 struct sockaddr_in lsocket;
907 int fd;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000908
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000909 /* create the socket right now */
910 /* inet_addr() returns a value that is already in network order */
911 memset(&lsocket, 0, sizeof(lsocket));
912 lsocket.sin_family = AF_INET;
913 lsocket.sin_addr.s_addr = INADDR_ANY;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000914 lsocket.sin_port = htons(config->port) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000915 fd = socket(AF_INET, SOCK_STREAM, 0);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000916 if (fd >= 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000917 /* tell the OS it's OK to reuse a previous address even though */
918 /* it may still be in a close down state. Allows bind to succeed. */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000919 int on = 1;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000920#ifdef SO_REUSEPORT
Glenn L McGrath06e95652003-02-09 06:51:14 +0000921 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *)&on, sizeof(on)) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000922#else
Glenn L McGrath06e95652003-02-09 06:51:14 +0000923 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000924#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000925 if (bind(fd, (struct sockaddr *)&lsocket, sizeof(lsocket)) == 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000926 listen(fd, 9);
927 signal(SIGCHLD, SIG_IGN); /* prevent zombie (defunct) processes */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000928 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000929 bb_perror_msg_and_die("bind");
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000930 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000931 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000932 bb_perror_msg_and_die("create socket");
Glenn L McGrath06e95652003-02-09 06:51:14 +0000933 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000934 return fd;
935}
Glenn L McGrath06e95652003-02-09 06:51:14 +0000936#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000937
938/****************************************************************************
939 *
940 > $Function: sendHeaders()
941 *
942 * $Description: Create and send HTTP response headers.
943 * The arguments are combined and sent as one write operation. Note that
944 * IE will puke big-time if the headers are not sent in one packet and the
Glenn L McGrath06e95652003-02-09 06:51:14 +0000945 * second packet is delayed for any reason.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000946 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000947 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000948 * (HttpResponseNum) responseNum . . . The result code to send.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000949 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000950 * $Return: (int) . . . . writing errors
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000951 *
952 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000953static int sendHeaders(HttpResponseNum responseNum)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000954{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000955 char *buf = config->buf;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000956 const char *responseString = "";
957 const char *infoString = 0;
Eric Andersen07f2fea2004-10-08 08:03:29 +0000958 const char *mime_type;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000959 unsigned int i;
960 time_t timer = time(0);
961 char timeStr[80];
Glenn L McGrath06e95652003-02-09 06:51:14 +0000962 int len;
963
964 for (i = 0;
965 i < (sizeof(httpResponseNames)/sizeof(httpResponseNames[0])); i++) {
966 if (httpResponseNames[i].type == responseNum) {
967 responseString = httpResponseNames[i].name;
968 infoString = httpResponseNames[i].info;
969 break;
970 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000971 }
Eric Andersen07f2fea2004-10-08 08:03:29 +0000972 /* error message is HTML */
973 mime_type = responseNum == HTTP_OK ?
974 config->httpd_found.found_mime_type : "text/html";
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000975
976 /* emit the current date */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000977 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&timer));
978 len = sprintf(buf,
979 "HTTP/1.0 %d %s\nContent-type: %s\r\n"
980 "Date: %s\r\nConnection: close\r\n",
Eric Andersen07f2fea2004-10-08 08:03:29 +0000981 responseNum, responseString, mime_type, timeStr);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000982
Glenn L McGrath3d2405c2003-02-10 22:28:21 +0000983#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath06e95652003-02-09 06:51:14 +0000984 if (responseNum == HTTP_UNAUTHORIZED) {
985 len += sprintf(buf+len, "WWW-Authenticate: Basic realm=\"%s\"\r\n",
986 config->realm);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000987 }
Glenn L McGrath3d2405c2003-02-10 22:28:21 +0000988#endif
Eric Andersen07f2fea2004-10-08 08:03:29 +0000989 if(responseNum == HTTP_MOVED_TEMPORARILY) {
990 len += sprintf(buf+len, "Location: %s/%s%s\r\n",
991 config->httpd_found.found_moved_temporarily,
992 (config->query ? "?" : ""),
993 (config->query ? config->query : ""));
994 }
995
Glenn L McGrath06e95652003-02-09 06:51:14 +0000996 if (config->ContentLength != -1) { /* file */
997 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&config->last_mod));
Glenn L McGrath5cd64612003-08-29 15:53:23 +0000998 len += sprintf(buf+len, "Last-Modified: %s\r\n%s " cont_l_fmt "\r\n",
Glenn L McGrath06e95652003-02-09 06:51:14 +0000999 timeStr, Content_length, config->ContentLength);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001000 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001001 strcat(buf, "\r\n");
1002 len += 2;
1003 if (infoString) {
1004 len += sprintf(buf+len,
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001005 "<HEAD><TITLE>%d %s</TITLE></HEAD>\n"
1006 "<BODY><H1>%d %s</H1>\n%s\n</BODY>\n",
1007 responseNum, responseString,
Glenn L McGrath06e95652003-02-09 06:51:14 +00001008 responseNum, responseString, infoString);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001009 }
1010#ifdef DEBUG
Glenn L McGrath06e95652003-02-09 06:51:14 +00001011 if (config->debugHttpd) fprintf(stderr, "Headers: '%s'", buf);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001012#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +00001013 return bb_full_write(a_c_w, buf, len);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001014}
1015
1016/****************************************************************************
1017 *
1018 > $Function: getLine()
1019 *
1020 * $Description: Read from the socket until an end of line char found.
1021 *
1022 * Characters are read one at a time until an eol sequence is found.
1023 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001024 * $Return: (int) . . . . number of characters read. -1 if error.
1025 *
1026 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001027static int getLine(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001028{
1029 int count = 0;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001030 char *buf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001031
1032 while (read(a_c_r, buf + count, 1) == 1) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001033 if (buf[count] == '\r') continue;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001034 if (buf[count] == '\n') {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001035 buf[count] = 0;
1036 return count;
1037 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001038 if(count < (MAX_MEMORY_BUFF-1)) /* check owerflow */
1039 count++;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001040 }
1041 if (count) return count;
1042 else return -1;
1043}
1044
Glenn L McGrath06e95652003-02-09 06:51:14 +00001045#ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001046/****************************************************************************
1047 *
1048 > $Function: sendCgi()
1049 *
1050 * $Description: Execute a CGI script and send it's stdout back
1051 *
1052 * Environment variables are set up and the script is invoked with pipes
1053 * for stdin/stdout. If a post is being done the script is fed the POST
1054 * data in addition to setting the QUERY_STRING variable (for GETs or POSTs).
1055 *
1056 * $Parameters:
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001057 * (const char *) url . . . . . . The requested URL (with leading /).
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001058 * (int bodyLen) . . . . . . . . Length of the post body.
1059 * (const char *cookie) . . . . . For set HTTP_COOKIE.
1060 * (const char *content_type) . . For set CONTENT_TYPE.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001061
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001062 *
1063 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
1064 *
1065 * $Errors: None
1066 *
1067 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001068static int sendCgi(const char *url,
Eric Andersen07f2fea2004-10-08 08:03:29 +00001069 const char *request, int bodyLen, const char *cookie,
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001070 const char *content_type)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001071{
1072 int fromCgi[2]; /* pipe for reading data from CGI */
1073 int toCgi[2]; /* pipe for sending data to CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001074
Glenn L McGrath06e95652003-02-09 06:51:14 +00001075 static char * argp[] = { 0, 0 };
1076 int pid = 0;
1077 int inFd;
1078 int outFd;
1079 int firstLine = 1;
1080
1081 do {
1082 if (pipe(fromCgi) != 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001083 break;
1084 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001085 if (pipe(toCgi) != 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001086 break;
1087 }
1088
1089 pid = fork();
Glenn L McGrath06e95652003-02-09 06:51:14 +00001090 if (pid < 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001091 pid = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001092 break;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001093 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001094
1095 if (!pid) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001096 /* child process */
1097 char *script;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001098 char *purl = strdup( url );
1099 char realpath_buff[MAXPATHLEN];
1100
1101 if(purl == NULL)
1102 _exit(242);
1103
1104 inFd = toCgi[0];
1105 outFd = fromCgi[1];
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001106
1107 dup2(inFd, 0); // replace stdin with the pipe
1108 dup2(outFd, 1); // replace stdout with the pipe
Glenn L McGrath06e95652003-02-09 06:51:14 +00001109
1110#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1111 if (!config->debugHttpd)
1112#endif
1113 dup2(outFd, 2); // replace stderr with the pipe
1114
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001115 close(toCgi[0]);
1116 close(toCgi[1]);
1117 close(fromCgi[0]);
1118 close(fromCgi[1]);
1119
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001120 /*
Glenn L McGrath06e95652003-02-09 06:51:14 +00001121 * Find PATH_INFO.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001122 */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001123 script = purl;
1124 while((script = strchr( script + 1, '/' )) != NULL) {
1125 /* have script.cgi/PATH_INFO or dirs/script.cgi[/PATH_INFO] */
1126 struct stat sb;
1127
1128 *script = '\0';
1129 if(is_directory(purl + 1, 1, &sb) == 0) {
1130 /* not directory, found script.cgi/PATH_INFO */
1131 *script = '/';
1132 break;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001133 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001134 *script = '/'; /* is directory, find next '/' */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001135 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001136 addEnv("PATH", "INFO", script); /* set /PATH_INFO or NULL */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001137 addEnv("PATH", "", getenv("PATH"));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001138 addEnv("REQUEST", "METHOD", request);
Eric Andersen07f2fea2004-10-08 08:03:29 +00001139 if(config->query) {
1140 char *uri = alloca(strlen(purl) + 2 + strlen(config->query));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001141 if(uri)
Eric Andersen07f2fea2004-10-08 08:03:29 +00001142 sprintf(uri, "%s?%s", purl, config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001143 addEnv("REQUEST", "URI", uri);
1144 } else {
1145 addEnv("REQUEST", "URI", purl);
1146 }
1147 if(script != NULL)
1148 *script = '\0'; /* reduce /PATH_INFO */
1149 /* set SCRIPT_NAME as full path: /cgi-bin/dirs/script.cgi */
1150 addEnv("SCRIPT_NAME", "", purl);
Eric Andersen07f2fea2004-10-08 08:03:29 +00001151 addEnv("QUERY_STRING", "", config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001152 addEnv("SERVER", "SOFTWARE", httpdVersion);
1153 addEnv("SERVER", "PROTOCOL", "HTTP/1.0");
1154 addEnv("GATEWAY_INTERFACE", "", "CGI/1.1");
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001155 addEnv("REMOTE", "ADDR", config->rmt_ip_str);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001156#ifdef CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
Glenn L McGrath06e95652003-02-09 06:51:14 +00001157 addEnvPort("REMOTE");
Glenn L McGrath06e95652003-02-09 06:51:14 +00001158#endif
1159 if(bodyLen) {
1160 char sbl[32];
1161
1162 sprintf(sbl, "%d", bodyLen);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001163 addEnv("CONTENT", "LENGTH", sbl);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001164 }
1165 if(cookie)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001166 addEnv("HTTP", "COOKIE", cookie);
1167 if(content_type)
1168 addEnv("CONTENT", "TYPE", content_type);
Eric Andersen769a3ef2003-12-19 11:23:47 +00001169#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001170 if(config->remoteuser) {
1171 addEnv("REMOTE", "USER", config->remoteuser);
1172 addEnv("AUTH_TYPE", "", "Basic");
Glenn L McGrath06e95652003-02-09 06:51:14 +00001173 }
Eric Andersen769a3ef2003-12-19 11:23:47 +00001174#endif
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001175 if(config->referer)
1176 addEnv("HTTP", "REFERER", config->referer);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001177
1178 /* set execve argp[0] without path */
1179 argp[0] = strrchr( purl, '/' ) + 1;
1180 /* but script argp[0] must have absolute path and chdiring to this */
1181 if(realpath(purl + 1, realpath_buff) != NULL) {
1182 script = strrchr(realpath_buff, '/');
1183 if(script) {
1184 *script = '\0';
1185 if(chdir(realpath_buff) == 0) {
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00001186 *script = '/';
1187 // now run the program. If it fails,
1188 // use _exit() so no destructors
1189 // get called and make a mess.
Glenn L McGrath5875be42003-09-08 15:39:09 +00001190 execv(realpath_buff, argp);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001191 }
1192 }
1193 }
1194#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1195 config->accepted_socket = 1; /* send to stdout */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001196#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001197 sendHeaders(HTTP_NOT_FOUND);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001198 _exit(242);
1199 } /* end child */
1200
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001201 } while (0);
1202
Glenn L McGrath06e95652003-02-09 06:51:14 +00001203 if (pid) {
1204 /* parent process */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001205 int status;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001206 size_t post_readed_size = 0, post_readed_idx = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001207
1208 inFd = fromCgi[0];
1209 outFd = toCgi[1];
1210 close(fromCgi[1]);
1211 close(toCgi[0]);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001212 signal(SIGPIPE, SIG_IGN);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001213
1214 while (1) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001215 fd_set readSet;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001216 fd_set writeSet;
1217 char wbuf[128];
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001218 int nfound;
1219 int count;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001220
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001221 FD_ZERO(&readSet);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001222 FD_ZERO(&writeSet);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001223 FD_SET(inFd, &readSet);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001224 if(bodyLen > 0 || post_readed_size > 0) {
1225 FD_SET(outFd, &writeSet);
1226 nfound = outFd > inFd ? outFd : inFd;
1227 if(post_readed_size == 0) {
1228 FD_SET(a_c_r, &readSet);
1229 if(nfound < a_c_r)
1230 nfound = a_c_r;
1231 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001232 /* Now wait on the set of sockets! */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001233 nfound = select(nfound + 1, &readSet, &writeSet, 0, NULL);
1234 } else {
1235 if(!bodyLen) {
1236 close(outFd);
1237 bodyLen = -1;
1238 }
1239 nfound = select(inFd + 1, &readSet, 0, 0, NULL);
1240 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001241
Glenn L McGrath06e95652003-02-09 06:51:14 +00001242 if (nfound <= 0) {
1243 if (waitpid(pid, &status, WNOHANG) > 0) {
1244 close(inFd);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001245#ifdef DEBUG
Glenn L McGrath06e95652003-02-09 06:51:14 +00001246 if (config->debugHttpd) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001247 if (WIFEXITED(status))
Manuel Novoa III cad53642003-03-19 09:13:01 +00001248 bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001249 if (WIFSIGNALED(status))
Manuel Novoa III cad53642003-03-19 09:13:01 +00001250 bb_error_msg("piped has exited with signal=%d", WTERMSIG(status));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001251 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001252#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001253 break;
1254 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001255 } else if(post_readed_size > 0 && FD_ISSET(outFd, &writeSet)) {
1256 count = bb_full_write(outFd, wbuf + post_readed_idx, post_readed_size);
1257 if(count > 0) {
1258 post_readed_size -= count;
1259 post_readed_idx += count;
1260 if(post_readed_size == 0)
1261 post_readed_idx = 0;
1262 }
1263 } else if(bodyLen > 0 && post_readed_size == 0 && FD_ISSET(a_c_r, &readSet)) {
1264 count = bodyLen > sizeof(wbuf) ? sizeof(wbuf) : bodyLen;
Eric Andersen97a1de12004-08-26 22:22:50 +00001265 count = safe_read(a_c_r, wbuf, count);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001266 if(count > 0) {
1267 post_readed_size += count;
1268 bodyLen -= count;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001269 } else {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001270 bodyLen = 0; /* closed */
1271 }
Eric Andersen97a1de12004-08-26 22:22:50 +00001272 }
1273 if(FD_ISSET(inFd, &readSet)) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001274 int s = a_c_w;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001275 char *rbuf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001276
Eric Andersen97a1de12004-08-26 22:22:50 +00001277#ifndef PIPE_BUF
1278# define PIPESIZE 4096 /* amount of buffering in a pipe */
1279#else
1280# define PIPESIZE PIPE_BUF
1281#endif
1282#if PIPESIZE >= MAX_MEMORY_BUFF
1283# error "PIPESIZE >= MAX_MEMORY_BUFF"
1284#endif
1285
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001286 // There is something to read
Eric Andersen97a1de12004-08-26 22:22:50 +00001287 count = safe_read(inFd, rbuf, PIPESIZE);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001288 if (count == 0)
1289 break; /* closed */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001290 if (count > 0) {
1291 if (firstLine) {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001292 rbuf[count] = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001293 /* check to see if the user script added headers */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001294 if(strncmp(rbuf, "HTTP/1.0 200 OK\n", 4) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001295 bb_full_write(s, "HTTP/1.0 200 OK\n", 16);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001296 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001297 if (strstr(rbuf, "ontent-") == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001298 bb_full_write(s, "Content-type: text/plain\n\n", 26);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001299 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001300 firstLine = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001301 }
Eric Andersenef437492004-02-04 11:10:28 +00001302 if (bb_full_write(s, rbuf, count) != count)
1303 break;
1304
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001305#ifdef DEBUG
Glenn L McGrath06e95652003-02-09 06:51:14 +00001306 if (config->debugHttpd)
1307 fprintf(stderr, "cgi read %d bytes\n", count);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001308#endif
1309 }
1310 }
1311 }
1312 }
1313 return 0;
1314}
Glenn L McGrath06e95652003-02-09 06:51:14 +00001315#endif /* CONFIG_FEATURE_HTTPD_CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001316
1317/****************************************************************************
1318 *
1319 > $Function: sendFile()
1320 *
1321 * $Description: Send a file response to an HTTP request
1322 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00001323 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001324 * (const char *) url . . The URL requested.
1325 *
1326 * $Return: (int) . . . . . . Always 0.
1327 *
1328 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001329static int sendFile(const char *url)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001330{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001331 char * suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001332 int f;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001333 const char * const * table;
1334 const char * try_suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001335
Glenn L McGrath06e95652003-02-09 06:51:14 +00001336 suffix = strrchr(url, '.');
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001337
Glenn L McGrath06e95652003-02-09 06:51:14 +00001338 for (table = suffixTable; *table; table += 2)
1339 if(suffix != NULL && (try_suffix = strstr(*table, suffix)) != 0) {
1340 try_suffix += strlen(suffix);
1341 if(*try_suffix == 0 || *try_suffix == '.')
1342 break;
1343 }
1344 /* also, if not found, set default as "application/octet-stream"; */
Eric Andersen07f2fea2004-10-08 08:03:29 +00001345 config->httpd_found.found_mime_type = *(table+1);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001346#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
1347 if (suffix) {
1348 Htaccess * cur;
1349
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001350 for (cur = config->mime_a; cur; cur = cur->next) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001351 if(strcmp(cur->before_colon, suffix) == 0) {
Eric Andersen07f2fea2004-10-08 08:03:29 +00001352 config->httpd_found.found_mime_type = cur->after_colon;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001353 break;
1354 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001355 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001356 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001357#endif /* CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES */
1358
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001359#ifdef DEBUG
Glenn L McGrath06e95652003-02-09 06:51:14 +00001360 if (config->debugHttpd)
1361 fprintf(stderr, "Sending file '%s' Content-type: %s\n",
Eric Andersen07f2fea2004-10-08 08:03:29 +00001362 url, config->httpd_found.found_mime_type);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001363#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001364
1365 f = open(url, O_RDONLY);
1366 if (f >= 0) {
1367 int count;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001368 char *buf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001369
1370 sendHeaders(HTTP_OK);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001371 while ((count = bb_full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
Glenn L McGrath9adcf732003-12-08 20:21:53 +00001372 if (bb_full_write(a_c_w, buf, count) != count)
1373 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001374 }
1375 close(f);
1376 } else {
1377#ifdef DEBUG
1378 if (config->debugHttpd)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001379 bb_perror_msg("Unable to open '%s'", url);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001380#endif
1381 sendHeaders(HTTP_NOT_FOUND);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001382 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001383
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001384 return 0;
1385}
1386
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001387static int checkPermIP(void)
1388{
1389 Htaccess_IP * cur;
1390
1391 /* This could stand some work */
1392 for (cur = config->ip_a_d; cur; cur = cur->next) {
1393#ifdef DEBUG
1394 if (config->debugHttpd) {
1395 fprintf(stderr, "checkPermIP: '%s' ? ", config->rmt_ip_str);
1396 fprintf(stderr, "'%u.%u.%u.%u/%u.%u.%u.%u'\n",
1397 (unsigned char)(cur->ip >> 24),
1398 (unsigned char)(cur->ip >> 16),
1399 (unsigned char)(cur->ip >> 8),
1400 cur->ip & 0xff,
1401 (unsigned char)(cur->mask >> 24),
1402 (unsigned char)(cur->mask >> 16),
1403 (unsigned char)(cur->mask >> 8),
1404 cur->mask & 0xff);
1405 }
1406#endif
1407 if((config->rmt_ip & cur->mask) == cur->ip)
1408 return cur->allow_deny == 'A'; /* Allow/Deny */
1409 }
1410
Eric Andersenaff114c2004-04-14 17:51:38 +00001411 /* if unconfigured, return 1 - access from all */
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001412 return !config->flg_deny_all;
1413}
1414
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001415/****************************************************************************
1416 *
1417 > $Function: checkPerm()
1418 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001419 * $Description: Check the permission file for access password protected.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001420 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001421 * If config file isn't present, everything is allowed.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001422 * Entries are of the form you can see example from header source
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001423 *
1424 * $Parameters:
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001425 * (const char *) path . . . . The file path.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001426 * (const char *) request . . . User information to validate.
1427 *
1428 * $Return: (int) . . . . . . . . . 1 if request OK, 0 otherwise.
1429 *
1430 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001431
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001432#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001433static int checkPerm(const char *path, const char *request)
1434{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001435 Htaccess * cur;
1436 const char *p;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001437 const char *p0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001438
Glenn L McGrath06e95652003-02-09 06:51:14 +00001439 const char *prev = NULL;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001440
1441 /* This could stand some work */
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001442 for (cur = config->auth; cur; cur = cur->next) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001443 p0 = cur->before_colon;
1444 if(prev != NULL && strcmp(prev, p0) != 0)
1445 continue; /* find next identical */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001446 p = cur->after_colon;
1447#ifdef DEBUG
1448 if (config->debugHttpd)
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001449 fprintf(stderr,"checkPerm: '%s' ? '%s'\n", p0, request);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001450#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001451 {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001452 int l = strlen(p0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001453
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001454 if(strncmp(p0, path, l) == 0 &&
1455 (l == 1 || path[l] == '/' || path[l] == 0)) {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001456 char *u;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001457 /* path match found. Check request */
Eric Andersen35e643b2003-07-28 07:40:39 +00001458 /* for check next /path:user:password */
1459 prev = p0;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001460 u = strchr(request, ':');
1461 if(u == NULL) {
1462 /* bad request, ':' required */
1463 break;
1464 }
1465
Eric Andersen35e643b2003-07-28 07:40:39 +00001466#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
1467 {
1468 char *cipher;
1469 char *pp;
Eric Andersen35e643b2003-07-28 07:40:39 +00001470
Eric Andersen35e643b2003-07-28 07:40:39 +00001471 if(strncmp(p, request, u-request) != 0) {
1472 /* user uncompared */
1473 continue;
1474 }
1475 pp = strchr(p, ':');
1476 if(pp && pp[1] == '$' && pp[2] == '1' &&
1477 pp[3] == '$' && pp[4]) {
1478 pp++;
1479 cipher = pw_encrypt(u+1, pp);
1480 if (strcmp(cipher, pp) == 0)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001481 goto set_remoteuser_var; /* Ok */
Eric Andersen35e643b2003-07-28 07:40:39 +00001482 /* unauthorized */
1483 continue;
1484 }
1485 }
1486#endif
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001487 if (strcmp(p, request) == 0) {
Glenn L McGrath9d1a33c2003-10-06 13:23:06 +00001488#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001489set_remoteuser_var:
Glenn L McGrath9d1a33c2003-10-06 13:23:06 +00001490#endif
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001491 config->remoteuser = strdup(request);
1492 if(config->remoteuser)
1493 config->remoteuser[(u - request)] = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001494 return 1; /* Ok */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001495 }
Eric Andersen35e643b2003-07-28 07:40:39 +00001496 /* unauthorized */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001497 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001498 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001499 } /* for */
1500
Glenn L McGrath06e95652003-02-09 06:51:14 +00001501 return prev == NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001502}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001503
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001504#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1505
Eric Andersen07f2fea2004-10-08 08:03:29 +00001506/****************************************************************************
1507 *
1508 > $Function: handleIncoming()
1509 *
1510 * $Description: Handle an incoming http request.
1511 *
1512 ****************************************************************************/
1513
1514static void
1515handle_sigalrm( int sig )
1516{
1517 sendHeaders(HTTP_REQUEST_TIMEOUT);
1518 config->alarm_signaled = sig;
1519}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001520
1521/****************************************************************************
1522 *
1523 > $Function: handleIncoming()
1524 *
1525 * $Description: Handle an incoming http request.
1526 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001527 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001528static void handleIncoming(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001529{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001530 char *buf = config->buf;
1531 char *url;
1532 char *purl;
1533 int blank = -1;
Glenn L McGrath3d752f72004-03-05 09:38:16 +00001534 char *test;
1535 struct stat sb;
1536 int ip_allowed;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001537#ifdef CONFIG_FEATURE_HTTPD_CGI
1538 const char *prequest = request_GET;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001539 long length=0;
1540 char *cookie = 0;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001541 char *content_type = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001542#endif
Glenn L McGrath3d752f72004-03-05 09:38:16 +00001543#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1544 fd_set s_fd;
1545 struct timeval tv;
Eric Andersend8746cd2004-02-24 07:28:38 +00001546 int retval;
Glenn L McGrath3d752f72004-03-05 09:38:16 +00001547#endif
Eric Andersen07f2fea2004-10-08 08:03:29 +00001548 struct sigaction sa;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001549
1550#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1551 int credentials = -1; /* if not requred this is Ok */
1552#endif
1553
Eric Andersen07f2fea2004-10-08 08:03:29 +00001554 sa.sa_handler = handle_sigalrm;
1555 sigemptyset(&sa.sa_mask);
1556 sa.sa_flags = 0; /* no SA_RESTART */
1557 sigaction(SIGALRM, &sa, NULL);
1558
Glenn L McGrath06e95652003-02-09 06:51:14 +00001559 do {
1560 int count;
1561
Eric Andersen07f2fea2004-10-08 08:03:29 +00001562 (void) alarm( TIMEOUT );
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001563 if (getLine() <= 0)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001564 break; /* closed */
1565
1566 purl = strpbrk(buf, " \t");
1567 if(purl == NULL) {
1568BAD_REQUEST:
1569 sendHeaders(HTTP_BAD_REQUEST);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001570 break;
1571 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001572 *purl = 0;
1573#ifdef CONFIG_FEATURE_HTTPD_CGI
1574 if(strcasecmp(buf, prequest) != 0) {
1575 prequest = "POST";
1576 if(strcasecmp(buf, prequest) != 0) {
1577 sendHeaders(HTTP_NOT_IMPLEMENTED);
1578 break;
1579 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001580 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001581#else
1582 if(strcasecmp(buf, request_GET) != 0) {
1583 sendHeaders(HTTP_NOT_IMPLEMENTED);
1584 break;
1585 }
1586#endif
1587 *purl = ' ';
1588 count = sscanf(purl, " %[^ ] HTTP/%d.%*d", buf, &blank);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001589
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001590 decodeString(buf, 0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001591 if (count < 1 || buf[0] != '/') {
1592 /* Garbled request/URL */
1593 goto BAD_REQUEST;
1594 }
1595 url = alloca(strlen(buf) + 12); /* + sizeof("/index.html\0") */
1596 if(url == NULL) {
1597 sendHeaders(HTTP_INTERNAL_SERVER_ERROR);
1598 break;
1599 }
1600 strcpy(url, buf);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001601 /* extract url args if present */
Eric Andersen07f2fea2004-10-08 08:03:29 +00001602 test = strchr(url, '?');
1603 if (test) {
1604 *test++ = 0;
1605 config->query = test;
1606 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001607
Manuel Novoa III cad53642003-03-19 09:13:01 +00001608 /* algorithm stolen from libbb bb_simplify_path(),
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001609 but don`t strdup and reducing trailing slash and protect out root */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001610 purl = test = url;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001611
Glenn L McGrath06e95652003-02-09 06:51:14 +00001612 do {
1613 if (*purl == '/') {
1614 if (*test == '/') { /* skip duplicate (or initial) slash */
1615 continue;
1616 } else if (*test == '.') {
1617 if (test[1] == '/' || test[1] == 0) { /* skip extra '.' */
1618 continue;
1619 } else if ((test[1] == '.') && (test[2] == '/' || test[2] == 0)) {
1620 ++test;
1621 if (purl == url) {
1622 /* protect out root */
1623 goto BAD_REQUEST;
1624 }
1625 while (*--purl != '/'); /* omit previous dir */
1626 continue;
1627 }
1628 }
1629 }
1630 *++purl = *test;
1631 } while (*++test);
1632
1633 *++purl = 0; /* so keep last character */
1634 test = purl; /* end ptr */
1635
1636 /* If URL is directory, adding '/' */
Eric Andersen07f2fea2004-10-08 08:03:29 +00001637 /* If URL is directory, adding '/' */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001638 if(test[-1] != '/') {
1639 if ( is_directory(url + 1, 1, &sb) ) {
Eric Andersen07f2fea2004-10-08 08:03:29 +00001640 config->httpd_found.found_moved_temporarily = url;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001641 }
1642 }
1643#ifdef DEBUG
1644 if (config->debugHttpd)
Eric Andersen07f2fea2004-10-08 08:03:29 +00001645 fprintf(stderr, "url='%s', args=%s\n", url, config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001646#endif
1647
1648 test = url;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001649 ip_allowed = checkPermIP();
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001650 while(ip_allowed && (test = strchr( test + 1, '/' )) != NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001651 /* have path1/path2 */
1652 *test = '\0';
1653 if( is_directory(url + 1, 1, &sb) ) {
1654 /* may be having subdir config */
1655 parse_conf(url + 1, SUBDIR_PARSE);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001656 ip_allowed = checkPermIP();
Glenn L McGrath06e95652003-02-09 06:51:14 +00001657 }
1658 *test = '/';
1659 }
1660
1661 // read until blank line for HTTP version specified, else parse immediate
Eric Andersen07f2fea2004-10-08 08:03:29 +00001662 while (blank >= 0 && alarm(TIMEOUT) >= 0 && (count = getLine()) > 0) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001663
1664#ifdef DEBUG
1665 if (config->debugHttpd) fprintf(stderr, "Header: '%s'\n", buf);
1666#endif
1667
1668#ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001669 /* try and do our best to parse more lines */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001670 if ((strncasecmp(buf, Content_length, 15) == 0)) {
1671 if(prequest != request_GET)
1672 length = strtol(buf + 15, 0, 0); // extra read only for POST
1673 } else if ((strncasecmp(buf, "Cookie:", 7) == 0)) {
1674 for(test = buf + 7; isspace(*test); test++)
1675 ;
1676 cookie = strdup(test);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001677 } else if ((strncasecmp(buf, "Content-Type:", 13) == 0)) {
1678 for(test = buf + 13; isspace(*test); test++)
1679 ;
1680 content_type = strdup(test);
1681 } else if ((strncasecmp(buf, "Referer:", 8) == 0)) {
1682 for(test = buf + 8; isspace(*test); test++)
1683 ;
1684 config->referer = strdup(test);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001685 }
1686#endif
1687
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001688#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath06e95652003-02-09 06:51:14 +00001689 if (strncasecmp(buf, "Authorization:", 14) == 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001690 /* We only allow Basic credentials.
1691 * It shows up as "Authorization: Basic <userid:password>" where
1692 * the userid:password is base64 encoded.
1693 */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001694 for(test = buf + 14; isspace(*test); test++)
1695 ;
1696 if (strncasecmp(test, "Basic", 5) != 0)
1697 continue;
1698
1699 test += 5; /* decodeBase64() skiping space self */
1700 decodeBase64(test);
1701 credentials = checkPerm(url, test);
1702 }
1703#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1704
1705 } /* while extra header reading */
1706
Eric Andersen07f2fea2004-10-08 08:03:29 +00001707 (void) alarm( 0 );
1708 if(config->alarm_signaled)
1709 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001710
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001711 if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001712 /* protect listing [/path]/httpd_conf or IP deny */
1713#ifdef CONFIG_FEATURE_HTTPD_CGI
1714FORBIDDEN: /* protect listing /cgi-bin */
1715#endif
1716 sendHeaders(HTTP_FORBIDDEN);
1717 break;
1718 }
1719
1720#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1721 if (credentials <= 0 && checkPerm(url, ":") == 0) {
1722 sendHeaders(HTTP_UNAUTHORIZED);
1723 break;
1724 }
1725#endif
1726
Eric Andersen07f2fea2004-10-08 08:03:29 +00001727 if(config->httpd_found.found_moved_temporarily) {
1728 sendHeaders(HTTP_MOVED_TEMPORARILY);
1729#ifdef DEBUG
1730 /* clear unforked memory flag */
1731 if(config->debugHttpd)
1732 config->httpd_found.found_moved_temporarily = NULL;
1733#endif
1734 break;
1735 }
1736
Glenn L McGrath06e95652003-02-09 06:51:14 +00001737 test = url + 1; /* skip first '/' */
1738
1739#ifdef CONFIG_FEATURE_HTTPD_CGI
1740 /* if strange Content-Length */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001741 if (length < 0)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001742 break;
1743
Glenn L McGrath06e95652003-02-09 06:51:14 +00001744 if (strncmp(test, "cgi-bin", 7) == 0) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001745 if(test[7] == '/' && test[8] == 0)
1746 goto FORBIDDEN; // protect listing cgi-bin/
Eric Andersen07f2fea2004-10-08 08:03:29 +00001747 sendCgi(url, prequest, length, cookie, content_type);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001748 } else {
1749 if (prequest != request_GET)
1750 sendHeaders(HTTP_NOT_IMPLEMENTED);
1751 else {
1752#endif /* CONFIG_FEATURE_HTTPD_CGI */
1753 if(purl[-1] == '/')
1754 strcpy(purl, "index.html");
1755 if ( stat(test, &sb ) == 0 ) {
1756 config->ContentLength = sb.st_size;
1757 config->last_mod = sb.st_mtime;
1758 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001759 sendFile(test);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001760#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1761 /* unset if non inetd looped */
1762 config->ContentLength = -1;
1763#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001764
Glenn L McGrath06e95652003-02-09 06:51:14 +00001765#ifdef CONFIG_FEATURE_HTTPD_CGI
1766 }
1767 }
1768#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001769
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001770 } while (0);
1771
Glenn L McGrath06e95652003-02-09 06:51:14 +00001772
1773#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1774/* from inetd don`t looping: freeing, closing automatic from exit always */
1775# ifdef DEBUG
1776 if (config->debugHttpd) fprintf(stderr, "closing socket\n");
1777# endif
1778# ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath06e95652003-02-09 06:51:14 +00001779 free(cookie);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001780 free(content_type);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001781 free(config->referer);
Eric Andersen769a3ef2003-12-19 11:23:47 +00001782#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1783 free(config->remoteuser);
1784#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001785# endif
1786 shutdown(a_c_w, SHUT_WR);
Eric Andersend8746cd2004-02-24 07:28:38 +00001787
1788 /* Properly wait for remote to closed */
1789 FD_ZERO (&s_fd) ;
1790 FD_SET (a_c_w, &s_fd) ;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001791
Eric Andersend8746cd2004-02-24 07:28:38 +00001792 do {
1793 tv.tv_sec = 2 ;
1794 tv.tv_usec = 0 ;
1795 retval = select (a_c_w + 1, &s_fd, NULL, NULL, &tv);
1796 } while (retval > 0 && (read (a_c_w, buf, sizeof (config->buf)) > 0));
1797
Glenn L McGrath06e95652003-02-09 06:51:14 +00001798 shutdown(a_c_r, SHUT_RD);
1799 close(config->accepted_socket);
1800#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001801}
1802
1803/****************************************************************************
1804 *
1805 > $Function: miniHttpd()
1806 *
1807 * $Description: The main http server function.
1808 *
1809 * Given an open socket fildes, listen for new connections and farm out
1810 * the processing as a forked process.
1811 *
1812 * $Parameters:
1813 * (int) server. . . The server socket fildes.
1814 *
1815 * $Return: (int) . . . . Always 0.
1816 *
1817 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001818#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001819static int miniHttpd(int server)
1820{
1821 fd_set readfd, portfd;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001822
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001823 FD_ZERO(&portfd);
1824 FD_SET(server, &portfd);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001825
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001826 /* copy the ports we are watching to the readfd set */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001827 while (1) {
1828 readfd = portfd;
1829
Eric Andersenaff114c2004-04-14 17:51:38 +00001830 /* Now wait INDEFINITELY on the set of sockets! */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001831 if (select(server + 1, &readfd, 0, 0, 0) > 0) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001832 if (FD_ISSET(server, &readfd)) {
1833 int on;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001834 struct sockaddr_in fromAddr;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001835
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001836 socklen_t fromAddrLen = sizeof(fromAddr);
1837 int s = accept(server,
Glenn L McGrath06e95652003-02-09 06:51:14 +00001838 (struct sockaddr *)&fromAddr, &fromAddrLen);
1839
1840 if (s < 0) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001841 continue;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001842 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001843 config->accepted_socket = s;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001844 config->rmt_ip = ntohl(fromAddr.sin_addr.s_addr);
1845#if defined(CONFIG_FEATURE_HTTPD_CGI) || defined(DEBUG)
1846 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1847 (unsigned char)(config->rmt_ip >> 24),
1848 (unsigned char)(config->rmt_ip >> 16),
1849 (unsigned char)(config->rmt_ip >> 8),
1850 config->rmt_ip & 0xff);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001851 config->port = ntohs(fromAddr.sin_port);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001852#ifdef DEBUG
Glenn L McGrath06e95652003-02-09 06:51:14 +00001853 if (config->debugHttpd) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001854 bb_error_msg("connection from IP=%s, port %u\n",
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001855 config->rmt_ip_str, config->port);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001856 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001857#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001858#endif /* CONFIG_FEATURE_HTTPD_CGI */
1859
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001860 /* set the KEEPALIVE option to cull dead connections */
1861 on = 1;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001862 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof (on));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001863
Glenn L McGrath06e95652003-02-09 06:51:14 +00001864 if (config->debugHttpd || fork() == 0) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001865 /* This is the spawned thread */
1866#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
1867 /* protect reload config, may be confuse checking */
1868 signal(SIGHUP, SIG_IGN);
1869#endif
1870 handleIncoming();
1871 if(!config->debugHttpd)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001872 exit(0);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001873 }
1874 close(s);
1875 }
1876 }
1877 } // while (1)
1878 return 0;
1879}
1880
Glenn L McGrath06e95652003-02-09 06:51:14 +00001881#else
1882 /* from inetd */
1883
1884static int miniHttpd(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001885{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001886 struct sockaddr_in fromAddrLen;
1887 socklen_t sinlen = sizeof (struct sockaddr_in);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001888
1889 getpeername (0, (struct sockaddr *)&fromAddrLen, &sinlen);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001890 config->rmt_ip = ntohl(fromAddrLen.sin_addr.s_addr);
1891#if defined(CONFIG_FEATURE_HTTPD_CGI) || defined(DEBUG)
1892 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1893 (unsigned char)(config->rmt_ip >> 24),
1894 (unsigned char)(config->rmt_ip >> 16),
1895 (unsigned char)(config->rmt_ip >> 8),
1896 config->rmt_ip & 0xff);
1897#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001898 config->port = ntohs(fromAddrLen.sin_port);
1899 handleIncoming();
1900 return 0;
1901}
1902#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
1903
1904#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
1905static void sighup_handler(int sig)
1906{
1907 /* set and reset */
1908 struct sigaction sa;
1909
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001910 parse_conf(default_path_httpd_conf,
1911 sig == SIGHUP ? SIGNALED_PARSE : FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001912 sa.sa_handler = sighup_handler;
1913 sigemptyset(&sa.sa_mask);
1914 sa.sa_flags = SA_RESTART;
1915 sigaction(SIGHUP, &sa, NULL);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001916}
1917#endif
1918
Eric Andersena3bb3e62003-06-26 09:05:32 +00001919
1920static const char httpd_opts[]="c:d:h:"
1921#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
1922 "e:"
1923#define OPT_INC_1 1
1924#else
1925#define OPT_INC_1 0
1926#endif
1927#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1928 "r:"
Eric Andersen35e643b2003-07-28 07:40:39 +00001929# ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
1930 "m:"
1931# define OPT_INC_2 2
1932# else
1933# define OPT_INC_2 1
1934#endif
Eric Andersena3bb3e62003-06-26 09:05:32 +00001935#else
1936#define OPT_INC_2 0
1937#endif
1938#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1939 "p:v"
1940#ifdef CONFIG_FEATURE_HTTPD_SETUID
1941 "u:"
1942#endif
Eric Andersen35e643b2003-07-28 07:40:39 +00001943#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
Eric Andersena3bb3e62003-06-26 09:05:32 +00001944 ;
1945
1946#define OPT_CONFIG_FILE (1<<0)
1947#define OPT_DECODE_URL (1<<1)
1948#define OPT_HOME_HTTPD (1<<2)
1949#define OPT_ENCODE_URL (1<<(2+OPT_INC_1))
Eric Andersen35e643b2003-07-28 07:40:39 +00001950#define OPT_REALM (1<<(3+OPT_INC_1))
1951#define OPT_MD5 (1<<(4+OPT_INC_1))
Eric Andersena3bb3e62003-06-26 09:05:32 +00001952#define OPT_PORT (1<<(3+OPT_INC_1+OPT_INC_2))
1953#define OPT_DEBUG (1<<(4+OPT_INC_1+OPT_INC_2))
1954#define OPT_SETUID (1<<(5+OPT_INC_1+OPT_INC_2))
1955
1956
Glenn L McGrath06e95652003-02-09 06:51:14 +00001957#ifdef HTTPD_STANDALONE
1958int main(int argc, char *argv[])
1959#else
1960int httpd_main(int argc, char *argv[])
1961#endif
1962{
Eric Andersena3bb3e62003-06-26 09:05:32 +00001963 unsigned long opt;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001964 const char *home_httpd = home;
Eric Andersena3bb3e62003-06-26 09:05:32 +00001965 char *url_for_decode;
1966#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
1967 const char *url_for_encode;
1968#endif
1969#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1970 const char *s_port;
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00001971#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001972
1973#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001974 int server;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001975#endif
1976
1977#ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersena3bb3e62003-06-26 09:05:32 +00001978 const char *s_uid;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001979 long uid = -1;
1980#endif
1981
Eric Andersen35e643b2003-07-28 07:40:39 +00001982#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
1983 const char *pass;
1984#endif
1985
Glenn L McGrath06e95652003-02-09 06:51:14 +00001986 config = xcalloc(1, sizeof(*config));
1987#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1988 config->realm = "Web Server Authentication";
1989#endif
1990
1991#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1992 config->port = 80;
1993#endif
1994
1995 config->ContentLength = -1;
1996
Eric Andersena3bb3e62003-06-26 09:05:32 +00001997 opt = bb_getopt_ulflags(argc, argv, httpd_opts,
1998 &(config->configFile), &url_for_decode, &home_httpd
Glenn L McGrath06e95652003-02-09 06:51:14 +00001999#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Eric Andersena3bb3e62003-06-26 09:05:32 +00002000 , &url_for_encode
Glenn L McGrath06e95652003-02-09 06:51:14 +00002001#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002002#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Eric Andersena3bb3e62003-06-26 09:05:32 +00002003 , &(config->realm)
Eric Andersen35e643b2003-07-28 07:40:39 +00002004# ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
2005 , &pass
2006# endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00002007#endif
Eric Andersena3bb3e62003-06-26 09:05:32 +00002008#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2009 , &s_port
Glenn L McGrath06e95652003-02-09 06:51:14 +00002010#ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersena3bb3e62003-06-26 09:05:32 +00002011 , &s_uid
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002012#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00002013#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002014 );
Eric Andersena3bb3e62003-06-26 09:05:32 +00002015
2016 if(opt & OPT_DECODE_URL) {
2017 printf("%s", decodeString(url_for_decode, 1));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002018 return 0;
Eric Andersena3bb3e62003-06-26 09:05:32 +00002019 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002020#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Eric Andersena3bb3e62003-06-26 09:05:32 +00002021 if(opt & OPT_ENCODE_URL) {
2022 printf("%s", encodeString(url_for_encode));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002023 return 0;
Eric Andersena3bb3e62003-06-26 09:05:32 +00002024 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002025#endif
Eric Andersen35e643b2003-07-28 07:40:39 +00002026#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
2027 if(opt & OPT_MD5) {
2028 printf("%s\n", pw_encrypt(pass, "$1$"));
2029 return 0;
2030 }
2031#endif
Eric Andersena3bb3e62003-06-26 09:05:32 +00002032#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2033 if(opt & OPT_PORT)
2034 config->port = bb_xgetlarg(s_port, 10, 1, 0xffff);
2035 config->debugHttpd = opt & OPT_DEBUG;
Glenn L McGrath06e95652003-02-09 06:51:14 +00002036#ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersena3bb3e62003-06-26 09:05:32 +00002037 if(opt & OPT_SETUID) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00002038 char *e;
2039
Eric Andersena3bb3e62003-06-26 09:05:32 +00002040 uid = strtol(s_uid, &e, 0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002041 if(*e != '\0') {
2042 /* not integer */
Eric Andersena3bb3e62003-06-26 09:05:32 +00002043 uid = my_getpwnam(s_uid);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002044 }
2045 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002046#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00002047#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002048
Glenn L McGrath06e95652003-02-09 06:51:14 +00002049 if(chdir(home_httpd)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002050 bb_perror_msg_and_die("can`t chdir to %s", home_httpd);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002051 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002052#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2053 server = openServer();
2054# ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersenaff114c2004-04-14 17:51:38 +00002055 /* drop privileges */
Glenn L McGrath06e95652003-02-09 06:51:14 +00002056 if(uid > 0)
2057 setuid(uid);
2058# endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002059#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00002060
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002061#ifdef CONFIG_FEATURE_HTTPD_CGI
2062 {
2063 char *p = getenv("PATH");
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002064 if(p) {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00002065 p = bb_xstrdup(p);
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002066 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00002067 clearenv();
2068 if(p)
2069 setenv("PATH", p, 1);
Glenn L McGrath14092a12003-09-12 00:44:50 +00002070# ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2071 addEnvPort("SERVER");
2072# endif
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002073 }
2074#endif
2075
Glenn L McGrath06e95652003-02-09 06:51:14 +00002076#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
2077 sighup_handler(0);
2078#else
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00002079 parse_conf(default_path_httpd_conf, FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002080#endif
2081
2082#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2083 if (!config->debugHttpd) {
2084 if (daemon(1, 0) < 0) /* don`t change curent directory */
Manuel Novoa III cad53642003-03-19 09:13:01 +00002085 bb_perror_msg_and_die("daemon");
Glenn L McGrath06e95652003-02-09 06:51:14 +00002086 }
2087 return miniHttpd(server);
2088#else
2089 return miniHttpd();
2090#endif
2091}