2 * httpd implementation for busybox
4 * Copyright (C) 2002,2003 Glenn Engel <glenne@engel.org>
5 * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
7 * simplify patch stolen from libbb without using strdup
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.
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.
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
23 *****************************************************************************
27 * httpd -p 8080 -h $HOME/public_html
28 * or for daemon start from rc script with uid=0:
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"
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
39 * the $CGI_foo environment variable to "Hello World" while
40 * CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV enabled.
42 * The server can also be invoked as a url arg decoder and html text encoder
44 * foo=`httpd -d $foo` # decode "Hello%20World" as "Hello World"
45 * bar=`httpd -e "<Hello World>"` # encode as "%3CHello%20World%3E"
47 * httpd.conf has the following format:
49 A:172.20. # Allow any address that begins with 172.20
50 A:10.10. # Allow any address that begins with 10.10.
51 A:10.10 # Allow any address that previous set and 10.100-109.X.X
52 A:127.0.0.1 # Allow local loopback connections
53 D:* # Deny from other IP connections
54 /cgi-bin:foo:bar # Require user foo, pwd bar on urls starting with /cgi-bin/
55 /adm:admin:setup # Require user admin, pwd setup on urls starting with /adm/
56 /adm:toor:PaSsWd # or user toor, pwd PaSsWd on urls starting with /adm/
57 .au:audio/basic # additional mime type for audio.au files
59 A shortes path and D:from[^*] automaticaly sorting to top.
60 All longest path can`t reset user:password if shorted protect setted.
62 A/D may be as a/d or allow/deny - first char case unsensitive parsed only.
64 Each subdir can have config file.
65 For protect as user:pass current subdir and subpathes set from subdir config:
67 if not, other subpathes for give effect must have path from httpd root
68 /current_subdir_path_from_httpd_root/subpath:user:pass
70 The Deny/Allow IP logic:
73 The config don`t set D: lines
75 2. Allow from setted only:
76 see the begin format example
78 3. Set deny, allow from other:
79 D:1.2.3. # deny from 1.2.3.0 - 1.2.3.255
80 D:2.3.4. # deny from 2.3.4.0 - 2.3.4.255
81 A:* # allow from other, this line not strongly require
83 A global and subdirs config merging logic:
84 allow rules reducing, deny lines strongled.
85 The algorithm combinations:
87 4. If current config have
90 subdir config A: lines skiping, D:from - moving top
92 5. If current config have
99 and seting D:* if subdir config have this
101 If -c don`t setted, used httpd root config, else httpd root config skiped.
102 Exited with fault if can`t open start config.
103 For set wide open server, use -c /dev/null ;=)
107 #include <ctype.h> /* for isspace */
109 #include <stdlib.h> /* for malloc */
111 #include <unistd.h> /* for close */
113 #include <sys/types.h>
114 #include <sys/socket.h> /* for connect and socket*/
115 #include <netinet/in.h> /* for sockaddr_in */
116 #include <sys/time.h>
117 #include <sys/stat.h>
118 #include <sys/wait.h>
119 #include <fcntl.h> /* for open modes */
123 static const char httpdVersion[] = "busybox httpd/1.20 31-Jan-2003";
124 static const char default_patch_httpd_conf[] = "/etc";
125 static const char httpd_conf[] = "httpd.conf";
126 static const char home[] = "/www";
128 // Note: xfuncs are not used because we want the server to keep running
129 // if something bad happens due to a malformed user request.
130 // As a result, all memory allocation after daemonize
131 // is checked rigorously
135 /* Configure options, disabled by default as nonstandart httpd feature */
136 //#define CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV
137 //#define CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
138 //#define CONFIG_FEATURE_HTTPD_DECODE_URL_STR
140 /* disabled as not necessary feature */
141 //#define CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
142 //#define CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
143 //#define CONFIG_FEATURE_HTTPD_SETUID
144 //#define CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
146 /* If seted this you can use this server from internet superserver only */
147 //#define CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
149 /* You can use this server as standalone, require libbb.a for linking */
150 //#define HTTPD_STANDALONE
152 /* Config options, disable this for do very small module */
153 //#define CONFIG_FEATURE_HTTPD_CGI
154 //#define CONFIG_FEATURE_HTTPD_BASIC_AUTH
156 #ifdef HTTPD_STANDALONE
157 /* standalone, enable all features */
158 #undef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
159 /* unset config option for remove warning as redefined */
160 #undef CONFIG_FEATURE_HTTPD_BASIC_AUTH
161 #undef CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV
162 #undef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
163 #undef CONFIG_FEATURE_HTTPD_DECODE_URL_STR
164 #undef CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
165 #undef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
166 #undef CONFIG_FEATURE_HTTPD_CGI
167 #undef CONFIG_FEATURE_HTTPD_SETUID
168 #undef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
169 /* enable all features now */
170 #define CONFIG_FEATURE_HTTPD_BASIC_AUTH
171 #define CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV
172 #define CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
173 #define CONFIG_FEATURE_HTTPD_DECODE_URL_STR
174 #define CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
175 #define CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
176 #define CONFIG_FEATURE_HTTPD_CGI
177 #define CONFIG_FEATURE_HTTPD_SETUID
178 #define CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
180 /* require from libbb.a for linking */
181 const char *bb_applet_name = "httpd";
183 void bb_show_usage(void)
185 fprintf(stderr, "Usage: %s [-p <port>] [-c configFile] [-d/-e <string>] "
186 "[-r realm] [-u user]\n", bb_applet_name);
191 #ifdef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
192 #undef CONFIG_FEATURE_HTTPD_SETUID /* use inetd user.group config settings */
193 #undef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP /* so is not daemon */
194 /* inetd set stderr to accepted socket and we can`t true see debug messages */
198 /* CGI environ size */
199 #ifdef CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV
200 #define ENVSIZE 50 /* set max 35 CGI_variable */
202 #define ENVSIZE 15 /* minimal requires */
205 #define MAX_POST_SIZE (64*1024) /* 64k. Its Small? May be ;) */
207 #define MAX_MEMORY_BUFF 8192 /* IO buffer */
209 typedef struct HT_ACCESS {
211 struct HT_ACCESS *next;
212 char before_colon[1]; /* really bigger, must last */
217 #ifdef CONFIG_FEATURE_HTTPD_CGI
218 char *envp[ENVSIZE+1];
221 char buf[MAX_MEMORY_BUFF];
223 #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
226 const char *configFile;
228 char rmt_ip[16]; /* for set env REMOTE_ADDR */
229 unsigned port; /* server initial port and for
230 set env REMOTE_PORT */
232 const char *found_mime_type;
233 off_t ContentLength; /* -1 - unknown */
235 #ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
237 #define a_c_r config->accepted_socket
238 #define a_c_w config->accepted_socket
239 int debugHttpd; /* if seted, don`t stay daemon */
244 Htaccess *Httpd_conf_parsed;
247 static HttpdConfig *config;
249 static const char request_GET[] = "GET"; /* size algorithic optimize */
251 static const char* const suffixTable [] = {
252 /* Warning: shorted equalent suffix in one line must be first */
253 ".htm.html", "text/html",
254 ".jpg.jpeg", "image/jpeg",
257 ".txt.h.c.cc.cpp", "text/plain",
260 ".avi", "video/x-msvideo",
261 ".qt.mov", "video/quicktime",
262 ".mpe.mpeg", "video/mpeg",
263 ".mid.midi", "audio/midi",
264 ".mp3", "audio/mpeg",
265 #if 0 /* unpopular */
266 ".au", "audio/basic",
267 ".pac", "application/x-ns-proxy-autoconfig",
268 ".vrml.wrl", "model/vrml",
270 0, "application/octet-stream" /* default */
276 HTTP_UNAUTHORIZED = 401, /* authentication needed, respond with auth hdr */
277 HTTP_NOT_FOUND = 404,
278 HTTP_NOT_IMPLEMENTED = 501, /* used for unrecognized requests */
279 HTTP_BAD_REQUEST = 400, /* malformed syntax */
280 HTTP_FORBIDDEN = 403,
281 HTTP_INTERNAL_SERVER_ERROR = 500,
282 #if 0 /* future use */
284 HTTP_SWITCHING_PROTOCOLS = 101,
287 HTTP_NON_AUTHORITATIVE_INFO = 203,
288 HTTP_NO_CONTENT = 204,
289 HTTP_MULTIPLE_CHOICES = 300,
290 HTTP_MOVED_PERMANENTLY = 301,
291 HTTP_MOVED_TEMPORARILY = 302,
292 HTTP_NOT_MODIFIED = 304,
293 HTTP_PAYMENT_REQUIRED = 402,
294 HTTP_BAD_GATEWAY = 502,
295 HTTP_SERVICE_UNAVAILABLE = 503, /* overload, maintenance */
296 HTTP_RESPONSE_SETSIZE=0xffffffff
302 HttpResponseNum type;
307 static const HttpEnumString httpResponseNames[] = {
309 { HTTP_NOT_IMPLEMENTED, "Not Implemented",
310 "The requested method is not recognized by this server." },
311 { HTTP_UNAUTHORIZED, "Unauthorized", "" },
312 { HTTP_NOT_FOUND, "Not Found",
313 "The requested URL was not found on this server." },
314 { HTTP_BAD_REQUEST, "Bad Request" ,
315 "Unsupported method." },
316 { HTTP_FORBIDDEN, "Forbidden", "" },
317 { HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error"
318 "Internal Server Error" },
320 { HTTP_CREATED, "Created" },
321 { HTTP_ACCEPTED, "Accepted" },
322 { HTTP_NO_CONTENT, "No Content" },
323 { HTTP_MULTIPLE_CHOICES, "Multiple Choices" },
324 { HTTP_MOVED_PERMANENTLY, "Moved Permanently" },
325 { HTTP_MOVED_TEMPORARILY, "Moved Temporarily" },
326 { HTTP_NOT_MODIFIED, "Not Modified" },
327 { HTTP_BAD_GATEWAY, "Bad Gateway", "" },
328 { HTTP_SERVICE_UNAVAILABLE, "Service Unavailable", "" },
333 static const char RFC1123FMT[] = "%a, %d %b %Y %H:%M:%S GMT";
334 static const char Content_length[] = "Content-length:";
341 * /path/subdir:user:pass
346 static int conf_sort(const void *p1, const void *p2)
348 const Htaccess *cl1 = *(const Htaccess **)p1;
349 const Htaccess *cl2 = *(const Htaccess **)p2;
350 char c1 = cl1->before_colon[0];
351 char c2 = cl2->before_colon[0];
354 #ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
355 /* .ext line up before other lines for simlify algorithm */
363 #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
365 /* /path line up before A/D lines for simlify algorithm */
369 /* a shortes path with user:pass must be first */
370 return strlen(cl1->before_colon) - strlen(cl2->before_colon);
375 /* D:from must move top */
376 test = c2 == 'D' && cl2->after_colon[0] != 0;
377 if(c1 == 'D' && cl1->after_colon[0] != 0) {
383 /* next lines - A:from */
384 test = c2 == 'A' && cl2->after_colon[0] != 0;
385 if(c1 == 'A' && cl1->after_colon[0] != 0) {
391 /* end lines - D:* */
392 test = c2 == 'D' && cl2->after_colon[0] == 0;
393 if(c1 == 'D' && cl1->after_colon[0] == 0) {
398 bb_error_msg_and_die("sort: can`t found compares!");
405 #define FIRST_PARSE 0
406 #define SUBDIR_PARSE 1
407 #define SIGNALED_PARSE 2
409 static void parse_conf(const char *path, int flag)
411 #define bc cur->before_colon[0]
412 #define ac cur->after_colon[0]
416 const char *cf = config->configFile;
419 int deny_all = 0; /* default A:* */
420 int n = 0; /* count config lines */
422 if(flag == SUBDIR_PARSE || cf == NULL) {
423 cf = p0 = alloca(strlen(path) + sizeof(httpd_conf) + 2);
425 if(flag == FIRST_PARSE)
426 bb_error_msg_and_die(bb_msg_memory_exhausted);
429 sprintf(p0, "%s/%s", path, httpd_conf);
432 while((f = fopen(cf, "r")) == NULL) {
433 if(flag != FIRST_PARSE)
434 return; /* subdir config not found */
435 if(p0 == NULL) /* if -c option gived */
436 bb_perror_msg_and_die("%s", cf);
438 cf = httpd_conf; /* set -c ./httpd_conf */
441 prev = config->Httpd_conf_parsed;
442 if(flag != SUBDIR_PARSE) {
443 /* free previous setuped */
449 config->Httpd_conf_parsed = prev; /* eq NULL */
451 /* parse previous IP logic for merge */
452 for(cur = prev; cur; cur = cur->next) {
453 if(bc == 'D' && ac == 0)
456 /* find last for set prev->next of merging */
462 /* This could stand some work */
463 while ( (p0 = fgets(buf, 80, f)) != NULL) {
467 for(p = colon = p0; *p; p++) {
479 else if(*p == ':' && colon <= p0)
483 /* test for empty or strange line */
484 if (colon <= p0 || colon[1] == 0)
487 colon[1] = 0; /* Allow all */
492 if(*p0 != 'A' && *p0 != 'D'
493 #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
496 #ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
501 #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
502 if(*p0 == '/' && colon[1] == 0) {
508 if(*p0 == 'A' || *p0 == 'D') {
510 if(*p0 == 'A' || deny_all != 0)
511 continue; /* skip default A:* or double D:* */
513 if(deny_all != 0 && *p0 == 'A')
514 continue; // if previous setted rule D:* skip all subdir A:
517 /* storing current config line */
518 cur = calloc(1, sizeof(Htaccess) + (p-p0));
520 if(*(colon-1) == '/' && (colon-1) != p0)
521 colon[-1] = 0; // remove last / from /path/
522 cur->after_colon = strcpy(cur->before_colon, p0);
523 cur->after_colon += (colon-p0);
524 *cur->after_colon++ = 0;
528 config->Httpd_conf_parsed = prev = cur;
539 /* sorting conf lines */
540 Htaccess ** pcur; /* array for qsort */
542 prev = config->Httpd_conf_parsed;
543 pcur = alloca((n + 1) * sizeof(Htaccess *));
545 if(flag == FIRST_PARSE)
546 bb_error_msg_and_die(bb_msg_memory_exhausted);
550 for(cur = prev; cur; cur = cur->next)
554 qsort(pcur, n, sizeof(Htaccess *), conf_sort);
556 /* storing sorted config */
557 config->Httpd_conf_parsed = *pcur;
558 for(cur = *pcur; cur; cur = cur->next) {
560 bb_error_msg("%s: %s:%s", cf, cur->before_colon, cur->after_colon);
567 #ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
568 /****************************************************************************
570 > $Function: encodeString()
572 * $Description: Given a string, html encode special characters.
573 * This is used for the -e command line option to provide an easy way
574 * for scripts to encode result data without confusing browsers. The
575 * returned string pointer is memory allocated by malloc().
578 * (const char *) string . . The first string to encode.
580 * $Return: (char *) . . . .. . . A pointer to the encoded string.
582 * $Errors: Returns a null string ("") if memory is not available.
584 ****************************************************************************/
585 static char *encodeString(const char *string)
587 /* take the simple route and encode everything */
588 /* could possibly scan once to get length. */
589 int len = strlen(string);
590 char *out = malloc(len*5 +1);
595 while ((ch = *string++)) {
596 // very simple check for what to encode
597 if (isalnum(ch)) *p++ = ch;
598 else p += sprintf(p, "&#%d", (unsigned char) ch);
603 #endif /* CONFIG_FEATURE_HTTPD_ENCODE_URL_STR */
605 #ifdef CONFIG_FEATURE_HTTPD_DECODE_URL_STR
606 /****************************************************************************
608 > $Function: decodeString()
610 * $Description: Given a URL encoded string, convert it to plain ascii.
611 * Since decoding always makes strings smaller, the decode is done in-place.
612 * Thus, callers should strdup() the argument if they do not want the
613 * argument modified. The return is the original pointer, allowing this
614 * function to be easily used as arguments to other functions.
617 * (char *) string . . . The first string to decode.
619 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
623 ****************************************************************************/
624 static char *decodeString(char *string)
626 /* note that decoded string is always shorter than original */
631 if (*ptr == '+') { *string++ = ' '; ptr++; }
632 else if (*ptr != '%') *string++ = *ptr++;
635 sscanf(ptr+1, "%2X", &value);
643 #endif /* CONFIG_FEATURE_HTTPD_DECODE_URL_STR */
646 #ifdef CONFIG_FEATURE_HTTPD_CGI
647 /****************************************************************************
649 > $Function: addEnv()
651 * $Description: Add an enviornment variable setting to the global list.
652 * A NAME=VALUE string is allocated, filled, and added to the list of
653 * environment settings passed to the cgi execution script.
656 * (char *) name_before_underline - The first part environment variable name.
657 * (char *) name_after_underline - The second part environment variable name.
658 * (char *) value . . The value to which the env variable is set.
662 * $Errors: Silently returns if the env runs out of space to hold the new item
664 ****************************************************************************/
665 static void addEnv(const char *name_before_underline,
666 const char *name_after_underline, const char *value)
670 if (config->envCount >= ENVSIZE)
674 s = malloc(strlen(name_before_underline) + strlen(name_after_underline) +
677 const char *underline = *name_after_underline ? "_" : "";
679 sprintf(s,"%s%s%s=%s", name_before_underline, underline,
680 name_after_underline, value);
681 config->envp[config->envCount++] = s;
682 config->envp[config->envCount] = 0;
686 /* set environs SERVER_PORT and REMOTE_PORT */
687 static void addEnvPort(const char *port_name)
691 sprintf(buf, "%u", config->port);
692 addEnv(port_name, "PORT", buf);
694 #endif /* CONFIG_FEATURE_HTTPD_CGI */
696 #ifdef CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV
697 /****************************************************************************
699 > $Function: addEnvCgi
701 * $Description: Create environment variables given a URL encoded arg list.
702 * For each variable setting the URL encoded arg list, create a corresponding
703 * environment variable. URL encoded arguments have the form
704 * name1=value1&name2=value2&name3=&ignores
705 * from this example, name3 set empty value, tail without '=' skiping
708 * (char *) pargs . . . . A pointer to the URL encoded arguments.
714 ****************************************************************************/
715 static void addEnvCgi(const char *pargs)
719 if (pargs==0) return;
721 /* args are a list of name=value&name2=value2 sequences */
722 memargs = args = strdup(pargs);
723 while (args && *args) {
724 const char *name = args;
725 char *value = strchr(args, '=');
727 if (!value) /* &XXX without '=' */
730 args = strchr(value, '&');
733 addEnv("CGI", name, decodeString(value));
737 #endif /* CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV */
740 #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
741 /****************************************************************************
743 > $Function: decodeBase64()
745 > $Description: Decode a base 64 data stream as per rfc1521.
746 * Note that the rfc states that none base64 chars are to be ignored.
747 * Since the decode always results in a shorter size than the input, it is
748 * OK to pass the input arg as an output arg.
751 * (char *) Data . . . . A pointer to a base64 encoded string.
752 * Where to place the decoded data.
758 ****************************************************************************/
759 static void decodeBase64(char *Data)
762 static const char base64ToBin[] =
763 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
765 const unsigned char *in = Data;
766 // The decoded size will be at most 3/4 the size of the encoded
767 unsigned long ch = 0;
770 unsigned char conv = 0;
775 p64 = strchr(base64ToBin, *in++);
778 conv = (p64 - base64ToBin);
781 ch = (ch << 6) | conv;
784 *Data++ = (char) (ch >> 16);
785 *Data++ = (char) (ch >> 8);
795 #ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
796 /****************************************************************************
798 > $Function: openServer()
800 * $Description: create a listen server socket on the designated port.
802 * $Return: (int) . . . A connection socket. -1 for errors.
806 ****************************************************************************/
807 static int openServer(void)
809 struct sockaddr_in lsocket;
812 /* create the socket right now */
813 /* inet_addr() returns a value that is already in network order */
814 memset(&lsocket, 0, sizeof(lsocket));
815 lsocket.sin_family = AF_INET;
816 lsocket.sin_addr.s_addr = INADDR_ANY;
817 lsocket.sin_port = htons(config->port) ;
818 fd = socket(AF_INET, SOCK_STREAM, 0);
820 /* tell the OS it's OK to reuse a previous address even though */
821 /* it may still be in a close down state. Allows bind to succeed. */
824 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *)&on, sizeof(on)) ;
826 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)) ;
828 if (bind(fd, (struct sockaddr *)&lsocket, sizeof(lsocket)) == 0) {
830 signal(SIGCHLD, SIG_IGN); /* prevent zombie (defunct) processes */
832 bb_perror_msg_and_die("bind");
835 bb_perror_msg_and_die("create socket");
839 #endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
841 /****************************************************************************
843 > $Function: sendHeaders()
845 * $Description: Create and send HTTP response headers.
846 * The arguments are combined and sent as one write operation. Note that
847 * IE will puke big-time if the headers are not sent in one packet and the
848 * second packet is delayed for any reason.
851 * (HttpResponseNum) responseNum . . . The result code to send.
853 * $Return: (int) . . . . writing errors
855 ****************************************************************************/
856 static int sendHeaders(HttpResponseNum responseNum)
858 char *buf = config->buf;
859 const char *responseString = "";
860 const char *infoString = 0;
862 time_t timer = time(0);
867 i < (sizeof(httpResponseNames)/sizeof(httpResponseNames[0])); i++) {
868 if (httpResponseNames[i].type == responseNum) {
869 responseString = httpResponseNames[i].name;
870 infoString = httpResponseNames[i].info;
874 if (responseNum != HTTP_OK) {
875 config->found_mime_type = "text/html"; // error message is HTML
878 /* emit the current date */
879 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&timer));
881 "HTTP/1.0 %d %s\nContent-type: %s\r\n"
882 "Date: %s\r\nConnection: close\r\n",
883 responseNum, responseString, config->found_mime_type, timeStr);
885 #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
886 if (responseNum == HTTP_UNAUTHORIZED) {
887 len += sprintf(buf+len, "WWW-Authenticate: Basic realm=\"%s\"\r\n",
891 if (config->ContentLength != -1) { /* file */
892 strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&config->last_mod));
893 len += sprintf(buf+len, "Last-Modified: %s\r\n%s %ld\r\n",
894 timeStr, Content_length, config->ContentLength);
899 len += sprintf(buf+len,
900 "<HEAD><TITLE>%d %s</TITLE></HEAD>\n"
901 "<BODY><H1>%d %s</H1>\n%s\n</BODY>\n",
902 responseNum, responseString,
903 responseNum, responseString, infoString);
906 if (config->debugHttpd) fprintf(stderr, "Headers: '%s'", buf);
908 return bb_full_write(a_c_w, buf, len);
911 /****************************************************************************
913 > $Function: getLine()
915 * $Description: Read from the socket until an end of line char found.
917 * Characters are read one at a time until an eol sequence is found.
920 * (char *) buf . . Where to place the read result.
922 * $Return: (int) . . . . number of characters read. -1 if error.
924 ****************************************************************************/
925 static int getLine(char *buf)
929 while (read(a_c_r, buf + count, 1) == 1) {
930 if (buf[count] == '\r') continue;
931 if (buf[count] == '\n') {
935 if(count < (MAX_MEMORY_BUFF-1)) /* check owerflow */
938 if (count) return count;
942 #ifdef CONFIG_FEATURE_HTTPD_CGI
943 /****************************************************************************
945 > $Function: sendCgi()
947 * $Description: Execute a CGI script and send it's stdout back
949 * Environment variables are set up and the script is invoked with pipes
950 * for stdin/stdout. If a post is being done the script is fed the POST
951 * data in addition to setting the QUERY_STRING variable (for GETs or POSTs).
954 * (const char *) url . . . The requested URL (with leading /).
955 * (const char *urlArgs). . Any URL arguments.
956 * (const char *body) . . . POST body contents.
957 * (int bodyLen) . . . . . Length of the post body.
958 * (const char *cookie) . . For set HTTP_COOKIE.
961 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
965 ****************************************************************************/
966 static int sendCgi(const char *url,
967 const char *request, const char *urlArgs,
968 const char *body, int bodyLen, const char *cookie)
970 int fromCgi[2]; /* pipe for reading data from CGI */
971 int toCgi[2]; /* pipe for sending data to CGI */
973 static char * argp[] = { 0, 0 };
980 if (pipe(fromCgi) != 0) {
983 if (pipe(toCgi) != 0) {
996 char *purl = strdup( url );
997 char realpath_buff[MAXPATHLEN];
1005 dup2(inFd, 0); // replace stdin with the pipe
1006 dup2(outFd, 1); // replace stdout with the pipe
1008 #ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1009 if (!config->debugHttpd)
1011 dup2(outFd, 2); // replace stderr with the pipe
1022 while((script = strchr( script + 1, '/' )) != NULL) {
1023 /* have script.cgi/PATH_INFO or dirs/script.cgi[/PATH_INFO] */
1027 if(is_directory(purl + 1, 1, &sb) == 0) {
1028 /* not directory, found script.cgi/PATH_INFO */
1032 *script = '/'; /* is directory, find next '/' */
1034 addEnv("PATH", "INFO", script); /* set /PATH_INFO or NULL */
1035 addEnv("PATH", "", getenv("PATH"));
1036 addEnv("REQUEST", "METHOD", request);
1038 char *uri = alloca(strlen(purl) + 2 + strlen(urlArgs));
1040 sprintf(uri, "%s?%s", purl, urlArgs);
1041 addEnv("REQUEST", "URI", uri);
1043 addEnv("REQUEST", "URI", purl);
1046 *script = '\0'; /* reduce /PATH_INFO */
1047 /* set SCRIPT_NAME as full path: /cgi-bin/dirs/script.cgi */
1048 addEnv("SCRIPT_NAME", "", purl);
1049 addEnv("QUERY_STRING", "", urlArgs);
1050 addEnv("SERVER", "SOFTWARE", httpdVersion);
1051 addEnv("SERVER", "PROTOCOL", "HTTP/1.0");
1052 addEnv("GATEWAY_INTERFACE", "", "CGI/1.1");
1053 #ifdef CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
1054 addEnv("REMOTE", "ADDR", config->rmt_ip);
1055 addEnvPort("REMOTE");
1057 addEnv("REMOTE_ADDR", "", config->rmt_ip);
1062 sprintf(sbl, "%d", bodyLen);
1063 addEnv("CONTENT_LENGTH", "", sbl);
1066 addEnv("HTTP_COOKIE", "", cookie);
1068 #ifdef CONFIG_FEATURE_HTTPD_SET_CGI_VARS_TO_ENV
1069 if (request != request_GET) {
1076 /* set execve argp[0] without path */
1077 argp[0] = strrchr( purl, '/' ) + 1;
1078 /* but script argp[0] must have absolute path and chdiring to this */
1079 if(realpath(purl + 1, realpath_buff) != NULL) {
1080 script = strrchr(realpath_buff, '/');
1083 if(chdir(realpath_buff) == 0) {
1085 // now run the program. If it fails, use _exit() so no destructors
1086 // get called and make a mess.
1087 execve(realpath_buff, argp, config->envp);
1091 #ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1092 config->accepted_socket = 1; /* send to stdout */
1094 sendHeaders(HTTP_NOT_FOUND);
1101 /* parent process */
1108 if (body) bb_full_write(outFd, body, bodyLen);
1112 struct timeval timeout;
1119 FD_SET(inFd, &readSet);
1121 /* Now wait on the set of sockets! */
1123 timeout.tv_usec = 10000;
1124 nfound = select(inFd + 1, &readSet, 0, 0, &timeout);
1127 if (waitpid(pid, &status, WNOHANG) > 0) {
1130 if (config->debugHttpd) {
1131 if (WIFEXITED(status))
1132 bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status));
1133 if (WIFSIGNALED(status))
1134 bb_error_msg("piped has exited with signal=%d", WTERMSIG(status));
1143 // There is something to read
1144 count = bb_full_read(inFd, buf, sizeof(buf)-1);
1145 // If a read returns 0 at this point then some type of error has
1146 // occurred. Bail now.
1147 if (count == 0) break;
1150 /* check to see if the user script added headers */
1151 if (strncmp(buf, "HTTP/1.0 200 OK\n", 4) != 0) {
1152 bb_full_write(s, "HTTP/1.0 200 OK\n", 16);
1154 if (strstr(buf, "ontent-") == 0) {
1155 bb_full_write(s, "Content-type: text/plain\n\n", 26);
1159 bb_full_write(s, buf, count);
1161 if (config->debugHttpd)
1162 fprintf(stderr, "cgi read %d bytes\n", count);
1170 #endif /* CONFIG_FEATURE_HTTPD_CGI */
1172 /****************************************************************************
1174 > $Function: sendFile()
1176 * $Description: Send a file response to an HTTP request
1179 * (const char *) url . . The URL requested.
1180 * (char *) buf . . . . . The stack buffer.
1182 * $Return: (int) . . . . . . Always 0.
1184 ****************************************************************************/
1185 static int sendFile(const char *url, char *buf)
1189 const char * const * table;
1190 const char * try_suffix;
1192 suffix = strrchr(url, '.');
1194 for (table = suffixTable; *table; table += 2)
1195 if(suffix != NULL && (try_suffix = strstr(*table, suffix)) != 0) {
1196 try_suffix += strlen(suffix);
1197 if(*try_suffix == 0 || *try_suffix == '.')
1200 /* also, if not found, set default as "application/octet-stream"; */
1201 config->found_mime_type = *(table+1);
1202 #ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
1206 for (cur = config->Httpd_conf_parsed; cur; cur = cur->next) {
1207 if(strcmp(cur->before_colon, suffix) == 0) {
1208 config->found_mime_type = cur->after_colon;
1213 #endif /* CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES */
1216 if (config->debugHttpd)
1217 fprintf(stderr, "Sending file '%s' Content-type: %s\n",
1218 url, config->found_mime_type);
1221 f = open(url, O_RDONLY);
1225 sendHeaders(HTTP_OK);
1226 while ((count = bb_full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
1227 bb_full_write(a_c_w, buf, count);
1232 if (config->debugHttpd)
1233 bb_perror_msg("Unable to open '%s'", url);
1235 sendHeaders(HTTP_NOT_FOUND);
1241 /****************************************************************************
1243 > $Function: checkPerm()
1245 * $Description: Check the permission file for access.
1247 * If config file isn't present, everything is allowed.
1248 * Entries are of the form you can see example from header source
1251 * (const char *) path . . . . The file path or NULL for ip addresses.
1252 * (const char *) request . . . User information to validate.
1254 * $Return: (int) . . . . . . . . . 1 if request OK, 0 otherwise.
1256 ****************************************************************************/
1259 static int checkPerm(const char *path, const char *request)
1264 #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1265 int ipaddr = path == NULL;
1266 const char *prev = NULL;
1271 /* This could stand some work */
1272 for (cur = config->Httpd_conf_parsed; cur; cur = cur->next) {
1273 const char *p0 = cur->before_colon;
1275 if(*p0 == 'A' || *p0 == 'D') {
1281 #ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
1285 #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1286 if(prev != NULL && strcmp(prev, p0) != 0)
1287 continue; /* find next identical */
1291 p = cur->after_colon;
1293 if (config->debugHttpd)
1294 fprintf(stderr,"checkPerm: '%s' ? '%s'\n",
1295 (ipaddr ? p : p0), request);
1298 if(strncmp(p, request, strlen(p)) != 0)
1300 return *p0 == 'A'; /* Allow/Deny */
1303 #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1307 if(strncmp(p0, path, l) == 0 &&
1308 (l == 1 || path[l] == '/' || path[l] == 0)) {
1309 /* path match found. Check request */
1310 if (strcmp(p, request) == 0)
1312 /* unauthorized, but check next /path:user:password */
1319 #ifndef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1320 /* if uncofigured, return 1 - access from all */
1323 return prev == NULL;
1328 /****************************************************************************
1330 > $Function: handleIncoming()
1332 * $Description: Handle an incoming http request.
1334 ****************************************************************************/
1335 static void handleIncoming(void)
1337 char *buf = config->buf;
1342 #ifdef CONFIG_FEATURE_HTTPD_CGI
1343 const char *prequest = request_GET;
1351 #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1352 int credentials = -1; /* if not requred this is Ok */
1358 if (getLine(buf) <= 0)
1361 purl = strpbrk(buf, " \t");
1364 sendHeaders(HTTP_BAD_REQUEST);
1368 #ifdef CONFIG_FEATURE_HTTPD_CGI
1369 if(strcasecmp(buf, prequest) != 0) {
1371 if(strcasecmp(buf, prequest) != 0) {
1372 sendHeaders(HTTP_NOT_IMPLEMENTED);
1377 if(strcasecmp(buf, request_GET) != 0) {
1378 sendHeaders(HTTP_NOT_IMPLEMENTED);
1383 count = sscanf(purl, " %[^ ] HTTP/%d.%*d", buf, &blank);
1385 if (count < 1 || buf[0] != '/') {
1386 /* Garbled request/URL */
1389 url = alloca(strlen(buf) + 12); /* + sizeof("/index.html\0") */
1391 sendHeaders(HTTP_INTERNAL_SERVER_ERROR);
1395 /* extract url args if present */
1396 urlArgs = strchr(url, '?');
1398 *urlArgs++ = 0; /* next code can set '/' to this pointer,
1399 but CGI script can`t be a directory */
1402 /* algorithm stolen from libbb bb_simplify_path(),
1403 but don`t strdup and reducing trailing slash */
1408 if (*test == '/') { /* skip duplicate (or initial) slash */
1410 } else if (*test == '.') {
1411 if (test[1] == '/' || test[1] == 0) { /* skip extra '.' */
1413 } else if ((test[1] == '.') && (test[2] == '/' || test[2] == 0)) {
1416 /* protect out root */
1419 while (*--purl != '/'); /* omit previous dir */
1427 *++purl = 0; /* so keep last character */
1428 test = purl; /* end ptr */
1430 /* If URL is directory, adding '/' */
1431 if(test[-1] != '/') {
1432 if ( is_directory(url + 1, 1, &sb) ) {
1435 purl = test; /* end ptr */
1439 if (config->debugHttpd)
1440 fprintf(stderr, "url='%s', args=%s\n", url, urlArgs);
1444 while((test = strchr( test + 1, '/' )) != NULL) {
1445 /* have path1/path2 */
1447 if( is_directory(url + 1, 1, &sb) ) {
1448 /* may be having subdir config */
1449 parse_conf(url + 1, SUBDIR_PARSE);
1454 // read until blank line for HTTP version specified, else parse immediate
1455 while (blank >= 0 && (count = getLine(buf)) > 0) {
1458 if (config->debugHttpd) fprintf(stderr, "Header: '%s'\n", buf);
1461 #ifdef CONFIG_FEATURE_HTTPD_CGI
1462 /* try and do our best to parse more lines */
1463 if ((strncasecmp(buf, Content_length, 15) == 0)) {
1464 if(prequest != request_GET)
1465 length = strtol(buf + 15, 0, 0); // extra read only for POST
1466 } else if ((strncasecmp(buf, "Cookie:", 7) == 0)) {
1467 for(test = buf + 7; isspace(*test); test++)
1469 cookie = strdup(test);
1473 #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1474 if (strncasecmp(buf, "Authorization:", 14) == 0) {
1475 /* We only allow Basic credentials.
1476 * It shows up as "Authorization: Basic <userid:password>" where
1477 * the userid:password is base64 encoded.
1479 for(test = buf + 14; isspace(*test); test++)
1481 if (strncasecmp(test, "Basic", 5) != 0)
1484 test += 5; /* decodeBase64() skiping space self */
1486 credentials = checkPerm(url, test);
1488 #endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
1490 } /* while extra header reading */
1493 if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 ||
1494 checkPerm(NULL, config->rmt_ip) == 0) {
1495 /* protect listing [/path]/httpd_conf or IP deny */
1496 #ifdef CONFIG_FEATURE_HTTPD_CGI
1497 FORBIDDEN: /* protect listing /cgi-bin */
1499 sendHeaders(HTTP_FORBIDDEN);
1503 #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1504 if (credentials <= 0 && checkPerm(url, ":") == 0) {
1505 sendHeaders(HTTP_UNAUTHORIZED);
1510 test = url + 1; /* skip first '/' */
1512 #ifdef CONFIG_FEATURE_HTTPD_CGI
1513 /* if strange Content-Length */
1514 if (length < 0 || length > MAX_POST_SIZE)
1518 body = malloc(length + 1);
1520 length = bb_full_read(a_c_r, body, length);
1521 if(length < 0) // closed
1523 body[length] = 0; // always null terminate for safety
1527 if (strncmp(test, "cgi-bin", 7) == 0) {
1528 if(test[7] == 0 || (test[7] == '/' && test[8] == 0))
1529 goto FORBIDDEN; // protect listing cgi-bin
1530 sendCgi(url, prequest, urlArgs, body, length, cookie);
1532 if (prequest != request_GET)
1533 sendHeaders(HTTP_NOT_IMPLEMENTED);
1535 #endif /* CONFIG_FEATURE_HTTPD_CGI */
1537 strcpy(purl, "index.html");
1538 if ( stat(test, &sb ) == 0 ) {
1539 config->ContentLength = sb.st_size;
1540 config->last_mod = sb.st_mtime;
1542 sendFile(test, buf);
1543 #ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1544 /* unset if non inetd looped */
1545 config->ContentLength = -1;
1548 #ifdef CONFIG_FEATURE_HTTPD_CGI
1556 #ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1557 /* from inetd don`t looping: freeing, closing automatic from exit always */
1559 if (config->debugHttpd) fprintf(stderr, "closing socket\n");
1561 # ifdef CONFIG_FEATURE_HTTPD_CGI
1565 shutdown(a_c_w, SHUT_WR);
1566 shutdown(a_c_r, SHUT_RD);
1567 close(config->accepted_socket);
1568 #endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
1571 /****************************************************************************
1573 > $Function: miniHttpd()
1575 * $Description: The main http server function.
1577 * Given an open socket fildes, listen for new connections and farm out
1578 * the processing as a forked process.
1581 * (int) server. . . The server socket fildes.
1583 * $Return: (int) . . . . Always 0.
1585 ****************************************************************************/
1586 #ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1587 static int miniHttpd(int server)
1589 fd_set readfd, portfd;
1593 FD_SET(server, &portfd);
1595 /* copy the ports we are watching to the readfd set */
1599 /* Now wait INDEFINATELY on the set of sockets! */
1600 nfound = select(server + 1, &readfd, 0, 0, 0);
1604 /* select timeout error! */
1610 if (FD_ISSET(server, &readfd)) {
1612 struct sockaddr_in fromAddr;
1615 socklen_t fromAddrLen = sizeof(fromAddr);
1616 int s = accept(server,
1617 (struct sockaddr *)&fromAddr, &fromAddrLen);
1622 config->accepted_socket = s;
1623 addr = ntohl(fromAddr.sin_addr.s_addr);
1624 sprintf(config->rmt_ip, "%u.%u.%u.%u",
1625 (unsigned char)(addr >> 24),
1626 (unsigned char)(addr >> 16),
1627 (unsigned char)(addr >> 8),
1629 config->port = ntohs(fromAddr.sin_port);
1631 if (config->debugHttpd) {
1632 bb_error_msg("connection from IP=%s, port %u\n",
1633 config->rmt_ip, config->port);
1636 /* set the KEEPALIVE option to cull dead connections */
1638 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof (on));
1640 if (config->debugHttpd || fork() == 0) {
1641 /* This is the spawned thread */
1643 if(!config->debugHttpd)
1656 static int miniHttpd(void)
1658 struct sockaddr_in fromAddrLen;
1659 socklen_t sinlen = sizeof (struct sockaddr_in);
1662 getpeername (0, (struct sockaddr *)&fromAddrLen, &sinlen);
1663 addr = ntohl(fromAddrLen.sin_addr.s_addr);
1664 sprintf(config->rmt_ip, "%u.%u.%u.%u",
1665 (unsigned char)(addr >> 24),
1666 (unsigned char)(addr >> 16),
1667 (unsigned char)(addr >> 8),
1669 config->port = ntohs(fromAddrLen.sin_port);
1673 #endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
1675 #ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
1676 static void sighup_handler(int sig)
1679 struct sigaction sa;
1681 sa.sa_handler = sighup_handler;
1682 sigemptyset(&sa.sa_mask);
1683 sa.sa_flags = SA_RESTART;
1684 sigaction(SIGHUP, &sa, NULL);
1685 parse_conf(default_patch_httpd_conf,
1686 sig == SIGHUP ? SIGNALED_PARSE : FIRST_PARSE);
1690 #ifdef HTTPD_STANDALONE
1691 int main(int argc, char *argv[])
1693 int httpd_main(int argc, char *argv[])
1696 const char *home_httpd = home;
1698 #ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1702 #ifdef CONFIG_FEATURE_HTTPD_SETUID
1706 config = xcalloc(1, sizeof(*config));
1707 #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1708 config->realm = "Web Server Authentication";
1711 #ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1715 config->ContentLength = -1;
1717 /* check if user supplied a port number */
1719 int c = getopt( argc, argv, "c:h:"
1720 #ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1723 #ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
1726 #ifdef CONFIG_FEATURE_HTTPD_DECODE_URL_STR
1729 #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1732 #ifdef CONFIG_FEATURE_HTTPD_SETUID
1736 if (c == EOF) break;
1739 config->configFile = optarg;
1742 home_httpd = optarg;
1744 #ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1746 config->debugHttpd = 1;
1749 config->port = atoi(optarg);
1750 if(config->port <= 0 || config->port > 0xffff)
1751 bb_error_msg_and_die("invalid %s for -p", optarg);
1754 #ifdef CONFIG_FEATURE_HTTPD_DECODE_URL_STR
1756 printf("%s", decodeString(optarg));
1759 #ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
1761 printf("%s", encodeString(optarg));
1764 #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
1766 config->realm = optarg;
1769 #ifdef CONFIG_FEATURE_HTTPD_SETUID
1774 uid = strtol(optarg, &e, 0);
1777 uid = my_getpwnam(optarg);
1783 bb_error_msg("%s", httpdVersion);
1788 if(chdir(home_httpd)) {
1789 bb_perror_msg_and_die("can`t chdir to %s", home_httpd);
1791 #ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1792 server = openServer();
1793 # ifdef CONFIG_FEATURE_HTTPD_SETUID
1794 /* drop privilegies */
1798 # ifdef CONFIG_FEATURE_HTTPD_CGI
1799 addEnvPort("SERVER");
1803 #ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
1806 parse_conf(default_patch_httpd_conf, FIRST_PARSE);
1809 #ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1810 if (!config->debugHttpd) {
1811 if (daemon(1, 0) < 0) /* don`t change curent directory */
1812 bb_perror_msg_and_die("daemon");
1814 return miniHttpd(server);