1 | /* vi: set sw=4 ts=4: */
|
---|
2 | /*
|
---|
3 | * httpd implementation for busybox
|
---|
4 | *
|
---|
5 | * Copyright (C) 2002,2003 Glenn Engel <glenne@engel.org>
|
---|
6 | * Copyright (C) 2003-2006 Vladimir Oleynik <dzo@simtreas.ru>
|
---|
7 | *
|
---|
8 | * simplify patch stolen from libbb without using strdup
|
---|
9 | *
|
---|
10 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
---|
11 | *
|
---|
12 | *****************************************************************************
|
---|
13 | *
|
---|
14 | * Typical usage:
|
---|
15 | * for non root user
|
---|
16 | * httpd -p 8080 -h $HOME/public_html
|
---|
17 | * or for daemon start from rc script with uid=0:
|
---|
18 | * httpd -u www
|
---|
19 | * This is equivalent if www user have uid=80 to
|
---|
20 | * httpd -p 80 -u 80 -h /www -c /etc/httpd.conf -r "Web Server Authentication"
|
---|
21 | *
|
---|
22 | *
|
---|
23 | * When a url contains "cgi-bin" it is assumed to be a cgi script. The
|
---|
24 | * server changes directory to the location of the script and executes it
|
---|
25 | * after setting QUERY_STRING and other environment variables.
|
---|
26 | *
|
---|
27 | * The server can also be invoked as a url arg decoder and html text encoder
|
---|
28 | * as follows:
|
---|
29 | * foo=`httpd -d $foo` # decode "Hello%20World" as "Hello World"
|
---|
30 | * bar=`httpd -e "<Hello World>"` # encode as "<Hello World>"
|
---|
31 | * Note that url encoding for arguments is not the same as html encoding for
|
---|
32 | * presentation. -d decodes a url-encoded argument while -e encodes in html
|
---|
33 | * for page display.
|
---|
34 | *
|
---|
35 | * httpd.conf has the following format:
|
---|
36 | *
|
---|
37 | * A:172.20. # Allow address from 172.20.0.0/16
|
---|
38 | * A:10.0.0.0/25 # Allow any address from 10.0.0.0-10.0.0.127
|
---|
39 | * A:10.0.0.0/255.255.255.128 # Allow any address that previous set
|
---|
40 | * A:127.0.0.1 # Allow local loopback connections
|
---|
41 | * D:* # Deny from other IP connections
|
---|
42 | * /cgi-bin:foo:bar # Require user foo, pwd bar on urls starting with /cgi-bin/
|
---|
43 | * /adm:admin:setup # Require user admin, pwd setup on urls starting with /adm/
|
---|
44 | * /adm:toor:PaSsWd # or user toor, pwd PaSsWd on urls starting with /adm/
|
---|
45 | * .au:audio/basic # additional mime type for audio.au files
|
---|
46 | * *.php:/path/php # running cgi.php scripts through an interpreter
|
---|
47 | *
|
---|
48 | * A/D may be as a/d or allow/deny - first char case insensitive
|
---|
49 | * Deny IP rules take precedence over allow rules.
|
---|
50 | *
|
---|
51 | *
|
---|
52 | * The Deny/Allow IP logic:
|
---|
53 | *
|
---|
54 | * - Default is to allow all. No addresses are denied unless
|
---|
55 | * denied with a D: rule.
|
---|
56 | * - Order of Deny/Allow rules is significant
|
---|
57 | * - Deny rules take precedence over allow rules.
|
---|
58 | * - If a deny all rule (D:*) is used it acts as a catch-all for unmatched
|
---|
59 | * addresses.
|
---|
60 | * - Specification of Allow all (A:*) is a no-op
|
---|
61 | *
|
---|
62 | * Example:
|
---|
63 | * 1. Allow only specified addresses
|
---|
64 | * A:172.20 # Allow any address that begins with 172.20.
|
---|
65 | * A:10.10. # Allow any address that begins with 10.10.
|
---|
66 | * A:127.0.0.1 # Allow local loopback connections
|
---|
67 | * D:* # Deny from other IP connections
|
---|
68 | *
|
---|
69 | * 2. Only deny specified addresses
|
---|
70 | * D:1.2.3. # deny from 1.2.3.0 - 1.2.3.255
|
---|
71 | * D:2.3.4. # deny from 2.3.4.0 - 2.3.4.255
|
---|
72 | * A:* # (optional line added for clarity)
|
---|
73 | *
|
---|
74 | * If a sub directory contains a config file it is parsed and merged with
|
---|
75 | * any existing settings as if it was appended to the original configuration.
|
---|
76 | *
|
---|
77 | * subdir paths are relative to the containing subdir and thus cannot
|
---|
78 | * affect the parent rules.
|
---|
79 | *
|
---|
80 | * Note that since the sub dir is parsed in the forked thread servicing the
|
---|
81 | * subdir http request, any merge is discarded when the process exits. As a
|
---|
82 | * result, the subdir settings only have a lifetime of a single request.
|
---|
83 | *
|
---|
84 | *
|
---|
85 | * If -c is not set, an attempt will be made to open the default
|
---|
86 | * root configuration file. If -c is set and the file is not found, the
|
---|
87 | * server exits with an error.
|
---|
88 | *
|
---|
89 | */
|
---|
90 |
|
---|
91 |
|
---|
92 | #include <stdio.h>
|
---|
93 | #include <ctype.h> /* for isspace */
|
---|
94 | #include <string.h>
|
---|
95 | #include <stdlib.h> /* for malloc */
|
---|
96 | #include <time.h>
|
---|
97 | #include <unistd.h> /* for close */
|
---|
98 | #include <signal.h>
|
---|
99 | #include <sys/types.h>
|
---|
100 | #include <sys/socket.h> /* for connect and socket*/
|
---|
101 | #include <netinet/in.h> /* for sockaddr_in */
|
---|
102 | #include <sys/stat.h>
|
---|
103 | #include <sys/wait.h>
|
---|
104 | #include <fcntl.h> /* for open modes */
|
---|
105 | #include "busybox.h"
|
---|
106 |
|
---|
107 |
|
---|
108 | static const char httpdVersion[] = "busybox httpd/1.35 6-Oct-2004";
|
---|
109 | static const char default_path_httpd_conf[] = "/etc";
|
---|
110 | static const char httpd_conf[] = "httpd.conf";
|
---|
111 | static const char home[] = "./";
|
---|
112 |
|
---|
113 | #ifdef CONFIG_LFS
|
---|
114 | # define cont_l_fmt "%lld"
|
---|
115 | # define cont_l_type (long long)
|
---|
116 | #else
|
---|
117 | # define cont_l_fmt "%ld"
|
---|
118 | # define cont_l_type (long)
|
---|
119 | #endif
|
---|
120 |
|
---|
121 | #define TIMEOUT 60
|
---|
122 |
|
---|
123 | // Note: busybox xfuncs are not used because we want the server to keep running
|
---|
124 | // if something bad happens due to a malformed user request.
|
---|
125 | // As a result, all memory allocation after daemonize
|
---|
126 | // is checked rigorously
|
---|
127 |
|
---|
128 | //#define DEBUG 1
|
---|
129 |
|
---|
130 | #ifndef DEBUG
|
---|
131 | # define DEBUG 0
|
---|
132 | #endif
|
---|
133 |
|
---|
134 | #define MAX_MEMORY_BUFF 8192 /* IO buffer */
|
---|
135 |
|
---|
136 | typedef struct HT_ACCESS {
|
---|
137 | char *after_colon;
|
---|
138 | struct HT_ACCESS *next;
|
---|
139 | char before_colon[1]; /* really bigger, must last */
|
---|
140 | } Htaccess;
|
---|
141 |
|
---|
142 | typedef struct HT_ACCESS_IP {
|
---|
143 | unsigned int ip;
|
---|
144 | unsigned int mask;
|
---|
145 | int allow_deny;
|
---|
146 | struct HT_ACCESS_IP *next;
|
---|
147 | } Htaccess_IP;
|
---|
148 |
|
---|
149 | typedef struct
|
---|
150 | {
|
---|
151 | char buf[MAX_MEMORY_BUFF];
|
---|
152 |
|
---|
153 | USE_FEATURE_HTTPD_BASIC_AUTH(const char *realm;)
|
---|
154 | USE_FEATURE_HTTPD_BASIC_AUTH(char *remoteuser;)
|
---|
155 |
|
---|
156 | const char *query;
|
---|
157 |
|
---|
158 | USE_FEATURE_HTTPD_CGI(char *referer;)
|
---|
159 |
|
---|
160 | const char *configFile;
|
---|
161 |
|
---|
162 | unsigned int rmt_ip;
|
---|
163 | #if defined(CONFIG_FEATURE_HTTPD_CGI) || DEBUG
|
---|
164 | char rmt_ip_str[16]; /* for set env REMOTE_ADDR */
|
---|
165 | #endif
|
---|
166 | unsigned port; /* server initial port and for
|
---|
167 | set env REMOTE_PORT */
|
---|
168 | union HTTPD_FOUND {
|
---|
169 | const char *found_mime_type;
|
---|
170 | const char *found_moved_temporarily;
|
---|
171 | } httpd_found;
|
---|
172 |
|
---|
173 | off_t ContentLength; /* -1 - unknown */
|
---|
174 | time_t last_mod;
|
---|
175 |
|
---|
176 | Htaccess_IP *ip_a_d; /* config allow/deny lines */
|
---|
177 | int flg_deny_all;
|
---|
178 | #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
|
---|
179 | Htaccess *auth; /* config user:password lines */
|
---|
180 | #endif
|
---|
181 | #ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
|
---|
182 | Htaccess *mime_a; /* config mime types */
|
---|
183 | #endif
|
---|
184 |
|
---|
185 | #ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
|
---|
186 | int accepted_socket;
|
---|
187 | # define a_c_r config->accepted_socket
|
---|
188 | # define a_c_w config->accepted_socket
|
---|
189 | #else
|
---|
190 | # define a_c_r 0
|
---|
191 | # define a_c_w 1
|
---|
192 | #endif
|
---|
193 | volatile int alarm_signaled;
|
---|
194 |
|
---|
195 | #ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
|
---|
196 | Htaccess *script_i; /* config script interpreters */
|
---|
197 | #endif
|
---|
198 | } HttpdConfig;
|
---|
199 |
|
---|
200 | static HttpdConfig *config;
|
---|
201 |
|
---|
202 | static const char request_GET[] = "GET"; /* size algorithmic optimize */
|
---|
203 |
|
---|
204 | static const char* const suffixTable [] = {
|
---|
205 | /* Warning: shorted equivalent suffix in one line must be first */
|
---|
206 | ".htm.html", "text/html",
|
---|
207 | ".jpg.jpeg", "image/jpeg",
|
---|
208 | ".gif", "image/gif",
|
---|
209 | ".png", "image/png",
|
---|
210 | ".txt.h.c.cc.cpp", "text/plain",
|
---|
211 | ".css", "text/css",
|
---|
212 | ".wav", "audio/wav",
|
---|
213 | ".avi", "video/x-msvideo",
|
---|
214 | ".qt.mov", "video/quicktime",
|
---|
215 | ".mpe.mpeg", "video/mpeg",
|
---|
216 | ".mid.midi", "audio/midi",
|
---|
217 | ".mp3", "audio/mpeg",
|
---|
218 | #if 0 /* unpopular */
|
---|
219 | ".au", "audio/basic",
|
---|
220 | ".pac", "application/x-ns-proxy-autoconfig",
|
---|
221 | ".vrml.wrl", "model/vrml",
|
---|
222 | #endif
|
---|
223 | 0, "application/octet-stream" /* default */
|
---|
224 | };
|
---|
225 |
|
---|
226 | typedef enum
|
---|
227 | {
|
---|
228 | HTTP_OK = 200,
|
---|
229 | HTTP_MOVED_TEMPORARILY = 302,
|
---|
230 | HTTP_BAD_REQUEST = 400, /* malformed syntax */
|
---|
231 | HTTP_UNAUTHORIZED = 401, /* authentication needed, respond with auth hdr */
|
---|
232 | HTTP_NOT_FOUND = 404,
|
---|
233 | HTTP_FORBIDDEN = 403,
|
---|
234 | HTTP_REQUEST_TIMEOUT = 408,
|
---|
235 | HTTP_NOT_IMPLEMENTED = 501, /* used for unrecognized requests */
|
---|
236 | HTTP_INTERNAL_SERVER_ERROR = 500,
|
---|
237 | #if 0 /* future use */
|
---|
238 | HTTP_CONTINUE = 100,
|
---|
239 | HTTP_SWITCHING_PROTOCOLS = 101,
|
---|
240 | HTTP_CREATED = 201,
|
---|
241 | HTTP_ACCEPTED = 202,
|
---|
242 | HTTP_NON_AUTHORITATIVE_INFO = 203,
|
---|
243 | HTTP_NO_CONTENT = 204,
|
---|
244 | HTTP_MULTIPLE_CHOICES = 300,
|
---|
245 | HTTP_MOVED_PERMANENTLY = 301,
|
---|
246 | HTTP_NOT_MODIFIED = 304,
|
---|
247 | HTTP_PAYMENT_REQUIRED = 402,
|
---|
248 | HTTP_BAD_GATEWAY = 502,
|
---|
249 | HTTP_SERVICE_UNAVAILABLE = 503, /* overload, maintenance */
|
---|
250 | HTTP_RESPONSE_SETSIZE=0xffffffff
|
---|
251 | #endif
|
---|
252 | } HttpResponseNum;
|
---|
253 |
|
---|
254 | typedef struct
|
---|
255 | {
|
---|
256 | HttpResponseNum type;
|
---|
257 | const char *name;
|
---|
258 | const char *info;
|
---|
259 | } HttpEnumString;
|
---|
260 |
|
---|
261 | static const HttpEnumString httpResponseNames[] = {
|
---|
262 | { HTTP_OK, "OK", NULL },
|
---|
263 | { HTTP_MOVED_TEMPORARILY, "Found", "Directories must end with a slash." },
|
---|
264 | { HTTP_REQUEST_TIMEOUT, "Request Timeout",
|
---|
265 | "No request appeared within a reasonable time period." },
|
---|
266 | { HTTP_NOT_IMPLEMENTED, "Not Implemented",
|
---|
267 | "The requested method is not recognized by this server." },
|
---|
268 | #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
|
---|
269 | { HTTP_UNAUTHORIZED, "Unauthorized", "" },
|
---|
270 | #endif
|
---|
271 | { HTTP_NOT_FOUND, "Not Found",
|
---|
272 | "The requested URL was not found on this server." },
|
---|
273 | { HTTP_BAD_REQUEST, "Bad Request", "Unsupported method." },
|
---|
274 | { HTTP_FORBIDDEN, "Forbidden", "" },
|
---|
275 | { HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error",
|
---|
276 | "Internal Server Error" },
|
---|
277 | #if 0 /* not implemented */
|
---|
278 | { HTTP_CREATED, "Created" },
|
---|
279 | { HTTP_ACCEPTED, "Accepted" },
|
---|
280 | { HTTP_NO_CONTENT, "No Content" },
|
---|
281 | { HTTP_MULTIPLE_CHOICES, "Multiple Choices" },
|
---|
282 | { HTTP_MOVED_PERMANENTLY, "Moved Permanently" },
|
---|
283 | { HTTP_NOT_MODIFIED, "Not Modified" },
|
---|
284 | { HTTP_BAD_GATEWAY, "Bad Gateway", "" },
|
---|
285 | { HTTP_SERVICE_UNAVAILABLE, "Service Unavailable", "" },
|
---|
286 | #endif
|
---|
287 | };
|
---|
288 |
|
---|
289 |
|
---|
290 | static const char RFC1123FMT[] = "%a, %d %b %Y %H:%M:%S GMT";
|
---|
291 | static const char Content_length[] = "Content-length:";
|
---|
292 |
|
---|
293 |
|
---|
294 | static int
|
---|
295 | scan_ip (const char **ep, unsigned int *ip, unsigned char endc)
|
---|
296 | {
|
---|
297 | const char *p = *ep;
|
---|
298 | int auto_mask = 8;
|
---|
299 | int j;
|
---|
300 |
|
---|
301 | *ip = 0;
|
---|
302 | for (j = 0; j < 4; j++) {
|
---|
303 | unsigned int octet;
|
---|
304 |
|
---|
305 | if ((*p < '0' || *p > '9') && (*p != '/' || j == 0) && *p != 0)
|
---|
306 | return -auto_mask;
|
---|
307 | octet = 0;
|
---|
308 | while (*p >= '0' && *p <= '9') {
|
---|
309 | octet *= 10;
|
---|
310 | octet += *p - '0';
|
---|
311 | if (octet > 255)
|
---|
312 | return -auto_mask;
|
---|
313 | p++;
|
---|
314 | }
|
---|
315 | if (*p == '.')
|
---|
316 | p++;
|
---|
317 | if (*p != '/' && *p != 0)
|
---|
318 | auto_mask += 8;
|
---|
319 | *ip = ((*ip) << 8) | octet;
|
---|
320 | }
|
---|
321 | if (*p != 0) {
|
---|
322 | if (*p != endc)
|
---|
323 | return -auto_mask;
|
---|
324 | p++;
|
---|
325 | if(*p == 0)
|
---|
326 | return -auto_mask;
|
---|
327 | }
|
---|
328 | *ep = p;
|
---|
329 | return auto_mask;
|
---|
330 | }
|
---|
331 |
|
---|
332 | static int
|
---|
333 | scan_ip_mask (const char *ipm, unsigned int *ip, unsigned int *mask)
|
---|
334 | {
|
---|
335 | int i;
|
---|
336 | unsigned int msk;
|
---|
337 |
|
---|
338 | i = scan_ip(&ipm, ip, '/');
|
---|
339 | if(i < 0)
|
---|
340 | return i;
|
---|
341 | if(*ipm) {
|
---|
342 | const char *p = ipm;
|
---|
343 |
|
---|
344 | i = 0;
|
---|
345 | while (*p) {
|
---|
346 | if (*p < '0' || *p > '9') {
|
---|
347 | if (*p == '.') {
|
---|
348 | i = scan_ip (&ipm, mask, 0);
|
---|
349 | return i != 32;
|
---|
350 | }
|
---|
351 | return -1;
|
---|
352 | }
|
---|
353 | i *= 10;
|
---|
354 | i += *p - '0';
|
---|
355 | p++;
|
---|
356 | }
|
---|
357 | }
|
---|
358 | if (i > 32 || i < 0)
|
---|
359 | return -1;
|
---|
360 | msk = 0x80000000;
|
---|
361 | *mask = 0;
|
---|
362 | while (i > 0) {
|
---|
363 | *mask |= msk;
|
---|
364 | msk >>= 1;
|
---|
365 | i--;
|
---|
366 | }
|
---|
367 | return 0;
|
---|
368 | }
|
---|
369 |
|
---|
370 | #if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
|
---|
371 | static void free_config_lines(Htaccess **pprev)
|
---|
372 | {
|
---|
373 | Htaccess *prev = *pprev;
|
---|
374 |
|
---|
375 | while( prev ) {
|
---|
376 | Htaccess *cur = prev;
|
---|
377 |
|
---|
378 | prev = cur->next;
|
---|
379 | free(cur);
|
---|
380 | }
|
---|
381 | *pprev = NULL;
|
---|
382 | }
|
---|
383 | #endif
|
---|
384 |
|
---|
385 | /* flag */
|
---|
386 | #define FIRST_PARSE 0
|
---|
387 | #define SUBDIR_PARSE 1
|
---|
388 | #define SIGNALED_PARSE 2
|
---|
389 | #define FIND_FROM_HTTPD_ROOT 3
|
---|
390 | /****************************************************************************
|
---|
391 | *
|
---|
392 | > $Function: parse_conf()
|
---|
393 | *
|
---|
394 | * $Description: parse configuration file into in-memory linked list.
|
---|
395 | *
|
---|
396 | * The first non-white character is examined to determine if the config line
|
---|
397 | * is one of the following:
|
---|
398 | * .ext:mime/type # new mime type not compiled into httpd
|
---|
399 | * [adAD]:from # ip address allow/deny, * for wildcard
|
---|
400 | * /path:user:pass # username/password
|
---|
401 | *
|
---|
402 | * Any previous IP rules are discarded.
|
---|
403 | * If the flag argument is not SUBDIR_PARSE then all /path and mime rules
|
---|
404 | * are also discarded. That is, previous settings are retained if flag is
|
---|
405 | * SUBDIR_PARSE.
|
---|
406 | *
|
---|
407 | * $Parameters:
|
---|
408 | * (const char *) path . . null for ip address checks, path for password
|
---|
409 | * checks.
|
---|
410 | * (int) flag . . . . . . the source of the parse request.
|
---|
411 | *
|
---|
412 | * $Return: (None)
|
---|
413 | *
|
---|
414 | ****************************************************************************/
|
---|
415 | static void parse_conf(const char *path, int flag)
|
---|
416 | {
|
---|
417 | FILE *f;
|
---|
418 | #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
|
---|
419 | Htaccess *prev, *cur;
|
---|
420 | #elif CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
|
---|
421 | Htaccess *cur;
|
---|
422 | #endif
|
---|
423 |
|
---|
424 | const char *cf = config->configFile;
|
---|
425 | char buf[160];
|
---|
426 | char *p0 = NULL;
|
---|
427 | char *c, *p;
|
---|
428 |
|
---|
429 | /* free previous ip setup if present */
|
---|
430 | Htaccess_IP *pip = config->ip_a_d;
|
---|
431 |
|
---|
432 | while( pip ) {
|
---|
433 | Htaccess_IP *cur_ipl = pip;
|
---|
434 |
|
---|
435 | pip = cur_ipl->next;
|
---|
436 | free(cur_ipl);
|
---|
437 | }
|
---|
438 | config->ip_a_d = NULL;
|
---|
439 |
|
---|
440 | config->flg_deny_all = 0;
|
---|
441 |
|
---|
442 | #if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR)
|
---|
443 | /* retain previous auth and mime config only for subdir parse */
|
---|
444 | if(flag != SUBDIR_PARSE) {
|
---|
445 | #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
|
---|
446 | free_config_lines(&config->auth);
|
---|
447 | #endif
|
---|
448 | #ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
|
---|
449 | free_config_lines(&config->mime_a);
|
---|
450 | #endif
|
---|
451 | #ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
|
---|
452 | free_config_lines(&config->script_i);
|
---|
453 | #endif
|
---|
454 | }
|
---|
455 | #endif
|
---|
456 |
|
---|
457 | if(flag == SUBDIR_PARSE || cf == NULL) {
|
---|
458 | cf = alloca(strlen(path) + sizeof(httpd_conf) + 2);
|
---|
459 | if(cf == NULL) {
|
---|
460 | if(flag == FIRST_PARSE)
|
---|
461 | bb_error_msg_and_die(bb_msg_memory_exhausted);
|
---|
462 | return;
|
---|
463 | }
|
---|
464 | sprintf((char *)cf, "%s/%s", path, httpd_conf);
|
---|
465 | }
|
---|
466 |
|
---|
467 | while((f = fopen(cf, "r")) == NULL) {
|
---|
468 | if(flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) {
|
---|
469 | /* config file not found, no changes to config */
|
---|
470 | return;
|
---|
471 | }
|
---|
472 | if(config->configFile && flag == FIRST_PARSE) /* if -c option given */
|
---|
473 | bb_perror_msg_and_die("%s", cf);
|
---|
474 | flag = FIND_FROM_HTTPD_ROOT;
|
---|
475 | cf = httpd_conf;
|
---|
476 | }
|
---|
477 |
|
---|
478 | #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
|
---|
479 | prev = config->auth;
|
---|
480 | #endif
|
---|
481 | /* This could stand some work */
|
---|
482 | while ( (p0 = fgets(buf, sizeof(buf), f)) != NULL) {
|
---|
483 | c = NULL;
|
---|
484 | for(p = p0; *p0 != 0 && *p0 != '#'; p0++) {
|
---|
485 | if(!isspace(*p0)) {
|
---|
486 | *p++ = *p0;
|
---|
487 | if(*p0 == ':' && c == NULL)
|
---|
488 | c = p;
|
---|
489 | }
|
---|
490 | }
|
---|
491 | *p = 0;
|
---|
492 |
|
---|
493 | /* test for empty or strange line */
|
---|
494 | if (c == NULL || *c == 0)
|
---|
495 | continue;
|
---|
496 | p0 = buf;
|
---|
497 | if(*p0 == 'd')
|
---|
498 | *p0 = 'D';
|
---|
499 | if(*c == '*') {
|
---|
500 | if(*p0 == 'D') {
|
---|
501 | /* memorize deny all */
|
---|
502 | config->flg_deny_all++;
|
---|
503 | }
|
---|
504 | /* skip default other "word:*" config lines */
|
---|
505 | continue;
|
---|
506 | }
|
---|
507 |
|
---|
508 | if(*p0 == 'a')
|
---|
509 | *p0 = 'A';
|
---|
510 | else if(*p0 != 'D' && *p0 != 'A'
|
---|
511 | #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
|
---|
512 | && *p0 != '/'
|
---|
513 | #endif
|
---|
514 | #ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
|
---|
515 | && *p0 != '.'
|
---|
516 | #endif
|
---|
517 | #ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
|
---|
518 | && *p0 != '*'
|
---|
519 | #endif
|
---|
520 | )
|
---|
521 | continue;
|
---|
522 | if(*p0 == 'A' || *p0 == 'D') {
|
---|
523 | /* storing current config IP line */
|
---|
524 | pip = calloc(1, sizeof(Htaccess_IP));
|
---|
525 | if(pip) {
|
---|
526 | if(scan_ip_mask (c, &(pip->ip), &(pip->mask))) {
|
---|
527 | /* syntax IP{/mask} error detected, protect all */
|
---|
528 | *p0 = 'D';
|
---|
529 | pip->mask = 0;
|
---|
530 | }
|
---|
531 | pip->allow_deny = *p0;
|
---|
532 | if(*p0 == 'D') {
|
---|
533 | /* Deny:form_IP move top */
|
---|
534 | pip->next = config->ip_a_d;
|
---|
535 | config->ip_a_d = pip;
|
---|
536 | } else {
|
---|
537 | /* add to bottom A:form_IP config line */
|
---|
538 | Htaccess_IP *prev_IP = config->ip_a_d;
|
---|
539 |
|
---|
540 | if(prev_IP == NULL) {
|
---|
541 | config->ip_a_d = pip;
|
---|
542 | } else {
|
---|
543 | while(prev_IP->next)
|
---|
544 | prev_IP = prev_IP->next;
|
---|
545 | prev_IP->next = pip;
|
---|
546 | }
|
---|
547 | }
|
---|
548 | }
|
---|
549 | continue;
|
---|
550 | }
|
---|
551 | #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
|
---|
552 | if(*p0 == '/') {
|
---|
553 | /* make full path from httpd root / curent_path / config_line_path */
|
---|
554 | cf = flag == SUBDIR_PARSE ? path : "";
|
---|
555 | p0 = malloc(strlen(cf) + (c - buf) + 2 + strlen(c));
|
---|
556 | if(p0 == NULL)
|
---|
557 | continue;
|
---|
558 | c[-1] = 0;
|
---|
559 | sprintf(p0, "/%s%s", cf, buf);
|
---|
560 |
|
---|
561 | /* another call bb_simplify_path */
|
---|
562 | cf = p = p0;
|
---|
563 |
|
---|
564 | do {
|
---|
565 | if (*p == '/') {
|
---|
566 | if (*cf == '/') { /* skip duplicate (or initial) slash */
|
---|
567 | continue;
|
---|
568 | } else if (*cf == '.') {
|
---|
569 | if (cf[1] == '/' || cf[1] == 0) { /* remove extra '.' */
|
---|
570 | continue;
|
---|
571 | } else if ((cf[1] == '.') && (cf[2] == '/' || cf[2] == 0)) {
|
---|
572 | ++cf;
|
---|
573 | if (p > p0) {
|
---|
574 | while (*--p != '/'); /* omit previous dir */
|
---|
575 | }
|
---|
576 | continue;
|
---|
577 | }
|
---|
578 | }
|
---|
579 | }
|
---|
580 | *++p = *cf;
|
---|
581 | } while (*++cf);
|
---|
582 |
|
---|
583 | if ((p == p0) || (*p != '/')) { /* not a trailing slash */
|
---|
584 | ++p; /* so keep last character */
|
---|
585 | }
|
---|
586 | *p = 0;
|
---|
587 | sprintf(p0, "%s:%s", p0, c);
|
---|
588 | }
|
---|
589 | #endif
|
---|
590 |
|
---|
591 | #if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR)
|
---|
592 | /* storing current config line */
|
---|
593 | cur = calloc(1, sizeof(Htaccess) + strlen(p0));
|
---|
594 | if(cur) {
|
---|
595 | cf = strcpy(cur->before_colon, p0);
|
---|
596 | c = strchr(cf, ':');
|
---|
597 | *c++ = 0;
|
---|
598 | cur->after_colon = c;
|
---|
599 | #ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
|
---|
600 | if(*cf == '.') {
|
---|
601 | /* config .mime line move top for overwrite previous */
|
---|
602 | cur->next = config->mime_a;
|
---|
603 | config->mime_a = cur;
|
---|
604 | continue;
|
---|
605 | }
|
---|
606 | #endif
|
---|
607 | #ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
|
---|
608 | if(*cf == '*' && cf[1] == '.') {
|
---|
609 | /* config script interpreter line move top for overwrite previous */
|
---|
610 | cur->next = config->script_i;
|
---|
611 | config->script_i = cur;
|
---|
612 | continue;
|
---|
613 | }
|
---|
614 | #endif
|
---|
615 | #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
|
---|
616 | free(p0);
|
---|
617 | if(prev == NULL) {
|
---|
618 | /* first line */
|
---|
619 | config->auth = prev = cur;
|
---|
620 | } else {
|
---|
621 | /* sort path, if current lenght eq or bigger then move up */
|
---|
622 | Htaccess *prev_hti = config->auth;
|
---|
623 | size_t l = strlen(cf);
|
---|
624 | Htaccess *hti;
|
---|
625 |
|
---|
626 | for(hti = prev_hti; hti; hti = hti->next) {
|
---|
627 | if(l >= strlen(hti->before_colon)) {
|
---|
628 | /* insert before hti */
|
---|
629 | cur->next = hti;
|
---|
630 | if(prev_hti != hti) {
|
---|
631 | prev_hti->next = cur;
|
---|
632 | } else {
|
---|
633 | /* insert as top */
|
---|
634 | config->auth = cur;
|
---|
635 | }
|
---|
636 | break;
|
---|
637 | }
|
---|
638 | if(prev_hti != hti)
|
---|
639 | prev_hti = prev_hti->next;
|
---|
640 | }
|
---|
641 | if(!hti) { /* not inserted, add to bottom */
|
---|
642 | prev->next = cur;
|
---|
643 | prev = cur;
|
---|
644 | }
|
---|
645 | }
|
---|
646 | #endif
|
---|
647 | }
|
---|
648 | #endif
|
---|
649 | }
|
---|
650 | fclose(f);
|
---|
651 | }
|
---|
652 |
|
---|
653 | #ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
|
---|
654 | /****************************************************************************
|
---|
655 | *
|
---|
656 | > $Function: encodeString()
|
---|
657 | *
|
---|
658 | * $Description: Given a string, html encode special characters.
|
---|
659 | * This is used for the -e command line option to provide an easy way
|
---|
660 | * for scripts to encode result data without confusing browsers. The
|
---|
661 | * returned string pointer is memory allocated by malloc().
|
---|
662 | *
|
---|
663 | * $Parameters:
|
---|
664 | * (const char *) string . . The first string to encode.
|
---|
665 | *
|
---|
666 | * $Return: (char *) . . . .. . . A pointer to the encoded string.
|
---|
667 | *
|
---|
668 | * $Errors: Returns a null string ("") if memory is not available.
|
---|
669 | *
|
---|
670 | ****************************************************************************/
|
---|
671 | static char *encodeString(const char *string)
|
---|
672 | {
|
---|
673 | /* take the simple route and encode everything */
|
---|
674 | /* could possibly scan once to get length. */
|
---|
675 | int len = strlen(string);
|
---|
676 | char *out = malloc(len * 6 + 1);
|
---|
677 | char *p=out;
|
---|
678 | char ch;
|
---|
679 |
|
---|
680 | if (!out) return "";
|
---|
681 | while ((ch = *string++)) {
|
---|
682 | // very simple check for what to encode
|
---|
683 | if (isalnum(ch)) *p++ = ch;
|
---|
684 | else p += sprintf(p, "&#%d;", (unsigned char) ch);
|
---|
685 | }
|
---|
686 | *p=0;
|
---|
687 | return out;
|
---|
688 | }
|
---|
689 | #endif /* CONFIG_FEATURE_HTTPD_ENCODE_URL_STR */
|
---|
690 |
|
---|
691 | /****************************************************************************
|
---|
692 | *
|
---|
693 | > $Function: decodeString()
|
---|
694 | *
|
---|
695 | * $Description: Given a URL encoded string, convert it to plain ascii.
|
---|
696 | * Since decoding always makes strings smaller, the decode is done in-place.
|
---|
697 | * Thus, callers should strdup() the argument if they do not want the
|
---|
698 | * argument modified. The return is the original pointer, allowing this
|
---|
699 | * function to be easily used as arguments to other functions.
|
---|
700 | *
|
---|
701 | * $Parameters:
|
---|
702 | * (char *) string . . . The first string to decode.
|
---|
703 | * (int) flag . . . 1 if require decode '+' as ' ' for CGI
|
---|
704 | *
|
---|
705 | * $Return: (char *) . . . . A pointer to the decoded string (same as input).
|
---|
706 | *
|
---|
707 | * $Errors: None
|
---|
708 | *
|
---|
709 | ****************************************************************************/
|
---|
710 | static char *decodeString(char *orig, int flag_plus_to_space)
|
---|
711 | {
|
---|
712 | /* note that decoded string is always shorter than original */
|
---|
713 | char *string = orig;
|
---|
714 | char *ptr = string;
|
---|
715 |
|
---|
716 | while (*ptr)
|
---|
717 | {
|
---|
718 | if (*ptr == '+' && flag_plus_to_space) { *string++ = ' '; ptr++; }
|
---|
719 | else if (*ptr != '%') *string++ = *ptr++;
|
---|
720 | else {
|
---|
721 | unsigned int value1, value2;
|
---|
722 |
|
---|
723 | ptr++;
|
---|
724 | if(sscanf(ptr, "%1X", &value1) != 1 ||
|
---|
725 | sscanf(ptr+1, "%1X", &value2) != 1) {
|
---|
726 | if(!flag_plus_to_space)
|
---|
727 | return NULL;
|
---|
728 | *string++ = '%';
|
---|
729 | } else {
|
---|
730 | value1 = value1 * 16 + value2;
|
---|
731 | if(value1 == '/' || value1 == 0)
|
---|
732 | return orig+1;
|
---|
733 | *string++ = value1;
|
---|
734 | ptr += 2;
|
---|
735 | }
|
---|
736 | }
|
---|
737 | }
|
---|
738 | *string = '\0';
|
---|
739 | return orig;
|
---|
740 | }
|
---|
741 |
|
---|
742 |
|
---|
743 | #ifdef CONFIG_FEATURE_HTTPD_CGI
|
---|
744 | /****************************************************************************
|
---|
745 | *
|
---|
746 | > $Function: addEnv()
|
---|
747 | *
|
---|
748 | * $Description: Add an environment variable setting to the global list.
|
---|
749 | * A NAME=VALUE string is allocated, filled, and added to the list of
|
---|
750 | * environment settings passed to the cgi execution script.
|
---|
751 | *
|
---|
752 | * $Parameters:
|
---|
753 | * (char *) name_before_underline - The first part environment variable name.
|
---|
754 | * (char *) name_after_underline - The second part environment variable name.
|
---|
755 | * (char *) value . . The value to which the env variable is set.
|
---|
756 | *
|
---|
757 | * $Return: (void)
|
---|
758 | *
|
---|
759 | * $Errors: Silently returns if the env runs out of space to hold the new item
|
---|
760 | *
|
---|
761 | ****************************************************************************/
|
---|
762 | static void addEnv(const char *name_before_underline,
|
---|
763 | const char *name_after_underline, const char *value)
|
---|
764 | {
|
---|
765 | char *s = NULL;
|
---|
766 | const char *underline;
|
---|
767 |
|
---|
768 | if (!value)
|
---|
769 | value = "";
|
---|
770 | underline = *name_after_underline ? "_" : "";
|
---|
771 | asprintf(&s, "%s%s%s=%s", name_before_underline, underline,
|
---|
772 | name_after_underline, value);
|
---|
773 | if(s) {
|
---|
774 | putenv(s);
|
---|
775 | }
|
---|
776 | }
|
---|
777 |
|
---|
778 | #if defined(CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV) || defined(CONFIG_FEATURE_HTTPD_WITHOUT_INETD)
|
---|
779 | /* set environs SERVER_PORT and REMOTE_PORT */
|
---|
780 | static void addEnvPort(const char *port_name)
|
---|
781 | {
|
---|
782 | char buf[16];
|
---|
783 |
|
---|
784 | sprintf(buf, "%u", config->port);
|
---|
785 | addEnv(port_name, "PORT", buf);
|
---|
786 | }
|
---|
787 | #endif
|
---|
788 | #endif /* CONFIG_FEATURE_HTTPD_CGI */
|
---|
789 |
|
---|
790 |
|
---|
791 | #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
|
---|
792 | /****************************************************************************
|
---|
793 | *
|
---|
794 | > $Function: decodeBase64()
|
---|
795 | *
|
---|
796 | > $Description: Decode a base 64 data stream as per rfc1521.
|
---|
797 | * Note that the rfc states that none base64 chars are to be ignored.
|
---|
798 | * Since the decode always results in a shorter size than the input, it is
|
---|
799 | * OK to pass the input arg as an output arg.
|
---|
800 | *
|
---|
801 | * $Parameter:
|
---|
802 | * (char *) Data . . . . A pointer to a base64 encoded string.
|
---|
803 | * Where to place the decoded data.
|
---|
804 | *
|
---|
805 | * $Return: void
|
---|
806 | *
|
---|
807 | * $Errors: None
|
---|
808 | *
|
---|
809 | ****************************************************************************/
|
---|
810 | static void decodeBase64(char *Data)
|
---|
811 | {
|
---|
812 |
|
---|
813 | const unsigned char *in = (const unsigned char *)Data;
|
---|
814 | // The decoded size will be at most 3/4 the size of the encoded
|
---|
815 | unsigned long ch = 0;
|
---|
816 | int i = 0;
|
---|
817 |
|
---|
818 | while (*in) {
|
---|
819 | int t = *in++;
|
---|
820 |
|
---|
821 | if(t >= '0' && t <= '9')
|
---|
822 | t = t - '0' + 52;
|
---|
823 | else if(t >= 'A' && t <= 'Z')
|
---|
824 | t = t - 'A';
|
---|
825 | else if(t >= 'a' && t <= 'z')
|
---|
826 | t = t - 'a' + 26;
|
---|
827 | else if(t == '+')
|
---|
828 | t = 62;
|
---|
829 | else if(t == '/')
|
---|
830 | t = 63;
|
---|
831 | else if(t == '=')
|
---|
832 | t = 0;
|
---|
833 | else
|
---|
834 | continue;
|
---|
835 |
|
---|
836 | ch = (ch << 6) | t;
|
---|
837 | i++;
|
---|
838 | if (i == 4) {
|
---|
839 | *Data++ = (char) (ch >> 16);
|
---|
840 | *Data++ = (char) (ch >> 8);
|
---|
841 | *Data++ = (char) ch;
|
---|
842 | i = 0;
|
---|
843 | }
|
---|
844 | }
|
---|
845 | *Data = 0;
|
---|
846 | }
|
---|
847 | #endif
|
---|
848 |
|
---|
849 |
|
---|
850 | #ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
|
---|
851 | /****************************************************************************
|
---|
852 | *
|
---|
853 | > $Function: openServer()
|
---|
854 | *
|
---|
855 | * $Description: create a listen server socket on the designated port.
|
---|
856 | *
|
---|
857 | * $Return: (int) . . . A connection socket. -1 for errors.
|
---|
858 | *
|
---|
859 | * $Errors: None
|
---|
860 | *
|
---|
861 | ****************************************************************************/
|
---|
862 | static int openServer(void)
|
---|
863 | {
|
---|
864 | struct sockaddr_in lsocket;
|
---|
865 | int fd;
|
---|
866 |
|
---|
867 | /* create the socket right now */
|
---|
868 | /* inet_addr() returns a value that is already in network order */
|
---|
869 | memset(&lsocket, 0, sizeof(lsocket));
|
---|
870 | lsocket.sin_family = AF_INET;
|
---|
871 | lsocket.sin_addr.s_addr = INADDR_ANY;
|
---|
872 | lsocket.sin_port = htons(config->port);
|
---|
873 | fd = bb_xsocket(AF_INET, SOCK_STREAM, 0);
|
---|
874 | /* tell the OS it's OK to reuse a previous address even though */
|
---|
875 | /* it may still be in a close down state. Allows bind to succeed. */
|
---|
876 | int on = 1;
|
---|
877 | #ifdef SO_REUSEPORT
|
---|
878 | setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *)&on, sizeof(on)) ;
|
---|
879 | #else
|
---|
880 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)) ;
|
---|
881 | #endif
|
---|
882 | bb_xbind(fd, (struct sockaddr *)&lsocket, sizeof(lsocket));
|
---|
883 | listen(fd, 9); /* bb_xlisten? */
|
---|
884 | signal(SIGCHLD, SIG_IGN); /* prevent zombie (defunct) processes */
|
---|
885 | return fd;
|
---|
886 | }
|
---|
887 | #endif /* CONFIG_FEATURE_HTTPD_WITHOUT_INETD */
|
---|
888 |
|
---|
889 | /****************************************************************************
|
---|
890 | *
|
---|
891 | > $Function: sendHeaders()
|
---|
892 | *
|
---|
893 | * $Description: Create and send HTTP response headers.
|
---|
894 | * The arguments are combined and sent as one write operation. Note that
|
---|
895 | * IE will puke big-time if the headers are not sent in one packet and the
|
---|
896 | * second packet is delayed for any reason.
|
---|
897 | *
|
---|
898 | * $Parameter:
|
---|
899 | * (HttpResponseNum) responseNum . . . The result code to send.
|
---|
900 | *
|
---|
901 | * $Return: (int) . . . . writing errors
|
---|
902 | *
|
---|
903 | ****************************************************************************/
|
---|
904 | static int sendHeaders(HttpResponseNum responseNum)
|
---|
905 | {
|
---|
906 | char *buf = config->buf;
|
---|
907 | const char *responseString = "";
|
---|
908 | const char *infoString = 0;
|
---|
909 | const char *mime_type;
|
---|
910 | unsigned int i;
|
---|
911 | time_t timer = time(0);
|
---|
912 | char timeStr[80];
|
---|
913 | int len;
|
---|
914 |
|
---|
915 | for (i = 0;
|
---|
916 | i < (sizeof(httpResponseNames)/sizeof(httpResponseNames[0])); i++) {
|
---|
917 | if (httpResponseNames[i].type == responseNum) {
|
---|
918 | responseString = httpResponseNames[i].name;
|
---|
919 | infoString = httpResponseNames[i].info;
|
---|
920 | break;
|
---|
921 | }
|
---|
922 | }
|
---|
923 | /* error message is HTML */
|
---|
924 | mime_type = responseNum == HTTP_OK ?
|
---|
925 | config->httpd_found.found_mime_type : "text/html";
|
---|
926 |
|
---|
927 | /* emit the current date */
|
---|
928 | strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&timer));
|
---|
929 | len = sprintf(buf,
|
---|
930 | "HTTP/1.0 %d %s\r\nContent-type: %s\r\n"
|
---|
931 | "Date: %s\r\nConnection: close\r\n",
|
---|
932 | responseNum, responseString, mime_type, timeStr);
|
---|
933 |
|
---|
934 | #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
|
---|
935 | if (responseNum == HTTP_UNAUTHORIZED) {
|
---|
936 | len += sprintf(buf+len, "WWW-Authenticate: Basic realm=\"%s\"\r\n",
|
---|
937 | config->realm);
|
---|
938 | }
|
---|
939 | #endif
|
---|
940 | if(responseNum == HTTP_MOVED_TEMPORARILY) {
|
---|
941 | len += sprintf(buf+len, "Location: %s/%s%s\r\n",
|
---|
942 | config->httpd_found.found_moved_temporarily,
|
---|
943 | (config->query ? "?" : ""),
|
---|
944 | (config->query ? config->query : ""));
|
---|
945 | }
|
---|
946 |
|
---|
947 | if (config->ContentLength != -1) { /* file */
|
---|
948 | strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&config->last_mod));
|
---|
949 | len += sprintf(buf+len, "Last-Modified: %s\r\n%s " cont_l_fmt "\r\n",
|
---|
950 | timeStr, Content_length, cont_l_type config->ContentLength);
|
---|
951 | }
|
---|
952 | strcat(buf, "\r\n");
|
---|
953 | len += 2;
|
---|
954 | if (infoString) {
|
---|
955 | len += sprintf(buf+len,
|
---|
956 | "<HEAD><TITLE>%d %s</TITLE></HEAD>\n"
|
---|
957 | "<BODY><H1>%d %s</H1>\n%s\n</BODY>\n",
|
---|
958 | responseNum, responseString,
|
---|
959 | responseNum, responseString, infoString);
|
---|
960 | }
|
---|
961 | #if DEBUG
|
---|
962 | fprintf(stderr, "Headers: '%s'", buf);
|
---|
963 | #endif
|
---|
964 | return bb_full_write(a_c_w, buf, len);
|
---|
965 | }
|
---|
966 |
|
---|
967 | /****************************************************************************
|
---|
968 | *
|
---|
969 | > $Function: getLine()
|
---|
970 | *
|
---|
971 | * $Description: Read from the socket until an end of line char found.
|
---|
972 | *
|
---|
973 | * Characters are read one at a time until an eol sequence is found.
|
---|
974 | *
|
---|
975 | * $Return: (int) . . . . number of characters read. -1 if error.
|
---|
976 | *
|
---|
977 | ****************************************************************************/
|
---|
978 | static int getLine(void)
|
---|
979 | {
|
---|
980 | int count = 0;
|
---|
981 | char *buf = config->buf;
|
---|
982 |
|
---|
983 | while (read(a_c_r, buf + count, 1) == 1) {
|
---|
984 | if (buf[count] == '\r') continue;
|
---|
985 | if (buf[count] == '\n') {
|
---|
986 | buf[count] = 0;
|
---|
987 | return count;
|
---|
988 | }
|
---|
989 | if(count < (MAX_MEMORY_BUFF-1)) /* check owerflow */
|
---|
990 | count++;
|
---|
991 | }
|
---|
992 | if (count) return count;
|
---|
993 | else return -1;
|
---|
994 | }
|
---|
995 |
|
---|
996 | #ifdef CONFIG_FEATURE_HTTPD_CGI
|
---|
997 | /****************************************************************************
|
---|
998 | *
|
---|
999 | > $Function: sendCgi()
|
---|
1000 | *
|
---|
1001 | * $Description: Execute a CGI script and send it's stdout back
|
---|
1002 | *
|
---|
1003 | * Environment variables are set up and the script is invoked with pipes
|
---|
1004 | * for stdin/stdout. If a post is being done the script is fed the POST
|
---|
1005 | * data in addition to setting the QUERY_STRING variable (for GETs or POSTs).
|
---|
1006 | *
|
---|
1007 | * $Parameters:
|
---|
1008 | * (const char *) url . . . . . . The requested URL (with leading /).
|
---|
1009 | * (int bodyLen) . . . . . . . . Length of the post body.
|
---|
1010 | * (const char *cookie) . . . . . For set HTTP_COOKIE.
|
---|
1011 | * (const char *content_type) . . For set CONTENT_TYPE.
|
---|
1012 |
|
---|
1013 | *
|
---|
1014 | * $Return: (char *) . . . . A pointer to the decoded string (same as input).
|
---|
1015 | *
|
---|
1016 | * $Errors: None
|
---|
1017 | *
|
---|
1018 | ****************************************************************************/
|
---|
1019 | static int sendCgi(const char *url,
|
---|
1020 | const char *request, int bodyLen, const char *cookie,
|
---|
1021 | const char *content_type)
|
---|
1022 | {
|
---|
1023 | int fromCgi[2]; /* pipe for reading data from CGI */
|
---|
1024 | int toCgi[2]; /* pipe for sending data to CGI */
|
---|
1025 |
|
---|
1026 | static char * argp[] = { 0, 0 };
|
---|
1027 | int pid = 0;
|
---|
1028 | int inFd;
|
---|
1029 | int outFd;
|
---|
1030 | int firstLine = 1;
|
---|
1031 |
|
---|
1032 | do {
|
---|
1033 | if (pipe(fromCgi) != 0) {
|
---|
1034 | break;
|
---|
1035 | }
|
---|
1036 | if (pipe(toCgi) != 0) {
|
---|
1037 | break;
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | pid = fork();
|
---|
1041 | if (pid < 0) {
|
---|
1042 | pid = 0;
|
---|
1043 | break;
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | if (!pid) {
|
---|
1047 | /* child process */
|
---|
1048 | char *script;
|
---|
1049 | char *purl = strdup( url );
|
---|
1050 | char realpath_buff[MAXPATHLEN];
|
---|
1051 |
|
---|
1052 | if(purl == NULL)
|
---|
1053 | _exit(242);
|
---|
1054 |
|
---|
1055 | inFd = toCgi[0];
|
---|
1056 | outFd = fromCgi[1];
|
---|
1057 |
|
---|
1058 | dup2(inFd, 0); // replace stdin with the pipe
|
---|
1059 | dup2(outFd, 1); // replace stdout with the pipe
|
---|
1060 | if(!DEBUG)
|
---|
1061 | dup2(outFd, 2); // replace stderr with the pipe
|
---|
1062 |
|
---|
1063 | close(toCgi[0]);
|
---|
1064 | close(toCgi[1]);
|
---|
1065 | close(fromCgi[0]);
|
---|
1066 | close(fromCgi[1]);
|
---|
1067 |
|
---|
1068 | /*
|
---|
1069 | * Find PATH_INFO.
|
---|
1070 | */
|
---|
1071 | script = purl;
|
---|
1072 | while((script = strchr( script + 1, '/' )) != NULL) {
|
---|
1073 | /* have script.cgi/PATH_INFO or dirs/script.cgi[/PATH_INFO] */
|
---|
1074 | struct stat sb;
|
---|
1075 |
|
---|
1076 | *script = '\0';
|
---|
1077 | if(is_directory(purl + 1, 1, &sb) == 0) {
|
---|
1078 | /* not directory, found script.cgi/PATH_INFO */
|
---|
1079 | *script = '/';
|
---|
1080 | break;
|
---|
1081 | }
|
---|
1082 | *script = '/'; /* is directory, find next '/' */
|
---|
1083 | }
|
---|
1084 | addEnv("PATH", "INFO", script); /* set /PATH_INFO or NULL */
|
---|
1085 | addEnv("PATH", "", getenv("PATH"));
|
---|
1086 | addEnv("REQUEST", "METHOD", request);
|
---|
1087 | if(config->query) {
|
---|
1088 | char *uri = alloca(strlen(purl) + 2 + strlen(config->query));
|
---|
1089 | if(uri)
|
---|
1090 | sprintf(uri, "%s?%s", purl, config->query);
|
---|
1091 | addEnv("REQUEST", "URI", uri);
|
---|
1092 | } else {
|
---|
1093 | addEnv("REQUEST", "URI", purl);
|
---|
1094 | }
|
---|
1095 | if(script != NULL)
|
---|
1096 | *script = '\0'; /* reduce /PATH_INFO */
|
---|
1097 | /* SCRIPT_FILENAME required by PHP in CGI mode */
|
---|
1098 | if(realpath(purl + 1, realpath_buff))
|
---|
1099 | addEnv("SCRIPT", "FILENAME", realpath_buff);
|
---|
1100 | else
|
---|
1101 | *realpath_buff = 0;
|
---|
1102 | /* set SCRIPT_NAME as full path: /cgi-bin/dirs/script.cgi */
|
---|
1103 | addEnv("SCRIPT_NAME", "", purl);
|
---|
1104 | addEnv("QUERY_STRING", "", config->query);
|
---|
1105 | addEnv("SERVER", "SOFTWARE", httpdVersion);
|
---|
1106 | addEnv("SERVER", "PROTOCOL", "HTTP/1.0");
|
---|
1107 | addEnv("GATEWAY_INTERFACE", "", "CGI/1.1");
|
---|
1108 | addEnv("REMOTE", "ADDR", config->rmt_ip_str);
|
---|
1109 | #ifdef CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
|
---|
1110 | addEnvPort("REMOTE");
|
---|
1111 | #endif
|
---|
1112 | if(bodyLen) {
|
---|
1113 | char sbl[32];
|
---|
1114 |
|
---|
1115 | sprintf(sbl, "%d", bodyLen);
|
---|
1116 | addEnv("CONTENT", "LENGTH", sbl);
|
---|
1117 | }
|
---|
1118 | if(cookie)
|
---|
1119 | addEnv("HTTP", "COOKIE", cookie);
|
---|
1120 | if(content_type)
|
---|
1121 | addEnv("CONTENT", "TYPE", content_type);
|
---|
1122 | #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
|
---|
1123 | if(config->remoteuser) {
|
---|
1124 | addEnv("REMOTE", "USER", config->remoteuser);
|
---|
1125 | addEnv("AUTH_TYPE", "", "Basic");
|
---|
1126 | }
|
---|
1127 | #endif
|
---|
1128 | if(config->referer)
|
---|
1129 | addEnv("HTTP", "REFERER", config->referer);
|
---|
1130 |
|
---|
1131 | /* set execve argp[0] without path */
|
---|
1132 | argp[0] = strrchr( purl, '/' ) + 1;
|
---|
1133 | /* but script argp[0] must have absolute path and chdiring to this */
|
---|
1134 | if(*realpath_buff) {
|
---|
1135 | script = strrchr(realpath_buff, '/');
|
---|
1136 | if(script) {
|
---|
1137 | *script = '\0';
|
---|
1138 | if(chdir(realpath_buff) == 0) {
|
---|
1139 | // now run the program. If it fails,
|
---|
1140 | // use _exit() so no destructors
|
---|
1141 | // get called and make a mess.
|
---|
1142 | #ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
|
---|
1143 | char *interpr = NULL;
|
---|
1144 | char *suffix = strrchr(purl, '.');
|
---|
1145 |
|
---|
1146 | if(suffix) {
|
---|
1147 | Htaccess * cur;
|
---|
1148 | for (cur = config->script_i; cur; cur = cur->next)
|
---|
1149 | if(strcmp(cur->before_colon + 1, suffix) == 0) {
|
---|
1150 | interpr = cur->after_colon;
|
---|
1151 | break;
|
---|
1152 | }
|
---|
1153 | }
|
---|
1154 | #endif
|
---|
1155 | *script = '/';
|
---|
1156 | #ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
|
---|
1157 | if (interpr)
|
---|
1158 | execv(interpr, argp);
|
---|
1159 | else
|
---|
1160 | #endif
|
---|
1161 | execv(realpath_buff, argp);
|
---|
1162 | }
|
---|
1163 | }
|
---|
1164 | }
|
---|
1165 | #ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
|
---|
1166 | config->accepted_socket = 1; /* send to stdout */
|
---|
1167 | #endif
|
---|
1168 | sendHeaders(HTTP_NOT_FOUND);
|
---|
1169 | _exit(242);
|
---|
1170 | } /* end child */
|
---|
1171 |
|
---|
1172 | } while (0);
|
---|
1173 |
|
---|
1174 | if (pid) {
|
---|
1175 | /* parent process */
|
---|
1176 | int status;
|
---|
1177 | size_t post_readed_size = 0, post_readed_idx = 0;
|
---|
1178 |
|
---|
1179 | inFd = fromCgi[0];
|
---|
1180 | outFd = toCgi[1];
|
---|
1181 | close(fromCgi[1]);
|
---|
1182 | close(toCgi[0]);
|
---|
1183 | signal(SIGPIPE, SIG_IGN);
|
---|
1184 |
|
---|
1185 | while (1) {
|
---|
1186 | fd_set readSet;
|
---|
1187 | fd_set writeSet;
|
---|
1188 | char wbuf[128];
|
---|
1189 | int nfound;
|
---|
1190 | int count;
|
---|
1191 |
|
---|
1192 | FD_ZERO(&readSet);
|
---|
1193 | FD_ZERO(&writeSet);
|
---|
1194 | FD_SET(inFd, &readSet);
|
---|
1195 | if(bodyLen > 0 || post_readed_size > 0) {
|
---|
1196 | FD_SET(outFd, &writeSet);
|
---|
1197 | nfound = outFd > inFd ? outFd : inFd;
|
---|
1198 | if(post_readed_size == 0) {
|
---|
1199 | FD_SET(a_c_r, &readSet);
|
---|
1200 | if(nfound < a_c_r)
|
---|
1201 | nfound = a_c_r;
|
---|
1202 | }
|
---|
1203 | /* Now wait on the set of sockets! */
|
---|
1204 | nfound = select(nfound + 1, &readSet, &writeSet, 0, NULL);
|
---|
1205 | } else {
|
---|
1206 | if(!bodyLen) {
|
---|
1207 | close(outFd);
|
---|
1208 | bodyLen = -1;
|
---|
1209 | }
|
---|
1210 | nfound = select(inFd + 1, &readSet, 0, 0, NULL);
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | if (nfound <= 0) {
|
---|
1214 | if (waitpid(pid, &status, WNOHANG) > 0) {
|
---|
1215 | close(inFd);
|
---|
1216 | #if DEBUG
|
---|
1217 | if (WIFEXITED(status))
|
---|
1218 | bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status));
|
---|
1219 | if (WIFSIGNALED(status))
|
---|
1220 | bb_error_msg("piped has exited with signal=%d", WTERMSIG(status));
|
---|
1221 | #endif
|
---|
1222 | break;
|
---|
1223 | }
|
---|
1224 | } else if(post_readed_size > 0 && FD_ISSET(outFd, &writeSet)) {
|
---|
1225 | count = bb_full_write(outFd, wbuf + post_readed_idx, post_readed_size);
|
---|
1226 | if(count > 0) {
|
---|
1227 | post_readed_size -= count;
|
---|
1228 | post_readed_idx += count;
|
---|
1229 | if(post_readed_size == 0)
|
---|
1230 | post_readed_idx = 0;
|
---|
1231 | } else {
|
---|
1232 | post_readed_size = post_readed_idx = bodyLen = 0; /* broken pipe to CGI */
|
---|
1233 | }
|
---|
1234 | } else if(bodyLen > 0 && post_readed_size == 0 && FD_ISSET(a_c_r, &readSet)) {
|
---|
1235 | count = bodyLen > (int)sizeof(wbuf) ? (int)sizeof(wbuf) : bodyLen;
|
---|
1236 | count = safe_read(a_c_r, wbuf, count);
|
---|
1237 | if(count > 0) {
|
---|
1238 | post_readed_size += count;
|
---|
1239 | bodyLen -= count;
|
---|
1240 | } else {
|
---|
1241 | bodyLen = 0; /* closed */
|
---|
1242 | }
|
---|
1243 | }
|
---|
1244 | if(FD_ISSET(inFd, &readSet)) {
|
---|
1245 | int s = a_c_w;
|
---|
1246 | char *rbuf = config->buf;
|
---|
1247 |
|
---|
1248 | #ifndef PIPE_BUF
|
---|
1249 | # define PIPESIZE 4096 /* amount of buffering in a pipe */
|
---|
1250 | #else
|
---|
1251 | # define PIPESIZE PIPE_BUF
|
---|
1252 | #endif
|
---|
1253 | #if PIPESIZE >= MAX_MEMORY_BUFF
|
---|
1254 | # error "PIPESIZE >= MAX_MEMORY_BUFF"
|
---|
1255 | #endif
|
---|
1256 |
|
---|
1257 | // There is something to read
|
---|
1258 | count = safe_read(inFd, rbuf, PIPESIZE);
|
---|
1259 | if (count == 0)
|
---|
1260 | break; /* closed */
|
---|
1261 | if (count > 0) {
|
---|
1262 | if (firstLine) {
|
---|
1263 | rbuf[count] = 0;
|
---|
1264 | /* check to see if the user script added headers */
|
---|
1265 | if(strncmp(rbuf, "HTTP/1.0 200 OK\r\n", 4) != 0) {
|
---|
1266 | bb_full_write(s, "HTTP/1.0 200 OK\r\n", 17);
|
---|
1267 | }
|
---|
1268 | if (strstr(rbuf, "ontent-") == 0) {
|
---|
1269 | bb_full_write(s, "Content-type: text/plain\r\n\r\n", 28);
|
---|
1270 | }
|
---|
1271 | firstLine = 0;
|
---|
1272 | }
|
---|
1273 | if (bb_full_write(s, rbuf, count) != count)
|
---|
1274 | break;
|
---|
1275 |
|
---|
1276 | #if DEBUG
|
---|
1277 | fprintf(stderr, "cgi read %d bytes\n", count);
|
---|
1278 | #endif
|
---|
1279 | }
|
---|
1280 | }
|
---|
1281 | }
|
---|
1282 | }
|
---|
1283 | return 0;
|
---|
1284 | }
|
---|
1285 | #endif /* CONFIG_FEATURE_HTTPD_CGI */
|
---|
1286 |
|
---|
1287 | /****************************************************************************
|
---|
1288 | *
|
---|
1289 | > $Function: sendFile()
|
---|
1290 | *
|
---|
1291 | * $Description: Send a file response to an HTTP request
|
---|
1292 | *
|
---|
1293 | * $Parameter:
|
---|
1294 | * (const char *) url . . The URL requested.
|
---|
1295 | *
|
---|
1296 | * $Return: (int) . . . . . . Always 0.
|
---|
1297 | *
|
---|
1298 | ****************************************************************************/
|
---|
1299 | static int sendFile(const char *url)
|
---|
1300 | {
|
---|
1301 | char * suffix;
|
---|
1302 | int f;
|
---|
1303 | const char * const * table;
|
---|
1304 | const char * try_suffix;
|
---|
1305 |
|
---|
1306 | suffix = strrchr(url, '.');
|
---|
1307 |
|
---|
1308 | for (table = suffixTable; *table; table += 2)
|
---|
1309 | if(suffix != NULL && (try_suffix = strstr(*table, suffix)) != 0) {
|
---|
1310 | try_suffix += strlen(suffix);
|
---|
1311 | if(*try_suffix == 0 || *try_suffix == '.')
|
---|
1312 | break;
|
---|
1313 | }
|
---|
1314 | /* also, if not found, set default as "application/octet-stream"; */
|
---|
1315 | config->httpd_found.found_mime_type = *(table+1);
|
---|
1316 | #ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
|
---|
1317 | if (suffix) {
|
---|
1318 | Htaccess * cur;
|
---|
1319 |
|
---|
1320 | for (cur = config->mime_a; cur; cur = cur->next) {
|
---|
1321 | if(strcmp(cur->before_colon, suffix) == 0) {
|
---|
1322 | config->httpd_found.found_mime_type = cur->after_colon;
|
---|
1323 | break;
|
---|
1324 | }
|
---|
1325 | }
|
---|
1326 | }
|
---|
1327 | #endif /* CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES */
|
---|
1328 |
|
---|
1329 | #if DEBUG
|
---|
1330 | fprintf(stderr, "Sending file '%s' Content-type: %s\n",
|
---|
1331 | url, config->httpd_found.found_mime_type);
|
---|
1332 | #endif
|
---|
1333 |
|
---|
1334 | f = open(url, O_RDONLY);
|
---|
1335 | if (f >= 0) {
|
---|
1336 | int count;
|
---|
1337 | char *buf = config->buf;
|
---|
1338 |
|
---|
1339 | sendHeaders(HTTP_OK);
|
---|
1340 | while ((count = bb_full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
|
---|
1341 | if (bb_full_write(a_c_w, buf, count) != count)
|
---|
1342 | break;
|
---|
1343 | }
|
---|
1344 | close(f);
|
---|
1345 | } else {
|
---|
1346 | #if DEBUG
|
---|
1347 | bb_perror_msg("Unable to open '%s'", url);
|
---|
1348 | #endif
|
---|
1349 | sendHeaders(HTTP_NOT_FOUND);
|
---|
1350 | }
|
---|
1351 |
|
---|
1352 | return 0;
|
---|
1353 | }
|
---|
1354 |
|
---|
1355 | static int checkPermIP(void)
|
---|
1356 | {
|
---|
1357 | Htaccess_IP * cur;
|
---|
1358 |
|
---|
1359 | /* This could stand some work */
|
---|
1360 | for (cur = config->ip_a_d; cur; cur = cur->next) {
|
---|
1361 | #if DEBUG
|
---|
1362 | fprintf(stderr, "checkPermIP: '%s' ? ", config->rmt_ip_str);
|
---|
1363 | fprintf(stderr, "'%u.%u.%u.%u/%u.%u.%u.%u'\n",
|
---|
1364 | (unsigned char)(cur->ip >> 24),
|
---|
1365 | (unsigned char)(cur->ip >> 16),
|
---|
1366 | (unsigned char)(cur->ip >> 8),
|
---|
1367 | cur->ip & 0xff,
|
---|
1368 | (unsigned char)(cur->mask >> 24),
|
---|
1369 | (unsigned char)(cur->mask >> 16),
|
---|
1370 | (unsigned char)(cur->mask >> 8),
|
---|
1371 | cur->mask & 0xff);
|
---|
1372 | #endif
|
---|
1373 | if((config->rmt_ip & cur->mask) == cur->ip)
|
---|
1374 | return cur->allow_deny == 'A'; /* Allow/Deny */
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | /* if unconfigured, return 1 - access from all */
|
---|
1378 | return !config->flg_deny_all;
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 | /****************************************************************************
|
---|
1382 | *
|
---|
1383 | > $Function: checkPerm()
|
---|
1384 | *
|
---|
1385 | * $Description: Check the permission file for access password protected.
|
---|
1386 | *
|
---|
1387 | * If config file isn't present, everything is allowed.
|
---|
1388 | * Entries are of the form you can see example from header source
|
---|
1389 | *
|
---|
1390 | * $Parameters:
|
---|
1391 | * (const char *) path . . . . The file path.
|
---|
1392 | * (const char *) request . . . User information to validate.
|
---|
1393 | *
|
---|
1394 | * $Return: (int) . . . . . . . . . 1 if request OK, 0 otherwise.
|
---|
1395 | *
|
---|
1396 | ****************************************************************************/
|
---|
1397 |
|
---|
1398 | #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
|
---|
1399 | static int checkPerm(const char *path, const char *request)
|
---|
1400 | {
|
---|
1401 | Htaccess * cur;
|
---|
1402 | const char *p;
|
---|
1403 | const char *p0;
|
---|
1404 |
|
---|
1405 | const char *prev = NULL;
|
---|
1406 |
|
---|
1407 | /* This could stand some work */
|
---|
1408 | for (cur = config->auth; cur; cur = cur->next) {
|
---|
1409 | p0 = cur->before_colon;
|
---|
1410 | if(prev != NULL && strcmp(prev, p0) != 0)
|
---|
1411 | continue; /* find next identical */
|
---|
1412 | p = cur->after_colon;
|
---|
1413 | #if DEBUG
|
---|
1414 | fprintf(stderr,"checkPerm: '%s' ? '%s'\n", p0, request);
|
---|
1415 | #endif
|
---|
1416 | {
|
---|
1417 | size_t l = strlen(p0);
|
---|
1418 |
|
---|
1419 | if(strncmp(p0, path, l) == 0 &&
|
---|
1420 | (l == 1 || path[l] == '/' || path[l] == 0)) {
|
---|
1421 | char *u;
|
---|
1422 | /* path match found. Check request */
|
---|
1423 | /* for check next /path:user:password */
|
---|
1424 | prev = p0;
|
---|
1425 | u = strchr(request, ':');
|
---|
1426 | if(u == NULL) {
|
---|
1427 | /* bad request, ':' required */
|
---|
1428 | break;
|
---|
1429 | }
|
---|
1430 |
|
---|
1431 | #ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
|
---|
1432 | {
|
---|
1433 | char *cipher;
|
---|
1434 | char *pp;
|
---|
1435 |
|
---|
1436 | if(strncmp(p, request, u-request) != 0) {
|
---|
1437 | /* user uncompared */
|
---|
1438 | continue;
|
---|
1439 | }
|
---|
1440 | pp = strchr(p, ':');
|
---|
1441 | if(pp && pp[1] == '$' && pp[2] == '1' &&
|
---|
1442 | pp[3] == '$' && pp[4]) {
|
---|
1443 | pp++;
|
---|
1444 | cipher = pw_encrypt(u+1, pp);
|
---|
1445 | if (strcmp(cipher, pp) == 0)
|
---|
1446 | goto set_remoteuser_var; /* Ok */
|
---|
1447 | /* unauthorized */
|
---|
1448 | continue;
|
---|
1449 | }
|
---|
1450 | }
|
---|
1451 | #endif
|
---|
1452 | if (strcmp(p, request) == 0) {
|
---|
1453 | #ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
|
---|
1454 | set_remoteuser_var:
|
---|
1455 | #endif
|
---|
1456 | config->remoteuser = strdup(request);
|
---|
1457 | if(config->remoteuser)
|
---|
1458 | config->remoteuser[(u - request)] = 0;
|
---|
1459 | return 1; /* Ok */
|
---|
1460 | }
|
---|
1461 | /* unauthorized */
|
---|
1462 | }
|
---|
1463 | }
|
---|
1464 | } /* for */
|
---|
1465 |
|
---|
1466 | return prev == NULL;
|
---|
1467 | }
|
---|
1468 |
|
---|
1469 | #endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
|
---|
1470 |
|
---|
1471 | /****************************************************************************
|
---|
1472 | *
|
---|
1473 | > $Function: handle_sigalrm()
|
---|
1474 | *
|
---|
1475 | * $Description: Handle timeouts
|
---|
1476 | *
|
---|
1477 | ****************************************************************************/
|
---|
1478 |
|
---|
1479 | static void
|
---|
1480 | handle_sigalrm( int sig )
|
---|
1481 | {
|
---|
1482 | sendHeaders(HTTP_REQUEST_TIMEOUT);
|
---|
1483 | config->alarm_signaled = sig;
|
---|
1484 | }
|
---|
1485 |
|
---|
1486 | /****************************************************************************
|
---|
1487 | *
|
---|
1488 | > $Function: handleIncoming()
|
---|
1489 | *
|
---|
1490 | * $Description: Handle an incoming http request.
|
---|
1491 | *
|
---|
1492 | ****************************************************************************/
|
---|
1493 | static void handleIncoming(void)
|
---|
1494 | {
|
---|
1495 | char *buf = config->buf;
|
---|
1496 | char *url;
|
---|
1497 | char *purl;
|
---|
1498 | int blank = -1;
|
---|
1499 | char *test;
|
---|
1500 | struct stat sb;
|
---|
1501 | int ip_allowed;
|
---|
1502 | #ifdef CONFIG_FEATURE_HTTPD_CGI
|
---|
1503 | const char *prequest = request_GET;
|
---|
1504 | long length=0;
|
---|
1505 | char *cookie = 0;
|
---|
1506 | char *content_type = 0;
|
---|
1507 | #endif
|
---|
1508 | fd_set s_fd;
|
---|
1509 | struct timeval tv;
|
---|
1510 | int retval;
|
---|
1511 | struct sigaction sa;
|
---|
1512 |
|
---|
1513 | #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
|
---|
1514 | int credentials = -1; /* if not requred this is Ok */
|
---|
1515 | #endif
|
---|
1516 |
|
---|
1517 | sa.sa_handler = handle_sigalrm;
|
---|
1518 | sigemptyset(&sa.sa_mask);
|
---|
1519 | sa.sa_flags = 0; /* no SA_RESTART */
|
---|
1520 | sigaction(SIGALRM, &sa, NULL);
|
---|
1521 |
|
---|
1522 | do {
|
---|
1523 | int count;
|
---|
1524 |
|
---|
1525 | (void) alarm( TIMEOUT );
|
---|
1526 | if (getLine() <= 0)
|
---|
1527 | break; /* closed */
|
---|
1528 |
|
---|
1529 | purl = strpbrk(buf, " \t");
|
---|
1530 | if(purl == NULL) {
|
---|
1531 | BAD_REQUEST:
|
---|
1532 | sendHeaders(HTTP_BAD_REQUEST);
|
---|
1533 | break;
|
---|
1534 | }
|
---|
1535 | *purl = 0;
|
---|
1536 | #ifdef CONFIG_FEATURE_HTTPD_CGI
|
---|
1537 | if(strcasecmp(buf, prequest) != 0) {
|
---|
1538 | prequest = "POST";
|
---|
1539 | if(strcasecmp(buf, prequest) != 0) {
|
---|
1540 | sendHeaders(HTTP_NOT_IMPLEMENTED);
|
---|
1541 | break;
|
---|
1542 | }
|
---|
1543 | }
|
---|
1544 | #else
|
---|
1545 | if(strcasecmp(buf, request_GET) != 0) {
|
---|
1546 | sendHeaders(HTTP_NOT_IMPLEMENTED);
|
---|
1547 | break;
|
---|
1548 | }
|
---|
1549 | #endif
|
---|
1550 | *purl = ' ';
|
---|
1551 | count = sscanf(purl, " %[^ ] HTTP/%d.%*d", buf, &blank);
|
---|
1552 |
|
---|
1553 | if (count < 1 || buf[0] != '/') {
|
---|
1554 | /* Garbled request/URL */
|
---|
1555 | goto BAD_REQUEST;
|
---|
1556 | }
|
---|
1557 | url = alloca(strlen(buf) + 12); /* + sizeof("/index.html\0") */
|
---|
1558 | if(url == NULL) {
|
---|
1559 | sendHeaders(HTTP_INTERNAL_SERVER_ERROR);
|
---|
1560 | break;
|
---|
1561 | }
|
---|
1562 | strcpy(url, buf);
|
---|
1563 | /* extract url args if present */
|
---|
1564 | test = strchr(url, '?');
|
---|
1565 | if (test) {
|
---|
1566 | *test++ = 0;
|
---|
1567 | config->query = test;
|
---|
1568 | }
|
---|
1569 |
|
---|
1570 | test = decodeString(url, 0);
|
---|
1571 | if(test == NULL)
|
---|
1572 | goto BAD_REQUEST;
|
---|
1573 | if(test == (buf+1)) {
|
---|
1574 | sendHeaders(HTTP_NOT_FOUND);
|
---|
1575 | break;
|
---|
1576 | }
|
---|
1577 | /* algorithm stolen from libbb bb_simplify_path(),
|
---|
1578 | but don`t strdup and reducing trailing slash and protect out root */
|
---|
1579 | purl = test = url;
|
---|
1580 |
|
---|
1581 | do {
|
---|
1582 | if (*purl == '/') {
|
---|
1583 | if (*test == '/') { /* skip duplicate (or initial) slash */
|
---|
1584 | continue;
|
---|
1585 | } else if (*test == '.') {
|
---|
1586 | if (test[1] == '/' || test[1] == 0) { /* skip extra '.' */
|
---|
1587 | continue;
|
---|
1588 | } else if ((test[1] == '.') && (test[2] == '/' || test[2] == 0)) {
|
---|
1589 | ++test;
|
---|
1590 | if (purl == url) {
|
---|
1591 | /* protect out root */
|
---|
1592 | goto BAD_REQUEST;
|
---|
1593 | }
|
---|
1594 | while (*--purl != '/'); /* omit previous dir */
|
---|
1595 | continue;
|
---|
1596 | }
|
---|
1597 | }
|
---|
1598 | }
|
---|
1599 | *++purl = *test;
|
---|
1600 | } while (*++test);
|
---|
1601 |
|
---|
1602 | *++purl = 0; /* so keep last character */
|
---|
1603 | test = purl; /* end ptr */
|
---|
1604 |
|
---|
1605 | /* If URL is directory, adding '/' */
|
---|
1606 | if(test[-1] != '/') {
|
---|
1607 | if ( is_directory(url + 1, 1, &sb) ) {
|
---|
1608 | config->httpd_found.found_moved_temporarily = url;
|
---|
1609 | }
|
---|
1610 | }
|
---|
1611 | #if DEBUG
|
---|
1612 | fprintf(stderr, "url='%s', args=%s\n", url, config->query);
|
---|
1613 | #endif
|
---|
1614 |
|
---|
1615 | test = url;
|
---|
1616 | ip_allowed = checkPermIP();
|
---|
1617 | while(ip_allowed && (test = strchr( test + 1, '/' )) != NULL) {
|
---|
1618 | /* have path1/path2 */
|
---|
1619 | *test = '\0';
|
---|
1620 | if( is_directory(url + 1, 1, &sb) ) {
|
---|
1621 | /* may be having subdir config */
|
---|
1622 | parse_conf(url + 1, SUBDIR_PARSE);
|
---|
1623 | ip_allowed = checkPermIP();
|
---|
1624 | }
|
---|
1625 | *test = '/';
|
---|
1626 | }
|
---|
1627 | if(blank >= 0) {
|
---|
1628 | // read until blank line for HTTP version specified, else parse immediate
|
---|
1629 | while(1) {
|
---|
1630 | alarm(TIMEOUT);
|
---|
1631 | count = getLine();
|
---|
1632 | if(count <= 0)
|
---|
1633 | break;
|
---|
1634 |
|
---|
1635 | #if DEBUG
|
---|
1636 | fprintf(stderr, "Header: '%s'\n", buf);
|
---|
1637 | #endif
|
---|
1638 |
|
---|
1639 | #ifdef CONFIG_FEATURE_HTTPD_CGI
|
---|
1640 | /* try and do our best to parse more lines */
|
---|
1641 | if ((strncasecmp(buf, Content_length, 15) == 0)) {
|
---|
1642 | if(prequest != request_GET)
|
---|
1643 | length = strtol(buf + 15, 0, 0); // extra read only for POST
|
---|
1644 | } else if ((strncasecmp(buf, "Cookie:", 7) == 0)) {
|
---|
1645 | for(test = buf + 7; isspace(*test); test++)
|
---|
1646 | ;
|
---|
1647 | cookie = strdup(test);
|
---|
1648 | } else if ((strncasecmp(buf, "Content-Type:", 13) == 0)) {
|
---|
1649 | for(test = buf + 13; isspace(*test); test++)
|
---|
1650 | ;
|
---|
1651 | content_type = strdup(test);
|
---|
1652 | } else if ((strncasecmp(buf, "Referer:", 8) == 0)) {
|
---|
1653 | for(test = buf + 8; isspace(*test); test++)
|
---|
1654 | ;
|
---|
1655 | config->referer = strdup(test);
|
---|
1656 | }
|
---|
1657 | #endif
|
---|
1658 |
|
---|
1659 | #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
|
---|
1660 | if (strncasecmp(buf, "Authorization:", 14) == 0) {
|
---|
1661 | /* We only allow Basic credentials.
|
---|
1662 | * It shows up as "Authorization: Basic <userid:password>" where
|
---|
1663 | * the userid:password is base64 encoded.
|
---|
1664 | */
|
---|
1665 | for(test = buf + 14; isspace(*test); test++)
|
---|
1666 | ;
|
---|
1667 | if (strncasecmp(test, "Basic", 5) != 0)
|
---|
1668 | continue;
|
---|
1669 |
|
---|
1670 | test += 5; /* decodeBase64() skiping space self */
|
---|
1671 | decodeBase64(test);
|
---|
1672 | credentials = checkPerm(url, test);
|
---|
1673 | }
|
---|
1674 | #endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
|
---|
1675 |
|
---|
1676 | } /* while extra header reading */
|
---|
1677 | }
|
---|
1678 | (void) alarm( 0 );
|
---|
1679 | if(config->alarm_signaled)
|
---|
1680 | break;
|
---|
1681 |
|
---|
1682 | if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) {
|
---|
1683 | /* protect listing [/path]/httpd_conf or IP deny */
|
---|
1684 | #ifdef CONFIG_FEATURE_HTTPD_CGI
|
---|
1685 | FORBIDDEN: /* protect listing /cgi-bin */
|
---|
1686 | #endif
|
---|
1687 | sendHeaders(HTTP_FORBIDDEN);
|
---|
1688 | break;
|
---|
1689 | }
|
---|
1690 |
|
---|
1691 | #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
|
---|
1692 | if (credentials <= 0 && checkPerm(url, ":") == 0) {
|
---|
1693 | sendHeaders(HTTP_UNAUTHORIZED);
|
---|
1694 | break;
|
---|
1695 | }
|
---|
1696 | #endif
|
---|
1697 |
|
---|
1698 | if(config->httpd_found.found_moved_temporarily) {
|
---|
1699 | sendHeaders(HTTP_MOVED_TEMPORARILY);
|
---|
1700 | #if DEBUG
|
---|
1701 | /* clear unforked memory flag */
|
---|
1702 | config->httpd_found.found_moved_temporarily = NULL;
|
---|
1703 | #endif
|
---|
1704 | break;
|
---|
1705 | }
|
---|
1706 |
|
---|
1707 | test = url + 1; /* skip first '/' */
|
---|
1708 |
|
---|
1709 | #ifdef CONFIG_FEATURE_HTTPD_CGI
|
---|
1710 | /* if strange Content-Length */
|
---|
1711 | if (length < 0)
|
---|
1712 | break;
|
---|
1713 |
|
---|
1714 | if (strncmp(test, "cgi-bin", 7) == 0) {
|
---|
1715 | if(test[7] == '/' && test[8] == 0)
|
---|
1716 | goto FORBIDDEN; // protect listing cgi-bin/
|
---|
1717 | sendCgi(url, prequest, length, cookie, content_type);
|
---|
1718 | } else {
|
---|
1719 | if (prequest != request_GET)
|
---|
1720 | sendHeaders(HTTP_NOT_IMPLEMENTED);
|
---|
1721 | else {
|
---|
1722 | #endif /* CONFIG_FEATURE_HTTPD_CGI */
|
---|
1723 | if(purl[-1] == '/')
|
---|
1724 | strcpy(purl, "index.html");
|
---|
1725 | if ( stat(test, &sb ) == 0 ) {
|
---|
1726 | config->ContentLength = sb.st_size;
|
---|
1727 | config->last_mod = sb.st_mtime;
|
---|
1728 | }
|
---|
1729 | sendFile(test);
|
---|
1730 | #ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
|
---|
1731 | /* unset if non inetd looped */
|
---|
1732 | config->ContentLength = -1;
|
---|
1733 | #endif
|
---|
1734 |
|
---|
1735 | #ifdef CONFIG_FEATURE_HTTPD_CGI
|
---|
1736 | }
|
---|
1737 | }
|
---|
1738 | #endif
|
---|
1739 |
|
---|
1740 | } while (0);
|
---|
1741 |
|
---|
1742 |
|
---|
1743 | #ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
|
---|
1744 | /* from inetd don`t looping: freeing, closing automatic from exit always */
|
---|
1745 | # if DEBUG
|
---|
1746 | fprintf(stderr, "closing socket\n");
|
---|
1747 | # endif
|
---|
1748 | # ifdef CONFIG_FEATURE_HTTPD_CGI
|
---|
1749 | free(cookie);
|
---|
1750 | free(content_type);
|
---|
1751 | free(config->referer);
|
---|
1752 | #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
|
---|
1753 | free(config->remoteuser);
|
---|
1754 | #endif
|
---|
1755 | # endif
|
---|
1756 | #endif /* CONFIG_FEATURE_HTTPD_WITHOUT_INETD */
|
---|
1757 | shutdown(a_c_w, SHUT_WR);
|
---|
1758 |
|
---|
1759 | /* Properly wait for remote to closed */
|
---|
1760 | FD_ZERO (&s_fd) ;
|
---|
1761 | FD_SET (a_c_r, &s_fd) ;
|
---|
1762 |
|
---|
1763 | do {
|
---|
1764 | tv.tv_sec = 2 ;
|
---|
1765 | tv.tv_usec = 0 ;
|
---|
1766 | retval = select (a_c_r + 1, &s_fd, NULL, NULL, &tv);
|
---|
1767 | } while (retval > 0 && (read (a_c_r, buf, sizeof (config->buf)) > 0));
|
---|
1768 |
|
---|
1769 | shutdown(a_c_r, SHUT_RD);
|
---|
1770 | #ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
|
---|
1771 | close(config->accepted_socket);
|
---|
1772 | #endif /* CONFIG_FEATURE_HTTPD_WITHOUT_INETD */
|
---|
1773 | }
|
---|
1774 |
|
---|
1775 | /****************************************************************************
|
---|
1776 | *
|
---|
1777 | > $Function: miniHttpd()
|
---|
1778 | *
|
---|
1779 | * $Description: The main http server function.
|
---|
1780 | *
|
---|
1781 | * Given an open socket fildes, listen for new connections and farm out
|
---|
1782 | * the processing as a forked process.
|
---|
1783 | *
|
---|
1784 | * $Parameters:
|
---|
1785 | * (int) server. . . The server socket fildes.
|
---|
1786 | *
|
---|
1787 | * $Return: (int) . . . . Always 0.
|
---|
1788 | *
|
---|
1789 | ****************************************************************************/
|
---|
1790 | #ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
|
---|
1791 | static int miniHttpd(int server)
|
---|
1792 | {
|
---|
1793 | fd_set readfd, portfd;
|
---|
1794 |
|
---|
1795 | FD_ZERO(&portfd);
|
---|
1796 | FD_SET(server, &portfd);
|
---|
1797 |
|
---|
1798 | /* copy the ports we are watching to the readfd set */
|
---|
1799 | while (1) {
|
---|
1800 | readfd = portfd;
|
---|
1801 |
|
---|
1802 | /* Now wait INDEFINITELY on the set of sockets! */
|
---|
1803 | if (select(server + 1, &readfd, 0, 0, 0) > 0) {
|
---|
1804 | if (FD_ISSET(server, &readfd)) {
|
---|
1805 | int on;
|
---|
1806 | struct sockaddr_in fromAddr;
|
---|
1807 |
|
---|
1808 | socklen_t fromAddrLen = sizeof(fromAddr);
|
---|
1809 | int s = accept(server,
|
---|
1810 | (struct sockaddr *)&fromAddr, &fromAddrLen);
|
---|
1811 |
|
---|
1812 | if (s < 0) {
|
---|
1813 | continue;
|
---|
1814 | }
|
---|
1815 | config->accepted_socket = s;
|
---|
1816 | config->rmt_ip = ntohl(fromAddr.sin_addr.s_addr);
|
---|
1817 | #if defined(CONFIG_FEATURE_HTTPD_CGI) || DEBUG
|
---|
1818 | sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
|
---|
1819 | (unsigned char)(config->rmt_ip >> 24),
|
---|
1820 | (unsigned char)(config->rmt_ip >> 16),
|
---|
1821 | (unsigned char)(config->rmt_ip >> 8),
|
---|
1822 | config->rmt_ip & 0xff);
|
---|
1823 | config->port = ntohs(fromAddr.sin_port);
|
---|
1824 | #if DEBUG
|
---|
1825 | bb_error_msg("connection from IP=%s, port %u\n",
|
---|
1826 | config->rmt_ip_str, config->port);
|
---|
1827 | #endif
|
---|
1828 | #endif /* CONFIG_FEATURE_HTTPD_CGI */
|
---|
1829 |
|
---|
1830 | /* set the KEEPALIVE option to cull dead connections */
|
---|
1831 | on = 1;
|
---|
1832 | setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof (on));
|
---|
1833 |
|
---|
1834 | #if !DEBUG
|
---|
1835 | if (fork() == 0)
|
---|
1836 | #endif
|
---|
1837 | {
|
---|
1838 | /* This is the spawned thread */
|
---|
1839 | #ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
|
---|
1840 | /* protect reload config, may be confuse checking */
|
---|
1841 | signal(SIGHUP, SIG_IGN);
|
---|
1842 | #endif
|
---|
1843 | handleIncoming();
|
---|
1844 | #if !DEBUG
|
---|
1845 | exit(0);
|
---|
1846 | #endif
|
---|
1847 | }
|
---|
1848 | close(s);
|
---|
1849 | }
|
---|
1850 | }
|
---|
1851 | } // while (1)
|
---|
1852 | return 0;
|
---|
1853 | }
|
---|
1854 |
|
---|
1855 | #else
|
---|
1856 | /* from inetd */
|
---|
1857 |
|
---|
1858 | static int miniHttpd(void)
|
---|
1859 | {
|
---|
1860 | struct sockaddr_in fromAddrLen;
|
---|
1861 | socklen_t sinlen = sizeof (struct sockaddr_in);
|
---|
1862 |
|
---|
1863 | getpeername (0, (struct sockaddr *)&fromAddrLen, &sinlen);
|
---|
1864 | config->rmt_ip = ntohl(fromAddrLen.sin_addr.s_addr);
|
---|
1865 | #ifdef CONFIG_FEATURE_HTTPD_CGI
|
---|
1866 | sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
|
---|
1867 | (unsigned char)(config->rmt_ip >> 24),
|
---|
1868 | (unsigned char)(config->rmt_ip >> 16),
|
---|
1869 | (unsigned char)(config->rmt_ip >> 8),
|
---|
1870 | config->rmt_ip & 0xff);
|
---|
1871 | #endif
|
---|
1872 | config->port = ntohs(fromAddrLen.sin_port);
|
---|
1873 | handleIncoming();
|
---|
1874 | return 0;
|
---|
1875 | }
|
---|
1876 | #endif /* CONFIG_FEATURE_HTTPD_WITHOUT_INETD */
|
---|
1877 |
|
---|
1878 | #ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
|
---|
1879 | static void sighup_handler(int sig)
|
---|
1880 | {
|
---|
1881 | /* set and reset */
|
---|
1882 | struct sigaction sa;
|
---|
1883 |
|
---|
1884 | parse_conf(default_path_httpd_conf,
|
---|
1885 | sig == SIGHUP ? SIGNALED_PARSE : FIRST_PARSE);
|
---|
1886 | sa.sa_handler = sighup_handler;
|
---|
1887 | sigemptyset(&sa.sa_mask);
|
---|
1888 | sa.sa_flags = SA_RESTART;
|
---|
1889 | sigaction(SIGHUP, &sa, NULL);
|
---|
1890 | }
|
---|
1891 | #endif
|
---|
1892 |
|
---|
1893 | enum httpd_opts_nums {
|
---|
1894 | c_opt_config_file = 0,
|
---|
1895 | d_opt_decode_url,
|
---|
1896 | h_opt_home_httpd,
|
---|
1897 | USE_FEATURE_HTTPD_ENCODE_URL_STR(e_opt_encode_url,)
|
---|
1898 | USE_FEATURE_HTTPD_BASIC_AUTH(r_opt_realm,)
|
---|
1899 | USE_FEATURE_HTTPD_AUTH_MD5(m_opt_md5,)
|
---|
1900 | USE_FEATURE_HTTPD_SETUID(u_opt_setuid,)
|
---|
1901 | USE_FEATURE_HTTPD_WITHOUT_INETD(p_opt_port,)
|
---|
1902 | };
|
---|
1903 |
|
---|
1904 | static const char httpd_opts[]="c:d:h:"
|
---|
1905 | USE_FEATURE_HTTPD_ENCODE_URL_STR("e:")
|
---|
1906 | USE_FEATURE_HTTPD_BASIC_AUTH("r:")
|
---|
1907 | USE_FEATURE_HTTPD_AUTH_MD5("m:")
|
---|
1908 | USE_FEATURE_HTTPD_SETUID("u:")
|
---|
1909 | USE_FEATURE_HTTPD_WITHOUT_INETD("p:");
|
---|
1910 |
|
---|
1911 | #define OPT_CONFIG_FILE (1<<c_opt_config_file)
|
---|
1912 | #define OPT_DECODE_URL (1<<d_opt_decode_url)
|
---|
1913 | #define OPT_HOME_HTTPD (1<<h_opt_home_httpd)
|
---|
1914 |
|
---|
1915 | #define OPT_ENCODE_URL USE_FEATURE_HTTPD_ENCODE_URL_STR((1<<e_opt_encode_url)) \
|
---|
1916 | SKIP_FEATURE_HTTPD_ENCODE_URL_STR(0)
|
---|
1917 |
|
---|
1918 | #define OPT_REALM USE_FEATURE_HTTPD_BASIC_AUTH((1<<r_opt_realm)) \
|
---|
1919 | SKIP_FEATURE_HTTPD_BASIC_AUTH(0)
|
---|
1920 |
|
---|
1921 | #define OPT_MD5 USE_FEATURE_HTTPD_AUTH_MD5((1<<m_opt_md5)) \
|
---|
1922 | SKIP_FEATURE_HTTPD_AUTH_MD5(0)
|
---|
1923 |
|
---|
1924 | #define OPT_SETUID USE_FEATURE_HTTPD_SETUID((1<<u_opt_setuid)) \
|
---|
1925 | SKIP_FEATURE_HTTPD_SETUID(0)
|
---|
1926 |
|
---|
1927 | #define OPT_PORT USE_FEATURE_HTTPD_WITHOUT_INETD((1<<p_opt_port)) \
|
---|
1928 | SKIP_FEATURE_HTTPD_WITHOUT_INETD(0)
|
---|
1929 |
|
---|
1930 |
|
---|
1931 | int httpd_main(int argc, char *argv[])
|
---|
1932 | {
|
---|
1933 | unsigned long opt;
|
---|
1934 | const char *home_httpd = home;
|
---|
1935 | char *url_for_decode;
|
---|
1936 | USE_FEATURE_HTTPD_ENCODE_URL_STR(const char *url_for_encode;)
|
---|
1937 | USE_FEATURE_HTTPD_WITHOUT_INETD(const char *s_port;)
|
---|
1938 | USE_FEATURE_HTTPD_WITHOUT_INETD(int server;)
|
---|
1939 |
|
---|
1940 | USE_FEATURE_HTTPD_SETUID(const char *s_uid;)
|
---|
1941 | USE_FEATURE_HTTPD_SETUID(long uid = -1;)
|
---|
1942 |
|
---|
1943 | USE_FEATURE_HTTPD_AUTH_MD5(const char *pass;)
|
---|
1944 |
|
---|
1945 | config = xcalloc(1, sizeof(*config));
|
---|
1946 | #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
|
---|
1947 | config->realm = "Web Server Authentication";
|
---|
1948 | #endif
|
---|
1949 |
|
---|
1950 | #ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
|
---|
1951 | config->port = 80;
|
---|
1952 | #endif
|
---|
1953 |
|
---|
1954 | config->ContentLength = -1;
|
---|
1955 |
|
---|
1956 | opt = bb_getopt_ulflags(argc, argv, httpd_opts,
|
---|
1957 | &(config->configFile), &url_for_decode, &home_httpd
|
---|
1958 | USE_FEATURE_HTTPD_ENCODE_URL_STR(, &url_for_encode)
|
---|
1959 | USE_FEATURE_HTTPD_BASIC_AUTH(, &(config->realm))
|
---|
1960 | USE_FEATURE_HTTPD_AUTH_MD5(, &pass)
|
---|
1961 | USE_FEATURE_HTTPD_SETUID(, &s_uid)
|
---|
1962 | USE_FEATURE_HTTPD_WITHOUT_INETD(, &s_port)
|
---|
1963 | );
|
---|
1964 |
|
---|
1965 | if(opt & OPT_DECODE_URL) {
|
---|
1966 | printf("%s", decodeString(url_for_decode, 1));
|
---|
1967 | return 0;
|
---|
1968 | }
|
---|
1969 | #ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
|
---|
1970 | if(opt & OPT_ENCODE_URL) {
|
---|
1971 | printf("%s", encodeString(url_for_encode));
|
---|
1972 | return 0;
|
---|
1973 | }
|
---|
1974 | #endif
|
---|
1975 | #ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
|
---|
1976 | if(opt & OPT_MD5) {
|
---|
1977 | printf("%s\n", pw_encrypt(pass, "$1$"));
|
---|
1978 | return 0;
|
---|
1979 | }
|
---|
1980 | #endif
|
---|
1981 | #ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
|
---|
1982 | if(opt & OPT_PORT)
|
---|
1983 | config->port = bb_xgetlarg(s_port, 10, 1, 0xffff);
|
---|
1984 | #ifdef CONFIG_FEATURE_HTTPD_SETUID
|
---|
1985 | if(opt & OPT_SETUID) {
|
---|
1986 | char *e;
|
---|
1987 |
|
---|
1988 | uid = strtol(s_uid, &e, 0);
|
---|
1989 | if(*e != '\0') {
|
---|
1990 | /* not integer */
|
---|
1991 | uid = bb_xgetpwnam(s_uid);
|
---|
1992 | }
|
---|
1993 | }
|
---|
1994 | #endif
|
---|
1995 | #endif
|
---|
1996 |
|
---|
1997 | bb_xchdir(home_httpd);
|
---|
1998 | #ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
|
---|
1999 | server = openServer();
|
---|
2000 | # ifdef CONFIG_FEATURE_HTTPD_SETUID
|
---|
2001 | /* drop privileges */
|
---|
2002 | if(uid > 0)
|
---|
2003 | setuid(uid);
|
---|
2004 | # endif
|
---|
2005 | #endif
|
---|
2006 |
|
---|
2007 | #ifdef CONFIG_FEATURE_HTTPD_CGI
|
---|
2008 | {
|
---|
2009 | char *p = getenv("PATH");
|
---|
2010 | if(p) {
|
---|
2011 | p = bb_xstrdup(p);
|
---|
2012 | }
|
---|
2013 | clearenv();
|
---|
2014 | if(p)
|
---|
2015 | setenv("PATH", p, 1);
|
---|
2016 | # ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
|
---|
2017 | addEnvPort("SERVER");
|
---|
2018 | # endif
|
---|
2019 | }
|
---|
2020 | #endif
|
---|
2021 |
|
---|
2022 | #ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
|
---|
2023 | sighup_handler(0);
|
---|
2024 | #else
|
---|
2025 | parse_conf(default_path_httpd_conf, FIRST_PARSE);
|
---|
2026 | #endif
|
---|
2027 |
|
---|
2028 | #ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
|
---|
2029 | # if !DEBUG
|
---|
2030 | bb_xdaemon(1, 0); /* don`t change curent directory */
|
---|
2031 | # endif
|
---|
2032 | return miniHttpd(server);
|
---|
2033 | #else
|
---|
2034 | return miniHttpd();
|
---|
2035 | #endif
|
---|
2036 | }
|
---|