blob: 452b56d19669c39c071fec9e63d8b4d7177f31d7 [file] [log] [blame]
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002/*
3 * httpd implementation for busybox
4 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00005 * Copyright (C) 2002,2003 Glenn Engel <glenne@engel.org>
"Vladimir N. Oleynik"79af7d52006-01-26 10:58:12 +00006 * Copyright (C) 2003-2006 Vladimir Oleynik <dzo@simtreas.ru>
Glenn L McGrath58c708a2003-01-05 04:01:56 +00007 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00008 * simplify patch stolen from libbb without using strdup
Glenn L McGrath58c708a2003-01-05 04:01:56 +00009 *
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +000010 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000011 *
12 *****************************************************************************
13 *
Glenn L McGrath06e95652003-02-09 06:51:14 +000014 * Typical usage:
15 * for non root user
16 * httpd -p 8080 -h $HOME/public_html
17 * or for daemon start from rc script with uid=0:
18 * httpd -u www
19 * This is equivalent if www user have uid=80 to
20 * httpd -p 80 -u 80 -h /www -c /etc/httpd.conf -r "Web Server Authentication"
21 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +000022 *
23 * When a url contains "cgi-bin" it is assumed to be a cgi script. The
24 * server changes directory to the location of the script and executes it
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +000025 * after setting QUERY_STRING and other environment variables.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000026 *
27 * The server can also be invoked as a url arg decoder and html text encoder
28 * as follows:
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000029 * foo=`httpd -d $foo` # decode "Hello%20World" as "Hello World"
Glenn L McGrathc9163fe2003-05-13 16:20:11 +000030 * bar=`httpd -e "<Hello World>"` # encode as "&#60Hello&#32World&#62"
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000031 * Note that url encoding for arguments is not the same as html encoding for
Eric Andersenaff114c2004-04-14 17:51:38 +000032 * presentation. -d decodes a url-encoded argument while -e encodes in html
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000033 * for page display.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000034 *
35 * httpd.conf has the following format:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000036 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +000037 * A:172.20. # Allow address from 172.20.0.0/16
38 * A:10.0.0.0/25 # Allow any address from 10.0.0.0-10.0.0.127
39 * A:10.0.0.0/255.255.255.128 # Allow any address that previous set
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000040 * A:127.0.0.1 # Allow local loopback connections
41 * D:* # Deny from other IP connections
42 * /cgi-bin:foo:bar # Require user foo, pwd bar on urls starting with /cgi-bin/
43 * /adm:admin:setup # Require user admin, pwd setup on urls starting with /adm/
44 * /adm:toor:PaSsWd # or user toor, pwd PaSsWd on urls starting with /adm/
45 * .au:audio/basic # additional mime type for audio.au files
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +000046 * *.php:/path/php # running cgi.php scripts through an interpreter
Eric Andersenc7bda1c2004-03-15 08:29:22 +000047 *
Eric Andersenaff114c2004-04-14 17:51:38 +000048 * A/D may be as a/d or allow/deny - first char case insensitive
Glenn L McGrath393183d2003-05-26 14:07:50 +000049 * Deny IP rules take precedence over allow rules.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000050 *
51 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000052 * The Deny/Allow IP logic:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000053 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000054 * - Default is to allow all. No addresses are denied unless
Eric Andersen97a1de12004-08-26 22:22:50 +000055 * denied with a D: rule.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000056 * - Order of Deny/Allow rules is significant
57 * - Deny rules take precedence over allow rules.
58 * - If a deny all rule (D:*) is used it acts as a catch-all for unmatched
Eric Andersen97a1de12004-08-26 22:22:50 +000059 * addresses.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000060 * - Specification of Allow all (A:*) is a no-op
Eric Andersenc7bda1c2004-03-15 08:29:22 +000061 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000062 * Example:
63 * 1. Allow only specified addresses
Glenn L McGrathb65422c2003-09-08 10:59:27 +000064 * A:172.20 # Allow any address that begins with 172.20.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000065 * A:10.10. # Allow any address that begins with 10.10.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000066 * A:127.0.0.1 # Allow local loopback connections
67 * D:* # Deny from other IP connections
Eric Andersenc7bda1c2004-03-15 08:29:22 +000068 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000069 * 2. Only deny specified addresses
70 * D:1.2.3. # deny from 1.2.3.0 - 1.2.3.255
71 * D:2.3.4. # deny from 2.3.4.0 - 2.3.4.255
72 * A:* # (optional line added for clarity)
Eric Andersenc7bda1c2004-03-15 08:29:22 +000073 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000074 * If a sub directory contains a config file it is parsed and merged with
Glenn L McGrathb65422c2003-09-08 10:59:27 +000075 * any existing settings as if it was appended to the original configuration.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000076 *
77 * subdir paths are relative to the containing subdir and thus cannot
78 * affect the parent rules.
79 *
80 * Note that since the sub dir is parsed in the forked thread servicing the
81 * subdir http request, any merge is discarded when the process exits. As a
82 * result, the subdir settings only have a lifetime of a single request.
83 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000084 *
85 * If -c is not set, an attempt will be made to open the default
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000086 * root configuration file. If -c is set and the file is not found, the
87 * server exits with an error.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000088 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000089*/
Glenn L McGrath58c708a2003-01-05 04:01:56 +000090
Glenn L McGrath06e95652003-02-09 06:51:14 +000091
Glenn L McGrath58c708a2003-01-05 04:01:56 +000092#include <stdio.h>
93#include <ctype.h> /* for isspace */
Glenn L McGrath06e95652003-02-09 06:51:14 +000094#include <string.h>
Glenn L McGrath58c708a2003-01-05 04:01:56 +000095#include <stdlib.h> /* for malloc */
96#include <time.h>
Glenn L McGrath58c708a2003-01-05 04:01:56 +000097#include <unistd.h> /* for close */
98#include <signal.h>
99#include <sys/types.h>
100#include <sys/socket.h> /* for connect and socket*/
101#include <netinet/in.h> /* for sockaddr_in */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000102#include <sys/stat.h>
103#include <sys/wait.h>
Glenn L McGrath06e95652003-02-09 06:51:14 +0000104#include <fcntl.h> /* for open modes */
105#include "busybox.h"
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000106
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000107
Eric Andersen07f2fea2004-10-08 08:03:29 +0000108static const char httpdVersion[] = "busybox httpd/1.35 6-Oct-2004";
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000109static const char default_path_httpd_conf[] = "/etc";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000110static const char httpd_conf[] = "httpd.conf";
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000111static const char home[] = "./";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000112
Glenn L McGrath5cd64612003-08-29 15:53:23 +0000113#ifdef CONFIG_LFS
114# define cont_l_fmt "%lld"
Eric Andersen0cb6f352006-01-30 22:30:41 +0000115# define cont_l_type (long long)
Glenn L McGrath5cd64612003-08-29 15:53:23 +0000116#else
117# define cont_l_fmt "%ld"
Eric Andersen0cb6f352006-01-30 22:30:41 +0000118# define cont_l_type (long)
Glenn L McGrath5cd64612003-08-29 15:53:23 +0000119#endif
120
Eric Andersen07f2fea2004-10-08 08:03:29 +0000121#define TIMEOUT 60
122
Eric Andersenaff114c2004-04-14 17:51:38 +0000123// Note: busybox xfuncs are not used because we want the server to keep running
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000124// if something bad happens due to a malformed user request.
Glenn L McGrath06e95652003-02-09 06:51:14 +0000125// As a result, all memory allocation after daemonize
126// is checked rigorously
127
128//#define DEBUG 1
129
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000130#ifndef DEBUG
131# define DEBUG 0
132#endif
133
Glenn L McGrath06e95652003-02-09 06:51:14 +0000134#define MAX_MEMORY_BUFF 8192 /* IO buffer */
135
136typedef struct HT_ACCESS {
137 char *after_colon;
138 struct HT_ACCESS *next;
139 char before_colon[1]; /* really bigger, must last */
140} Htaccess;
141
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000142typedef struct HT_ACCESS_IP {
143 unsigned int ip;
144 unsigned int mask;
145 int allow_deny;
146 struct HT_ACCESS_IP *next;
147} Htaccess_IP;
148
Glenn L McGrath06e95652003-02-09 06:51:14 +0000149typedef struct
150{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000151 char buf[MAX_MEMORY_BUFF];
152
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +0000153 USE_FEATURE_HTTPD_BASIC_AUTH(const char *realm;)
154 USE_FEATURE_HTTPD_BASIC_AUTH(char *remoteuser;)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000155
Eric Andersen07f2fea2004-10-08 08:03:29 +0000156 const char *query;
157
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +0000158 USE_FEATURE_HTTPD_CGI(char *referer;)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000159
Glenn L McGrath06e95652003-02-09 06:51:14 +0000160 const char *configFile;
161
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000162 unsigned int rmt_ip;
Rob Landleyd086b502006-04-14 02:32:29 +0000163#if defined(CONFIG_FEATURE_HTTPD_CGI) || DEBUG
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000164 char rmt_ip_str[16]; /* for set env REMOTE_ADDR */
165#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000166 unsigned port; /* server initial port and for
167 set env REMOTE_PORT */
Eric Andersen07f2fea2004-10-08 08:03:29 +0000168 union HTTPD_FOUND {
169 const char *found_mime_type;
170 const char *found_moved_temporarily;
171 } httpd_found;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000172
Glenn L McGrath06e95652003-02-09 06:51:14 +0000173 off_t ContentLength; /* -1 - unknown */
174 time_t last_mod;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000175
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000176 Htaccess_IP *ip_a_d; /* config allow/deny lines */
Glenn L McGrath393183d2003-05-26 14:07:50 +0000177 int flg_deny_all;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000178#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
179 Htaccess *auth; /* config user:password lines */
180#endif
181#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
182 Htaccess *mime_a; /* config mime types */
183#endif
184
Rob Landleya2d9a172006-04-28 19:38:04 +0000185#ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
Glenn L McGrath06e95652003-02-09 06:51:14 +0000186 int accepted_socket;
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000187# define a_c_r config->accepted_socket
188# define a_c_w config->accepted_socket
Glenn L McGrath06e95652003-02-09 06:51:14 +0000189#else
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000190# define a_c_r 0
191# define a_c_w 1
Glenn L McGrath06e95652003-02-09 06:51:14 +0000192#endif
Eric Andersen07f2fea2004-10-08 08:03:29 +0000193 volatile int alarm_signaled;
194
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000195#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
196 Htaccess *script_i; /* config script interpreters */
197#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000198} HttpdConfig;
199
200static HttpdConfig *config;
201
Mike Frysingerfa6c4842006-05-26 01:48:17 +0000202static const char request_GET[] = "GET"; /* size algorithmic optimize */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000203
204static const char* const suffixTable [] = {
Eric Andersenaff114c2004-04-14 17:51:38 +0000205/* Warning: shorted equivalent suffix in one line must be first */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000206 ".htm.html", "text/html",
207 ".jpg.jpeg", "image/jpeg",
208 ".gif", "image/gif",
209 ".png", "image/png",
210 ".txt.h.c.cc.cpp", "text/plain",
Glenn L McGrath06e95652003-02-09 06:51:14 +0000211 ".css", "text/css",
212 ".wav", "audio/wav",
213 ".avi", "video/x-msvideo",
214 ".qt.mov", "video/quicktime",
215 ".mpe.mpeg", "video/mpeg",
216 ".mid.midi", "audio/midi",
217 ".mp3", "audio/mpeg",
218#if 0 /* unpopular */
219 ".au", "audio/basic",
220 ".pac", "application/x-ns-proxy-autoconfig",
221 ".vrml.wrl", "model/vrml",
222#endif
223 0, "application/octet-stream" /* default */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000224 };
225
226typedef enum
227{
228 HTTP_OK = 200,
Eric Andersen07f2fea2004-10-08 08:03:29 +0000229 HTTP_MOVED_TEMPORARILY = 302,
230 HTTP_BAD_REQUEST = 400, /* malformed syntax */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000231 HTTP_UNAUTHORIZED = 401, /* authentication needed, respond with auth hdr */
232 HTTP_NOT_FOUND = 404,
Glenn L McGrath06e95652003-02-09 06:51:14 +0000233 HTTP_FORBIDDEN = 403,
Eric Andersen07f2fea2004-10-08 08:03:29 +0000234 HTTP_REQUEST_TIMEOUT = 408,
235 HTTP_NOT_IMPLEMENTED = 501, /* used for unrecognized requests */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000236 HTTP_INTERNAL_SERVER_ERROR = 500,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000237#if 0 /* future use */
238 HTTP_CONTINUE = 100,
239 HTTP_SWITCHING_PROTOCOLS = 101,
240 HTTP_CREATED = 201,
241 HTTP_ACCEPTED = 202,
242 HTTP_NON_AUTHORITATIVE_INFO = 203,
243 HTTP_NO_CONTENT = 204,
244 HTTP_MULTIPLE_CHOICES = 300,
245 HTTP_MOVED_PERMANENTLY = 301,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000246 HTTP_NOT_MODIFIED = 304,
247 HTTP_PAYMENT_REQUIRED = 402,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000248 HTTP_BAD_GATEWAY = 502,
249 HTTP_SERVICE_UNAVAILABLE = 503, /* overload, maintenance */
250 HTTP_RESPONSE_SETSIZE=0xffffffff
251#endif
252} HttpResponseNum;
253
254typedef struct
255{
256 HttpResponseNum type;
257 const char *name;
258 const char *info;
259} HttpEnumString;
260
261static const HttpEnumString httpResponseNames[] = {
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +0000262 { HTTP_OK, "OK", NULL },
Eric Andersen07f2fea2004-10-08 08:03:29 +0000263 { HTTP_MOVED_TEMPORARILY, "Found", "Directories must end with a slash." },
264 { HTTP_REQUEST_TIMEOUT, "Request Timeout",
265 "No request appeared within a reasonable time period." },
Glenn L McGrath06e95652003-02-09 06:51:14 +0000266 { HTTP_NOT_IMPLEMENTED, "Not Implemented",
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000267 "The requested method is not recognized by this server." },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000268#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000269 { HTTP_UNAUTHORIZED, "Unauthorized", "" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000270#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000271 { HTTP_NOT_FOUND, "Not Found",
272 "The requested URL was not found on this server." },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000273 { HTTP_BAD_REQUEST, "Bad Request", "Unsupported method." },
Glenn L McGrath06e95652003-02-09 06:51:14 +0000274 { HTTP_FORBIDDEN, "Forbidden", "" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000275 { HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error",
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000276 "Internal Server Error" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000277#if 0 /* not implemented */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000278 { HTTP_CREATED, "Created" },
279 { HTTP_ACCEPTED, "Accepted" },
280 { HTTP_NO_CONTENT, "No Content" },
281 { HTTP_MULTIPLE_CHOICES, "Multiple Choices" },
282 { HTTP_MOVED_PERMANENTLY, "Moved Permanently" },
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000283 { HTTP_NOT_MODIFIED, "Not Modified" },
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000284 { HTTP_BAD_GATEWAY, "Bad Gateway", "" },
285 { HTTP_SERVICE_UNAVAILABLE, "Service Unavailable", "" },
286#endif
287};
288
Glenn L McGrath06e95652003-02-09 06:51:14 +0000289
290static const char RFC1123FMT[] = "%a, %d %b %Y %H:%M:%S GMT";
291static const char Content_length[] = "Content-length:";
292
293
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000294static int
295scan_ip (const char **ep, unsigned int *ip, unsigned char endc)
296{
297 const char *p = *ep;
298 int auto_mask = 8;
299 int j;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000300
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000301 *ip = 0;
302 for (j = 0; j < 4; j++) {
303 unsigned int octet;
304
305 if ((*p < '0' || *p > '9') && (*p != '/' || j == 0) && *p != 0)
306 return -auto_mask;
307 octet = 0;
308 while (*p >= '0' && *p <= '9') {
309 octet *= 10;
310 octet += *p - '0';
311 if (octet > 255)
312 return -auto_mask;
313 p++;
314 }
315 if (*p == '.')
316 p++;
317 if (*p != '/' && *p != 0)
318 auto_mask += 8;
319 *ip = ((*ip) << 8) | octet;
320 }
321 if (*p != 0) {
322 if (*p != endc)
323 return -auto_mask;
324 p++;
325 if(*p == 0)
326 return -auto_mask;
327 }
328 *ep = p;
329 return auto_mask;
330}
331
332static int
333scan_ip_mask (const char *ipm, unsigned int *ip, unsigned int *mask)
334{
335 int i;
336 unsigned int msk;
337
338 i = scan_ip(&ipm, ip, '/');
339 if(i < 0)
340 return i;
341 if(*ipm) {
342 const char *p = ipm;
343
344 i = 0;
345 while (*p) {
346 if (*p < '0' || *p > '9') {
347 if (*p == '.') {
348 i = scan_ip (&ipm, mask, 0);
349 return i != 32;
350 }
351 return -1;
352 }
353 i *= 10;
354 i += *p - '0';
355 p++;
356 }
357 }
358 if (i > 32 || i < 0)
359 return -1;
360 msk = 0x80000000;
361 *mask = 0;
362 while (i > 0) {
363 *mask |= msk;
364 msk >>= 1;
365 i--;
366 }
367 return 0;
368}
369
370#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000371static void free_config_lines(Htaccess **pprev)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000372{
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000373 Htaccess *prev = *pprev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000374
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000375 while( prev ) {
376 Htaccess *cur = prev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000377
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000378 prev = cur->next;
379 free(cur);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000380 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000381 *pprev = NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000382}
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000383#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000384
Glenn L McGrath06e95652003-02-09 06:51:14 +0000385/* flag */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000386#define FIRST_PARSE 0
387#define SUBDIR_PARSE 1
388#define SIGNALED_PARSE 2
389#define FIND_FROM_HTTPD_ROOT 3
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000390/****************************************************************************
391 *
392 > $Function: parse_conf()
393 *
394 * $Description: parse configuration file into in-memory linked list.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000395 *
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000396 * The first non-white character is examined to determine if the config line
397 * is one of the following:
398 * .ext:mime/type # new mime type not compiled into httpd
399 * [adAD]:from # ip address allow/deny, * for wildcard
400 * /path:user:pass # username/password
401 *
402 * Any previous IP rules are discarded.
403 * If the flag argument is not SUBDIR_PARSE then all /path and mime rules
404 * are also discarded. That is, previous settings are retained if flag is
405 * SUBDIR_PARSE.
406 *
407 * $Parameters:
408 * (const char *) path . . null for ip address checks, path for password
409 * checks.
410 * (int) flag . . . . . . the source of the parse request.
411 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000412 * $Return: (None)
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000413 *
414 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000415static void parse_conf(const char *path, int flag)
416{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000417 FILE *f;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000418#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000419 Htaccess *prev, *cur;
420#elif CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
421 Htaccess *cur;
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000422#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000423
Glenn L McGrath06e95652003-02-09 06:51:14 +0000424 const char *cf = config->configFile;
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000425 char buf[160];
Glenn L McGrath06e95652003-02-09 06:51:14 +0000426 char *p0 = NULL;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000427 char *c, *p;
428
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000429 /* free previous ip setup if present */
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000430 Htaccess_IP *pip = config->ip_a_d;
431
432 while( pip ) {
433 Htaccess_IP *cur_ipl = pip;
434
435 pip = cur_ipl->next;
436 free(cur_ipl);
437 }
438 config->ip_a_d = NULL;
439
Glenn L McGrath24833432003-06-10 17:22:49 +0000440 config->flg_deny_all = 0;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000441
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000442#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR)
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000443 /* retain previous auth and mime config only for subdir parse */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000444 if(flag != SUBDIR_PARSE) {
445#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000446 free_config_lines(&config->auth);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000447#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000448#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
449 free_config_lines(&config->mime_a);
450#endif
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000451#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
452 free_config_lines(&config->script_i);
453#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000454 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000455#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000456
457 if(flag == SUBDIR_PARSE || cf == NULL) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000458 cf = alloca(strlen(path) + sizeof(httpd_conf) + 2);
459 if(cf == NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000460 if(flag == FIRST_PARSE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000461 bb_error_msg_and_die(bb_msg_memory_exhausted);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000462 return;
463 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000464 sprintf((char *)cf, "%s/%s", path, httpd_conf);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000465 }
466
467 while((f = fopen(cf, "r")) == NULL) {
Eric Andersen35e643b2003-07-28 07:40:39 +0000468 if(flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) {
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000469 /* config file not found, no changes to config */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000470 return;
471 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000472 if(config->configFile && flag == FIRST_PARSE) /* if -c option given */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000473 bb_perror_msg_and_die("%s", cf);
474 flag = FIND_FROM_HTTPD_ROOT;
475 cf = httpd_conf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000476 }
477
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000478#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
479 prev = config->auth;
480#endif
481 /* This could stand some work */
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000482 while ( (p0 = fgets(buf, sizeof(buf), f)) != NULL) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000483 c = NULL;
484 for(p = p0; *p0 != 0 && *p0 != '#'; p0++) {
485 if(!isspace(*p0)) {
486 *p++ = *p0;
487 if(*p0 == ':' && c == NULL)
488 c = p;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000489 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000490 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000491 *p = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000492
493 /* test for empty or strange line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000494 if (c == NULL || *c == 0)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000495 continue;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000496 p0 = buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000497 if(*p0 == 'd')
498 *p0 = 'D';
Glenn L McGrath393183d2003-05-26 14:07:50 +0000499 if(*c == '*') {
500 if(*p0 == 'D') {
501 /* memorize deny all */
502 config->flg_deny_all++;
503 }
504 /* skip default other "word:*" config lines */
505 continue;
506 }
507
508 if(*p0 == 'a')
509 *p0 = 'A';
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000510 else if(*p0 != 'D' && *p0 != 'A'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000511#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000512 && *p0 != '/'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000513#endif
514#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000515 && *p0 != '.'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000516#endif
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000517#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
518 && *p0 != '*'
519#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000520 )
Glenn L McGrath06e95652003-02-09 06:51:14 +0000521 continue;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000522 if(*p0 == 'A' || *p0 == 'D') {
523 /* storing current config IP line */
524 pip = calloc(1, sizeof(Htaccess_IP));
525 if(pip) {
526 if(scan_ip_mask (c, &(pip->ip), &(pip->mask))) {
527 /* syntax IP{/mask} error detected, protect all */
528 *p0 = 'D';
529 pip->mask = 0;
530 }
531 pip->allow_deny = *p0;
532 if(*p0 == 'D') {
533 /* Deny:form_IP move top */
534 pip->next = config->ip_a_d;
535 config->ip_a_d = pip;
536 } else {
537 /* add to bottom A:form_IP config line */
538 Htaccess_IP *prev_IP = config->ip_a_d;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000539
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000540 if(prev_IP == NULL) {
541 config->ip_a_d = pip;
542 } else {
543 while(prev_IP->next)
544 prev_IP = prev_IP->next;
545 prev_IP->next = pip;
546 }
547 }
548 }
549 continue;
550 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000551#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
552 if(*p0 == '/') {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000553 /* make full path from httpd root / curent_path / config_line_path */
554 cf = flag == SUBDIR_PARSE ? path : "";
555 p0 = malloc(strlen(cf) + (c - buf) + 2 + strlen(c));
556 if(p0 == NULL)
557 continue;
558 c[-1] = 0;
559 sprintf(p0, "/%s%s", cf, buf);
560
561 /* another call bb_simplify_path */
562 cf = p = p0;
563
564 do {
565 if (*p == '/') {
566 if (*cf == '/') { /* skip duplicate (or initial) slash */
567 continue;
568 } else if (*cf == '.') {
569 if (cf[1] == '/' || cf[1] == 0) { /* remove extra '.' */
570 continue;
571 } else if ((cf[1] == '.') && (cf[2] == '/' || cf[2] == 0)) {
572 ++cf;
573 if (p > p0) {
574 while (*--p != '/'); /* omit previous dir */
575 }
576 continue;
577 }
578 }
579 }
580 *++p = *cf;
581 } while (*++cf);
582
583 if ((p == p0) || (*p != '/')) { /* not a trailing slash */
584 ++p; /* so keep last character */
585 }
586 *p = 0;
587 sprintf(p0, "%s:%s", p0, c);
588 }
589#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000590
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000591#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR)
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000592 /* storing current config line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000593 cur = calloc(1, sizeof(Htaccess) + strlen(p0));
594 if(cur) {
595 cf = strcpy(cur->before_colon, p0);
596 c = strchr(cf, ':');
597 *c++ = 0;
598 cur->after_colon = c;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000599#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Glenn L McGrath5875be42003-09-08 15:39:09 +0000600 if(*cf == '.') {
Glenn L McGrath874e3382003-05-14 12:11:36 +0000601 /* config .mime line move top for overwrite previous */
602 cur->next = config->mime_a;
603 config->mime_a = cur;
Glenn L McGrath5875be42003-09-08 15:39:09 +0000604 continue;
Glenn L McGrath874e3382003-05-14 12:11:36 +0000605 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000606#endif
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +0000607#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
608 if(*cf == '*' && cf[1] == '.') {
609 /* config script interpreter line move top for overwrite previous */
610 cur->next = config->script_i;
611 config->script_i = cur;
612 continue;
613 }
614#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000615#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath5875be42003-09-08 15:39:09 +0000616 free(p0);
617 if(prev == NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000618 /* first line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000619 config->auth = prev = cur;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000620 } else {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000621 /* sort path, if current lenght eq or bigger then move up */
622 Htaccess *prev_hti = config->auth;
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +0000623 size_t l = strlen(cf);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000624 Htaccess *hti;
625
626 for(hti = prev_hti; hti; hti = hti->next) {
627 if(l >= strlen(hti->before_colon)) {
628 /* insert before hti */
629 cur->next = hti;
630 if(prev_hti != hti) {
631 prev_hti->next = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000632 } else {
633 /* insert as top */
634 config->auth = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000635 }
Glenn L McGrath393183d2003-05-26 14:07:50 +0000636 break;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000637 }
638 if(prev_hti != hti)
639 prev_hti = prev_hti->next;
640 }
641 if(!hti) { /* not inserted, add to bottom */
642 prev->next = cur;
643 prev = cur;
644 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000645 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000646#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000647 }
Glenn L McGrathbaaa6e92003-09-15 15:00:43 +0000648#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000649 }
650 fclose(f);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000651}
652
653#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000654/****************************************************************************
655 *
656 > $Function: encodeString()
657 *
658 * $Description: Given a string, html encode special characters.
659 * This is used for the -e command line option to provide an easy way
660 * for scripts to encode result data without confusing browsers. The
661 * returned string pointer is memory allocated by malloc().
662 *
663 * $Parameters:
664 * (const char *) string . . The first string to encode.
665 *
666 * $Return: (char *) . . . .. . . A pointer to the encoded string.
667 *
668 * $Errors: Returns a null string ("") if memory is not available.
669 *
670 ****************************************************************************/
671static char *encodeString(const char *string)
672{
673 /* take the simple route and encode everything */
674 /* could possibly scan once to get length. */
675 int len = strlen(string);
"Vladimir N. Oleynik"0bf67e82005-12-26 17:26:59 +0000676 char *out = malloc(len * 6 + 1);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000677 char *p=out;
678 char ch;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000679
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000680 if (!out) return "";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000681 while ((ch = *string++)) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000682 // very simple check for what to encode
683 if (isalnum(ch)) *p++ = ch;
Eric Andersen3efa51d2005-06-23 05:51:48 +0000684 else p += sprintf(p, "&#%d;", (unsigned char) ch);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000685 }
686 *p=0;
687 return out;
688}
Glenn L McGrath06e95652003-02-09 06:51:14 +0000689#endif /* CONFIG_FEATURE_HTTPD_ENCODE_URL_STR */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000690
691/****************************************************************************
692 *
693 > $Function: decodeString()
694 *
695 * $Description: Given a URL encoded string, convert it to plain ascii.
696 * Since decoding always makes strings smaller, the decode is done in-place.
697 * Thus, callers should strdup() the argument if they do not want the
698 * argument modified. The return is the original pointer, allowing this
699 * function to be easily used as arguments to other functions.
700 *
701 * $Parameters:
702 * (char *) string . . . The first string to decode.
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000703 * (int) flag . . . 1 if require decode '+' as ' ' for CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000704 *
705 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
706 *
707 * $Errors: None
708 *
709 ****************************************************************************/
Eric Andersena3bb3e62003-06-26 09:05:32 +0000710static char *decodeString(char *orig, int flag_plus_to_space)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000711{
712 /* note that decoded string is always shorter than original */
Eric Andersena3bb3e62003-06-26 09:05:32 +0000713 char *string = orig;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000714 char *ptr = string;
Eric Andersena3bb3e62003-06-26 09:05:32 +0000715
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000716 while (*ptr)
717 {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000718 if (*ptr == '+' && flag_plus_to_space) { *string++ = ' '; ptr++; }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000719 else if (*ptr != '%') *string++ = *ptr++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000720 else {
"Vladimir N. Oleynik"0bf67e82005-12-26 17:26:59 +0000721 unsigned int value1, value2;
722
723 ptr++;
724 if(sscanf(ptr, "%1X", &value1) != 1 ||
725 sscanf(ptr+1, "%1X", &value2) != 1) {
726 if(!flag_plus_to_space)
727 return NULL;
728 *string++ = '%';
729 } else {
730 value1 = value1 * 16 + value2;
731 if(value1 == '/' || value1 == 0)
732 return orig+1;
733 *string++ = value1;
734 ptr += 2;
735 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000736 }
737 }
738 *string = '\0';
739 return orig;
740}
741
742
Glenn L McGrath06e95652003-02-09 06:51:14 +0000743#ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000744/****************************************************************************
745 *
746 > $Function: addEnv()
747 *
Eric Andersenaff114c2004-04-14 17:51:38 +0000748 * $Description: Add an environment variable setting to the global list.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000749 * A NAME=VALUE string is allocated, filled, and added to the list of
750 * environment settings passed to the cgi execution script.
751 *
752 * $Parameters:
Glenn L McGrath06e95652003-02-09 06:51:14 +0000753 * (char *) name_before_underline - The first part environment variable name.
754 * (char *) name_after_underline - The second part environment variable name.
755 * (char *) value . . The value to which the env variable is set.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000756 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000757 * $Return: (void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000758 *
759 * $Errors: Silently returns if the env runs out of space to hold the new item
760 *
761 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000762static void addEnv(const char *name_before_underline,
763 const char *name_after_underline, const char *value)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000764{
Glenn L McGrath5875be42003-09-08 15:39:09 +0000765 char *s = NULL;
Glenn L McGrath393183d2003-05-26 14:07:50 +0000766 const char *underline;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000767
Glenn L McGrath06e95652003-02-09 06:51:14 +0000768 if (!value)
769 value = "";
Glenn L McGrath393183d2003-05-26 14:07:50 +0000770 underline = *name_after_underline ? "_" : "";
771 asprintf(&s, "%s%s%s=%s", name_before_underline, underline,
Glenn L McGrath06e95652003-02-09 06:51:14 +0000772 name_after_underline, value);
Glenn L McGrath393183d2003-05-26 14:07:50 +0000773 if(s) {
Glenn L McGrath5875be42003-09-08 15:39:09 +0000774 putenv(s);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000775 }
776}
777
Rob Landleya2d9a172006-04-28 19:38:04 +0000778#if defined(CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV) || defined(CONFIG_FEATURE_HTTPD_WITHOUT_INETD)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000779/* set environs SERVER_PORT and REMOTE_PORT */
780static void addEnvPort(const char *port_name)
781{
782 char buf[16];
783
784 sprintf(buf, "%u", config->port);
785 addEnv(port_name, "PORT", buf);
786}
Eric Andersena3bb3e62003-06-26 09:05:32 +0000787#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000788#endif /* CONFIG_FEATURE_HTTPD_CGI */
789
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000790
791#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000792/****************************************************************************
793 *
794 > $Function: decodeBase64()
795 *
796 > $Description: Decode a base 64 data stream as per rfc1521.
797 * Note that the rfc states that none base64 chars are to be ignored.
798 * Since the decode always results in a shorter size than the input, it is
799 * OK to pass the input arg as an output arg.
800 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000801 * $Parameter:
802 * (char *) Data . . . . A pointer to a base64 encoded string.
803 * Where to place the decoded data.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000804 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000805 * $Return: void
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000806 *
807 * $Errors: None
808 *
809 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000810static void decodeBase64(char *Data)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000811{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000812
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +0000813 const unsigned char *in = (const unsigned char *)Data;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000814 // The decoded size will be at most 3/4 the size of the encoded
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000815 unsigned long ch = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000816 int i = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000817
818 while (*in) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000819 int t = *in++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000820
Glenn L McGrath874e3382003-05-14 12:11:36 +0000821 if(t >= '0' && t <= '9')
822 t = t - '0' + 52;
823 else if(t >= 'A' && t <= 'Z')
824 t = t - 'A';
825 else if(t >= 'a' && t <= 'z')
826 t = t - 'a' + 26;
827 else if(t == '+')
828 t = 62;
829 else if(t == '/')
830 t = 63;
831 else if(t == '=')
832 t = 0;
833 else
834 continue;
835
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000836 ch = (ch << 6) | t;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000837 i++;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000838 if (i == 4) {
839 *Data++ = (char) (ch >> 16);
840 *Data++ = (char) (ch >> 8);
841 *Data++ = (char) ch;
842 i = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000843 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000844 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000845 *Data = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000846}
847#endif
848
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000849
Rob Landleya2d9a172006-04-28 19:38:04 +0000850#ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000851/****************************************************************************
852 *
853 > $Function: openServer()
854 *
855 * $Description: create a listen server socket on the designated port.
856 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000857 * $Return: (int) . . . A connection socket. -1 for errors.
858 *
859 * $Errors: None
860 *
861 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000862static int openServer(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000863{
864 struct sockaddr_in lsocket;
865 int fd;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000866
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000867 /* create the socket right now */
868 /* inet_addr() returns a value that is already in network order */
869 memset(&lsocket, 0, sizeof(lsocket));
870 lsocket.sin_family = AF_INET;
871 lsocket.sin_addr.s_addr = INADDR_ANY;
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +0000872 lsocket.sin_port = htons(config->port);
873 fd = bb_xsocket(AF_INET, SOCK_STREAM, 0);
874 /* tell the OS it's OK to reuse a previous address even though */
875 /* it may still be in a close down state. Allows bind to succeed. */
876 int on = 1;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000877#ifdef SO_REUSEPORT
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +0000878 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *)&on, sizeof(on)) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000879#else
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +0000880 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000881#endif
Bernhard Reutner-Fischer67f641e2006-04-12 18:24:37 +0000882 bb_xbind(fd, (struct sockaddr *)&lsocket, sizeof(lsocket));
883 listen(fd, 9); /* bb_xlisten? */
884 signal(SIGCHLD, SIG_IGN); /* prevent zombie (defunct) processes */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000885 return fd;
886}
Rob Landleya2d9a172006-04-28 19:38:04 +0000887#endif /* CONFIG_FEATURE_HTTPD_WITHOUT_INETD */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000888
889/****************************************************************************
890 *
891 > $Function: sendHeaders()
892 *
893 * $Description: Create and send HTTP response headers.
894 * The arguments are combined and sent as one write operation. Note that
895 * IE will puke big-time if the headers are not sent in one packet and the
Glenn L McGrath06e95652003-02-09 06:51:14 +0000896 * second packet is delayed for any reason.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000897 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000898 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000899 * (HttpResponseNum) responseNum . . . The result code to send.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000900 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000901 * $Return: (int) . . . . writing errors
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000902 *
903 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000904static int sendHeaders(HttpResponseNum responseNum)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000905{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000906 char *buf = config->buf;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000907 const char *responseString = "";
908 const char *infoString = 0;
Eric Andersen07f2fea2004-10-08 08:03:29 +0000909 const char *mime_type;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000910 unsigned int i;
911 time_t timer = time(0);
912 char timeStr[80];
Glenn L McGrath06e95652003-02-09 06:51:14 +0000913 int len;
914
915 for (i = 0;
916 i < (sizeof(httpResponseNames)/sizeof(httpResponseNames[0])); i++) {
917 if (httpResponseNames[i].type == responseNum) {
918 responseString = httpResponseNames[i].name;
919 infoString = httpResponseNames[i].info;
920 break;
921 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000922 }
Eric Andersen07f2fea2004-10-08 08:03:29 +0000923 /* error message is HTML */
924 mime_type = responseNum == HTTP_OK ?
925 config->httpd_found.found_mime_type : "text/html";
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000926
927 /* emit the current date */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000928 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&timer));
929 len = sprintf(buf,
"Vladimir N. Oleynik"2e33daa2006-01-26 10:46:14 +0000930 "HTTP/1.0 %d %s\r\nContent-type: %s\r\n"
Glenn L McGrath06e95652003-02-09 06:51:14 +0000931 "Date: %s\r\nConnection: close\r\n",
Eric Andersen07f2fea2004-10-08 08:03:29 +0000932 responseNum, responseString, mime_type, timeStr);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000933
Glenn L McGrath3d2405c2003-02-10 22:28:21 +0000934#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath06e95652003-02-09 06:51:14 +0000935 if (responseNum == HTTP_UNAUTHORIZED) {
936 len += sprintf(buf+len, "WWW-Authenticate: Basic realm=\"%s\"\r\n",
937 config->realm);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000938 }
Glenn L McGrath3d2405c2003-02-10 22:28:21 +0000939#endif
Eric Andersen07f2fea2004-10-08 08:03:29 +0000940 if(responseNum == HTTP_MOVED_TEMPORARILY) {
941 len += sprintf(buf+len, "Location: %s/%s%s\r\n",
942 config->httpd_found.found_moved_temporarily,
943 (config->query ? "?" : ""),
944 (config->query ? config->query : ""));
945 }
946
Glenn L McGrath06e95652003-02-09 06:51:14 +0000947 if (config->ContentLength != -1) { /* file */
948 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&config->last_mod));
Glenn L McGrath5cd64612003-08-29 15:53:23 +0000949 len += sprintf(buf+len, "Last-Modified: %s\r\n%s " cont_l_fmt "\r\n",
Eric Andersen0cb6f352006-01-30 22:30:41 +0000950 timeStr, Content_length, cont_l_type config->ContentLength);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000951 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000952 strcat(buf, "\r\n");
953 len += 2;
954 if (infoString) {
955 len += sprintf(buf+len,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000956 "<HEAD><TITLE>%d %s</TITLE></HEAD>\n"
957 "<BODY><H1>%d %s</H1>\n%s\n</BODY>\n",
958 responseNum, responseString,
Glenn L McGrath06e95652003-02-09 06:51:14 +0000959 responseNum, responseString, infoString);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000960 }
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +0000961#if DEBUG
962 fprintf(stderr, "Headers: '%s'", buf);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000963#endif
Rob Landley53437472006-07-16 08:14:35 +0000964 return full_write(a_c_w, buf, len);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000965}
966
967/****************************************************************************
968 *
969 > $Function: getLine()
970 *
971 * $Description: Read from the socket until an end of line char found.
972 *
973 * Characters are read one at a time until an eol sequence is found.
974 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000975 * $Return: (int) . . . . number of characters read. -1 if error.
976 *
977 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000978static int getLine(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000979{
980 int count = 0;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +0000981 char *buf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000982
983 while (read(a_c_r, buf + count, 1) == 1) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000984 if (buf[count] == '\r') continue;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000985 if (buf[count] == '\n') {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000986 buf[count] = 0;
987 return count;
988 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000989 if(count < (MAX_MEMORY_BUFF-1)) /* check owerflow */
990 count++;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000991 }
992 if (count) return count;
993 else return -1;
994}
995
Glenn L McGrath06e95652003-02-09 06:51:14 +0000996#ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000997/****************************************************************************
998 *
999 > $Function: sendCgi()
1000 *
1001 * $Description: Execute a CGI script and send it's stdout back
1002 *
1003 * Environment variables are set up and the script is invoked with pipes
1004 * for stdin/stdout. If a post is being done the script is fed the POST
1005 * data in addition to setting the QUERY_STRING variable (for GETs or POSTs).
1006 *
1007 * $Parameters:
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001008 * (const char *) url . . . . . . The requested URL (with leading /).
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001009 * (int bodyLen) . . . . . . . . Length of the post body.
1010 * (const char *cookie) . . . . . For set HTTP_COOKIE.
1011 * (const char *content_type) . . For set CONTENT_TYPE.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001012
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001013 *
1014 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
1015 *
1016 * $Errors: None
1017 *
1018 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001019static int sendCgi(const char *url,
Eric Andersen07f2fea2004-10-08 08:03:29 +00001020 const char *request, int bodyLen, const char *cookie,
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001021 const char *content_type)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001022{
1023 int fromCgi[2]; /* pipe for reading data from CGI */
1024 int toCgi[2]; /* pipe for sending data to CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001025
Glenn L McGrath06e95652003-02-09 06:51:14 +00001026 static char * argp[] = { 0, 0 };
1027 int pid = 0;
1028 int inFd;
1029 int outFd;
1030 int firstLine = 1;
1031
1032 do {
1033 if (pipe(fromCgi) != 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001034 break;
1035 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001036 if (pipe(toCgi) != 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001037 break;
1038 }
1039
1040 pid = fork();
Glenn L McGrath06e95652003-02-09 06:51:14 +00001041 if (pid < 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001042 pid = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001043 break;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001044 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001045
1046 if (!pid) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001047 /* child process */
1048 char *script;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001049 char *purl = strdup( url );
1050 char realpath_buff[MAXPATHLEN];
1051
1052 if(purl == NULL)
1053 _exit(242);
1054
1055 inFd = toCgi[0];
1056 outFd = fromCgi[1];
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001057
1058 dup2(inFd, 0); // replace stdin with the pipe
1059 dup2(outFd, 1); // replace stdout with the pipe
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001060 if(!DEBUG)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001061 dup2(outFd, 2); // replace stderr with the pipe
1062
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001063 close(toCgi[0]);
1064 close(toCgi[1]);
1065 close(fromCgi[0]);
1066 close(fromCgi[1]);
1067
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001068 /*
Glenn L McGrath06e95652003-02-09 06:51:14 +00001069 * Find PATH_INFO.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001070 */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001071 script = purl;
1072 while((script = strchr( script + 1, '/' )) != NULL) {
1073 /* have script.cgi/PATH_INFO or dirs/script.cgi[/PATH_INFO] */
1074 struct stat sb;
1075
1076 *script = '\0';
1077 if(is_directory(purl + 1, 1, &sb) == 0) {
1078 /* not directory, found script.cgi/PATH_INFO */
1079 *script = '/';
1080 break;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001081 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001082 *script = '/'; /* is directory, find next '/' */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001083 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001084 addEnv("PATH", "INFO", script); /* set /PATH_INFO or NULL */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001085 addEnv("PATH", "", getenv("PATH"));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001086 addEnv("REQUEST", "METHOD", request);
Eric Andersen07f2fea2004-10-08 08:03:29 +00001087 if(config->query) {
1088 char *uri = alloca(strlen(purl) + 2 + strlen(config->query));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001089 if(uri)
Eric Andersen07f2fea2004-10-08 08:03:29 +00001090 sprintf(uri, "%s?%s", purl, config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001091 addEnv("REQUEST", "URI", uri);
1092 } else {
1093 addEnv("REQUEST", "URI", purl);
1094 }
1095 if(script != NULL)
1096 *script = '\0'; /* reduce /PATH_INFO */
Rob Landley344ea472005-09-01 09:38:32 +00001097 /* SCRIPT_FILENAME required by PHP in CGI mode */
1098 if(realpath(purl + 1, realpath_buff))
"Vladimir N. Oleynik"54deebf2005-09-19 10:46:44 +00001099 addEnv("SCRIPT", "FILENAME", realpath_buff);
Rob Landley344ea472005-09-01 09:38:32 +00001100 else
"Vladimir N. Oleynik"54deebf2005-09-19 10:46:44 +00001101 *realpath_buff = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001102 /* set SCRIPT_NAME as full path: /cgi-bin/dirs/script.cgi */
1103 addEnv("SCRIPT_NAME", "", purl);
Eric Andersen07f2fea2004-10-08 08:03:29 +00001104 addEnv("QUERY_STRING", "", config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001105 addEnv("SERVER", "SOFTWARE", httpdVersion);
1106 addEnv("SERVER", "PROTOCOL", "HTTP/1.0");
1107 addEnv("GATEWAY_INTERFACE", "", "CGI/1.1");
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001108 addEnv("REMOTE", "ADDR", config->rmt_ip_str);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001109#ifdef CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
Glenn L McGrath06e95652003-02-09 06:51:14 +00001110 addEnvPort("REMOTE");
Glenn L McGrath06e95652003-02-09 06:51:14 +00001111#endif
1112 if(bodyLen) {
1113 char sbl[32];
1114
1115 sprintf(sbl, "%d", bodyLen);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001116 addEnv("CONTENT", "LENGTH", sbl);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001117 }
1118 if(cookie)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001119 addEnv("HTTP", "COOKIE", cookie);
1120 if(content_type)
1121 addEnv("CONTENT", "TYPE", content_type);
Eric Andersen769a3ef2003-12-19 11:23:47 +00001122#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001123 if(config->remoteuser) {
1124 addEnv("REMOTE", "USER", config->remoteuser);
1125 addEnv("AUTH_TYPE", "", "Basic");
Glenn L McGrath06e95652003-02-09 06:51:14 +00001126 }
Eric Andersen769a3ef2003-12-19 11:23:47 +00001127#endif
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001128 if(config->referer)
1129 addEnv("HTTP", "REFERER", config->referer);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001130
1131 /* set execve argp[0] without path */
1132 argp[0] = strrchr( purl, '/' ) + 1;
1133 /* but script argp[0] must have absolute path and chdiring to this */
Rob Landley344ea472005-09-01 09:38:32 +00001134 if(*realpath_buff) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001135 script = strrchr(realpath_buff, '/');
1136 if(script) {
1137 *script = '\0';
1138 if(chdir(realpath_buff) == 0) {
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00001139 // now run the program. If it fails,
1140 // use _exit() so no destructors
1141 // get called and make a mess.
"Vladimir N. Oleynik"4333a092006-01-31 13:53:30 +00001142#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
1143 char *interpr = NULL;
1144 char *suffix = strrchr(purl, '.');
1145
1146 if(suffix) {
1147 Htaccess * cur;
1148 for (cur = config->script_i; cur; cur = cur->next)
1149 if(strcmp(cur->before_colon + 1, suffix) == 0) {
1150 interpr = cur->after_colon;
1151 break;
1152 }
1153 }
1154#endif
1155 *script = '/';
1156#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
1157 if (interpr)
1158 execv(interpr, argp);
1159 else
1160#endif
1161 execv(realpath_buff, argp);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001162 }
1163 }
1164 }
Rob Landleya2d9a172006-04-28 19:38:04 +00001165#ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
Glenn L McGrath06e95652003-02-09 06:51:14 +00001166 config->accepted_socket = 1; /* send to stdout */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001167#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001168 sendHeaders(HTTP_NOT_FOUND);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001169 _exit(242);
1170 } /* end child */
1171
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001172 } while (0);
1173
Glenn L McGrath06e95652003-02-09 06:51:14 +00001174 if (pid) {
1175 /* parent process */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001176 int status;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001177 size_t post_readed_size = 0, post_readed_idx = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001178
1179 inFd = fromCgi[0];
1180 outFd = toCgi[1];
1181 close(fromCgi[1]);
1182 close(toCgi[0]);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001183 signal(SIGPIPE, SIG_IGN);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001184
1185 while (1) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001186 fd_set readSet;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001187 fd_set writeSet;
1188 char wbuf[128];
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001189 int nfound;
1190 int count;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001191
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001192 FD_ZERO(&readSet);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001193 FD_ZERO(&writeSet);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001194 FD_SET(inFd, &readSet);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001195 if(bodyLen > 0 || post_readed_size > 0) {
1196 FD_SET(outFd, &writeSet);
1197 nfound = outFd > inFd ? outFd : inFd;
1198 if(post_readed_size == 0) {
1199 FD_SET(a_c_r, &readSet);
1200 if(nfound < a_c_r)
1201 nfound = a_c_r;
1202 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001203 /* Now wait on the set of sockets! */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001204 nfound = select(nfound + 1, &readSet, &writeSet, 0, NULL);
1205 } else {
1206 if(!bodyLen) {
1207 close(outFd);
1208 bodyLen = -1;
1209 }
1210 nfound = select(inFd + 1, &readSet, 0, 0, NULL);
1211 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001212
Glenn L McGrath06e95652003-02-09 06:51:14 +00001213 if (nfound <= 0) {
1214 if (waitpid(pid, &status, WNOHANG) > 0) {
1215 close(inFd);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001216#if DEBUG
1217 if (WIFEXITED(status))
Manuel Novoa III cad53642003-03-19 09:13:01 +00001218 bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status));
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001219 if (WIFSIGNALED(status))
Manuel Novoa III cad53642003-03-19 09:13:01 +00001220 bb_error_msg("piped has exited with signal=%d", WTERMSIG(status));
Glenn L McGrath06e95652003-02-09 06:51:14 +00001221#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001222 break;
1223 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001224 } else if(post_readed_size > 0 && FD_ISSET(outFd, &writeSet)) {
Rob Landley53437472006-07-16 08:14:35 +00001225 count = full_write(outFd, wbuf + post_readed_idx, post_readed_size);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001226 if(count > 0) {
1227 post_readed_size -= count;
1228 post_readed_idx += count;
1229 if(post_readed_size == 0)
1230 post_readed_idx = 0;
Paul Fox77ee5232005-07-20 18:42:52 +00001231 } else {
1232 post_readed_size = post_readed_idx = bodyLen = 0; /* broken pipe to CGI */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001233 }
1234 } else if(bodyLen > 0 && post_readed_size == 0 && FD_ISSET(a_c_r, &readSet)) {
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001235 count = bodyLen > (int)sizeof(wbuf) ? (int)sizeof(wbuf) : bodyLen;
Eric Andersen97a1de12004-08-26 22:22:50 +00001236 count = safe_read(a_c_r, wbuf, count);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001237 if(count > 0) {
1238 post_readed_size += count;
1239 bodyLen -= count;
Paul Fox77ee5232005-07-20 18:42:52 +00001240 } else {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001241 bodyLen = 0; /* closed */
1242 }
Eric Andersen97a1de12004-08-26 22:22:50 +00001243 }
1244 if(FD_ISSET(inFd, &readSet)) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001245 int s = a_c_w;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001246 char *rbuf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001247
Eric Andersen97a1de12004-08-26 22:22:50 +00001248#ifndef PIPE_BUF
1249# define PIPESIZE 4096 /* amount of buffering in a pipe */
1250#else
1251# define PIPESIZE PIPE_BUF
1252#endif
1253#if PIPESIZE >= MAX_MEMORY_BUFF
1254# error "PIPESIZE >= MAX_MEMORY_BUFF"
1255#endif
1256
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001257 // There is something to read
Eric Andersen97a1de12004-08-26 22:22:50 +00001258 count = safe_read(inFd, rbuf, PIPESIZE);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001259 if (count == 0)
1260 break; /* closed */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001261 if (count > 0) {
1262 if (firstLine) {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001263 rbuf[count] = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001264 /* check to see if the user script added headers */
"Vladimir N. Oleynik"2e33daa2006-01-26 10:46:14 +00001265 if(strncmp(rbuf, "HTTP/1.0 200 OK\r\n", 4) != 0) {
Rob Landley53437472006-07-16 08:14:35 +00001266 full_write(s, "HTTP/1.0 200 OK\r\n", 17);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001267 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001268 if (strstr(rbuf, "ontent-") == 0) {
Rob Landley53437472006-07-16 08:14:35 +00001269 full_write(s, "Content-type: text/plain\r\n\r\n", 28);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001270 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001271 firstLine = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001272 }
Rob Landley53437472006-07-16 08:14:35 +00001273 if (full_write(s, rbuf, count) != count)
Eric Andersenef437492004-02-04 11:10:28 +00001274 break;
1275
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001276#if DEBUG
1277 fprintf(stderr, "cgi read %d bytes\n", count);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001278#endif
1279 }
1280 }
1281 }
1282 }
1283 return 0;
1284}
Glenn L McGrath06e95652003-02-09 06:51:14 +00001285#endif /* CONFIG_FEATURE_HTTPD_CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001286
1287/****************************************************************************
1288 *
1289 > $Function: sendFile()
1290 *
1291 * $Description: Send a file response to an HTTP request
1292 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00001293 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001294 * (const char *) url . . The URL requested.
1295 *
1296 * $Return: (int) . . . . . . Always 0.
1297 *
1298 ****************************************************************************/
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001299static int sendFile(const char *url)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001300{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001301 char * suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001302 int f;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001303 const char * const * table;
1304 const char * try_suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001305
Glenn L McGrath06e95652003-02-09 06:51:14 +00001306 suffix = strrchr(url, '.');
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001307
Glenn L McGrath06e95652003-02-09 06:51:14 +00001308 for (table = suffixTable; *table; table += 2)
1309 if(suffix != NULL && (try_suffix = strstr(*table, suffix)) != 0) {
1310 try_suffix += strlen(suffix);
1311 if(*try_suffix == 0 || *try_suffix == '.')
1312 break;
1313 }
1314 /* also, if not found, set default as "application/octet-stream"; */
Eric Andersen07f2fea2004-10-08 08:03:29 +00001315 config->httpd_found.found_mime_type = *(table+1);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001316#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
1317 if (suffix) {
1318 Htaccess * cur;
1319
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001320 for (cur = config->mime_a; cur; cur = cur->next) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001321 if(strcmp(cur->before_colon, suffix) == 0) {
Eric Andersen07f2fea2004-10-08 08:03:29 +00001322 config->httpd_found.found_mime_type = cur->after_colon;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001323 break;
1324 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001325 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001326 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001327#endif /* CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES */
1328
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001329#if DEBUG
1330 fprintf(stderr, "Sending file '%s' Content-type: %s\n",
1331 url, config->httpd_found.found_mime_type);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001332#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001333
1334 f = open(url, O_RDONLY);
1335 if (f >= 0) {
1336 int count;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001337 char *buf = config->buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001338
1339 sendHeaders(HTTP_OK);
Rob Landley53437472006-07-16 08:14:35 +00001340 while ((count = full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
1341 if (full_write(a_c_w, buf, count) != count)
Glenn L McGrath9adcf732003-12-08 20:21:53 +00001342 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001343 }
1344 close(f);
1345 } else {
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001346#if DEBUG
1347 bb_perror_msg("Unable to open '%s'", url);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001348#endif
1349 sendHeaders(HTTP_NOT_FOUND);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001350 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001351
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001352 return 0;
1353}
1354
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001355static int checkPermIP(void)
1356{
1357 Htaccess_IP * cur;
1358
1359 /* This could stand some work */
1360 for (cur = config->ip_a_d; cur; cur = cur->next) {
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001361#if DEBUG
1362 fprintf(stderr, "checkPermIP: '%s' ? ", config->rmt_ip_str);
1363 fprintf(stderr, "'%u.%u.%u.%u/%u.%u.%u.%u'\n",
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001364 (unsigned char)(cur->ip >> 24),
1365 (unsigned char)(cur->ip >> 16),
1366 (unsigned char)(cur->ip >> 8),
1367 cur->ip & 0xff,
1368 (unsigned char)(cur->mask >> 24),
1369 (unsigned char)(cur->mask >> 16),
1370 (unsigned char)(cur->mask >> 8),
1371 cur->mask & 0xff);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001372#endif
1373 if((config->rmt_ip & cur->mask) == cur->ip)
1374 return cur->allow_deny == 'A'; /* Allow/Deny */
1375 }
1376
Eric Andersenaff114c2004-04-14 17:51:38 +00001377 /* if unconfigured, return 1 - access from all */
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001378 return !config->flg_deny_all;
1379}
1380
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001381/****************************************************************************
1382 *
1383 > $Function: checkPerm()
1384 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001385 * $Description: Check the permission file for access password protected.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001386 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001387 * If config file isn't present, everything is allowed.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001388 * Entries are of the form you can see example from header source
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001389 *
1390 * $Parameters:
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001391 * (const char *) path . . . . The file path.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001392 * (const char *) request . . . User information to validate.
1393 *
1394 * $Return: (int) . . . . . . . . . 1 if request OK, 0 otherwise.
1395 *
1396 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001397
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001398#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001399static int checkPerm(const char *path, const char *request)
1400{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001401 Htaccess * cur;
1402 const char *p;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001403 const char *p0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001404
Glenn L McGrath06e95652003-02-09 06:51:14 +00001405 const char *prev = NULL;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001406
1407 /* This could stand some work */
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001408 for (cur = config->auth; cur; cur = cur->next) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001409 p0 = cur->before_colon;
1410 if(prev != NULL && strcmp(prev, p0) != 0)
1411 continue; /* find next identical */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001412 p = cur->after_colon;
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001413#if DEBUG
1414 fprintf(stderr,"checkPerm: '%s' ? '%s'\n", p0, request);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001415#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001416 {
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001417 size_t l = strlen(p0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001418
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001419 if(strncmp(p0, path, l) == 0 &&
1420 (l == 1 || path[l] == '/' || path[l] == 0)) {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001421 char *u;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001422 /* path match found. Check request */
Eric Andersen35e643b2003-07-28 07:40:39 +00001423 /* for check next /path:user:password */
1424 prev = p0;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001425 u = strchr(request, ':');
1426 if(u == NULL) {
1427 /* bad request, ':' required */
1428 break;
1429 }
1430
Eric Andersen35e643b2003-07-28 07:40:39 +00001431#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
1432 {
1433 char *cipher;
1434 char *pp;
Eric Andersen35e643b2003-07-28 07:40:39 +00001435
Eric Andersen35e643b2003-07-28 07:40:39 +00001436 if(strncmp(p, request, u-request) != 0) {
1437 /* user uncompared */
1438 continue;
1439 }
1440 pp = strchr(p, ':');
1441 if(pp && pp[1] == '$' && pp[2] == '1' &&
1442 pp[3] == '$' && pp[4]) {
1443 pp++;
1444 cipher = pw_encrypt(u+1, pp);
1445 if (strcmp(cipher, pp) == 0)
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001446 goto set_remoteuser_var; /* Ok */
Eric Andersen35e643b2003-07-28 07:40:39 +00001447 /* unauthorized */
1448 continue;
1449 }
1450 }
1451#endif
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001452 if (strcmp(p, request) == 0) {
Glenn L McGrath9d1a33c2003-10-06 13:23:06 +00001453#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001454set_remoteuser_var:
Glenn L McGrath9d1a33c2003-10-06 13:23:06 +00001455#endif
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001456 config->remoteuser = strdup(request);
1457 if(config->remoteuser)
1458 config->remoteuser[(u - request)] = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001459 return 1; /* Ok */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001460 }
Eric Andersen35e643b2003-07-28 07:40:39 +00001461 /* unauthorized */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001462 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001463 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001464 } /* for */
1465
Glenn L McGrath06e95652003-02-09 06:51:14 +00001466 return prev == NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001467}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001468
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001469#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1470
Eric Andersen07f2fea2004-10-08 08:03:29 +00001471/****************************************************************************
1472 *
Mike Frysingerbb12d6f2006-01-03 23:59:01 +00001473 > $Function: handle_sigalrm()
Eric Andersen07f2fea2004-10-08 08:03:29 +00001474 *
Mike Frysingerbb12d6f2006-01-03 23:59:01 +00001475 * $Description: Handle timeouts
Eric Andersen07f2fea2004-10-08 08:03:29 +00001476 *
1477 ****************************************************************************/
1478
1479static void
1480handle_sigalrm( int sig )
1481{
1482 sendHeaders(HTTP_REQUEST_TIMEOUT);
1483 config->alarm_signaled = sig;
1484}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001485
1486/****************************************************************************
1487 *
1488 > $Function: handleIncoming()
1489 *
1490 * $Description: Handle an incoming http request.
1491 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001492 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001493static void handleIncoming(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001494{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001495 char *buf = config->buf;
1496 char *url;
1497 char *purl;
1498 int blank = -1;
Glenn L McGrath3d752f72004-03-05 09:38:16 +00001499 char *test;
1500 struct stat sb;
1501 int ip_allowed;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001502#ifdef CONFIG_FEATURE_HTTPD_CGI
1503 const char *prequest = request_GET;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001504 long length=0;
1505 char *cookie = 0;
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001506 char *content_type = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001507#endif
Glenn L McGrath3d752f72004-03-05 09:38:16 +00001508 fd_set s_fd;
1509 struct timeval tv;
Eric Andersend8746cd2004-02-24 07:28:38 +00001510 int retval;
Eric Andersen07f2fea2004-10-08 08:03:29 +00001511 struct sigaction sa;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001512
1513#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1514 int credentials = -1; /* if not requred this is Ok */
1515#endif
1516
Eric Andersen07f2fea2004-10-08 08:03:29 +00001517 sa.sa_handler = handle_sigalrm;
1518 sigemptyset(&sa.sa_mask);
1519 sa.sa_flags = 0; /* no SA_RESTART */
1520 sigaction(SIGALRM, &sa, NULL);
1521
Glenn L McGrath06e95652003-02-09 06:51:14 +00001522 do {
1523 int count;
1524
Eric Andersen07f2fea2004-10-08 08:03:29 +00001525 (void) alarm( TIMEOUT );
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001526 if (getLine() <= 0)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001527 break; /* closed */
1528
1529 purl = strpbrk(buf, " \t");
1530 if(purl == NULL) {
1531BAD_REQUEST:
1532 sendHeaders(HTTP_BAD_REQUEST);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001533 break;
1534 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001535 *purl = 0;
1536#ifdef CONFIG_FEATURE_HTTPD_CGI
1537 if(strcasecmp(buf, prequest) != 0) {
1538 prequest = "POST";
1539 if(strcasecmp(buf, prequest) != 0) {
1540 sendHeaders(HTTP_NOT_IMPLEMENTED);
1541 break;
1542 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001543 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001544#else
1545 if(strcasecmp(buf, request_GET) != 0) {
1546 sendHeaders(HTTP_NOT_IMPLEMENTED);
1547 break;
1548 }
1549#endif
1550 *purl = ' ';
1551 count = sscanf(purl, " %[^ ] HTTP/%d.%*d", buf, &blank);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001552
Glenn L McGrath06e95652003-02-09 06:51:14 +00001553 if (count < 1 || buf[0] != '/') {
1554 /* Garbled request/URL */
1555 goto BAD_REQUEST;
1556 }
1557 url = alloca(strlen(buf) + 12); /* + sizeof("/index.html\0") */
1558 if(url == NULL) {
1559 sendHeaders(HTTP_INTERNAL_SERVER_ERROR);
1560 break;
1561 }
1562 strcpy(url, buf);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001563 /* extract url args if present */
Eric Andersen07f2fea2004-10-08 08:03:29 +00001564 test = strchr(url, '?');
1565 if (test) {
1566 *test++ = 0;
1567 config->query = test;
1568 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001569
"Vladimir N. Oleynik"ab90b9f2006-01-24 12:02:27 +00001570 test = decodeString(url, 0);
1571 if(test == NULL)
1572 goto BAD_REQUEST;
1573 if(test == (buf+1)) {
1574 sendHeaders(HTTP_NOT_FOUND);
1575 break;
1576 }
Manuel Novoa III cad53642003-03-19 09:13:01 +00001577 /* algorithm stolen from libbb bb_simplify_path(),
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001578 but don`t strdup and reducing trailing slash and protect out root */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001579 purl = test = url;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001580
Glenn L McGrath06e95652003-02-09 06:51:14 +00001581 do {
1582 if (*purl == '/') {
1583 if (*test == '/') { /* skip duplicate (or initial) slash */
1584 continue;
1585 } else if (*test == '.') {
1586 if (test[1] == '/' || test[1] == 0) { /* skip extra '.' */
1587 continue;
1588 } else if ((test[1] == '.') && (test[2] == '/' || test[2] == 0)) {
1589 ++test;
1590 if (purl == url) {
1591 /* protect out root */
1592 goto BAD_REQUEST;
1593 }
1594 while (*--purl != '/'); /* omit previous dir */
1595 continue;
1596 }
1597 }
1598 }
1599 *++purl = *test;
1600 } while (*++test);
1601
1602 *++purl = 0; /* so keep last character */
1603 test = purl; /* end ptr */
1604
1605 /* If URL is directory, adding '/' */
1606 if(test[-1] != '/') {
1607 if ( is_directory(url + 1, 1, &sb) ) {
Eric Andersen07f2fea2004-10-08 08:03:29 +00001608 config->httpd_found.found_moved_temporarily = url;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001609 }
1610 }
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001611#if DEBUG
1612 fprintf(stderr, "url='%s', args=%s\n", url, config->query);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001613#endif
1614
1615 test = url;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001616 ip_allowed = checkPermIP();
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001617 while(ip_allowed && (test = strchr( test + 1, '/' )) != NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001618 /* have path1/path2 */
1619 *test = '\0';
1620 if( is_directory(url + 1, 1, &sb) ) {
1621 /* may be having subdir config */
1622 parse_conf(url + 1, SUBDIR_PARSE);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001623 ip_allowed = checkPermIP();
Glenn L McGrath06e95652003-02-09 06:51:14 +00001624 }
1625 *test = '/';
1626 }
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001627 if(blank >= 0) {
1628 // read until blank line for HTTP version specified, else parse immediate
1629 while(1) {
1630 alarm(TIMEOUT);
1631 count = getLine();
1632 if(count <= 0)
1633 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001634
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001635#if DEBUG
1636 fprintf(stderr, "Header: '%s'\n", buf);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001637#endif
1638
1639#ifdef CONFIG_FEATURE_HTTPD_CGI
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001640 /* try and do our best to parse more lines */
1641 if ((strncasecmp(buf, Content_length, 15) == 0)) {
1642 if(prequest != request_GET)
1643 length = strtol(buf + 15, 0, 0); // extra read only for POST
1644 } else if ((strncasecmp(buf, "Cookie:", 7) == 0)) {
1645 for(test = buf + 7; isspace(*test); test++)
1646 ;
1647 cookie = strdup(test);
1648 } else if ((strncasecmp(buf, "Content-Type:", 13) == 0)) {
1649 for(test = buf + 13; isspace(*test); test++)
1650 ;
1651 content_type = strdup(test);
1652 } else if ((strncasecmp(buf, "Referer:", 8) == 0)) {
1653 for(test = buf + 8; isspace(*test); test++)
1654 ;
1655 config->referer = strdup(test);
1656 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001657#endif
1658
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001659#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001660 if (strncasecmp(buf, "Authorization:", 14) == 0) {
1661 /* We only allow Basic credentials.
1662 * It shows up as "Authorization: Basic <userid:password>" where
1663 * the userid:password is base64 encoded.
1664 */
1665 for(test = buf + 14; isspace(*test); test++)
1666 ;
1667 if (strncasecmp(test, "Basic", 5) != 0)
1668 continue;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001669
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001670 test += 5; /* decodeBase64() skiping space self */
1671 decodeBase64(test);
1672 credentials = checkPerm(url, test);
1673 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001674#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1675
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001676 } /* while extra header reading */
1677 }
Eric Andersen07f2fea2004-10-08 08:03:29 +00001678 (void) alarm( 0 );
1679 if(config->alarm_signaled)
1680 break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001681
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001682 if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001683 /* protect listing [/path]/httpd_conf or IP deny */
1684#ifdef CONFIG_FEATURE_HTTPD_CGI
1685FORBIDDEN: /* protect listing /cgi-bin */
1686#endif
1687 sendHeaders(HTTP_FORBIDDEN);
1688 break;
1689 }
1690
1691#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1692 if (credentials <= 0 && checkPerm(url, ":") == 0) {
1693 sendHeaders(HTTP_UNAUTHORIZED);
1694 break;
1695 }
1696#endif
1697
Eric Andersen07f2fea2004-10-08 08:03:29 +00001698 if(config->httpd_found.found_moved_temporarily) {
1699 sendHeaders(HTTP_MOVED_TEMPORARILY);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001700#if DEBUG
Eric Andersen07f2fea2004-10-08 08:03:29 +00001701 /* clear unforked memory flag */
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001702 config->httpd_found.found_moved_temporarily = NULL;
Eric Andersen07f2fea2004-10-08 08:03:29 +00001703#endif
1704 break;
1705 }
1706
Glenn L McGrath06e95652003-02-09 06:51:14 +00001707 test = url + 1; /* skip first '/' */
1708
1709#ifdef CONFIG_FEATURE_HTTPD_CGI
1710 /* if strange Content-Length */
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001711 if (length < 0)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001712 break;
1713
Glenn L McGrath06e95652003-02-09 06:51:14 +00001714 if (strncmp(test, "cgi-bin", 7) == 0) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001715 if(test[7] == '/' && test[8] == 0)
1716 goto FORBIDDEN; // protect listing cgi-bin/
Eric Andersen07f2fea2004-10-08 08:03:29 +00001717 sendCgi(url, prequest, length, cookie, content_type);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001718 } else {
1719 if (prequest != request_GET)
1720 sendHeaders(HTTP_NOT_IMPLEMENTED);
1721 else {
1722#endif /* CONFIG_FEATURE_HTTPD_CGI */
1723 if(purl[-1] == '/')
1724 strcpy(purl, "index.html");
1725 if ( stat(test, &sb ) == 0 ) {
1726 config->ContentLength = sb.st_size;
1727 config->last_mod = sb.st_mtime;
1728 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001729 sendFile(test);
Rob Landleya2d9a172006-04-28 19:38:04 +00001730#ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
Glenn L McGrath06e95652003-02-09 06:51:14 +00001731 /* unset if non inetd looped */
1732 config->ContentLength = -1;
1733#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001734
Glenn L McGrath06e95652003-02-09 06:51:14 +00001735#ifdef CONFIG_FEATURE_HTTPD_CGI
1736 }
1737 }
1738#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001739
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001740 } while (0);
1741
Glenn L McGrath06e95652003-02-09 06:51:14 +00001742
Rob Landleya2d9a172006-04-28 19:38:04 +00001743#ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
Glenn L McGrath06e95652003-02-09 06:51:14 +00001744/* from inetd don`t looping: freeing, closing automatic from exit always */
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001745# if DEBUG
1746 fprintf(stderr, "closing socket\n");
Glenn L McGrath06e95652003-02-09 06:51:14 +00001747# endif
1748# ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath06e95652003-02-09 06:51:14 +00001749 free(cookie);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001750 free(content_type);
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00001751 free(config->referer);
Eric Andersen769a3ef2003-12-19 11:23:47 +00001752#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1753 free(config->remoteuser);
1754#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001755# endif
Rob Landleya2d9a172006-04-28 19:38:04 +00001756#endif /* CONFIG_FEATURE_HTTPD_WITHOUT_INETD */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001757 shutdown(a_c_w, SHUT_WR);
Eric Andersend8746cd2004-02-24 07:28:38 +00001758
1759 /* Properly wait for remote to closed */
1760 FD_ZERO (&s_fd) ;
Bernhard Reutner-Fischere7f8a322006-04-11 13:51:50 +00001761 FD_SET (a_c_r, &s_fd) ;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001762
Eric Andersend8746cd2004-02-24 07:28:38 +00001763 do {
1764 tv.tv_sec = 2 ;
1765 tv.tv_usec = 0 ;
Bernhard Reutner-Fischere7f8a322006-04-11 13:51:50 +00001766 retval = select (a_c_r + 1, &s_fd, NULL, NULL, &tv);
1767 } while (retval > 0 && (read (a_c_r, buf, sizeof (config->buf)) > 0));
Eric Andersend8746cd2004-02-24 07:28:38 +00001768
Glenn L McGrath06e95652003-02-09 06:51:14 +00001769 shutdown(a_c_r, SHUT_RD);
Rob Landleya2d9a172006-04-28 19:38:04 +00001770#ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
Glenn L McGrath06e95652003-02-09 06:51:14 +00001771 close(config->accepted_socket);
Rob Landleya2d9a172006-04-28 19:38:04 +00001772#endif /* CONFIG_FEATURE_HTTPD_WITHOUT_INETD */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001773}
1774
1775/****************************************************************************
1776 *
1777 > $Function: miniHttpd()
1778 *
1779 * $Description: The main http server function.
1780 *
1781 * Given an open socket fildes, listen for new connections and farm out
1782 * the processing as a forked process.
1783 *
1784 * $Parameters:
1785 * (int) server. . . The server socket fildes.
1786 *
1787 * $Return: (int) . . . . Always 0.
1788 *
1789 ****************************************************************************/
Rob Landleya2d9a172006-04-28 19:38:04 +00001790#ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001791static int miniHttpd(int server)
1792{
1793 fd_set readfd, portfd;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001794
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001795 FD_ZERO(&portfd);
1796 FD_SET(server, &portfd);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001797
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001798 /* copy the ports we are watching to the readfd set */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001799 while (1) {
1800 readfd = portfd;
1801
Eric Andersenaff114c2004-04-14 17:51:38 +00001802 /* Now wait INDEFINITELY on the set of sockets! */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001803 if (select(server + 1, &readfd, 0, 0, 0) > 0) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001804 if (FD_ISSET(server, &readfd)) {
1805 int on;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001806 struct sockaddr_in fromAddr;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001807
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001808 socklen_t fromAddrLen = sizeof(fromAddr);
1809 int s = accept(server,
Glenn L McGrath06e95652003-02-09 06:51:14 +00001810 (struct sockaddr *)&fromAddr, &fromAddrLen);
1811
1812 if (s < 0) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001813 continue;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001814 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001815 config->accepted_socket = s;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001816 config->rmt_ip = ntohl(fromAddr.sin_addr.s_addr);
Rob Landleyd086b502006-04-14 02:32:29 +00001817#if defined(CONFIG_FEATURE_HTTPD_CGI) || DEBUG
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001818 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1819 (unsigned char)(config->rmt_ip >> 24),
1820 (unsigned char)(config->rmt_ip >> 16),
1821 (unsigned char)(config->rmt_ip >> 8),
1822 config->rmt_ip & 0xff);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001823 config->port = ntohs(fromAddr.sin_port);
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001824#if DEBUG
1825 bb_error_msg("connection from IP=%s, port %u\n",
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001826 config->rmt_ip_str, config->port);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001827#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001828#endif /* CONFIG_FEATURE_HTTPD_CGI */
1829
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001830 /* set the KEEPALIVE option to cull dead connections */
1831 on = 1;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001832 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof (on));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001833
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001834#if !DEBUG
1835 if (fork() == 0)
1836#endif
1837 {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001838 /* This is the spawned thread */
1839#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
1840 /* protect reload config, may be confuse checking */
1841 signal(SIGHUP, SIG_IGN);
1842#endif
1843 handleIncoming();
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00001844#if !DEBUG
1845 exit(0);
1846#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001847 }
1848 close(s);
1849 }
1850 }
1851 } // while (1)
1852 return 0;
1853}
1854
Glenn L McGrath06e95652003-02-09 06:51:14 +00001855#else
1856 /* from inetd */
1857
1858static int miniHttpd(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001859{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001860 struct sockaddr_in fromAddrLen;
1861 socklen_t sinlen = sizeof (struct sockaddr_in);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001862
1863 getpeername (0, (struct sockaddr *)&fromAddrLen, &sinlen);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001864 config->rmt_ip = ntohl(fromAddrLen.sin_addr.s_addr);
Rob Landleyd086b502006-04-14 02:32:29 +00001865#ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001866 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1867 (unsigned char)(config->rmt_ip >> 24),
1868 (unsigned char)(config->rmt_ip >> 16),
1869 (unsigned char)(config->rmt_ip >> 8),
1870 config->rmt_ip & 0xff);
1871#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001872 config->port = ntohs(fromAddrLen.sin_port);
1873 handleIncoming();
1874 return 0;
1875}
Rob Landleya2d9a172006-04-28 19:38:04 +00001876#endif /* CONFIG_FEATURE_HTTPD_WITHOUT_INETD */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001877
1878#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
1879static void sighup_handler(int sig)
1880{
1881 /* set and reset */
1882 struct sigaction sa;
1883
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001884 parse_conf(default_path_httpd_conf,
1885 sig == SIGHUP ? SIGNALED_PARSE : FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001886 sa.sa_handler = sighup_handler;
1887 sigemptyset(&sa.sa_mask);
1888 sa.sa_flags = SA_RESTART;
1889 sigaction(SIGHUP, &sa, NULL);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001890}
1891#endif
1892
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001893enum httpd_opts_nums {
1894 c_opt_config_file = 0,
1895 d_opt_decode_url,
1896 h_opt_home_httpd,
1897 USE_FEATURE_HTTPD_ENCODE_URL_STR(e_opt_encode_url,)
1898 USE_FEATURE_HTTPD_BASIC_AUTH(r_opt_realm,)
1899 USE_FEATURE_HTTPD_AUTH_MD5(m_opt_md5,)
1900 USE_FEATURE_HTTPD_SETUID(u_opt_setuid,)
Rob Landleya2d9a172006-04-28 19:38:04 +00001901 USE_FEATURE_HTTPD_WITHOUT_INETD(p_opt_port,)
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001902};
Eric Andersena3bb3e62003-06-26 09:05:32 +00001903
1904static const char httpd_opts[]="c:d:h:"
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001905 USE_FEATURE_HTTPD_ENCODE_URL_STR("e:")
1906 USE_FEATURE_HTTPD_BASIC_AUTH("r:")
1907 USE_FEATURE_HTTPD_AUTH_MD5("m:")
1908 USE_FEATURE_HTTPD_SETUID("u:")
Rob Landleya2d9a172006-04-28 19:38:04 +00001909 USE_FEATURE_HTTPD_WITHOUT_INETD("p:");
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001910
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001911#define OPT_CONFIG_FILE (1<<c_opt_config_file)
1912#define OPT_DECODE_URL (1<<d_opt_decode_url)
1913#define OPT_HOME_HTTPD (1<<h_opt_home_httpd)
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001914
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001915#define OPT_ENCODE_URL USE_FEATURE_HTTPD_ENCODE_URL_STR((1<<e_opt_encode_url)) \
Rob Landley0d8766a2006-02-20 23:05:06 +00001916 SKIP_FEATURE_HTTPD_ENCODE_URL_STR(0)
"Vladimir N. Oleynik"27d42a02005-12-02 09:46:04 +00001917
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001918#define OPT_REALM USE_FEATURE_HTTPD_BASIC_AUTH((1<<r_opt_realm)) \
Rob Landley0d8766a2006-02-20 23:05:06 +00001919 SKIP_FEATURE_HTTPD_BASIC_AUTH(0)
Eric Andersena3bb3e62003-06-26 09:05:32 +00001920
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001921#define OPT_MD5 USE_FEATURE_HTTPD_AUTH_MD5((1<<m_opt_md5)) \
Rob Landley0d8766a2006-02-20 23:05:06 +00001922 SKIP_FEATURE_HTTPD_AUTH_MD5(0)
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001923
1924#define OPT_SETUID USE_FEATURE_HTTPD_SETUID((1<<u_opt_setuid)) \
Rob Landley0d8766a2006-02-20 23:05:06 +00001925 SKIP_FEATURE_HTTPD_SETUID(0)
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001926
Rob Landleya2d9a172006-04-28 19:38:04 +00001927#define OPT_PORT USE_FEATURE_HTTPD_WITHOUT_INETD((1<<p_opt_port)) \
1928 SKIP_FEATURE_HTTPD_WITHOUT_INETD(0)
Eric Andersena3bb3e62003-06-26 09:05:32 +00001929
1930
Glenn L McGrath06e95652003-02-09 06:51:14 +00001931int httpd_main(int argc, char *argv[])
Glenn L McGrath06e95652003-02-09 06:51:14 +00001932{
Eric Andersena3bb3e62003-06-26 09:05:32 +00001933 unsigned long opt;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001934 const char *home_httpd = home;
Eric Andersena3bb3e62003-06-26 09:05:32 +00001935 char *url_for_decode;
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001936 USE_FEATURE_HTTPD_ENCODE_URL_STR(const char *url_for_encode;)
Rob Landleya2d9a172006-04-28 19:38:04 +00001937 USE_FEATURE_HTTPD_WITHOUT_INETD(const char *s_port;)
1938 USE_FEATURE_HTTPD_WITHOUT_INETD(int server;)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001939
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001940 USE_FEATURE_HTTPD_SETUID(const char *s_uid;)
1941 USE_FEATURE_HTTPD_SETUID(long uid = -1;)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001942
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001943 USE_FEATURE_HTTPD_AUTH_MD5(const char *pass;)
Eric Andersen35e643b2003-07-28 07:40:39 +00001944
Glenn L McGrath06e95652003-02-09 06:51:14 +00001945 config = xcalloc(1, sizeof(*config));
1946#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1947 config->realm = "Web Server Authentication";
1948#endif
1949
Rob Landleya2d9a172006-04-28 19:38:04 +00001950#ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
Glenn L McGrath06e95652003-02-09 06:51:14 +00001951 config->port = 80;
1952#endif
1953
1954 config->ContentLength = -1;
1955
Eric Andersena3bb3e62003-06-26 09:05:32 +00001956 opt = bb_getopt_ulflags(argc, argv, httpd_opts,
1957 &(config->configFile), &url_for_decode, &home_httpd
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001958 USE_FEATURE_HTTPD_ENCODE_URL_STR(, &url_for_encode)
1959 USE_FEATURE_HTTPD_BASIC_AUTH(, &(config->realm))
1960 USE_FEATURE_HTTPD_AUTH_MD5(, &pass)
1961 USE_FEATURE_HTTPD_SETUID(, &s_uid)
Rob Landleya2d9a172006-04-28 19:38:04 +00001962 USE_FEATURE_HTTPD_WITHOUT_INETD(, &s_port)
"Vladimir N. Oleynik"9a515402006-02-15 13:27:18 +00001963 );
Eric Andersena3bb3e62003-06-26 09:05:32 +00001964
1965 if(opt & OPT_DECODE_URL) {
1966 printf("%s", decodeString(url_for_decode, 1));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001967 return 0;
Eric Andersena3bb3e62003-06-26 09:05:32 +00001968 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001969#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Eric Andersena3bb3e62003-06-26 09:05:32 +00001970 if(opt & OPT_ENCODE_URL) {
1971 printf("%s", encodeString(url_for_encode));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001972 return 0;
Eric Andersena3bb3e62003-06-26 09:05:32 +00001973 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001974#endif
Eric Andersen35e643b2003-07-28 07:40:39 +00001975#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
1976 if(opt & OPT_MD5) {
1977 printf("%s\n", pw_encrypt(pass, "$1$"));
1978 return 0;
1979 }
1980#endif
Rob Landleya2d9a172006-04-28 19:38:04 +00001981#ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
Eric Andersena3bb3e62003-06-26 09:05:32 +00001982 if(opt & OPT_PORT)
1983 config->port = bb_xgetlarg(s_port, 10, 1, 0xffff);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001984#ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersena3bb3e62003-06-26 09:05:32 +00001985 if(opt & OPT_SETUID) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001986 char *e;
1987
Eric Andersena3bb3e62003-06-26 09:05:32 +00001988 uid = strtol(s_uid, &e, 0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001989 if(*e != '\0') {
1990 /* not integer */
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +00001991 uid = bb_xgetpwnam(s_uid);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001992 }
1993 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001994#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00001995#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001996
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +00001997 bb_xchdir(home_httpd);
Rob Landleya2d9a172006-04-28 19:38:04 +00001998#ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
Glenn L McGrath06e95652003-02-09 06:51:14 +00001999 server = openServer();
2000# ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersenaff114c2004-04-14 17:51:38 +00002001 /* drop privileges */
Glenn L McGrath06e95652003-02-09 06:51:14 +00002002 if(uid > 0)
Rob Landley53437472006-07-16 08:14:35 +00002003 xsetuid(uid);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002004# endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002005#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00002006
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002007#ifdef CONFIG_FEATURE_HTTPD_CGI
2008 {
2009 char *p = getenv("PATH");
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002010 if(p) {
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00002011 p = bb_xstrdup(p);
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002012 }
Glenn L McGrath1dc0cca2003-10-03 10:50:56 +00002013 clearenv();
2014 if(p)
2015 setenv("PATH", p, 1);
Rob Landleya2d9a172006-04-28 19:38:04 +00002016# ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
Glenn L McGrath14092a12003-09-12 00:44:50 +00002017 addEnvPort("SERVER");
2018# endif
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002019 }
2020#endif
2021
Glenn L McGrath06e95652003-02-09 06:51:14 +00002022#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
2023 sighup_handler(0);
2024#else
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00002025 parse_conf(default_path_httpd_conf, FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002026#endif
2027
Rob Landleya2d9a172006-04-28 19:38:04 +00002028#ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00002029# if !DEBUG
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +00002030 bb_xdaemon(1, 0); /* don`t change curent directory */
"Vladimir N. Oleynik"6b903a22005-12-20 11:02:54 +00002031# endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00002032 return miniHttpd(server);
2033#else
2034 return miniHttpd();
2035#endif
2036}