blob: eb03f34c0d8d27f5c5b2df1e8d7156404822b6b8 [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
205/* CGI environ size */
206#ifdef CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000207#define ENVSIZE 70 /* set max CGI variable */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000208#else
209#define ENVSIZE 15 /* minimal requires */
210#endif
211
212#define MAX_POST_SIZE (64*1024) /* 64k. Its Small? May be ;) */
213
214#define MAX_MEMORY_BUFF 8192 /* IO buffer */
215
216typedef struct HT_ACCESS {
217 char *after_colon;
218 struct HT_ACCESS *next;
219 char before_colon[1]; /* really bigger, must last */
220} Htaccess;
221
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000222typedef struct HT_ACCESS_IP {
223 unsigned int ip;
224 unsigned int mask;
225 int allow_deny;
226 struct HT_ACCESS_IP *next;
227} Htaccess_IP;
228
Glenn L McGrath06e95652003-02-09 06:51:14 +0000229typedef struct
230{
231#ifdef CONFIG_FEATURE_HTTPD_CGI
232 char *envp[ENVSIZE+1];
233 int envCount;
234#endif
235 char buf[MAX_MEMORY_BUFF];
236
237#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
238 const char *realm;
239#endif
240 const char *configFile;
241
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000242 unsigned int rmt_ip;
243#if defined(CONFIG_FEATURE_HTTPD_CGI) || defined(DEBUG)
244 char rmt_ip_str[16]; /* for set env REMOTE_ADDR */
245#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000246 unsigned port; /* server initial port and for
247 set env REMOTE_PORT */
248
249 const char *found_mime_type;
250 off_t ContentLength; /* -1 - unknown */
251 time_t last_mod;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000252
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000253 Htaccess_IP *ip_a_d; /* config allow/deny lines */
Glenn L McGrath393183d2003-05-26 14:07:50 +0000254 int flg_deny_all;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000255#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
256 Htaccess *auth; /* config user:password lines */
257#endif
258#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
259 Htaccess *mime_a; /* config mime types */
260#endif
261
Glenn L McGrath06e95652003-02-09 06:51:14 +0000262#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
263 int accepted_socket;
264#define a_c_r config->accepted_socket
265#define a_c_w config->accepted_socket
266 int debugHttpd; /* if seted, don`t stay daemon */
267#else
268#define a_c_r 0
269#define a_c_w 1
270#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000271} HttpdConfig;
272
273static HttpdConfig *config;
274
275static const char request_GET[] = "GET"; /* size algorithic optimize */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000276
277static const char* const suffixTable [] = {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000278/* Warning: shorted equalent suffix in one line must be first */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000279 ".htm.html", "text/html",
280 ".jpg.jpeg", "image/jpeg",
281 ".gif", "image/gif",
282 ".png", "image/png",
283 ".txt.h.c.cc.cpp", "text/plain",
Glenn L McGrath06e95652003-02-09 06:51:14 +0000284 ".css", "text/css",
285 ".wav", "audio/wav",
286 ".avi", "video/x-msvideo",
287 ".qt.mov", "video/quicktime",
288 ".mpe.mpeg", "video/mpeg",
289 ".mid.midi", "audio/midi",
290 ".mp3", "audio/mpeg",
291#if 0 /* unpopular */
292 ".au", "audio/basic",
293 ".pac", "application/x-ns-proxy-autoconfig",
294 ".vrml.wrl", "model/vrml",
295#endif
296 0, "application/octet-stream" /* default */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000297 };
298
299typedef enum
300{
301 HTTP_OK = 200,
302 HTTP_UNAUTHORIZED = 401, /* authentication needed, respond with auth hdr */
303 HTTP_NOT_FOUND = 404,
Glenn L McGrath06e95652003-02-09 06:51:14 +0000304 HTTP_NOT_IMPLEMENTED = 501, /* used for unrecognized requests */
305 HTTP_BAD_REQUEST = 400, /* malformed syntax */
306 HTTP_FORBIDDEN = 403,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000307 HTTP_INTERNAL_SERVER_ERROR = 500,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000308#if 0 /* future use */
309 HTTP_CONTINUE = 100,
310 HTTP_SWITCHING_PROTOCOLS = 101,
311 HTTP_CREATED = 201,
312 HTTP_ACCEPTED = 202,
313 HTTP_NON_AUTHORITATIVE_INFO = 203,
314 HTTP_NO_CONTENT = 204,
315 HTTP_MULTIPLE_CHOICES = 300,
316 HTTP_MOVED_PERMANENTLY = 301,
317 HTTP_MOVED_TEMPORARILY = 302,
318 HTTP_NOT_MODIFIED = 304,
319 HTTP_PAYMENT_REQUIRED = 402,
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000320 HTTP_BAD_GATEWAY = 502,
321 HTTP_SERVICE_UNAVAILABLE = 503, /* overload, maintenance */
322 HTTP_RESPONSE_SETSIZE=0xffffffff
323#endif
324} HttpResponseNum;
325
326typedef struct
327{
328 HttpResponseNum type;
329 const char *name;
330 const char *info;
331} HttpEnumString;
332
333static const HttpEnumString httpResponseNames[] = {
334 { HTTP_OK, "OK" },
Glenn L McGrath06e95652003-02-09 06:51:14 +0000335 { HTTP_NOT_IMPLEMENTED, "Not Implemented",
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000336 "The requested method is not recognized by this server." },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000337#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000338 { HTTP_UNAUTHORIZED, "Unauthorized", "" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000339#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000340 { HTTP_NOT_FOUND, "Not Found",
341 "The requested URL was not found on this server." },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000342 { HTTP_BAD_REQUEST, "Bad Request", "Unsupported method." },
Glenn L McGrath06e95652003-02-09 06:51:14 +0000343 { HTTP_FORBIDDEN, "Forbidden", "" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000344 { HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error",
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000345 "Internal Server Error" },
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000346#if 0 /* not implemented */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000347 { HTTP_CREATED, "Created" },
348 { HTTP_ACCEPTED, "Accepted" },
349 { HTTP_NO_CONTENT, "No Content" },
350 { HTTP_MULTIPLE_CHOICES, "Multiple Choices" },
351 { HTTP_MOVED_PERMANENTLY, "Moved Permanently" },
352 { HTTP_MOVED_TEMPORARILY, "Moved Temporarily" },
353 { HTTP_NOT_MODIFIED, "Not Modified" },
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000354 { HTTP_BAD_GATEWAY, "Bad Gateway", "" },
355 { HTTP_SERVICE_UNAVAILABLE, "Service Unavailable", "" },
356#endif
357};
358
Glenn L McGrath06e95652003-02-09 06:51:14 +0000359
360static const char RFC1123FMT[] = "%a, %d %b %Y %H:%M:%S GMT";
361static const char Content_length[] = "Content-length:";
362
363
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000364static int
365scan_ip (const char **ep, unsigned int *ip, unsigned char endc)
366{
367 const char *p = *ep;
368 int auto_mask = 8;
369 int j;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000370
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000371 *ip = 0;
372 for (j = 0; j < 4; j++) {
373 unsigned int octet;
374
375 if ((*p < '0' || *p > '9') && (*p != '/' || j == 0) && *p != 0)
376 return -auto_mask;
377 octet = 0;
378 while (*p >= '0' && *p <= '9') {
379 octet *= 10;
380 octet += *p - '0';
381 if (octet > 255)
382 return -auto_mask;
383 p++;
384 }
385 if (*p == '.')
386 p++;
387 if (*p != '/' && *p != 0)
388 auto_mask += 8;
389 *ip = ((*ip) << 8) | octet;
390 }
391 if (*p != 0) {
392 if (*p != endc)
393 return -auto_mask;
394 p++;
395 if(*p == 0)
396 return -auto_mask;
397 }
398 *ep = p;
399 return auto_mask;
400}
401
402static int
403scan_ip_mask (const char *ipm, unsigned int *ip, unsigned int *mask)
404{
405 int i;
406 unsigned int msk;
407
408 i = scan_ip(&ipm, ip, '/');
409 if(i < 0)
410 return i;
411 if(*ipm) {
412 const char *p = ipm;
413
414 i = 0;
415 while (*p) {
416 if (*p < '0' || *p > '9') {
417 if (*p == '.') {
418 i = scan_ip (&ipm, mask, 0);
419 return i != 32;
420 }
421 return -1;
422 }
423 i *= 10;
424 i += *p - '0';
425 p++;
426 }
427 }
428 if (i > 32 || i < 0)
429 return -1;
430 msk = 0x80000000;
431 *mask = 0;
432 while (i > 0) {
433 *mask |= msk;
434 msk >>= 1;
435 i--;
436 }
437 return 0;
438}
439
440#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000441static void free_config_lines(Htaccess **pprev)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000442{
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000443 Htaccess *prev = *pprev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000444
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000445 while( prev ) {
446 Htaccess *cur = prev;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000447
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000448 prev = cur->next;
449 free(cur);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000450 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000451 *pprev = NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000452}
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000453#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000454
Glenn L McGrath06e95652003-02-09 06:51:14 +0000455/* flag */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000456#define FIRST_PARSE 0
457#define SUBDIR_PARSE 1
458#define SIGNALED_PARSE 2
459#define FIND_FROM_HTTPD_ROOT 3
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000460/****************************************************************************
461 *
462 > $Function: parse_conf()
463 *
464 * $Description: parse configuration file into in-memory linked list.
465 *
466 * The first non-white character is examined to determine if the config line
467 * is one of the following:
468 * .ext:mime/type # new mime type not compiled into httpd
469 * [adAD]:from # ip address allow/deny, * for wildcard
470 * /path:user:pass # username/password
471 *
472 * Any previous IP rules are discarded.
473 * If the flag argument is not SUBDIR_PARSE then all /path and mime rules
474 * are also discarded. That is, previous settings are retained if flag is
475 * SUBDIR_PARSE.
476 *
477 * $Parameters:
478 * (const char *) path . . null for ip address checks, path for password
479 * checks.
480 * (int) flag . . . . . . the source of the parse request.
481 *
482 * $Return: (None)
483 *
484 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000485static void parse_conf(const char *path, int flag)
486{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000487 FILE *f;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000488 Htaccess *cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000489#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
490 Htaccess *prev;
491#endif
492
Glenn L McGrath06e95652003-02-09 06:51:14 +0000493 const char *cf = config->configFile;
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000494 char buf[160];
Glenn L McGrath06e95652003-02-09 06:51:14 +0000495 char *p0 = NULL;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000496 char *c, *p;
497
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000498 /* free previous ip setup if present */
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000499 Htaccess_IP *pip = config->ip_a_d;
500
501 while( pip ) {
502 Htaccess_IP *cur_ipl = pip;
503
504 pip = cur_ipl->next;
505 free(cur_ipl);
506 }
507 config->ip_a_d = NULL;
508
Glenn L McGrath24833432003-06-10 17:22:49 +0000509 config->flg_deny_all = 0;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000510
511#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000512 /* retain previous auth and mime config only for subdir parse */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000513 if(flag != SUBDIR_PARSE) {
514#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000515 free_config_lines(&config->auth);
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000516#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000517#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
518 free_config_lines(&config->mime_a);
519#endif
520 }
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000521#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000522
523 if(flag == SUBDIR_PARSE || cf == NULL) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000524 cf = alloca(strlen(path) + sizeof(httpd_conf) + 2);
525 if(cf == NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000526 if(flag == FIRST_PARSE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000527 bb_error_msg_and_die(bb_msg_memory_exhausted);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000528 return;
529 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000530 sprintf((char *)cf, "%s/%s", path, httpd_conf);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000531 }
532
533 while((f = fopen(cf, "r")) == NULL) {
Eric Andersen35e643b2003-07-28 07:40:39 +0000534 if(flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) {
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000535 /* config file not found, no changes to config */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000536 return;
537 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000538 if(config->configFile && flag == FIRST_PARSE) /* if -c option given */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000539 bb_perror_msg_and_die("%s", cf);
540 flag = FIND_FROM_HTTPD_ROOT;
541 cf = httpd_conf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000542 }
543
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000544#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
545 prev = config->auth;
546#endif
547 /* This could stand some work */
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000548 while ( (p0 = fgets(buf, sizeof(buf), f)) != NULL) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000549 c = NULL;
550 for(p = p0; *p0 != 0 && *p0 != '#'; p0++) {
551 if(!isspace(*p0)) {
552 *p++ = *p0;
553 if(*p0 == ':' && c == NULL)
554 c = p;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000555 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000556 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000557 *p = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000558
559 /* test for empty or strange line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000560 if (c == NULL || *c == 0)
Glenn L McGrath06e95652003-02-09 06:51:14 +0000561 continue;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000562 p0 = buf;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000563 if(*p0 == 'd')
564 *p0 = 'D';
Glenn L McGrath393183d2003-05-26 14:07:50 +0000565 if(*c == '*') {
566 if(*p0 == 'D') {
567 /* memorize deny all */
568 config->flg_deny_all++;
569 }
570 /* skip default other "word:*" config lines */
571 continue;
572 }
573
574 if(*p0 == 'a')
575 *p0 = 'A';
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000576 else if(*p0 != 'D' && *p0 != 'A'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000577#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000578 && *p0 != '/'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000579#endif
580#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000581 && *p0 != '.'
Glenn L McGrath06e95652003-02-09 06:51:14 +0000582#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000583 )
Glenn L McGrath06e95652003-02-09 06:51:14 +0000584 continue;
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000585 if(*p0 == 'A' || *p0 == 'D') {
586 /* storing current config IP line */
587 pip = calloc(1, sizeof(Htaccess_IP));
588 if(pip) {
589 if(scan_ip_mask (c, &(pip->ip), &(pip->mask))) {
590 /* syntax IP{/mask} error detected, protect all */
591 *p0 = 'D';
592 pip->mask = 0;
593 }
594 pip->allow_deny = *p0;
595 if(*p0 == 'D') {
596 /* Deny:form_IP move top */
597 pip->next = config->ip_a_d;
598 config->ip_a_d = pip;
599 } else {
600 /* add to bottom A:form_IP config line */
601 Htaccess_IP *prev_IP = config->ip_a_d;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000602
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000603 if(prev_IP == NULL) {
604 config->ip_a_d = pip;
605 } else {
606 while(prev_IP->next)
607 prev_IP = prev_IP->next;
608 prev_IP->next = pip;
609 }
610 }
611 }
612 continue;
613 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000614#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
615 if(*p0 == '/') {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000616 /* make full path from httpd root / curent_path / config_line_path */
617 cf = flag == SUBDIR_PARSE ? path : "";
618 p0 = malloc(strlen(cf) + (c - buf) + 2 + strlen(c));
619 if(p0 == NULL)
620 continue;
621 c[-1] = 0;
622 sprintf(p0, "/%s%s", cf, buf);
623
624 /* another call bb_simplify_path */
625 cf = p = p0;
626
627 do {
628 if (*p == '/') {
629 if (*cf == '/') { /* skip duplicate (or initial) slash */
630 continue;
631 } else if (*cf == '.') {
632 if (cf[1] == '/' || cf[1] == 0) { /* remove extra '.' */
633 continue;
634 } else if ((cf[1] == '.') && (cf[2] == '/' || cf[2] == 0)) {
635 ++cf;
636 if (p > p0) {
637 while (*--p != '/'); /* omit previous dir */
638 }
639 continue;
640 }
641 }
642 }
643 *++p = *cf;
644 } while (*++cf);
645
646 if ((p == p0) || (*p != '/')) { /* not a trailing slash */
647 ++p; /* so keep last character */
648 }
649 *p = 0;
650 sprintf(p0, "%s:%s", p0, c);
651 }
652#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000653
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000654#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
655 /* storing current config line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000656 cur = calloc(1, sizeof(Htaccess) + strlen(p0));
657 if(cur) {
658 cf = strcpy(cur->before_colon, p0);
659 c = strchr(cf, ':');
660 *c++ = 0;
661 cur->after_colon = c;
662#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
663 if(*cf == '/')
664 free(p0);
665#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000666#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
Glenn L McGrath874e3382003-05-14 12:11:36 +0000667 else if(*cf == '.') {
668 /* config .mime line move top for overwrite previous */
669 cur->next = config->mime_a;
670 config->mime_a = cur;
671 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000672#endif
673
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000674#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
675 else if(prev == NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000676 /* first line */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000677 config->auth = prev = cur;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000678 } else {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000679 /* sort path, if current lenght eq or bigger then move up */
680 Htaccess *prev_hti = config->auth;
681 int l = strlen(cf);
682 Htaccess *hti;
683
684 for(hti = prev_hti; hti; hti = hti->next) {
685 if(l >= strlen(hti->before_colon)) {
686 /* insert before hti */
687 cur->next = hti;
688 if(prev_hti != hti) {
689 prev_hti->next = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000690 } else {
691 /* insert as top */
692 config->auth = cur;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000693 }
Glenn L McGrath393183d2003-05-26 14:07:50 +0000694 break;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000695 }
696 if(prev_hti != hti)
697 prev_hti = prev_hti->next;
698 }
699 if(!hti) { /* not inserted, add to bottom */
700 prev->next = cur;
701 prev = cur;
702 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000703 }
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000704#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +0000705#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000706 }
707 }
708 fclose(f);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000709}
710
711#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000712/****************************************************************************
713 *
714 > $Function: encodeString()
715 *
716 * $Description: Given a string, html encode special characters.
717 * This is used for the -e command line option to provide an easy way
718 * for scripts to encode result data without confusing browsers. The
719 * returned string pointer is memory allocated by malloc().
720 *
721 * $Parameters:
722 * (const char *) string . . The first string to encode.
723 *
724 * $Return: (char *) . . . .. . . A pointer to the encoded string.
725 *
726 * $Errors: Returns a null string ("") if memory is not available.
727 *
728 ****************************************************************************/
729static char *encodeString(const char *string)
730{
731 /* take the simple route and encode everything */
732 /* could possibly scan once to get length. */
733 int len = strlen(string);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000734 char *out = malloc(len*5 +1);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000735 char *p=out;
736 char ch;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000737
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000738 if (!out) return "";
Glenn L McGrath06e95652003-02-09 06:51:14 +0000739 while ((ch = *string++)) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000740 // very simple check for what to encode
741 if (isalnum(ch)) *p++ = ch;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000742 else p += sprintf(p, "&#%d", (unsigned char) ch);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000743 }
744 *p=0;
745 return out;
746}
Glenn L McGrath06e95652003-02-09 06:51:14 +0000747#endif /* CONFIG_FEATURE_HTTPD_ENCODE_URL_STR */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000748
749/****************************************************************************
750 *
751 > $Function: decodeString()
752 *
753 * $Description: Given a URL encoded string, convert it to plain ascii.
754 * Since decoding always makes strings smaller, the decode is done in-place.
755 * Thus, callers should strdup() the argument if they do not want the
756 * argument modified. The return is the original pointer, allowing this
757 * function to be easily used as arguments to other functions.
758 *
759 * $Parameters:
760 * (char *) string . . . The first string to decode.
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000761 * (int) flag . . . 1 if require decode '+' as ' ' for CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000762 *
763 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
764 *
765 * $Errors: None
766 *
767 ****************************************************************************/
Eric Andersena3bb3e62003-06-26 09:05:32 +0000768static char *decodeString(char *orig, int flag_plus_to_space)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000769{
770 /* note that decoded string is always shorter than original */
Eric Andersena3bb3e62003-06-26 09:05:32 +0000771 char *string = orig;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000772 char *ptr = string;
Eric Andersena3bb3e62003-06-26 09:05:32 +0000773
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000774 while (*ptr)
775 {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000776 if (*ptr == '+' && flag_plus_to_space) { *string++ = ' '; ptr++; }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000777 else if (*ptr != '%') *string++ = *ptr++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000778 else {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000779 unsigned int value;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000780 sscanf(ptr+1, "%2X", &value);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000781 *string++ = value;
782 ptr += 3;
783 }
784 }
785 *string = '\0';
786 return orig;
787}
788
789
Glenn L McGrath06e95652003-02-09 06:51:14 +0000790#ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000791/****************************************************************************
792 *
793 > $Function: addEnv()
794 *
795 * $Description: Add an enviornment variable setting to the global list.
796 * A NAME=VALUE string is allocated, filled, and added to the list of
797 * environment settings passed to the cgi execution script.
798 *
799 * $Parameters:
Glenn L McGrath06e95652003-02-09 06:51:14 +0000800 * (char *) name_before_underline - The first part environment variable name.
801 * (char *) name_after_underline - The second part environment variable name.
802 * (char *) value . . The value to which the env variable is set.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000803 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000804 * $Return: (void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000805 *
806 * $Errors: Silently returns if the env runs out of space to hold the new item
807 *
808 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000809static void addEnv(const char *name_before_underline,
810 const char *name_after_underline, const char *value)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000811{
812 char *s;
Glenn L McGrath393183d2003-05-26 14:07:50 +0000813 const char *underline;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000814
815 if (config->envCount >= ENVSIZE)
816 return;
817 if (!value)
818 value = "";
Glenn L McGrath393183d2003-05-26 14:07:50 +0000819 underline = *name_after_underline ? "_" : "";
820 asprintf(&s, "%s%s%s=%s", name_before_underline, underline,
Glenn L McGrath06e95652003-02-09 06:51:14 +0000821 name_after_underline, value);
Glenn L McGrath393183d2003-05-26 14:07:50 +0000822 if(s) {
Glenn L McGrath06e95652003-02-09 06:51:14 +0000823 config->envp[config->envCount++] = s;
824 config->envp[config->envCount] = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000825 }
826}
827
Eric Andersena3bb3e62003-06-26 09:05:32 +0000828#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 +0000829/* set environs SERVER_PORT and REMOTE_PORT */
830static void addEnvPort(const char *port_name)
831{
832 char buf[16];
833
834 sprintf(buf, "%u", config->port);
835 addEnv(port_name, "PORT", buf);
836}
Eric Andersena3bb3e62003-06-26 09:05:32 +0000837#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000838#endif /* CONFIG_FEATURE_HTTPD_CGI */
839
840#ifdef CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000841/****************************************************************************
842 *
843 > $Function: addEnvCgi
844 *
845 * $Description: Create environment variables given a URL encoded arg list.
846 * For each variable setting the URL encoded arg list, create a corresponding
847 * environment variable. URL encoded arguments have the form
Glenn L McGrath06e95652003-02-09 06:51:14 +0000848 * name1=value1&name2=value2&name3=&ignores
849 * from this example, name3 set empty value, tail without '=' skiping
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000850 *
851 * $Parameters:
852 * (char *) pargs . . . . A pointer to the URL encoded arguments.
853 *
854 * $Return: None
855 *
856 * $Errors: None
857 *
858 ****************************************************************************/
859static void addEnvCgi(const char *pargs)
860{
861 char *args;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000862 char *memargs;
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000863 char *namelist; /* space separated list of arg names */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000864 if (pargs==0) return;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000865
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000866 /* args are a list of name=value&name2=value2 sequences */
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000867 namelist = (char *) malloc(strlen(pargs));
868 if (namelist) namelist[0]=0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000869 memargs = args = strdup(pargs);
870 while (args && *args) {
871 const char *name = args;
872 char *value = strchr(args, '=');
873
874 if (!value) /* &XXX without '=' */
875 break;
876 *value++ = 0;
877 args = strchr(value, '&');
878 if (args)
879 *args++ = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000880 addEnv("CGI", name, decodeString(value, 1));
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000881 if (*namelist) strcat(namelist, " ");
Glenn L McGrath393183d2003-05-26 14:07:50 +0000882 strcat(namelist, name);
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000883 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000884 free(memargs);
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000885 if (namelist) {
Glenn L McGrath393183d2003-05-26 14:07:50 +0000886 addEnv("CGI", "ARGLIST_", namelist);
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +0000887 free(namelist);
888 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000889}
Glenn L McGrath06e95652003-02-09 06:51:14 +0000890#endif /* CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV */
891
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000892
893#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000894/****************************************************************************
895 *
896 > $Function: decodeBase64()
897 *
898 > $Description: Decode a base 64 data stream as per rfc1521.
899 * Note that the rfc states that none base64 chars are to be ignored.
900 * Since the decode always results in a shorter size than the input, it is
901 * OK to pass the input arg as an output arg.
902 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000903 * $Parameter:
904 * (char *) Data . . . . A pointer to a base64 encoded string.
905 * Where to place the decoded data.
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000906 *
Glenn L McGrath06e95652003-02-09 06:51:14 +0000907 * $Return: void
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000908 *
909 * $Errors: None
910 *
911 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000912static void decodeBase64(char *Data)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000913{
Glenn L McGrath06e95652003-02-09 06:51:14 +0000914
915 const unsigned char *in = Data;
916 // The decoded size will be at most 3/4 the size of the encoded
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000917 unsigned long ch = 0;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000918 int i = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000919
920 while (*in) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000921 int t = *in++;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000922
Glenn L McGrath874e3382003-05-14 12:11:36 +0000923 if(t >= '0' && t <= '9')
924 t = t - '0' + 52;
925 else if(t >= 'A' && t <= 'Z')
926 t = t - 'A';
927 else if(t >= 'a' && t <= 'z')
928 t = t - 'a' + 26;
929 else if(t == '+')
930 t = 62;
931 else if(t == '/')
932 t = 63;
933 else if(t == '=')
934 t = 0;
935 else
936 continue;
937
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000938 ch = (ch << 6) | t;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000939 i++;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +0000940 if (i == 4) {
941 *Data++ = (char) (ch >> 16);
942 *Data++ = (char) (ch >> 8);
943 *Data++ = (char) ch;
944 i = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000945 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000946 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000947 *Data = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000948}
949#endif
950
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000951
Glenn L McGrath06e95652003-02-09 06:51:14 +0000952#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000953/****************************************************************************
954 *
955 > $Function: openServer()
956 *
957 * $Description: create a listen server socket on the designated port.
958 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000959 * $Return: (int) . . . A connection socket. -1 for errors.
960 *
961 * $Errors: None
962 *
963 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +0000964static int openServer(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000965{
966 struct sockaddr_in lsocket;
967 int fd;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000968
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000969 /* create the socket right now */
970 /* inet_addr() returns a value that is already in network order */
971 memset(&lsocket, 0, sizeof(lsocket));
972 lsocket.sin_family = AF_INET;
973 lsocket.sin_addr.s_addr = INADDR_ANY;
Glenn L McGrath06e95652003-02-09 06:51:14 +0000974 lsocket.sin_port = htons(config->port) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000975 fd = socket(AF_INET, SOCK_STREAM, 0);
Glenn L McGrath06e95652003-02-09 06:51:14 +0000976 if (fd >= 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000977 /* tell the OS it's OK to reuse a previous address even though */
978 /* it may still be in a close down state. Allows bind to succeed. */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000979 int on = 1;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000980#ifdef SO_REUSEPORT
Glenn L McGrath06e95652003-02-09 06:51:14 +0000981 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *)&on, sizeof(on)) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000982#else
Glenn L McGrath06e95652003-02-09 06:51:14 +0000983 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)) ;
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000984#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +0000985 if (bind(fd, (struct sockaddr *)&lsocket, sizeof(lsocket)) == 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000986 listen(fd, 9);
987 signal(SIGCHLD, SIG_IGN); /* prevent zombie (defunct) processes */
Glenn L McGrath06e95652003-02-09 06:51:14 +0000988 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000989 bb_perror_msg_and_die("bind");
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000990 }
Glenn L McGrath06e95652003-02-09 06:51:14 +0000991 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000992 bb_perror_msg_and_die("create socket");
Glenn L McGrath06e95652003-02-09 06:51:14 +0000993 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000994 return fd;
995}
Glenn L McGrath06e95652003-02-09 06:51:14 +0000996#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
Glenn L McGrath58c708a2003-01-05 04:01:56 +0000997
998/****************************************************************************
999 *
1000 > $Function: sendHeaders()
1001 *
1002 * $Description: Create and send HTTP response headers.
1003 * The arguments are combined and sent as one write operation. Note that
1004 * IE will puke big-time if the headers are not sent in one packet and the
Glenn L McGrath06e95652003-02-09 06:51:14 +00001005 * second packet is delayed for any reason.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001006 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00001007 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001008 * (HttpResponseNum) responseNum . . . The result code to send.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001009 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00001010 * $Return: (int) . . . . writing errors
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001011 *
1012 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001013static int sendHeaders(HttpResponseNum responseNum)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001014{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001015 char *buf = config->buf;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001016 const char *responseString = "";
1017 const char *infoString = 0;
1018 unsigned int i;
1019 time_t timer = time(0);
1020 char timeStr[80];
Glenn L McGrath06e95652003-02-09 06:51:14 +00001021 int len;
1022
1023 for (i = 0;
1024 i < (sizeof(httpResponseNames)/sizeof(httpResponseNames[0])); i++) {
1025 if (httpResponseNames[i].type == responseNum) {
1026 responseString = httpResponseNames[i].name;
1027 infoString = httpResponseNames[i].info;
1028 break;
1029 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001030 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001031 if (responseNum != HTTP_OK) {
1032 config->found_mime_type = "text/html"; // error message is HTML
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001033 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001034
1035 /* emit the current date */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001036 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&timer));
1037 len = sprintf(buf,
1038 "HTTP/1.0 %d %s\nContent-type: %s\r\n"
1039 "Date: %s\r\nConnection: close\r\n",
1040 responseNum, responseString, config->found_mime_type, timeStr);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001041
Glenn L McGrath3d2405c2003-02-10 22:28:21 +00001042#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath06e95652003-02-09 06:51:14 +00001043 if (responseNum == HTTP_UNAUTHORIZED) {
1044 len += sprintf(buf+len, "WWW-Authenticate: Basic realm=\"%s\"\r\n",
1045 config->realm);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001046 }
Glenn L McGrath3d2405c2003-02-10 22:28:21 +00001047#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001048 if (config->ContentLength != -1) { /* file */
1049 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&config->last_mod));
Glenn L McGrath5cd64612003-08-29 15:53:23 +00001050 len += sprintf(buf+len, "Last-Modified: %s\r\n%s " cont_l_fmt "\r\n",
Glenn L McGrath06e95652003-02-09 06:51:14 +00001051 timeStr, Content_length, config->ContentLength);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001052 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001053 strcat(buf, "\r\n");
1054 len += 2;
1055 if (infoString) {
1056 len += sprintf(buf+len,
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001057 "<HEAD><TITLE>%d %s</TITLE></HEAD>\n"
1058 "<BODY><H1>%d %s</H1>\n%s\n</BODY>\n",
1059 responseNum, responseString,
Glenn L McGrath06e95652003-02-09 06:51:14 +00001060 responseNum, responseString, infoString);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001061 }
1062#ifdef DEBUG
Glenn L McGrath06e95652003-02-09 06:51:14 +00001063 if (config->debugHttpd) fprintf(stderr, "Headers: '%s'", buf);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001064#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +00001065 return bb_full_write(a_c_w, buf, len);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001066}
1067
1068/****************************************************************************
1069 *
1070 > $Function: getLine()
1071 *
1072 * $Description: Read from the socket until an end of line char found.
1073 *
1074 * Characters are read one at a time until an eol sequence is found.
1075 *
1076 * $Parameters:
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001077 * (char *) buf . . Where to place the read result.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001078 *
1079 * $Return: (int) . . . . number of characters read. -1 if error.
1080 *
1081 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001082static int getLine(char *buf)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001083{
1084 int count = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001085
1086 while (read(a_c_r, buf + count, 1) == 1) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001087 if (buf[count] == '\r') continue;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001088 if (buf[count] == '\n') {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001089 buf[count] = 0;
1090 return count;
1091 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001092 if(count < (MAX_MEMORY_BUFF-1)) /* check owerflow */
1093 count++;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001094 }
1095 if (count) return count;
1096 else return -1;
1097}
1098
Glenn L McGrath06e95652003-02-09 06:51:14 +00001099#ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001100/****************************************************************************
1101 *
1102 > $Function: sendCgi()
1103 *
1104 * $Description: Execute a CGI script and send it's stdout back
1105 *
1106 * Environment variables are set up and the script is invoked with pipes
1107 * for stdin/stdout. If a post is being done the script is fed the POST
1108 * data in addition to setting the QUERY_STRING variable (for GETs or POSTs).
1109 *
1110 * $Parameters:
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001111 * (const char *) url . . . The requested URL (with leading /).
1112 * (const char *urlArgs). . Any URL arguments.
1113 * (const char *body) . . . POST body contents.
1114 * (int bodyLen) . . . . . Length of the post body.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001115 * (const char *cookie) . . For set HTTP_COOKIE.
1116
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001117 *
1118 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
1119 *
1120 * $Errors: None
1121 *
1122 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001123static int sendCgi(const char *url,
1124 const char *request, const char *urlArgs,
1125 const char *body, int bodyLen, const char *cookie)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001126{
1127 int fromCgi[2]; /* pipe for reading data from CGI */
1128 int toCgi[2]; /* pipe for sending data to CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001129
Glenn L McGrath06e95652003-02-09 06:51:14 +00001130 static char * argp[] = { 0, 0 };
1131 int pid = 0;
1132 int inFd;
1133 int outFd;
1134 int firstLine = 1;
1135
1136 do {
1137 if (pipe(fromCgi) != 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001138 break;
1139 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001140 if (pipe(toCgi) != 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001141 break;
1142 }
1143
1144 pid = fork();
Glenn L McGrath06e95652003-02-09 06:51:14 +00001145 if (pid < 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001146 pid = 0;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001147 break;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001148 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001149
1150 if (!pid) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001151 /* child process */
1152 char *script;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001153 char *purl = strdup( url );
1154 char realpath_buff[MAXPATHLEN];
1155
1156 if(purl == NULL)
1157 _exit(242);
1158
1159 inFd = toCgi[0];
1160 outFd = fromCgi[1];
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001161
1162 dup2(inFd, 0); // replace stdin with the pipe
1163 dup2(outFd, 1); // replace stdout with the pipe
Glenn L McGrath06e95652003-02-09 06:51:14 +00001164
1165#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1166 if (!config->debugHttpd)
1167#endif
1168 dup2(outFd, 2); // replace stderr with the pipe
1169
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001170 close(toCgi[0]);
1171 close(toCgi[1]);
1172 close(fromCgi[0]);
1173 close(fromCgi[1]);
1174
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001175 /*
Glenn L McGrath06e95652003-02-09 06:51:14 +00001176 * Find PATH_INFO.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001177 */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001178 script = purl;
1179 while((script = strchr( script + 1, '/' )) != NULL) {
1180 /* have script.cgi/PATH_INFO or dirs/script.cgi[/PATH_INFO] */
1181 struct stat sb;
1182
1183 *script = '\0';
1184 if(is_directory(purl + 1, 1, &sb) == 0) {
1185 /* not directory, found script.cgi/PATH_INFO */
1186 *script = '/';
1187 break;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001188 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001189 *script = '/'; /* is directory, find next '/' */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001190 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001191 addEnv("PATH", "INFO", script); /* set /PATH_INFO or NULL */
1192 addEnv("PATH", "", getenv("PATH"));
1193 addEnv("REQUEST", "METHOD", request);
1194 if(urlArgs) {
1195 char *uri = alloca(strlen(purl) + 2 + strlen(urlArgs));
1196 if(uri)
1197 sprintf(uri, "%s?%s", purl, urlArgs);
1198 addEnv("REQUEST", "URI", uri);
1199 } else {
1200 addEnv("REQUEST", "URI", purl);
1201 }
1202 if(script != NULL)
1203 *script = '\0'; /* reduce /PATH_INFO */
1204 /* set SCRIPT_NAME as full path: /cgi-bin/dirs/script.cgi */
1205 addEnv("SCRIPT_NAME", "", purl);
1206 addEnv("QUERY_STRING", "", urlArgs);
1207 addEnv("SERVER", "SOFTWARE", httpdVersion);
1208 addEnv("SERVER", "PROTOCOL", "HTTP/1.0");
1209 addEnv("GATEWAY_INTERFACE", "", "CGI/1.1");
1210#ifdef CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001211 addEnv("REMOTE", "ADDR", config->rmt_ip_str);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001212 addEnvPort("REMOTE");
1213#else
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001214 addEnv("REMOTE_ADDR", "", config->rmt_ip_str);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001215#endif
1216 if(bodyLen) {
1217 char sbl[32];
1218
1219 sprintf(sbl, "%d", bodyLen);
1220 addEnv("CONTENT_LENGTH", "", sbl);
1221 }
1222 if(cookie)
1223 addEnv("HTTP_COOKIE", "", cookie);
1224
1225#ifdef CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV
1226 if (request != request_GET) {
1227 addEnvCgi(body);
1228 } else {
1229 addEnvCgi(urlArgs);
1230 }
1231#endif
1232
1233 /* set execve argp[0] without path */
1234 argp[0] = strrchr( purl, '/' ) + 1;
1235 /* but script argp[0] must have absolute path and chdiring to this */
1236 if(realpath(purl + 1, realpath_buff) != NULL) {
1237 script = strrchr(realpath_buff, '/');
1238 if(script) {
1239 *script = '\0';
1240 if(chdir(realpath_buff) == 0) {
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00001241 *script = '/';
1242 // now run the program. If it fails,
1243 // use _exit() so no destructors
1244 // get called and make a mess.
1245 execve(realpath_buff, argp, config->envp);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001246 }
1247 }
1248 }
1249#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1250 config->accepted_socket = 1; /* send to stdout */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001251#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001252 sendHeaders(HTTP_NOT_FOUND);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001253 _exit(242);
1254 } /* end child */
1255
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001256 } while (0);
1257
Glenn L McGrath06e95652003-02-09 06:51:14 +00001258 if (pid) {
1259 /* parent process */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001260 int status;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001261
1262 inFd = fromCgi[0];
1263 outFd = toCgi[1];
1264 close(fromCgi[1]);
1265 close(toCgi[0]);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001266 if (body) bb_full_write(outFd, body, bodyLen);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001267 close(outFd);
1268
1269 while (1) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001270 struct timeval timeout;
1271 fd_set readSet;
1272 char buf[160];
1273 int nfound;
1274 int count;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001275
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001276 FD_ZERO(&readSet);
1277 FD_SET(inFd, &readSet);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001278
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001279 /* Now wait on the set of sockets! */
1280 timeout.tv_sec = 0;
1281 timeout.tv_usec = 10000;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001282 nfound = select(inFd + 1, &readSet, 0, 0, &timeout);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001283
Glenn L McGrath06e95652003-02-09 06:51:14 +00001284 if (nfound <= 0) {
1285 if (waitpid(pid, &status, WNOHANG) > 0) {
1286 close(inFd);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001287#ifdef DEBUG
Glenn L McGrath06e95652003-02-09 06:51:14 +00001288 if (config->debugHttpd) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001289 if (WIFEXITED(status))
Manuel Novoa III cad53642003-03-19 09:13:01 +00001290 bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001291 if (WIFSIGNALED(status))
Manuel Novoa III cad53642003-03-19 09:13:01 +00001292 bb_error_msg("piped has exited with signal=%d", WTERMSIG(status));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001293 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001294#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001295 pid = -1;
1296 break;
1297 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001298 } else {
1299 int s = a_c_w;
1300
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001301 // There is something to read
Manuel Novoa III cad53642003-03-19 09:13:01 +00001302 count = bb_full_read(inFd, buf, sizeof(buf)-1);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001303 // If a read returns 0 at this point then some type of error has
1304 // occurred. Bail now.
1305 if (count == 0) break;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001306 if (count > 0) {
1307 if (firstLine) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001308 /* check to see if the user script added headers */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001309 if (strncmp(buf, "HTTP/1.0 200 OK\n", 4) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001310 bb_full_write(s, "HTTP/1.0 200 OK\n", 16);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001311 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001312 if (strstr(buf, "ontent-") == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001313 bb_full_write(s, "Content-type: text/plain\n\n", 26);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001314 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001315 firstLine=0;
1316 }
Manuel Novoa III cad53642003-03-19 09:13:01 +00001317 bb_full_write(s, buf, count);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001318#ifdef DEBUG
Glenn L McGrath06e95652003-02-09 06:51:14 +00001319 if (config->debugHttpd)
1320 fprintf(stderr, "cgi read %d bytes\n", count);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001321#endif
1322 }
1323 }
1324 }
1325 }
1326 return 0;
1327}
Glenn L McGrath06e95652003-02-09 06:51:14 +00001328#endif /* CONFIG_FEATURE_HTTPD_CGI */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001329
1330/****************************************************************************
1331 *
1332 > $Function: sendFile()
1333 *
1334 * $Description: Send a file response to an HTTP request
1335 *
Glenn L McGrath06e95652003-02-09 06:51:14 +00001336 * $Parameter:
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001337 * (const char *) url . . The URL requested.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001338 * (char *) buf . . . . . The stack buffer.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001339 *
1340 * $Return: (int) . . . . . . Always 0.
1341 *
1342 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001343static int sendFile(const char *url, char *buf)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001344{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001345 char * suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001346 int f;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001347 const char * const * table;
1348 const char * try_suffix;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001349
Glenn L McGrath06e95652003-02-09 06:51:14 +00001350 suffix = strrchr(url, '.');
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001351
Glenn L McGrath06e95652003-02-09 06:51:14 +00001352 for (table = suffixTable; *table; table += 2)
1353 if(suffix != NULL && (try_suffix = strstr(*table, suffix)) != 0) {
1354 try_suffix += strlen(suffix);
1355 if(*try_suffix == 0 || *try_suffix == '.')
1356 break;
1357 }
1358 /* also, if not found, set default as "application/octet-stream"; */
1359 config->found_mime_type = *(table+1);
1360#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
1361 if (suffix) {
1362 Htaccess * cur;
1363
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001364 for (cur = config->mime_a; cur; cur = cur->next) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001365 if(strcmp(cur->before_colon, suffix) == 0) {
1366 config->found_mime_type = cur->after_colon;
1367 break;
1368 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001369 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001370 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001371#endif /* CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES */
1372
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001373#ifdef DEBUG
Glenn L McGrath06e95652003-02-09 06:51:14 +00001374 if (config->debugHttpd)
1375 fprintf(stderr, "Sending file '%s' Content-type: %s\n",
1376 url, config->found_mime_type);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001377#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001378
1379 f = open(url, O_RDONLY);
1380 if (f >= 0) {
1381 int count;
1382
1383 sendHeaders(HTTP_OK);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001384 while ((count = bb_full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
1385 bb_full_write(a_c_w, buf, count);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001386 }
1387 close(f);
1388 } else {
1389#ifdef DEBUG
1390 if (config->debugHttpd)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001391 bb_perror_msg("Unable to open '%s'", url);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001392#endif
1393 sendHeaders(HTTP_NOT_FOUND);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001394 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001395
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001396 return 0;
1397}
1398
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001399static int checkPermIP(void)
1400{
1401 Htaccess_IP * cur;
1402
1403 /* This could stand some work */
1404 for (cur = config->ip_a_d; cur; cur = cur->next) {
1405#ifdef DEBUG
1406 if (config->debugHttpd) {
1407 fprintf(stderr, "checkPermIP: '%s' ? ", config->rmt_ip_str);
1408 fprintf(stderr, "'%u.%u.%u.%u/%u.%u.%u.%u'\n",
1409 (unsigned char)(cur->ip >> 24),
1410 (unsigned char)(cur->ip >> 16),
1411 (unsigned char)(cur->ip >> 8),
1412 cur->ip & 0xff,
1413 (unsigned char)(cur->mask >> 24),
1414 (unsigned char)(cur->mask >> 16),
1415 (unsigned char)(cur->mask >> 8),
1416 cur->mask & 0xff);
1417 }
1418#endif
1419 if((config->rmt_ip & cur->mask) == cur->ip)
1420 return cur->allow_deny == 'A'; /* Allow/Deny */
1421 }
1422
1423 /* if uncofigured, return 1 - access from all */
1424 return !config->flg_deny_all;
1425}
1426
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001427/****************************************************************************
1428 *
1429 > $Function: checkPerm()
1430 *
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001431 * $Description: Check the permission file for access password protected.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001432 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001433 * If config file isn't present, everything is allowed.
Glenn L McGrath06e95652003-02-09 06:51:14 +00001434 * Entries are of the form you can see example from header source
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001435 *
1436 * $Parameters:
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001437 * (const char *) path . . . . The file path.
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001438 * (const char *) request . . . User information to validate.
1439 *
1440 * $Return: (int) . . . . . . . . . 1 if request OK, 0 otherwise.
1441 *
1442 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001443
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001444#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001445static int checkPerm(const char *path, const char *request)
1446{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001447 Htaccess * cur;
1448 const char *p;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001449 const char *p0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001450
Glenn L McGrath06e95652003-02-09 06:51:14 +00001451 const char *prev = NULL;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001452
1453 /* This could stand some work */
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001454 for (cur = config->auth; cur; cur = cur->next) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001455 p0 = cur->before_colon;
1456 if(prev != NULL && strcmp(prev, p0) != 0)
1457 continue; /* find next identical */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001458 p = cur->after_colon;
1459#ifdef DEBUG
1460 if (config->debugHttpd)
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001461 fprintf(stderr,"checkPerm: '%s' ? '%s'\n", p0, request);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001462#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001463 {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001464 int l = strlen(p0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001465
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001466 if(strncmp(p0, path, l) == 0 &&
1467 (l == 1 || path[l] == '/' || path[l] == 0)) {
1468 /* path match found. Check request */
Eric Andersen35e643b2003-07-28 07:40:39 +00001469
1470 /* for check next /path:user:password */
1471 prev = p0;
1472#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
1473 {
1474 char *cipher;
1475 char *pp;
1476 char *u = strchr(request, ':');
1477
1478 if(u == NULL) {
1479 /* bad request, ':' required */
1480 continue;
1481 }
1482 if(strncmp(p, request, u-request) != 0) {
1483 /* user uncompared */
1484 continue;
1485 }
1486 pp = strchr(p, ':');
1487 if(pp && pp[1] == '$' && pp[2] == '1' &&
1488 pp[3] == '$' && pp[4]) {
1489 pp++;
1490 cipher = pw_encrypt(u+1, pp);
1491 if (strcmp(cipher, pp) == 0)
1492 return 1; /* Ok */
1493 /* unauthorized */
1494 continue;
1495 }
1496 }
1497#endif
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001498 if (strcmp(p, request) == 0)
1499 return 1; /* Ok */
Eric Andersen35e643b2003-07-28 07:40:39 +00001500 /* unauthorized */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001501 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001502 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001503 } /* for */
1504
Glenn L McGrath06e95652003-02-09 06:51:14 +00001505 return prev == NULL;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001506}
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001507
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001508#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1509
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001510
1511/****************************************************************************
1512 *
1513 > $Function: handleIncoming()
1514 *
1515 * $Description: Handle an incoming http request.
1516 *
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001517 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001518static void handleIncoming(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001519{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001520 char *buf = config->buf;
1521 char *url;
1522 char *purl;
1523 int blank = -1;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001524 char *urlArgs;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001525#ifdef CONFIG_FEATURE_HTTPD_CGI
1526 const char *prequest = request_GET;
1527 char *body = 0;
1528 long length=0;
1529 char *cookie = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001530#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001531 char *test;
1532 struct stat sb;
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001533 int ip_allowed;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001534
1535#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1536 int credentials = -1; /* if not requred this is Ok */
1537#endif
1538
1539 do {
1540 int count;
1541
1542 if (getLine(buf) <= 0)
1543 break; /* closed */
1544
1545 purl = strpbrk(buf, " \t");
1546 if(purl == NULL) {
1547BAD_REQUEST:
1548 sendHeaders(HTTP_BAD_REQUEST);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001549 break;
1550 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001551 *purl = 0;
1552#ifdef CONFIG_FEATURE_HTTPD_CGI
1553 if(strcasecmp(buf, prequest) != 0) {
1554 prequest = "POST";
1555 if(strcasecmp(buf, prequest) != 0) {
1556 sendHeaders(HTTP_NOT_IMPLEMENTED);
1557 break;
1558 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001559 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001560#else
1561 if(strcasecmp(buf, request_GET) != 0) {
1562 sendHeaders(HTTP_NOT_IMPLEMENTED);
1563 break;
1564 }
1565#endif
1566 *purl = ' ';
1567 count = sscanf(purl, " %[^ ] HTTP/%d.%*d", buf, &blank);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001568
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001569 decodeString(buf, 0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001570 if (count < 1 || buf[0] != '/') {
1571 /* Garbled request/URL */
1572 goto BAD_REQUEST;
1573 }
1574 url = alloca(strlen(buf) + 12); /* + sizeof("/index.html\0") */
1575 if(url == NULL) {
1576 sendHeaders(HTTP_INTERNAL_SERVER_ERROR);
1577 break;
1578 }
1579 strcpy(url, buf);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001580 /* extract url args if present */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001581 urlArgs = strchr(url, '?');
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001582 if (urlArgs)
1583 *urlArgs++ = 0;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001584
Manuel Novoa III cad53642003-03-19 09:13:01 +00001585 /* algorithm stolen from libbb bb_simplify_path(),
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001586 but don`t strdup and reducing trailing slash and protect out root */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001587 purl = test = url;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001588
Glenn L McGrath06e95652003-02-09 06:51:14 +00001589 do {
1590 if (*purl == '/') {
1591 if (*test == '/') { /* skip duplicate (or initial) slash */
1592 continue;
1593 } else if (*test == '.') {
1594 if (test[1] == '/' || test[1] == 0) { /* skip extra '.' */
1595 continue;
1596 } else if ((test[1] == '.') && (test[2] == '/' || test[2] == 0)) {
1597 ++test;
1598 if (purl == url) {
1599 /* protect out root */
1600 goto BAD_REQUEST;
1601 }
1602 while (*--purl != '/'); /* omit previous dir */
1603 continue;
1604 }
1605 }
1606 }
1607 *++purl = *test;
1608 } while (*++test);
1609
1610 *++purl = 0; /* so keep last character */
1611 test = purl; /* end ptr */
1612
1613 /* If URL is directory, adding '/' */
1614 if(test[-1] != '/') {
1615 if ( is_directory(url + 1, 1, &sb) ) {
1616 *test++ = '/';
1617 *test = 0;
1618 purl = test; /* end ptr */
1619 }
1620 }
1621#ifdef DEBUG
1622 if (config->debugHttpd)
1623 fprintf(stderr, "url='%s', args=%s\n", url, urlArgs);
1624#endif
1625
1626 test = url;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001627 ip_allowed = checkPermIP();
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001628 while(ip_allowed && (test = strchr( test + 1, '/' )) != NULL) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001629 /* have path1/path2 */
1630 *test = '\0';
1631 if( is_directory(url + 1, 1, &sb) ) {
1632 /* may be having subdir config */
1633 parse_conf(url + 1, SUBDIR_PARSE);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001634 ip_allowed = checkPermIP();
Glenn L McGrath06e95652003-02-09 06:51:14 +00001635 }
1636 *test = '/';
1637 }
1638
1639 // read until blank line for HTTP version specified, else parse immediate
1640 while (blank >= 0 && (count = getLine(buf)) > 0) {
1641
1642#ifdef DEBUG
1643 if (config->debugHttpd) fprintf(stderr, "Header: '%s'\n", buf);
1644#endif
1645
1646#ifdef CONFIG_FEATURE_HTTPD_CGI
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001647 /* try and do our best to parse more lines */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001648 if ((strncasecmp(buf, Content_length, 15) == 0)) {
1649 if(prequest != request_GET)
1650 length = strtol(buf + 15, 0, 0); // extra read only for POST
1651 } else if ((strncasecmp(buf, "Cookie:", 7) == 0)) {
1652 for(test = buf + 7; isspace(*test); test++)
1653 ;
1654 cookie = strdup(test);
1655 }
1656#endif
1657
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001658#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Glenn L McGrath06e95652003-02-09 06:51:14 +00001659 if (strncasecmp(buf, "Authorization:", 14) == 0) {
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001660 /* We only allow Basic credentials.
1661 * It shows up as "Authorization: Basic <userid:password>" where
1662 * the userid:password is base64 encoded.
1663 */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001664 for(test = buf + 14; isspace(*test); test++)
1665 ;
1666 if (strncasecmp(test, "Basic", 5) != 0)
1667 continue;
1668
1669 test += 5; /* decodeBase64() skiping space self */
1670 decodeBase64(test);
1671 credentials = checkPerm(url, test);
1672 }
1673#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1674
1675 } /* while extra header reading */
1676
1677
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001678 if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001679 /* protect listing [/path]/httpd_conf or IP deny */
1680#ifdef CONFIG_FEATURE_HTTPD_CGI
1681FORBIDDEN: /* protect listing /cgi-bin */
1682#endif
1683 sendHeaders(HTTP_FORBIDDEN);
1684 break;
1685 }
1686
1687#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1688 if (credentials <= 0 && checkPerm(url, ":") == 0) {
1689 sendHeaders(HTTP_UNAUTHORIZED);
1690 break;
1691 }
1692#endif
1693
1694 test = url + 1; /* skip first '/' */
1695
1696#ifdef CONFIG_FEATURE_HTTPD_CGI
1697 /* if strange Content-Length */
1698 if (length < 0 || length > MAX_POST_SIZE)
1699 break;
1700
1701 if (length > 0) {
1702 body = malloc(length + 1);
1703 if (body) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001704 length = bb_full_read(a_c_r, body, length);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001705 if(length < 0) // closed
1706 length = 0;
1707 body[length] = 0; // always null terminate for safety
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001708 }
1709 }
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001710
Glenn L McGrath06e95652003-02-09 06:51:14 +00001711 if (strncmp(test, "cgi-bin", 7) == 0) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001712 if(test[7] == '/' && test[8] == 0)
1713 goto FORBIDDEN; // protect listing cgi-bin/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001714 sendCgi(url, prequest, urlArgs, body, length, cookie);
1715 } else {
1716 if (prequest != request_GET)
1717 sendHeaders(HTTP_NOT_IMPLEMENTED);
1718 else {
1719#endif /* CONFIG_FEATURE_HTTPD_CGI */
1720 if(purl[-1] == '/')
1721 strcpy(purl, "index.html");
1722 if ( stat(test, &sb ) == 0 ) {
1723 config->ContentLength = sb.st_size;
1724 config->last_mod = sb.st_mtime;
1725 }
1726 sendFile(test, buf);
1727#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1728 /* unset if non inetd looped */
1729 config->ContentLength = -1;
1730#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001731
Glenn L McGrath06e95652003-02-09 06:51:14 +00001732#ifdef CONFIG_FEATURE_HTTPD_CGI
1733 }
1734 }
1735#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001736
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001737 } while (0);
1738
Glenn L McGrath06e95652003-02-09 06:51:14 +00001739
1740#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1741/* from inetd don`t looping: freeing, closing automatic from exit always */
1742# ifdef DEBUG
1743 if (config->debugHttpd) fprintf(stderr, "closing socket\n");
1744# endif
1745# ifdef CONFIG_FEATURE_HTTPD_CGI
1746 free(body);
1747 free(cookie);
1748# endif
1749 shutdown(a_c_w, SHUT_WR);
1750 shutdown(a_c_r, SHUT_RD);
1751 close(config->accepted_socket);
1752#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001753}
1754
1755/****************************************************************************
1756 *
1757 > $Function: miniHttpd()
1758 *
1759 * $Description: The main http server function.
1760 *
1761 * Given an open socket fildes, listen for new connections and farm out
1762 * the processing as a forked process.
1763 *
1764 * $Parameters:
1765 * (int) server. . . The server socket fildes.
1766 *
1767 * $Return: (int) . . . . Always 0.
1768 *
1769 ****************************************************************************/
Glenn L McGrath06e95652003-02-09 06:51:14 +00001770#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001771static int miniHttpd(int server)
1772{
1773 fd_set readfd, portfd;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001774
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001775 FD_ZERO(&portfd);
1776 FD_SET(server, &portfd);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001777
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001778 /* copy the ports we are watching to the readfd set */
Glenn L McGrath06e95652003-02-09 06:51:14 +00001779 while (1) {
1780 readfd = portfd;
1781
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001782 /* Now wait INDEFINATELY on the set of sockets! */
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001783 if (select(server + 1, &readfd, 0, 0, 0) > 0) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001784 if (FD_ISSET(server, &readfd)) {
1785 int on;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001786 struct sockaddr_in fromAddr;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001787
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001788 socklen_t fromAddrLen = sizeof(fromAddr);
1789 int s = accept(server,
Glenn L McGrath06e95652003-02-09 06:51:14 +00001790 (struct sockaddr *)&fromAddr, &fromAddrLen);
1791
1792 if (s < 0) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001793 continue;
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001794 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001795 config->accepted_socket = s;
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001796 config->rmt_ip = ntohl(fromAddr.sin_addr.s_addr);
1797#if defined(CONFIG_FEATURE_HTTPD_CGI) || defined(DEBUG)
1798 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1799 (unsigned char)(config->rmt_ip >> 24),
1800 (unsigned char)(config->rmt_ip >> 16),
1801 (unsigned char)(config->rmt_ip >> 8),
1802 config->rmt_ip & 0xff);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001803 config->port = ntohs(fromAddr.sin_port);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001804#ifdef DEBUG
Glenn L McGrath06e95652003-02-09 06:51:14 +00001805 if (config->debugHttpd) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001806 bb_error_msg("connection from IP=%s, port %u\n",
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001807 config->rmt_ip_str, config->port);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001808 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001809#endif
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001810#endif /* CONFIG_FEATURE_HTTPD_CGI */
1811
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001812 /* set the KEEPALIVE option to cull dead connections */
1813 on = 1;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001814 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof (on));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001815
Glenn L McGrath06e95652003-02-09 06:51:14 +00001816 if (config->debugHttpd || fork() == 0) {
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00001817 /* This is the spawned thread */
1818#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
1819 /* protect reload config, may be confuse checking */
1820 signal(SIGHUP, SIG_IGN);
1821#endif
1822 handleIncoming();
1823 if(!config->debugHttpd)
Glenn L McGrath06e95652003-02-09 06:51:14 +00001824 exit(0);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001825 }
1826 close(s);
1827 }
1828 }
1829 } // while (1)
1830 return 0;
1831}
1832
Glenn L McGrath06e95652003-02-09 06:51:14 +00001833#else
1834 /* from inetd */
1835
1836static int miniHttpd(void)
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001837{
Glenn L McGrath06e95652003-02-09 06:51:14 +00001838 struct sockaddr_in fromAddrLen;
1839 socklen_t sinlen = sizeof (struct sockaddr_in);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001840
1841 getpeername (0, (struct sockaddr *)&fromAddrLen, &sinlen);
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001842 config->rmt_ip = ntohl(fromAddrLen.sin_addr.s_addr);
1843#if defined(CONFIG_FEATURE_HTTPD_CGI) || defined(DEBUG)
1844 sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
1845 (unsigned char)(config->rmt_ip >> 24),
1846 (unsigned char)(config->rmt_ip >> 16),
1847 (unsigned char)(config->rmt_ip >> 8),
1848 config->rmt_ip & 0xff);
1849#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001850 config->port = ntohs(fromAddrLen.sin_port);
1851 handleIncoming();
1852 return 0;
1853}
1854#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
1855
1856#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
1857static void sighup_handler(int sig)
1858{
1859 /* set and reset */
1860 struct sigaction sa;
1861
Glenn L McGrathb65422c2003-09-08 10:59:27 +00001862 parse_conf(default_path_httpd_conf,
1863 sig == SIGHUP ? SIGNALED_PARSE : FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001864 sa.sa_handler = sighup_handler;
1865 sigemptyset(&sa.sa_mask);
1866 sa.sa_flags = SA_RESTART;
1867 sigaction(SIGHUP, &sa, NULL);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001868}
1869#endif
1870
Eric Andersena3bb3e62003-06-26 09:05:32 +00001871
1872static const char httpd_opts[]="c:d:h:"
1873#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
1874 "e:"
1875#define OPT_INC_1 1
1876#else
1877#define OPT_INC_1 0
1878#endif
1879#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1880 "r:"
Eric Andersen35e643b2003-07-28 07:40:39 +00001881# ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
1882 "m:"
1883# define OPT_INC_2 2
1884# else
1885# define OPT_INC_2 1
1886#endif
Eric Andersena3bb3e62003-06-26 09:05:32 +00001887#else
1888#define OPT_INC_2 0
1889#endif
1890#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1891 "p:v"
1892#ifdef CONFIG_FEATURE_HTTPD_SETUID
1893 "u:"
1894#endif
Eric Andersen35e643b2003-07-28 07:40:39 +00001895#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
Eric Andersena3bb3e62003-06-26 09:05:32 +00001896 ;
1897
1898#define OPT_CONFIG_FILE (1<<0)
1899#define OPT_DECODE_URL (1<<1)
1900#define OPT_HOME_HTTPD (1<<2)
1901#define OPT_ENCODE_URL (1<<(2+OPT_INC_1))
Eric Andersen35e643b2003-07-28 07:40:39 +00001902#define OPT_REALM (1<<(3+OPT_INC_1))
1903#define OPT_MD5 (1<<(4+OPT_INC_1))
Eric Andersena3bb3e62003-06-26 09:05:32 +00001904#define OPT_PORT (1<<(3+OPT_INC_1+OPT_INC_2))
1905#define OPT_DEBUG (1<<(4+OPT_INC_1+OPT_INC_2))
1906#define OPT_SETUID (1<<(5+OPT_INC_1+OPT_INC_2))
1907
1908
Glenn L McGrath06e95652003-02-09 06:51:14 +00001909#ifdef HTTPD_STANDALONE
1910int main(int argc, char *argv[])
1911#else
1912int httpd_main(int argc, char *argv[])
1913#endif
1914{
Eric Andersena3bb3e62003-06-26 09:05:32 +00001915 unsigned long opt;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001916 const char *home_httpd = home;
Eric Andersena3bb3e62003-06-26 09:05:32 +00001917 char *url_for_decode;
1918#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
1919 const char *url_for_encode;
1920#endif
1921#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1922 const char *s_port;
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00001923#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001924
1925#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001926 int server;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001927#endif
1928
1929#ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersena3bb3e62003-06-26 09:05:32 +00001930 const char *s_uid;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001931 long uid = -1;
1932#endif
1933
Eric Andersen35e643b2003-07-28 07:40:39 +00001934#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
1935 const char *pass;
1936#endif
1937
Glenn L McGrath06e95652003-02-09 06:51:14 +00001938 config = xcalloc(1, sizeof(*config));
1939#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1940 config->realm = "Web Server Authentication";
1941#endif
1942
1943#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1944 config->port = 80;
1945#endif
1946
1947 config->ContentLength = -1;
1948
Eric Andersena3bb3e62003-06-26 09:05:32 +00001949 opt = bb_getopt_ulflags(argc, argv, httpd_opts,
1950 &(config->configFile), &url_for_decode, &home_httpd
Glenn L McGrath06e95652003-02-09 06:51:14 +00001951#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Eric Andersena3bb3e62003-06-26 09:05:32 +00001952 , &url_for_encode
Glenn L McGrath06e95652003-02-09 06:51:14 +00001953#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001954#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
Eric Andersena3bb3e62003-06-26 09:05:32 +00001955 , &(config->realm)
Eric Andersen35e643b2003-07-28 07:40:39 +00001956# ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
1957 , &pass
1958# endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00001959#endif
Eric Andersena3bb3e62003-06-26 09:05:32 +00001960#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1961 , &s_port
Glenn L McGrath06e95652003-02-09 06:51:14 +00001962#ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersena3bb3e62003-06-26 09:05:32 +00001963 , &s_uid
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001964#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00001965#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001966 );
Eric Andersena3bb3e62003-06-26 09:05:32 +00001967
1968 if(opt & OPT_DECODE_URL) {
1969 printf("%s", decodeString(url_for_decode, 1));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001970 return 0;
Eric Andersena3bb3e62003-06-26 09:05:32 +00001971 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001972#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
Eric Andersena3bb3e62003-06-26 09:05:32 +00001973 if(opt & OPT_ENCODE_URL) {
1974 printf("%s", encodeString(url_for_encode));
Glenn L McGrath58c708a2003-01-05 04:01:56 +00001975 return 0;
Eric Andersena3bb3e62003-06-26 09:05:32 +00001976 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001977#endif
Eric Andersen35e643b2003-07-28 07:40:39 +00001978#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
1979 if(opt & OPT_MD5) {
1980 printf("%s\n", pw_encrypt(pass, "$1$"));
1981 return 0;
1982 }
1983#endif
Eric Andersena3bb3e62003-06-26 09:05:32 +00001984#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1985 if(opt & OPT_PORT)
1986 config->port = bb_xgetlarg(s_port, 10, 1, 0xffff);
1987 config->debugHttpd = opt & OPT_DEBUG;
Glenn L McGrath06e95652003-02-09 06:51:14 +00001988#ifdef CONFIG_FEATURE_HTTPD_SETUID
Eric Andersena3bb3e62003-06-26 09:05:32 +00001989 if(opt & OPT_SETUID) {
Glenn L McGrath06e95652003-02-09 06:51:14 +00001990 char *e;
1991
Eric Andersena3bb3e62003-06-26 09:05:32 +00001992 uid = strtol(s_uid, &e, 0);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001993 if(*e != '\0') {
1994 /* not integer */
Eric Andersena3bb3e62003-06-26 09:05:32 +00001995 uid = my_getpwnam(s_uid);
Glenn L McGrath06e95652003-02-09 06:51:14 +00001996 }
1997 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00001998#endif
Glenn L McGrath4fe3ff82003-05-19 05:56:16 +00001999#endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002000
Glenn L McGrath06e95652003-02-09 06:51:14 +00002001 if(chdir(home_httpd)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002002 bb_perror_msg_and_die("can`t chdir to %s", home_httpd);
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002003 }
Glenn L McGrath06e95652003-02-09 06:51:14 +00002004#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2005 server = openServer();
2006# ifdef CONFIG_FEATURE_HTTPD_SETUID
2007 /* drop privilegies */
2008 if(uid > 0)
2009 setuid(uid);
2010# endif
2011# ifdef CONFIG_FEATURE_HTTPD_CGI
2012 addEnvPort("SERVER");
2013# endif
Glenn L McGrath58c708a2003-01-05 04:01:56 +00002014#endif
Glenn L McGrath06e95652003-02-09 06:51:14 +00002015
2016#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
2017 sighup_handler(0);
2018#else
Glenn L McGrathc9163fe2003-05-13 16:20:11 +00002019 parse_conf(default_path_httpd_conf, FIRST_PARSE);
Glenn L McGrath06e95652003-02-09 06:51:14 +00002020#endif
2021
2022#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
2023 if (!config->debugHttpd) {
2024 if (daemon(1, 0) < 0) /* don`t change curent directory */
Manuel Novoa III cad53642003-03-19 09:13:01 +00002025 bb_perror_msg_and_die("daemon");
Glenn L McGrath06e95652003-02-09 06:51:14 +00002026 }
2027 return miniHttpd(server);
2028#else
2029 return miniHttpd();
2030#endif
2031}