blob: b27437ab23eb0ca35ed6c43c390c45f3b2988929 [file] [log] [blame]
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001/*
2 * httpd implementation for busybox
3 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00004 * Copyright (C) 2002,2003 Glenn Engel <glenne@engel.org>
5 * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
Glenn L McGrath58c708a2003-01-05 04:01:56 +00006 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00007 * simplify patch stolen from libbb without using strdup
Glenn L McGrath58c708a2003-01-05 04:01:56 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 *****************************************************************************
24 *
Glenn L McGrath06e95652003-02-09 06:51:14 +000025 * Typical usage:
26 * for non root user
27 * httpd -p 8080 -h $HOME/public_html
28 * or for daemon start from rc script with uid=0:
29 * httpd -u www
30 * This is equivalent if www user have uid=80 to
31 * httpd -p 80 -u 80 -h /www -c /etc/httpd.conf -r "Web Server Authentication"
32 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +000033 *
34 * When a url contains "cgi-bin" it is assumed to be a cgi script. The
35 * server changes directory to the location of the script and executes it
36 * after setting QUERY_STRING and other environment variables. If url args
37 * are included in the url or as a post, the args are placed into decoded
38 * environment variables. e.g. /cgi-bin/setup?foo=Hello%20World will set
Glenn L McGrath06e95652003-02-09 06:51:14 +000039 * the $CGI_foo environment variable to "Hello World" while
40 * CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV enabled.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000041 *
42 * The server can also be invoked as a url arg decoder and html text encoder
43 * as follows:
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000044 * foo=`httpd -d $foo` # decode "Hello%20World" as "Hello World"
Glenn L McGrathc9163fe2003-05-13 16:20:11 +000045 * bar=`httpd -e "<Hello World>"` # encode as "&#60Hello&#32World&#62"
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000046 * Note that url encoding for arguments is not the same as html encoding for
47 * presenation. -d decodes a url-encoded argument while -e encodes in html
48 * for page display.
Glenn L McGrath58c708a2003-01-05 04:01:56 +000049 *
50 * httpd.conf has the following format:
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000051 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +000052 * A:172.20. # Allow address from 172.20.0.0/16
53 * A:10.0.0.0/25 # Allow any address from 10.0.0.0-10.0.0.127
54 * A:10.0.0.0/255.255.255.128 # Allow any address that previous set
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000055 * A:127.0.0.1 # Allow local loopback connections
56 * D:* # Deny from other IP connections
57 * /cgi-bin:foo:bar # Require user foo, pwd bar on urls starting with /cgi-bin/
58 * /adm:admin:setup # Require user admin, pwd setup on urls starting with /adm/
59 * /adm:toor:PaSsWd # or user toor, pwd PaSsWd on urls starting with /adm/
60 * .au:audio/basic # additional mime type for audio.au files
61 *
62 * A/D may be as a/d or allow/deny - first char case unsensitive
Glenn L McGrath393183d2003-05-26 14:07:50 +000063 * Deny IP rules take precedence over allow rules.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000064 *
65 *
66 * The Deny/Allow IP logic:
67 *
68 * - Default is to allow all. No addresses are denied unless
69 * denied with a D: rule.
70 * - Order of Deny/Allow rules is significant
71 * - Deny rules take precedence over allow rules.
72 * - If a deny all rule (D:*) is used it acts as a catch-all for unmatched
73 * addresses.
74 * - Specification of Allow all (A:*) is a no-op
75 *
76 * Example:
77 * 1. Allow only specified addresses
Glenn L McGrathb65422c2003-09-08 10:59:27 +000078 * A:172.20 # Allow any address that begins with 172.20.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000079 * A:10.10. # Allow any address that begins with 10.10.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000080 * A:127.0.0.1 # Allow local loopback connections
81 * D:* # Deny from other IP connections
82 *
83 * 2. Only deny specified addresses
84 * D:1.2.3. # deny from 1.2.3.0 - 1.2.3.255
85 * D:2.3.4. # deny from 2.3.4.0 - 2.3.4.255
86 * A:* # (optional line added for clarity)
87 *
88 * If a sub directory contains a config file it is parsed and merged with
Glenn L McGrathb65422c2003-09-08 10:59:27 +000089 * any existing settings as if it was appended to the original configuration.
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +000090 *
91 * subdir paths are relative to the containing subdir and thus cannot
92 * affect the parent rules.
93 *
94 * Note that since the sub dir is parsed in the forked thread servicing the
95 * subdir http request, any merge is discarded when the process exits. As a
96 * result, the subdir settings only have a lifetime of a single request.
97 *
98 *
99 * If -c is not set, an attempt will be made to open the default
100 * root configuration file. If -c is set and the file is not found, the
101 * server exits with an error.
102 *
103*/
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000104
Glenn L McGrath06e95652003-02-09 06:51:14 +0000105
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000106#include <stdio.h>
107#include <ctype.h> /* for isspace */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000108#include <string.h>
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000109#include <stdlib.h> /* for malloc */
110#include <time.h>
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000111#include <unistd.h> /* for close */
112#include <signal.h>
113#include <sys/types.h>
114#include <sys/socket.h> /* for connect and socket*/
115#include <netinet/in.h> /* for sockaddr_in */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000116#include <sys/time.h>
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000117#include <sys/stat.h>
118#include <sys/wait.h>
Glenn L McGrath06e95652003-02-09 06:51:14 +0000119#include <fcntl.h> /* for open modes */
120#include "busybox.h"
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000121
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000122
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000123static const char httpdVersion[] = "busybox httpd/1.30 7-Sep-2003";
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000124static const char default_path_httpd_conf[] = "/etc";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000125static const char httpd_conf[] = "httpd.conf";
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000126static const char home[] = "./";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000127
Glenn L McGrath5cd64612003-08-29 15:53:23 +0000128#ifdef CONFIG_LFS
129# define cont_l_fmt "%lld"
130#else
131# define cont_l_fmt "%ld"
132#endif
133
134
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000135// Note: bussybox xfuncs are not used because we want the server to keep running
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000136// if something bad happens due to a malformed user request.
Glenn L McGrath06e95652003-02-09 06:51:14 +0000137// As a result, all memory allocation after daemonize
138// is checked rigorously
139
140//#define DEBUG 1
141
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000142/* Configure options, disabled by default as custom httpd feature */
143
144/* disabled as optional features */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000145//#define CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV
146//#define CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath06e95652003-02-09 06:51:14 +0000147//#define CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
148//#define CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
149//#define CONFIG_FEATURE_HTTPD_SETUID
150//#define CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
151
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000152/* If set, use this server from internet superserver only */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000153//#define CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
154
155/* You can use this server as standalone, require libbb.a for linking */
156//#define HTTPD_STANDALONE
157
158/* Config options, disable this for do very small module */
159//#define CONFIG_FEATURE_HTTPD_CGI
160//#define CONFIG_FEATURE_HTTPD_BASIC_AUTH
Eric Andersen35e643b2003-07-28 07:40:39 +0000161//#define CONFIG_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath06e95652003-02-09 06:51:14 +0000162
163#ifdef HTTPD_STANDALONE
164/* standalone, enable all features */
165#undef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
166/* unset config option for remove warning as redefined */
167#undef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Eric Andersen35e643b2003-07-28 07:40:39 +0000168#undef CONFIG_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath06e95652003-02-09 06:51:14 +0000169#undef CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV
170#undef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath06e95652003-02-09 06:51:14 +0000171#undef CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
172#undef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
173#undef CONFIG_FEATURE_HTTPD_CGI
174#undef CONFIG_FEATURE_HTTPD_SETUID
175#undef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
176/* enable all features now */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000177#define CONFIG_FEATURE_HTTPD_BASIC_AUTH
Eric Andersen35e643b2003-07-28 07:40:39 +0000178#define CONFIG_FEATURE_HTTPD_AUTH_MD5
Glenn L McGrath06e95652003-02-09 06:51:14 +0000179#define CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV
180#define CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath06e95652003-02-09 06:51:14 +0000181#define CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
182#define CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
183#define CONFIG_FEATURE_HTTPD_CGI
184#define CONFIG_FEATURE_HTTPD_SETUID
185#define CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
186
187/* require from libbb.a for linking */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000188const char *bb_applet_name = "httpd";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000189
Manuel Novoa III cad53642003-03-19 09:13:01 +0000190void bb_show_usage(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000191{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000192 fprintf(stderr, "Usage: %s [-p <port>] [-c configFile] [-d/-e <string>] "
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000193 "[-r realm] [-u user] [-h homedir]\n", bb_applet_name);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000194 exit(1);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000195}
196#endif
197
Glenn L McGrath06e95652003-02-09 06:51:14 +0000198#ifdef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
199#undef CONFIG_FEATURE_HTTPD_SETUID /* use inetd user.group config settings */
200#undef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP /* so is not daemon */
201/* inetd set stderr to accepted socket and we can`t true see debug messages */
202#undef DEBUG
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000203#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000204
Glenn L McGrath06e95652003-02-09 06:51:14 +0000205#define MAX_POST_SIZE (64*1024) /* 64k. Its Small? May be ;) */
206
207#define MAX_MEMORY_BUFF 8192 /* IO buffer */
208
209typedef struct HT_ACCESS {
210 char *after_colon;
211 struct HT_ACCESS *next;
212 char before_colon[1]; /* really bigger, must last */
213} Htaccess;
214
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000215typedef struct HT_ACCESS_IP {
216 unsigned int ip;
217 unsigned int mask;
218 int allow_deny;
219 struct HT_ACCESS_IP *next;
220} Htaccess_IP;
221
Glenn L McGrath06e95652003-02-09 06:51:14 +0000222typedef struct
223{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000224 char buf[MAX_MEMORY_BUFF];
225
226#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
227 const char *realm;
228#endif
229 const char *configFile;
230
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000231 unsigned int rmt_ip;
232#if defined(CONFIG_FEATURE_HTTPD_CGI) || defined(DEBUG)
233 char rmt_ip_str[16]; /* for set env REMOTE_ADDR */
234#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000235 unsigned port; /* server initial port and for
236 set env REMOTE_PORT */
237
238 const char *found_mime_type;
239 off_t ContentLength; /* -1 - unknown */
240 time_t last_mod;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000241
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000242 Htaccess_IP *ip_a_d; /* config allow/deny lines */
Glenn L McGrath393183d2003-05-26 14:07:50 +0000243 int flg_deny_all;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000244#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
245 Htaccess *auth; /* config user:password lines */
246#endif
247#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
248 Htaccess *mime_a; /* config mime types */
249#endif
250
Glenn L McGrath06e95652003-02-09 06:51:14 +0000251#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
252 int accepted_socket;
253#define a_c_r config->accepted_socket
254#define a_c_w config->accepted_socket
255 int debugHttpd; /* if seted, don`t stay daemon */
256#else
257#define a_c_r 0
258#define a_c_w 1
259#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000260} HttpdConfig;
261
262static HttpdConfig *config;
263
264static const char request_GET[] = "GET"; /* size algorithic optimize */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000265
266static const char* const suffixTable [] = {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000267/* Warning: shorted equalent suffix in one line must be first */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000268 ".htm.html", "text/html",
269 ".jpg.jpeg", "image/jpeg",
270 ".gif", "image/gif",
271 ".png", "image/png",
272 ".txt.h.c.cc.cpp", "text/plain",
Glenn L McGrath06e95652003-02-09 06:51:14 +0000273 ".css", "text/css",
274 ".wav", "audio/wav",
275 ".avi", "video/x-msvideo",
276 ".qt.mov", "video/quicktime",
277 ".mpe.mpeg", "video/mpeg",
278 ".mid.midi", "audio/midi",
279 ".mp3", "audio/mpeg",
280#if 0 /* unpopular */
281 ".au", "audio/basic",
282 ".pac", "application/x-ns-proxy-autoconfig",
283 ".vrml.wrl", "model/vrml",
284#endif
285 0, "application/octet-stream" /* default */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000286 };
287
288typedef enum
289{
290 HTTP_OK = 200,
291 HTTP_UNAUTHORIZED = 401, /* authentication needed, respond with auth hdr */
292 HTTP_NOT_FOUND = 404,
Glenn L McGrath06e95652003-02-09 06:51:14 +0000293 HTTP_NOT_IMPLEMENTED = 501, /* used for unrecognized requests */
294 HTTP_BAD_REQUEST = 400, /* malformed syntax */
295 HTTP_FORBIDDEN = 403,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000296 HTTP_INTERNAL_SERVER_ERROR = 500,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000297#if 0 /* future use */
298 HTTP_CONTINUE = 100,
299 HTTP_SWITCHING_PROTOCOLS = 101,
300 HTTP_CREATED = 201,
301 HTTP_ACCEPTED = 202,
302 HTTP_NON_AUTHORITATIVE_INFO = 203,
303 HTTP_NO_CONTENT = 204,
304 HTTP_MULTIPLE_CHOICES = 300,
305 HTTP_MOVED_PERMANENTLY = 301,
306 HTTP_MOVED_TEMPORARILY = 302,
307 HTTP_NOT_MODIFIED = 304,
308 HTTP_PAYMENT_REQUIRED = 402,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000309 HTTP_BAD_GATEWAY = 502,
310 HTTP_SERVICE_UNAVAILABLE = 503, /* overload, maintenance */
311 HTTP_RESPONSE_SETSIZE=0xffffffff
312#endif
313} HttpResponseNum;
314
315typedef struct
316{
317 HttpResponseNum type;
318 const char *name;
319 const char *info;
320} HttpEnumString;
321
322static const HttpEnumString httpResponseNames[] = {
323 { HTTP_OK, "OK" },
Glenn L McGrath06e95652003-02-09 06:51:14 +0000324 { HTTP_NOT_IMPLEMENTED, "Not Implemented",
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000325 "The requested method is not recognized by this server." },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000326#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000327 { HTTP_UNAUTHORIZED, "Unauthorized", "" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000328#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000329 { HTTP_NOT_FOUND, "Not Found",
330 "The requested URL was not found on this server." },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000331 { HTTP_BAD_REQUEST, "Bad Request", "Unsupported method." },
Glenn L McGrath06e95652003-02-09 06:51:14 +0000332 { HTTP_FORBIDDEN, "Forbidden", "" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000333 { HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error",
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000334 "Internal Server Error" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000335#if 0 /* not implemented */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000336 { HTTP_CREATED, "Created" },
337 { HTTP_ACCEPTED, "Accepted" },
338 { HTTP_NO_CONTENT, "No Content" },
339 { HTTP_MULTIPLE_CHOICES, "Multiple Choices" },
340 { HTTP_MOVED_PERMANENTLY, "Moved Permanently" },
341 { HTTP_MOVED_TEMPORARILY, "Moved Temporarily" },
342 { HTTP_NOT_MODIFIED, "Not Modified" },
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000343 { HTTP_BAD_GATEWAY, "Bad Gateway", "" },
344 { HTTP_SERVICE_UNAVAILABLE, "Service Unavailable", "" },
345#endif
346};
347
Glenn L McGrath06e95652003-02-09 06:51:14 +0000348
349static const char RFC1123FMT[] = "%a, %d %b %Y %H:%M:%S GMT";
350static const char Content_length[] = "Content-length:";
351
352
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000353static int
354scan_ip (const char **ep, unsigned int *ip, unsigned char endc)
355{
356 const char *p = *ep;
357 int auto_mask = 8;
358 int j;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000359
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000360 *ip = 0;
361 for (j = 0; j < 4; j++) {
362 unsigned int octet;
363
364 if ((*p < '0' || *p > '9') && (*p != '/' || j == 0) && *p != 0)
365 return -auto_mask;
366 octet = 0;
367 while (*p >= '0' && *p <= '9') {
368 octet *= 10;
369 octet += *p - '0';
370 if (octet > 255)
371 return -auto_mask;
372 p++;
373 }
374 if (*p == '.')
375 p++;
376 if (*p != '/' && *p != 0)
377 auto_mask += 8;
378 *ip = ((*ip) << 8) | octet;
379 }
380 if (*p != 0) {
381 if (*p != endc)
382 return -auto_mask;
383 p++;
384 if(*p == 0)
385 return -auto_mask;
386 }
387 *ep = p;
388 return auto_mask;
389}
390
391static int
392scan_ip_mask (const char *ipm, unsigned int *ip, unsigned int *mask)
393{
394 int i;
395 unsigned int msk;
396
397 i = scan_ip(&ipm, ip, '/');
398 if(i < 0)
399 return i;
400 if(*ipm) {
401 const char *p = ipm;
402
403 i = 0;
404 while (*p) {
405 if (*p < '0' || *p > '9') {
406 if (*p == '.') {
407 i = scan_ip (&ipm, mask, 0);
408 return i != 32;
409 }
410 return -1;
411 }
412 i *= 10;
413 i += *p - '0';
414 p++;
415 }
416 }
417 if (i > 32 || i < 0)
418 return -1;
419 msk = 0x80000000;
420 *mask = 0;
421 while (i > 0) {
422 *mask |= msk;
423 msk >>= 1;
424 i--;
425 }
426 return 0;
427}
428
429#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000430static void free_config_lines(Htaccess **pprev)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000431{
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000432 Htaccess *prev = *pprev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000433
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000434 while( prev ) {
435 Htaccess *cur = prev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000436
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000437 prev = cur->next;
438 free(cur);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000439 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000440 *pprev = NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000441}
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000442#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000443
Glenn L McGrath06e95652003-02-09 06:51:14 +0000444/* flag */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000445#define FIRST_PARSE 0
446#define SUBDIR_PARSE 1
447#define SIGNALED_PARSE 2
448#define FIND_FROM_HTTPD_ROOT 3
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000449/****************************************************************************
450 *
451 > $Function: parse_conf()
452 *
453 * $Description: parse configuration file into in-memory linked list.
454 *
455 * The first non-white character is examined to determine if the config line
456 * is one of the following:
457 * .ext:mime/type # new mime type not compiled into httpd
458 * [adAD]:from # ip address allow/deny, * for wildcard
459 * /path:user:pass # username/password
460 *
461 * Any previous IP rules are discarded.
462 * If the flag argument is not SUBDIR_PARSE then all /path and mime rules
463 * are also discarded. That is, previous settings are retained if flag is
464 * SUBDIR_PARSE.
465 *
466 * $Parameters:
467 * (const char *) path . . null for ip address checks, path for password
468 * checks.
469 * (int) flag . . . . . . the source of the parse request.
470 *
471 * $Return: (None)
472 *
473 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000474static void parse_conf(const char *path, int flag)
475{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000476 FILE *f;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000477 Htaccess *cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000478#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
479 Htaccess *prev;
480#endif
481
Glenn L McGrath06e95652003-02-09 06:51:14 +0000482 const char *cf = config->configFile;
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000483 char buf[160];
Glenn L McGrath06e95652003-02-09 06:51:14 +0000484 char *p0 = NULL;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000485 char *c, *p;
486
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000487 /* free previous ip setup if present */
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000488 Htaccess_IP *pip = config->ip_a_d;
489
490 while( pip ) {
491 Htaccess_IP *cur_ipl = pip;
492
493 pip = cur_ipl->next;
494 free(cur_ipl);
495 }
496 config->ip_a_d = NULL;
497
Glenn L McGrath24833432003-06-10 17:22:49 +0000498 config->flg_deny_all = 0;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000499
500#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000501 /* retain previous auth and mime config only for subdir parse */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000502 if(flag != SUBDIR_PARSE) {
503#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000504 free_config_lines(&config->auth);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000505#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000506#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
507 free_config_lines(&config->mime_a);
508#endif
509 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000510#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000511
512 if(flag == SUBDIR_PARSE || cf == NULL) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000513 cf = alloca(strlen(path) + sizeof(httpd_conf) + 2);
514 if(cf == NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000515 if(flag == FIRST_PARSE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000516 bb_error_msg_and_die(bb_msg_memory_exhausted);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000517 return;
518 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000519 sprintf((char *)cf, "%s/%s", path, httpd_conf);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000520 }
521
522 while((f = fopen(cf, "r")) == NULL) {
Eric Andersen35e643b2003-07-28 07:40:39 +0000523 if(flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) {
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000524 /* config file not found, no changes to config */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000525 return;
526 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000527 if(config->configFile && flag == FIRST_PARSE) /* if -c option given */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000528 bb_perror_msg_and_die("%s", cf);
529 flag = FIND_FROM_HTTPD_ROOT;
530 cf = httpd_conf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000531 }
532
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000533#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
534 prev = config->auth;
535#endif
536 /* This could stand some work */
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000537 while ( (p0 = fgets(buf, sizeof(buf), f)) != NULL) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000538 c = NULL;
539 for(p = p0; *p0 != 0 && *p0 != '#'; p0++) {
540 if(!isspace(*p0)) {
541 *p++ = *p0;
542 if(*p0 == ':' && c == NULL)
543 c = p;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000544 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000545 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000546 *p = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000547
548 /* test for empty or strange line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000549 if (c == NULL || *c == 0)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000550 continue;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000551 p0 = buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000552 if(*p0 == 'd')
553 *p0 = 'D';
Glenn L McGrath393183d2003-05-26 14:07:50 +0000554 if(*c == '*') {
555 if(*p0 == 'D') {
556 /* memorize deny all */
557 config->flg_deny_all++;
558 }
559 /* skip default other "word:*" config lines */
560 continue;
561 }
562
563 if(*p0 == 'a')
564 *p0 = 'A';
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000565 else if(*p0 != 'D' && *p0 != 'A'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000566#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000567 && *p0 != '/'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000568#endif
569#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000570 && *p0 != '.'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000571#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000572 )
Glenn L McGrath06e95652003-02-09 06:51:14 +0000573 continue;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000574 if(*p0 == 'A' || *p0 == 'D') {
575 /* storing current config IP line */
576 pip = calloc(1, sizeof(Htaccess_IP));
577 if(pip) {
578 if(scan_ip_mask (c, &(pip->ip), &(pip->mask))) {
579 /* syntax IP{/mask} error detected, protect all */
580 *p0 = 'D';
581 pip->mask = 0;
582 }
583 pip->allow_deny = *p0;
584 if(*p0 == 'D') {
585 /* Deny:form_IP move top */
586 pip->next = config->ip_a_d;
587 config->ip_a_d = pip;
588 } else {
589 /* add to bottom A:form_IP config line */
590 Htaccess_IP *prev_IP = config->ip_a_d;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000591
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000592 if(prev_IP == NULL) {
593 config->ip_a_d = pip;
594 } else {
595 while(prev_IP->next)
596 prev_IP = prev_IP->next;
597 prev_IP->next = pip;
598 }
599 }
600 }
601 continue;
602 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000603#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
604 if(*p0 == '/') {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000605 /* make full path from httpd root / curent_path / config_line_path */
606 cf = flag == SUBDIR_PARSE ? path : "";
607 p0 = malloc(strlen(cf) + (c - buf) + 2 + strlen(c));
608 if(p0 == NULL)
609 continue;
610 c[-1] = 0;
611 sprintf(p0, "/%s%s", cf, buf);
612
613 /* another call bb_simplify_path */
614 cf = p = p0;
615
616 do {
617 if (*p == '/') {
618 if (*cf == '/') { /* skip duplicate (or initial) slash */
619 continue;
620 } else if (*cf == '.') {
621 if (cf[1] == '/' || cf[1] == 0) { /* remove extra '.' */
622 continue;
623 } else if ((cf[1] == '.') && (cf[2] == '/' || cf[2] == 0)) {
624 ++cf;
625 if (p > p0) {
626 while (*--p != '/'); /* omit previous dir */
627 }
628 continue;
629 }
630 }
631 }
632 *++p = *cf;
633 } while (*++cf);
634
635 if ((p == p0) || (*p != '/')) { /* not a trailing slash */
636 ++p; /* so keep last character */
637 }
638 *p = 0;
639 sprintf(p0, "%s:%s", p0, c);
640 }
641#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000642
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000643#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
644 /* storing current config line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000645 cur = calloc(1, sizeof(Htaccess) + strlen(p0));
646 if(cur) {
647 cf = strcpy(cur->before_colon, p0);
648 c = strchr(cf, ':');
649 *c++ = 0;
650 cur->after_colon = c;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000651#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Glenn L McGrath5875be42003-09-08 15:39:09 +0000652 if(*cf == '.') {
Glenn L McGrath874e3382003-05-14 12:11:36 +0000653 /* config .mime line move top for overwrite previous */
654 cur->next = config->mime_a;
655 config->mime_a = cur;
Glenn L McGrath5875be42003-09-08 15:39:09 +0000656 continue;
Glenn L McGrath874e3382003-05-14 12:11:36 +0000657 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000658#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000659#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath5875be42003-09-08 15:39:09 +0000660 free(p0);
661 if(prev == NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000662 /* first line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000663 config->auth = prev = cur;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000664 } else {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000665 /* sort path, if current lenght eq or bigger then move up */
666 Htaccess *prev_hti = config->auth;
667 int l = strlen(cf);
668 Htaccess *hti;
669
670 for(hti = prev_hti; hti; hti = hti->next) {
671 if(l >= strlen(hti->before_colon)) {
672 /* insert before hti */
673 cur->next = hti;
674 if(prev_hti != hti) {
675 prev_hti->next = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000676 } else {
677 /* insert as top */
678 config->auth = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000679 }
Glenn L McGrath393183d2003-05-26 14:07:50 +0000680 break;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000681 }
682 if(prev_hti != hti)
683 prev_hti = prev_hti->next;
684 }
685 if(!hti) { /* not inserted, add to bottom */
686 prev->next = cur;
687 prev = cur;
688 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000689 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000690#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000691#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000692 }
693 }
694 fclose(f);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000695}
696
697#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000698/****************************************************************************
699 *
700 > $Function: encodeString()
701 *
702 * $Description: Given a string, html encode special characters.
703 * This is used for the -e command line option to provide an easy way
704 * for scripts to encode result data without confusing browsers. The
705 * returned string pointer is memory allocated by malloc().
706 *
707 * $Parameters:
708 * (const char *) string . . The first string to encode.
709 *
710 * $Return: (char *) . . . .. . . A pointer to the encoded string.
711 *
712 * $Errors: Returns a null string ("") if memory is not available.
713 *
714 ****************************************************************************/
715static char *encodeString(const char *string)
716{
717 /* take the simple route and encode everything */
718 /* could possibly scan once to get length. */
719 int len = strlen(string);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000720 char *out = malloc(len*5 +1);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000721 char *p=out;
722 char ch;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000723
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000724 if (!out) return "";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000725 while ((ch = *string++)) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000726 // very simple check for what to encode
727 if (isalnum(ch)) *p++ = ch;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000728 else p += sprintf(p, "&#%d", (unsigned char) ch);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000729 }
730 *p=0;
731 return out;
732}
Glenn L McGrath06e95652003-02-09 06:51:14 +0000733#endif /* CONFIG_FEATURE_HTTPD_ENCODE_URL_STR */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000734
735/****************************************************************************
736 *
737 > $Function: decodeString()
738 *
739 * $Description: Given a URL encoded string, convert it to plain ascii.
740 * Since decoding always makes strings smaller, the decode is done in-place.
741 * Thus, callers should strdup() the argument if they do not want the
742 * argument modified. The return is the original pointer, allowing this
743 * function to be easily used as arguments to other functions.
744 *
745 * $Parameters:
746 * (char *) string . . . The first string to decode.
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000747 * (int) flag . . . 1 if require decode '+' as ' ' for CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000748 *
749 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
750 *
751 * $Errors: None
752 *
753 ****************************************************************************/
Eric Andersena3bb3e62003-06-26 09:05:32 +0000754static char *decodeString(char *orig, int flag_plus_to_space)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000755{
756 /* note that decoded string is always shorter than original */
Eric Andersena3bb3e62003-06-26 09:05:32 +0000757 char *string = orig;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000758 char *ptr = string;
Eric Andersena3bb3e62003-06-26 09:05:32 +0000759
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000760 while (*ptr)
761 {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000762 if (*ptr == '+' && flag_plus_to_space) { *string++ = ' '; ptr++; }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000763 else if (*ptr != '%') *string++ = *ptr++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000764 else {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000765 unsigned int value;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000766 sscanf(ptr+1, "%2X", &value);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000767 *string++ = value;
768 ptr += 3;
769 }
770 }
771 *string = '\0';
772 return orig;
773}
774
775
Glenn L McGrath06e95652003-02-09 06:51:14 +0000776#ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000777/****************************************************************************
778 *
779 > $Function: addEnv()
780 *
781 * $Description: Add an enviornment variable setting to the global list.
782 * A NAME=VALUE string is allocated, filled, and added to the list of
783 * environment settings passed to the cgi execution script.
784 *
785 * $Parameters:
Glenn L McGrath06e95652003-02-09 06:51:14 +0000786 * (char *) name_before_underline - The first part environment variable name.
787 * (char *) name_after_underline - The second part environment variable name.
788 * (char *) value . . The value to which the env variable is set.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000789 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000790 * $Return: (void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000791 *
792 * $Errors: Silently returns if the env runs out of space to hold the new item
793 *
794 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000795static void addEnv(const char *name_before_underline,
796 const char *name_after_underline, const char *value)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000797{
Glenn L McGrath5875be42003-09-08 15:39:09 +0000798 char *s = NULL;
Glenn L McGrath393183d2003-05-26 14:07:50 +0000799 const char *underline;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000800
Glenn L McGrath06e95652003-02-09 06:51:14 +0000801 if (!value)
802 value = "";
Glenn L McGrath393183d2003-05-26 14:07:50 +0000803 underline = *name_after_underline ? "_" : "";
804 asprintf(&s, "%s%s%s=%s", name_before_underline, underline,
Glenn L McGrath06e95652003-02-09 06:51:14 +0000805 name_after_underline, value);
Glenn L McGrath393183d2003-05-26 14:07:50 +0000806 if(s) {
Glenn L McGrath5875be42003-09-08 15:39:09 +0000807 putenv(s);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000808 }
809}
810
Eric Andersena3bb3e62003-06-26 09:05:32 +0000811#if defined(CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV) || !defined(CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000812/* set environs SERVER_PORT and REMOTE_PORT */
813static void addEnvPort(const char *port_name)
814{
815 char buf[16];
816
817 sprintf(buf, "%u", config->port);
818 addEnv(port_name, "PORT", buf);
819}
Eric Andersena3bb3e62003-06-26 09:05:32 +0000820#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000821#endif /* CONFIG_FEATURE_HTTPD_CGI */
822
823#ifdef CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000824/****************************************************************************
825 *
826 > $Function: addEnvCgi
827 *
828 * $Description: Create environment variables given a URL encoded arg list.
829 * For each variable setting the URL encoded arg list, create a corresponding
830 * environment variable. URL encoded arguments have the form
Glenn L McGrath06e95652003-02-09 06:51:14 +0000831 * name1=value1&name2=value2&name3=&ignores
832 * from this example, name3 set empty value, tail without '=' skiping
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000833 *
834 * $Parameters:
835 * (char *) pargs . . . . A pointer to the URL encoded arguments.
836 *
837 * $Return: None
838 *
839 * $Errors: None
840 *
841 ****************************************************************************/
842static void addEnvCgi(const char *pargs)
843{
844 char *args;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000845 char *memargs;
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000846 char *namelist; /* space separated list of arg names */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000847 if (pargs==0) return;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000848
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000849 /* args are a list of name=value&name2=value2 sequences */
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000850 namelist = (char *) malloc(strlen(pargs));
851 if (namelist) namelist[0]=0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000852 memargs = args = strdup(pargs);
853 while (args && *args) {
854 const char *name = args;
855 char *value = strchr(args, '=');
856
857 if (!value) /* &XXX without '=' */
858 break;
859 *value++ = 0;
860 args = strchr(value, '&');
861 if (args)
862 *args++ = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000863 addEnv("CGI", name, decodeString(value, 1));
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000864 if (*namelist) strcat(namelist, " ");
Glenn L McGrath393183d2003-05-26 14:07:50 +0000865 strcat(namelist, name);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000866 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000867 free(memargs);
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000868 if (namelist) {
Glenn L McGrath393183d2003-05-26 14:07:50 +0000869 addEnv("CGI", "ARGLIST_", namelist);
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000870 free(namelist);
871 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000872}
Glenn L McGrath06e95652003-02-09 06:51:14 +0000873#endif /* CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV */
874
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000875
876#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000877/****************************************************************************
878 *
879 > $Function: decodeBase64()
880 *
881 > $Description: Decode a base 64 data stream as per rfc1521.
882 * Note that the rfc states that none base64 chars are to be ignored.
883 * Since the decode always results in a shorter size than the input, it is
884 * OK to pass the input arg as an output arg.
885 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000886 * $Parameter:
887 * (char *) Data . . . . A pointer to a base64 encoded string.
888 * Where to place the decoded data.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000889 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000890 * $Return: void
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000891 *
892 * $Errors: None
893 *
894 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000895static void decodeBase64(char *Data)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000896{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000897
898 const unsigned char *in = Data;
899 // The decoded size will be at most 3/4 the size of the encoded
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000900 unsigned long ch = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000901 int i = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000902
903 while (*in) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000904 int t = *in++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000905
Glenn L McGrath874e3382003-05-14 12:11:36 +0000906 if(t >= '0' && t <= '9')
907 t = t - '0' + 52;
908 else if(t >= 'A' && t <= 'Z')
909 t = t - 'A';
910 else if(t >= 'a' && t <= 'z')
911 t = t - 'a' + 26;
912 else if(t == '+')
913 t = 62;
914 else if(t == '/')
915 t = 63;
916 else if(t == '=')
917 t = 0;
918 else
919 continue;
920
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000921 ch = (ch << 6) | t;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000922 i++;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000923 if (i == 4) {
924 *Data++ = (char) (ch >> 16);
925 *Data++ = (char) (ch >> 8);
926 *Data++ = (char) ch;
927 i = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000928 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000929 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000930 *Data = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000931}
932#endif
933
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000934
Glenn L McGrath06e95652003-02-09 06:51:14 +0000935#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000936/****************************************************************************
937 *
938 > $Function: openServer()
939 *
940 * $Description: create a listen server socket on the designated port.
941 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000942 * $Return: (int) . . . A connection socket. -1 for errors.
943 *
944 * $Errors: None
945 *
946 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000947static int openServer(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000948{
949 struct sockaddr_in lsocket;
950 int fd;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000951
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000952 /* create the socket right now */
953 /* inet_addr() returns a value that is already in network order */
954 memset(&lsocket, 0, sizeof(lsocket));
955 lsocket.sin_family = AF_INET;
956 lsocket.sin_addr.s_addr = INADDR_ANY;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000957 lsocket.sin_port = htons(config->port) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000958 fd = socket(AF_INET, SOCK_STREAM, 0);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000959 if (fd >= 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000960 /* tell the OS it's OK to reuse a previous address even though */
961 /* it may still be in a close down state. Allows bind to succeed. */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000962 int on = 1;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000963#ifdef SO_REUSEPORT
Glenn L McGrath06e95652003-02-09 06:51:14 +0000964 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *)&on, sizeof(on)) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000965#else
Glenn L McGrath06e95652003-02-09 06:51:14 +0000966 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000967#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000968 if (bind(fd, (struct sockaddr *)&lsocket, sizeof(lsocket)) == 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000969 listen(fd, 9);
970 signal(SIGCHLD, SIG_IGN); /* prevent zombie (defunct) processes */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000971 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000972 bb_perror_msg_and_die("bind");
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000973 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000974 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000975 bb_perror_msg_and_die("create socket");
Glenn L McGrath06e95652003-02-09 06:51:14 +0000976 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000977 return fd;
978}
Glenn L McGrath06e95652003-02-09 06:51:14 +0000979#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000980
981/****************************************************************************
982 *
983 > $Function: sendHeaders()
984 *
985 * $Description: Create and send HTTP response headers.
986 * The arguments are combined and sent as one write operation. Note that
987 * IE will puke big-time if the headers are not sent in one packet and the
Glenn L McGrath06e95652003-02-09 06:51:14 +0000988 * second packet is delayed for any reason.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000989 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000990 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000991 * (HttpResponseNum) responseNum . . . The result code to send.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000992 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000993 * $Return: (int) . . . . writing errors
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000994 *
995 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000996static int sendHeaders(HttpResponseNum responseNum)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000997{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000998 char *buf = config->buf;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000999 const char *responseString = "";
1000 const char *infoString = 0;
1001 unsigned int i;
1002 time_t timer = time(0);
1003 char timeStr[80];
Glenn L McGrath06e95652003-02-09 06:51:14 +00001004 int len;
1005
1006 for (i = 0;
1007 i < (sizeof(httpResponseNames)/sizeof(httpResponseNames[0])); i++) {
1008 if (httpResponseNames[i].type == responseNum) {
1009 responseString = httpResponseNames[i].name;
1010 infoString = httpResponseNames[i].info;
1011 break;
1012 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001013 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001014 if (responseNum != HTTP_OK) {
1015 config->found_mime_type = "text/html"; // error message is HTML
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001016 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001017
1018 /* emit the current date */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001019 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&timer));
1020 len = sprintf(buf,
1021 "HTTP/1.0 %d %s\nContent-type: %s\r\n"
1022 "Date: %s\r\nConnection: close\r\n",
1023 responseNum, responseString, config->found_mime_type, timeStr);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001024
Glenn L McGrath3d2405c2003-02-10 22:28:21 +00001025#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath06e95652003-02-09 06:51:14 +00001026 if (responseNum == HTTP_UNAUTHORIZED) {
1027 len += sprintf(buf+len, "WWW-Authenticate: Basic realm=\"%s\"\r\n",
1028 config->realm);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001029 }
Glenn L McGrath3d2405c2003-02-10 22:28:21 +00001030#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001031 if (config->ContentLength != -1) { /* file */
1032 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&config->last_mod));
Glenn L McGrath5cd64612003-08-29 15:53:23 +00001033 len += sprintf(buf+len, "Last-Modified: %s\r\n%s " cont_l_fmt "\r\n",
Glenn L McGrath06e95652003-02-09 06:51:14 +00001034 timeStr, Content_length, config->ContentLength);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001035 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001036 strcat(buf, "\r\n");
1037 len += 2;
1038 if (infoString) {
1039 len += sprintf(buf+len,
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001040 "<HEAD><TITLE>%d %s</TITLE></HEAD>\n"
1041 "<BODY><H1>%d %s</H1>\n%s\n</BODY>\n",
1042 responseNum, responseString,
Glenn L McGrath06e95652003-02-09 06:51:14 +00001043 responseNum, responseString, infoString);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001044 }
1045#ifdef DEBUG
Glenn L McGrath06e95652003-02-09 06:51:14 +00001046 if (config->debugHttpd) fprintf(stderr, "Headers: '%s'", buf);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001047#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +00001048 return bb_full_write(a_c_w, buf, len);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001049}
1050
1051/****************************************************************************
1052 *
1053 > $Function: getLine()
1054 *
1055 * $Description: Read from the socket until an end of line char found.
1056 *
1057 * Characters are read one at a time until an eol sequence is found.
1058 *
1059 * $Parameters:
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001060 * (char *) buf . . Where to place the read result.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001061 *
1062 * $Return: (int) . . . . number of characters read. -1 if error.
1063 *
1064 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001065static int getLine(char *buf)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001066{
1067 int count = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001068
1069 while (read(a_c_r, buf + count, 1) == 1) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001070 if (buf[count] == '\r') continue;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001071 if (buf[count] == '\n') {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001072 buf[count] = 0;
1073 return count;
1074 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001075 if(count < (MAX_MEMORY_BUFF-1)) /* check owerflow */
1076 count++;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001077 }
1078 if (count) return count;
1079 else return -1;
1080}
1081
Glenn L McGrath06e95652003-02-09 06:51:14 +00001082#ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001083/****************************************************************************
1084 *
1085 > $Function: sendCgi()
1086 *
1087 * $Description: Execute a CGI script and send it's stdout back
1088 *
1089 * Environment variables are set up and the script is invoked with pipes
1090 * for stdin/stdout. If a post is being done the script is fed the POST
1091 * data in addition to setting the QUERY_STRING variable (for GETs or POSTs).
1092 *
1093 * $Parameters:
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001094 * (const char *) url . . . The requested URL (with leading /).
1095 * (const char *urlArgs). . Any URL arguments.
1096 * (const char *body) . . . POST body contents.
1097 * (int bodyLen) . . . . . Length of the post body.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001098 * (const char *cookie) . . For set HTTP_COOKIE.
1099
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001100 *
1101 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
1102 *
1103 * $Errors: None
1104 *
1105 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001106static int sendCgi(const char *url,
1107 const char *request, const char *urlArgs,
1108 const char *body, int bodyLen, const char *cookie)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001109{
1110 int fromCgi[2]; /* pipe for reading data from CGI */
1111 int toCgi[2]; /* pipe for sending data to CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001112
Glenn L McGrath06e95652003-02-09 06:51:14 +00001113 static char * argp[] = { 0, 0 };
1114 int pid = 0;
1115 int inFd;
1116 int outFd;
1117 int firstLine = 1;
1118
1119 do {
1120 if (pipe(fromCgi) != 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001121 break;
1122 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001123 if (pipe(toCgi) != 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001124 break;
1125 }
1126
1127 pid = fork();
Glenn L McGrath06e95652003-02-09 06:51:14 +00001128 if (pid < 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001129 pid = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001130 break;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001131 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001132
1133 if (!pid) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001134 /* child process */
1135 char *script;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001136 char *purl = strdup( url );
1137 char realpath_buff[MAXPATHLEN];
1138
1139 if(purl == NULL)
1140 _exit(242);
1141
1142 inFd = toCgi[0];
1143 outFd = fromCgi[1];
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001144
1145 dup2(inFd, 0); // replace stdin with the pipe
1146 dup2(outFd, 1); // replace stdout with the pipe
Glenn L McGrath06e95652003-02-09 06:51:14 +00001147
1148#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1149 if (!config->debugHttpd)
1150#endif
1151 dup2(outFd, 2); // replace stderr with the pipe
1152
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001153 close(toCgi[0]);
1154 close(toCgi[1]);
1155 close(fromCgi[0]);
1156 close(fromCgi[1]);
1157
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001158 /*
Glenn L McGrath06e95652003-02-09 06:51:14 +00001159 * Find PATH_INFO.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001160 */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001161 script = purl;
1162 while((script = strchr( script + 1, '/' )) != NULL) {
1163 /* have script.cgi/PATH_INFO or dirs/script.cgi[/PATH_INFO] */
1164 struct stat sb;
1165
1166 *script = '\0';
1167 if(is_directory(purl + 1, 1, &sb) == 0) {
1168 /* not directory, found script.cgi/PATH_INFO */
1169 *script = '/';
1170 break;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001171 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001172 *script = '/'; /* is directory, find next '/' */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001173 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001174 addEnv("PATH", "INFO", script); /* set /PATH_INFO or NULL */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001175 addEnv("REQUEST", "METHOD", request);
1176 if(urlArgs) {
1177 char *uri = alloca(strlen(purl) + 2 + strlen(urlArgs));
1178 if(uri)
1179 sprintf(uri, "%s?%s", purl, urlArgs);
1180 addEnv("REQUEST", "URI", uri);
1181 } else {
1182 addEnv("REQUEST", "URI", purl);
1183 }
1184 if(script != NULL)
1185 *script = '\0'; /* reduce /PATH_INFO */
1186 /* set SCRIPT_NAME as full path: /cgi-bin/dirs/script.cgi */
1187 addEnv("SCRIPT_NAME", "", purl);
1188 addEnv("QUERY_STRING", "", urlArgs);
1189 addEnv("SERVER", "SOFTWARE", httpdVersion);
1190 addEnv("SERVER", "PROTOCOL", "HTTP/1.0");
1191 addEnv("GATEWAY_INTERFACE", "", "CGI/1.1");
1192#ifdef CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001193 addEnv("REMOTE", "ADDR", config->rmt_ip_str);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001194 addEnvPort("REMOTE");
1195#else
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001196 addEnv("REMOTE_ADDR", "", config->rmt_ip_str);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001197#endif
1198 if(bodyLen) {
1199 char sbl[32];
1200
1201 sprintf(sbl, "%d", bodyLen);
1202 addEnv("CONTENT_LENGTH", "", sbl);
1203 }
1204 if(cookie)
1205 addEnv("HTTP_COOKIE", "", cookie);
1206
1207#ifdef CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV
1208 if (request != request_GET) {
1209 addEnvCgi(body);
1210 } else {
1211 addEnvCgi(urlArgs);
1212 }
1213#endif
1214
1215 /* set execve argp[0] without path */
1216 argp[0] = strrchr( purl, '/' ) + 1;
1217 /* but script argp[0] must have absolute path and chdiring to this */
1218 if(realpath(purl + 1, realpath_buff) != NULL) {
1219 script = strrchr(realpath_buff, '/');
1220 if(script) {
1221 *script = '\0';
1222 if(chdir(realpath_buff) == 0) {
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00001223 *script = '/';
1224 // now run the program. If it fails,
1225 // use _exit() so no destructors
1226 // get called and make a mess.
Glenn L McGrath5875be42003-09-08 15:39:09 +00001227 execv(realpath_buff, argp);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001228 }
1229 }
1230 }
1231#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1232 config->accepted_socket = 1; /* send to stdout */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001233#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001234 sendHeaders(HTTP_NOT_FOUND);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001235 _exit(242);
1236 } /* end child */
1237
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001238 } while (0);
1239
Glenn L McGrath06e95652003-02-09 06:51:14 +00001240 if (pid) {
1241 /* parent process */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001242 int status;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001243
1244 inFd = fromCgi[0];
1245 outFd = toCgi[1];
1246 close(fromCgi[1]);
1247 close(toCgi[0]);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001248 if (body) bb_full_write(outFd, body, bodyLen);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001249 close(outFd);
1250
1251 while (1) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001252 struct timeval timeout;
1253 fd_set readSet;
1254 char buf[160];
1255 int nfound;
1256 int count;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001257
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001258 FD_ZERO(&readSet);
1259 FD_SET(inFd, &readSet);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001260
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001261 /* Now wait on the set of sockets! */
1262 timeout.tv_sec = 0;
1263 timeout.tv_usec = 10000;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001264 nfound = select(inFd + 1, &readSet, 0, 0, &timeout);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001265
Glenn L McGrath06e95652003-02-09 06:51:14 +00001266 if (nfound <= 0) {
1267 if (waitpid(pid, &status, WNOHANG) > 0) {
1268 close(inFd);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001269#ifdef DEBUG
Glenn L McGrath06e95652003-02-09 06:51:14 +00001270 if (config->debugHttpd) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001271 if (WIFEXITED(status))
Manuel Novoa III cad53642003-03-19 09:13:01 +00001272 bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001273 if (WIFSIGNALED(status))
Manuel Novoa III cad53642003-03-19 09:13:01 +00001274 bb_error_msg("piped has exited with signal=%d", WTERMSIG(status));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001275 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001276#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001277 pid = -1;
1278 break;
1279 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001280 } else {
1281 int s = a_c_w;
1282
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001283 // There is something to read
Manuel Novoa III cad53642003-03-19 09:13:01 +00001284 count = bb_full_read(inFd, buf, sizeof(buf)-1);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001285 // If a read returns 0 at this point then some type of error has
1286 // occurred. Bail now.
1287 if (count == 0) break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001288 if (count > 0) {
1289 if (firstLine) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001290 /* check to see if the user script added headers */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001291 if (strncmp(buf, "HTTP/1.0 200 OK\n", 4) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001292 bb_full_write(s, "HTTP/1.0 200 OK\n", 16);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001293 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001294 if (strstr(buf, "ontent-") == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001295 bb_full_write(s, "Content-type: text/plain\n\n", 26);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001296 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001297 firstLine=0;
1298 }
Manuel Novoa III cad53642003-03-19 09:13:01 +00001299 bb_full_write(s, buf, count);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001300#ifdef DEBUG
Glenn L McGrath06e95652003-02-09 06:51:14 +00001301 if (config->debugHttpd)
1302 fprintf(stderr, "cgi read %d bytes\n", count);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001303#endif
1304 }
1305 }
1306 }
1307 }
1308 return 0;
1309}
Glenn L McGrath06e95652003-02-09 06:51:14 +00001310#endif /* CONFIG_FEATURE_HTTPD_CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001311
1312/****************************************************************************
1313 *
1314 > $Function: sendFile()
1315 *
1316 * $Description: Send a file response to an HTTP request
1317 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00001318 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001319 * (const char *) url . . The URL requested.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001320 * (char *) buf . . . . . The stack buffer.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001321 *
1322 * $Return: (int) . . . . . . Always 0.
1323 *
1324 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001325static int sendFile(const char *url, char *buf)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001326{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001327 char * suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001328 int f;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001329 const char * const * table;
1330 const char * try_suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001331
Glenn L McGrath06e95652003-02-09 06:51:14 +00001332 suffix = strrchr(url, '.');
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001333
Glenn L McGrath06e95652003-02-09 06:51:14 +00001334 for (table = suffixTable; *table; table += 2)
1335 if(suffix != NULL && (try_suffix = strstr(*table, suffix)) != 0) {
1336 try_suffix += strlen(suffix);
1337 if(*try_suffix == 0 || *try_suffix == '.')
1338 break;
1339 }
1340 /* also, if not found, set default as "application/octet-stream"; */
1341 config->found_mime_type = *(table+1);
1342#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
1343 if (suffix) {
1344 Htaccess * cur;
1345
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001346 for (cur = config->mime_a; cur; cur = cur->next) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001347 if(strcmp(cur->before_colon, suffix) == 0) {
1348 config->found_mime_type = cur->after_colon;
1349 break;
1350 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001351 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001352 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001353#endif /* CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES */
1354
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001355#ifdef DEBUG
Glenn L McGrath06e95652003-02-09 06:51:14 +00001356 if (config->debugHttpd)
1357 fprintf(stderr, "Sending file '%s' Content-type: %s\n",
1358 url, config->found_mime_type);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001359#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001360
1361 f = open(url, O_RDONLY);
1362 if (f >= 0) {
1363 int count;
1364
1365 sendHeaders(HTTP_OK);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001366 while ((count = bb_full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
1367 bb_full_write(a_c_w, buf, count);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001368 }
1369 close(f);
1370 } else {
1371#ifdef DEBUG
1372 if (config->debugHttpd)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001373 bb_perror_msg("Unable to open '%s'", url);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001374#endif
1375 sendHeaders(HTTP_NOT_FOUND);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001376 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001377
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001378 return 0;
1379}
1380
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001381static int checkPermIP(void)
1382{
1383 Htaccess_IP * cur;
1384
1385 /* This could stand some work */
1386 for (cur = config->ip_a_d; cur; cur = cur->next) {
1387#ifdef DEBUG
1388 if (config->debugHttpd) {
1389 fprintf(stderr, "checkPermIP: '%s' ? ", config->rmt_ip_str);
1390 fprintf(stderr, "'%u.%u.%u.%u/%u.%u.%u.%u'\n",
1391 (unsigned char)(cur->ip >> 24),
1392 (unsigned char)(cur->ip >> 16),
1393 (unsigned char)(cur->ip >> 8),
1394 cur->ip & 0xff,
1395 (unsigned char)(cur->mask >> 24),
1396 (unsigned char)(cur->mask >> 16),
1397 (unsigned char)(cur->mask >> 8),
1398 cur->mask & 0xff);
1399 }
1400#endif
1401 if((config->rmt_ip & cur->mask) == cur->ip)
1402 return cur->allow_deny == 'A'; /* Allow/Deny */
1403 }
1404
1405 /* if uncofigured, return 1 - access from all */
1406 return !config->flg_deny_all;
1407}
1408
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001409/****************************************************************************
1410 *
1411 > $Function: checkPerm()
1412 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001413 * $Description: Check the permission file for access password protected.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001414 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001415 * If config file isn't present, everything is allowed.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001416 * Entries are of the form you can see example from header source
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001417 *
1418 * $Parameters:
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001419 * (const char *) path . . . . The file path.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001420 * (const char *) request . . . User information to validate.
1421 *
1422 * $Return: (int) . . . . . . . . . 1 if request OK, 0 otherwise.
1423 *
1424 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001425
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001426#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001427static int checkPerm(const char *path, const char *request)
1428{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001429 Htaccess * cur;
1430 const char *p;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001431 const char *p0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001432
Glenn L McGrath06e95652003-02-09 06:51:14 +00001433 const char *prev = NULL;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001434
1435 /* This could stand some work */
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001436 for (cur = config->auth; cur; cur = cur->next) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001437 p0 = cur->before_colon;
1438 if(prev != NULL && strcmp(prev, p0) != 0)
1439 continue; /* find next identical */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001440 p = cur->after_colon;
1441#ifdef DEBUG
1442 if (config->debugHttpd)
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001443 fprintf(stderr,"checkPerm: '%s' ? '%s'\n", p0, request);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001444#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001445 {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001446 int l = strlen(p0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001447
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001448 if(strncmp(p0, path, l) == 0 &&
1449 (l == 1 || path[l] == '/' || path[l] == 0)) {
1450 /* path match found. Check request */
Eric Andersen35e643b2003-07-28 07:40:39 +00001451
1452 /* for check next /path:user:password */
1453 prev = p0;
1454#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
1455 {
1456 char *cipher;
1457 char *pp;
1458 char *u = strchr(request, ':');
1459
1460 if(u == NULL) {
1461 /* bad request, ':' required */
1462 continue;
1463 }
1464 if(strncmp(p, request, u-request) != 0) {
1465 /* user uncompared */
1466 continue;
1467 }
1468 pp = strchr(p, ':');
1469 if(pp && pp[1] == '$' && pp[2] == '1' &&
1470 pp[3] == '$' && pp[4]) {
1471 pp++;
1472 cipher = pw_encrypt(u+1, pp);
1473 if (strcmp(cipher, pp) == 0)
1474 return 1; /* Ok */
1475 /* unauthorized */
1476 continue;
1477 }
1478 }
1479#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001480 if (strcmp(p, request) == 0)
1481 return 1; /* Ok */
Eric Andersen35e643b2003-07-28 07:40:39 +00001482 /* unauthorized */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001483 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001484 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001485 } /* for */
1486
Glenn L McGrath06e95652003-02-09 06:51:14 +00001487 return prev == NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001488}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001489
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001490#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1491
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001492
1493/****************************************************************************
1494 *
1495 > $Function: handleIncoming()
1496 *
1497 * $Description: Handle an incoming http request.
1498 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001499 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001500static void handleIncoming(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001501{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001502 char *buf = config->buf;
1503 char *url;
1504 char *purl;
1505 int blank = -1;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001506 char *urlArgs;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001507#ifdef CONFIG_FEATURE_HTTPD_CGI
1508 const char *prequest = request_GET;
1509 char *body = 0;
1510 long length=0;
1511 char *cookie = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001512#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001513 char *test;
1514 struct stat sb;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001515 int ip_allowed;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001516
1517#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1518 int credentials = -1; /* if not requred this is Ok */
1519#endif
1520
1521 do {
1522 int count;
1523
1524 if (getLine(buf) <= 0)
1525 break; /* closed */
1526
1527 purl = strpbrk(buf, " \t");
1528 if(purl == NULL) {
1529BAD_REQUEST:
1530 sendHeaders(HTTP_BAD_REQUEST);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001531 break;
1532 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001533 *purl = 0;
1534#ifdef CONFIG_FEATURE_HTTPD_CGI
1535 if(strcasecmp(buf, prequest) != 0) {
1536 prequest = "POST";
1537 if(strcasecmp(buf, prequest) != 0) {
1538 sendHeaders(HTTP_NOT_IMPLEMENTED);
1539 break;
1540 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001541 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001542#else
1543 if(strcasecmp(buf, request_GET) != 0) {
1544 sendHeaders(HTTP_NOT_IMPLEMENTED);
1545 break;
1546 }
1547#endif
1548 *purl = ' ';
1549 count = sscanf(purl, " %[^ ] HTTP/%d.%*d", buf, &blank);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001550
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001551 decodeString(buf, 0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001552 if (count < 1 || buf[0] != '/') {
1553 /* Garbled request/URL */
1554 goto BAD_REQUEST;
1555 }
1556 url = alloca(strlen(buf) + 12); /* + sizeof("/index.html\0") */
1557 if(url == NULL) {
1558 sendHeaders(HTTP_INTERNAL_SERVER_ERROR);
1559 break;
1560 }
1561 strcpy(url, buf);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001562 /* extract url args if present */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001563 urlArgs = strchr(url, '?');
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001564 if (urlArgs)
1565 *urlArgs++ = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001566
Manuel Novoa III cad53642003-03-19 09:13:01 +00001567 /* algorithm stolen from libbb bb_simplify_path(),
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001568 but don`t strdup and reducing trailing slash and protect out root */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001569 purl = test = url;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001570
Glenn L McGrath06e95652003-02-09 06:51:14 +00001571 do {
1572 if (*purl == '/') {
1573 if (*test == '/') { /* skip duplicate (or initial) slash */
1574 continue;
1575 } else if (*test == '.') {
1576 if (test[1] == '/' || test[1] == 0) { /* skip extra '.' */
1577 continue;
1578 } else if ((test[1] == '.') && (test[2] == '/' || test[2] == 0)) {
1579 ++test;
1580 if (purl == url) {
1581 /* protect out root */
1582 goto BAD_REQUEST;
1583 }
1584 while (*--purl != '/'); /* omit previous dir */
1585 continue;
1586 }
1587 }
1588 }
1589 *++purl = *test;
1590 } while (*++test);
1591
1592 *++purl = 0; /* so keep last character */
1593 test = purl; /* end ptr */
1594
1595 /* If URL is directory, adding '/' */
1596 if(test[-1] != '/') {
1597 if ( is_directory(url + 1, 1, &sb) ) {
1598 *test++ = '/';
1599 *test = 0;
1600 purl = test; /* end ptr */
1601 }
1602 }
1603#ifdef DEBUG
1604 if (config->debugHttpd)
1605 fprintf(stderr, "url='%s', args=%s\n", url, urlArgs);
1606#endif
1607
1608 test = url;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001609 ip_allowed = checkPermIP();
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001610 while(ip_allowed && (test = strchr( test + 1, '/' )) != NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001611 /* have path1/path2 */
1612 *test = '\0';
1613 if( is_directory(url + 1, 1, &sb) ) {
1614 /* may be having subdir config */
1615 parse_conf(url + 1, SUBDIR_PARSE);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001616 ip_allowed = checkPermIP();
Glenn L McGrath06e95652003-02-09 06:51:14 +00001617 }
1618 *test = '/';
1619 }
1620
1621 // read until blank line for HTTP version specified, else parse immediate
1622 while (blank >= 0 && (count = getLine(buf)) > 0) {
1623
1624#ifdef DEBUG
1625 if (config->debugHttpd) fprintf(stderr, "Header: '%s'\n", buf);
1626#endif
1627
1628#ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001629 /* try and do our best to parse more lines */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001630 if ((strncasecmp(buf, Content_length, 15) == 0)) {
1631 if(prequest != request_GET)
1632 length = strtol(buf + 15, 0, 0); // extra read only for POST
1633 } else if ((strncasecmp(buf, "Cookie:", 7) == 0)) {
1634 for(test = buf + 7; isspace(*test); test++)
1635 ;
1636 cookie = strdup(test);
1637 }
1638#endif
1639
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001640#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath06e95652003-02-09 06:51:14 +00001641 if (strncasecmp(buf, "Authorization:", 14) == 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001642 /* We only allow Basic credentials.
1643 * It shows up as "Authorization: Basic <userid:password>" where
1644 * the userid:password is base64 encoded.
1645 */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001646 for(test = buf + 14; isspace(*test); test++)
1647 ;
1648 if (strncasecmp(test, "Basic", 5) != 0)
1649 continue;
1650
1651 test += 5; /* decodeBase64() skiping space self */
1652 decodeBase64(test);
1653 credentials = checkPerm(url, test);
1654 }
1655#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1656
1657 } /* while extra header reading */
1658
1659
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001660 if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001661 /* protect listing [/path]/httpd_conf or IP deny */
1662#ifdef CONFIG_FEATURE_HTTPD_CGI
1663FORBIDDEN: /* protect listing /cgi-bin */
1664#endif
1665 sendHeaders(HTTP_FORBIDDEN);
1666 break;
1667 }
1668
1669#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1670 if (credentials <= 0 && checkPerm(url, ":") == 0) {
1671 sendHeaders(HTTP_UNAUTHORIZED);
1672 break;
1673 }
1674#endif
1675
1676 test = url + 1; /* skip first '/' */
1677
1678#ifdef CONFIG_FEATURE_HTTPD_CGI
1679 /* if strange Content-Length */
1680 if (length < 0 || length > MAX_POST_SIZE)
1681 break;
1682
1683 if (length > 0) {
1684 body = malloc(length + 1);
1685 if (body) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001686 length = bb_full_read(a_c_r, body, length);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001687 if(length < 0) // closed
1688 length = 0;
1689 body[length] = 0; // always null terminate for safety
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001690 }
1691 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001692
Glenn L McGrath06e95652003-02-09 06:51:14 +00001693 if (strncmp(test, "cgi-bin", 7) == 0) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001694 if(test[7] == '/' && test[8] == 0)
1695 goto FORBIDDEN; // protect listing cgi-bin/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001696 sendCgi(url, prequest, urlArgs, body, length, cookie);
1697 } else {
1698 if (prequest != request_GET)
1699 sendHeaders(HTTP_NOT_IMPLEMENTED);
1700 else {
1701#endif /* CONFIG_FEATURE_HTTPD_CGI */
1702 if(purl[-1] == '/')
1703 strcpy(purl, "index.html");
1704 if ( stat(test, &sb ) == 0 ) {
1705 config->ContentLength = sb.st_size;
1706 config->last_mod = sb.st_mtime;
1707 }
1708 sendFile(test, buf);
1709#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1710 /* unset if non inetd looped */
1711 config->ContentLength = -1;
1712#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001713
Glenn L McGrath06e95652003-02-09 06:51:14 +00001714#ifdef CONFIG_FEATURE_HTTPD_CGI
1715 }
1716 }
1717#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001718
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001719 } while (0);
1720
Glenn L McGrath06e95652003-02-09 06:51:14 +00001721
1722#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1723/* from inetd don`t looping: freeing, closing automatic from exit always */
1724# ifdef DEBUG
1725 if (config->debugHttpd) fprintf(stderr, "closing socket\n");
1726# endif
1727# ifdef CONFIG_FEATURE_HTTPD_CGI
1728 free(body);
1729 free(cookie);
1730# endif
1731 shutdown(a_c_w, SHUT_WR);
1732 shutdown(a_c_r, SHUT_RD);
1733 close(config->accepted_socket);
1734#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001735}
1736
1737/****************************************************************************
1738 *
1739 > $Function: miniHttpd()
1740 *
1741 * $Description: The main http server function.
1742 *
1743 * Given an open socket fildes, listen for new connections and farm out
1744 * the processing as a forked process.
1745 *
1746 * $Parameters:
1747 * (int) server. . . The server socket fildes.
1748 *
1749 * $Return: (int) . . . . Always 0.
1750 *
1751 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001752#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001753static int miniHttpd(int server)
1754{
1755 fd_set readfd, portfd;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001756
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001757 FD_ZERO(&portfd);
1758 FD_SET(server, &portfd);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001759
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001760 /* copy the ports we are watching to the readfd set */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001761 while (1) {
1762 readfd = portfd;
1763
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001764 /* Now wait INDEFINATELY on the set of sockets! */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001765 if (select(server + 1, &readfd, 0, 0, 0) > 0) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001766 if (FD_ISSET(server, &readfd)) {
1767 int on;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001768 struct sockaddr_in fromAddr;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001769
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001770 socklen_t fromAddrLen = sizeof(fromAddr);
1771 int s = accept(server,
Glenn L McGrath06e95652003-02-09 06:51:14 +00001772 (struct sockaddr *)&fromAddr, &fromAddrLen);
1773
1774 if (s < 0) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001775 continue;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001776 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001777 config->accepted_socket = s;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001778 config->rmt_ip = ntohl(fromAddr.sin_addr.s_addr);
1779#if defined(CONFIG_FEATURE_HTTPD_CGI) || defined(DEBUG)
1780 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1781 (unsigned char)(config->rmt_ip >> 24),
1782 (unsigned char)(config->rmt_ip >> 16),
1783 (unsigned char)(config->rmt_ip >> 8),
1784 config->rmt_ip & 0xff);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001785 config->port = ntohs(fromAddr.sin_port);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001786#ifdef DEBUG
Glenn L McGrath06e95652003-02-09 06:51:14 +00001787 if (config->debugHttpd) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001788 bb_error_msg("connection from IP=%s, port %u\n",
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001789 config->rmt_ip_str, config->port);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001790 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001791#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001792#endif /* CONFIG_FEATURE_HTTPD_CGI */
1793
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001794 /* set the KEEPALIVE option to cull dead connections */
1795 on = 1;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001796 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof (on));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001797
Glenn L McGrath06e95652003-02-09 06:51:14 +00001798 if (config->debugHttpd || fork() == 0) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001799 /* This is the spawned thread */
1800#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
1801 /* protect reload config, may be confuse checking */
1802 signal(SIGHUP, SIG_IGN);
1803#endif
1804 handleIncoming();
1805 if(!config->debugHttpd)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001806 exit(0);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001807 }
1808 close(s);
1809 }
1810 }
1811 } // while (1)
1812 return 0;
1813}
1814
Glenn L McGrath06e95652003-02-09 06:51:14 +00001815#else
1816 /* from inetd */
1817
1818static int miniHttpd(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001819{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001820 struct sockaddr_in fromAddrLen;
1821 socklen_t sinlen = sizeof (struct sockaddr_in);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001822
1823 getpeername (0, (struct sockaddr *)&fromAddrLen, &sinlen);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001824 config->rmt_ip = ntohl(fromAddrLen.sin_addr.s_addr);
1825#if defined(CONFIG_FEATURE_HTTPD_CGI) || defined(DEBUG)
1826 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1827 (unsigned char)(config->rmt_ip >> 24),
1828 (unsigned char)(config->rmt_ip >> 16),
1829 (unsigned char)(config->rmt_ip >> 8),
1830 config->rmt_ip & 0xff);
1831#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001832 config->port = ntohs(fromAddrLen.sin_port);
1833 handleIncoming();
1834 return 0;
1835}
1836#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
1837
1838#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
1839static void sighup_handler(int sig)
1840{
1841 /* set and reset */
1842 struct sigaction sa;
1843
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001844 parse_conf(default_path_httpd_conf,
1845 sig == SIGHUP ? SIGNALED_PARSE : FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001846 sa.sa_handler = sighup_handler;
1847 sigemptyset(&sa.sa_mask);
1848 sa.sa_flags = SA_RESTART;
1849 sigaction(SIGHUP, &sa, NULL);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001850}
1851#endif
1852
Eric Andersena3bb3e62003-06-26 09:05:32 +00001853
1854static const char httpd_opts[]="c:d:h:"
1855#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
1856 "e:"
1857#define OPT_INC_1 1
1858#else
1859#define OPT_INC_1 0
1860#endif
1861#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1862 "r:"
Eric Andersen35e643b2003-07-28 07:40:39 +00001863# ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
1864 "m:"
1865# define OPT_INC_2 2
1866# else
1867# define OPT_INC_2 1
1868#endif
Eric Andersena3bb3e62003-06-26 09:05:32 +00001869#else
1870#define OPT_INC_2 0
1871#endif
1872#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1873 "p:v"
1874#ifdef CONFIG_FEATURE_HTTPD_SETUID
1875 "u:"
1876#endif
Eric Andersen35e643b2003-07-28 07:40:39 +00001877#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
Eric Andersena3bb3e62003-06-26 09:05:32 +00001878 ;
1879
1880#define OPT_CONFIG_FILE (1<<0)
1881#define OPT_DECODE_URL (1<<1)
1882#define OPT_HOME_HTTPD (1<<2)
1883#define OPT_ENCODE_URL (1<<(2+OPT_INC_1))
Eric Andersen35e643b2003-07-28 07:40:39 +00001884#define OPT_REALM (1<<(3+OPT_INC_1))
1885#define OPT_MD5 (1<<(4+OPT_INC_1))
Eric Andersena3bb3e62003-06-26 09:05:32 +00001886#define OPT_PORT (1<<(3+OPT_INC_1+OPT_INC_2))
1887#define OPT_DEBUG (1<<(4+OPT_INC_1+OPT_INC_2))
1888#define OPT_SETUID (1<<(5+OPT_INC_1+OPT_INC_2))
1889
1890
Glenn L McGrath06e95652003-02-09 06:51:14 +00001891#ifdef HTTPD_STANDALONE
1892int main(int argc, char *argv[])
1893#else
1894int httpd_main(int argc, char *argv[])
1895#endif
1896{
Eric Andersena3bb3e62003-06-26 09:05:32 +00001897 unsigned long opt;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001898 const char *home_httpd = home;
Eric Andersena3bb3e62003-06-26 09:05:32 +00001899 char *url_for_decode;
1900#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
1901 const char *url_for_encode;
1902#endif
1903#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1904 const char *s_port;
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00001905#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001906
1907#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001908 int server;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001909#endif
1910
1911#ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersena3bb3e62003-06-26 09:05:32 +00001912 const char *s_uid;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001913 long uid = -1;
1914#endif
1915
Eric Andersen35e643b2003-07-28 07:40:39 +00001916#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
1917 const char *pass;
1918#endif
1919
Glenn L McGrath06e95652003-02-09 06:51:14 +00001920 config = xcalloc(1, sizeof(*config));
1921#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1922 config->realm = "Web Server Authentication";
1923#endif
1924
1925#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1926 config->port = 80;
1927#endif
1928
1929 config->ContentLength = -1;
1930
Eric Andersena3bb3e62003-06-26 09:05:32 +00001931 opt = bb_getopt_ulflags(argc, argv, httpd_opts,
1932 &(config->configFile), &url_for_decode, &home_httpd
Glenn L McGrath06e95652003-02-09 06:51:14 +00001933#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Eric Andersena3bb3e62003-06-26 09:05:32 +00001934 , &url_for_encode
Glenn L McGrath06e95652003-02-09 06:51:14 +00001935#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001936#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Eric Andersena3bb3e62003-06-26 09:05:32 +00001937 , &(config->realm)
Eric Andersen35e643b2003-07-28 07:40:39 +00001938# ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
1939 , &pass
1940# endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001941#endif
Eric Andersena3bb3e62003-06-26 09:05:32 +00001942#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1943 , &s_port
Glenn L McGrath06e95652003-02-09 06:51:14 +00001944#ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersena3bb3e62003-06-26 09:05:32 +00001945 , &s_uid
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001946#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00001947#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001948 );
Eric Andersena3bb3e62003-06-26 09:05:32 +00001949
1950 if(opt & OPT_DECODE_URL) {
1951 printf("%s", decodeString(url_for_decode, 1));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001952 return 0;
Eric Andersena3bb3e62003-06-26 09:05:32 +00001953 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001954#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Eric Andersena3bb3e62003-06-26 09:05:32 +00001955 if(opt & OPT_ENCODE_URL) {
1956 printf("%s", encodeString(url_for_encode));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001957 return 0;
Eric Andersena3bb3e62003-06-26 09:05:32 +00001958 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001959#endif
Eric Andersen35e643b2003-07-28 07:40:39 +00001960#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
1961 if(opt & OPT_MD5) {
1962 printf("%s\n", pw_encrypt(pass, "$1$"));
1963 return 0;
1964 }
1965#endif
Eric Andersena3bb3e62003-06-26 09:05:32 +00001966#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1967 if(opt & OPT_PORT)
1968 config->port = bb_xgetlarg(s_port, 10, 1, 0xffff);
1969 config->debugHttpd = opt & OPT_DEBUG;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001970#ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersena3bb3e62003-06-26 09:05:32 +00001971 if(opt & OPT_SETUID) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001972 char *e;
1973
Eric Andersena3bb3e62003-06-26 09:05:32 +00001974 uid = strtol(s_uid, &e, 0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001975 if(*e != '\0') {
1976 /* not integer */
Eric Andersena3bb3e62003-06-26 09:05:32 +00001977 uid = my_getpwnam(s_uid);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001978 }
1979 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001980#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00001981#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001982
Glenn L McGrath06e95652003-02-09 06:51:14 +00001983 if(chdir(home_httpd)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001984 bb_perror_msg_and_die("can`t chdir to %s", home_httpd);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001985 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001986#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1987 server = openServer();
1988# ifdef CONFIG_FEATURE_HTTPD_SETUID
1989 /* drop privilegies */
1990 if(uid > 0)
1991 setuid(uid);
1992# endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001993#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001994
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00001995#ifdef CONFIG_FEATURE_HTTPD_CGI
1996 {
1997 char *p = getenv("PATH");
1998
1999 if(p)
2000 p = bb_xstrdup(p);
2001 clearenv();
2002 if(p) {
2003 setenv("PATH", p, 0);
2004 }
Glenn L McGrath14092a12003-09-12 00:44:50 +00002005# ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2006 addEnvPort("SERVER");
2007# endif
Glenn L McGrathfe538ba2003-09-10 23:35:45 +00002008 }
2009#endif
2010
Glenn L McGrath06e95652003-02-09 06:51:14 +00002011#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
2012 sighup_handler(0);
2013#else
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00002014 parse_conf(default_path_httpd_conf, FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002015#endif
2016
2017#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2018 if (!config->debugHttpd) {
2019 if (daemon(1, 0) < 0) /* don`t change curent directory */
Manuel Novoa III cad53642003-03-19 09:13:01 +00002020 bb_perror_msg_and_die("daemon");
Glenn L McGrath06e95652003-02-09 06:51:14 +00002021 }
2022 return miniHttpd(server);
2023#else
2024 return miniHttpd();
2025#endif
2026}