aboutsummaryrefslogtreecommitdiff
path: root/networking
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-06-22 00:47:18 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2009-06-22 00:47:18 +0200
commitfbb12ddc6a53ad97ff6bcc7ed9b253c09001ad2f (patch)
tree94c82f9eac30c86a1ec4f4d06f6c17b72b47cf3e /networking
parentb6bca7703bbe6aacec0bda964c82fad389a02b69 (diff)
downloadbusybox-w32-1_14_2.tar.gz
busybox-w32-1_14_2.tar.bz2
busybox-w32-1_14_2.zip
post 1.14.1 fixes; bump version to 1.14.21_14_2
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking')
-rw-r--r--networking/ftpd.c5
-rw-r--r--networking/httpd.c380
-rw-r--r--networking/telnetd.c42
3 files changed, 232 insertions, 195 deletions
diff --git a/networking/ftpd.c b/networking/ftpd.c
index ac6896117..e85e4f8ea 100644
--- a/networking/ftpd.c
+++ b/networking/ftpd.c
@@ -1320,6 +1320,8 @@ int ftpd_main(int argc UNUSED_PARAM, char **argv)
1320 handle_appe(); 1320 handle_appe();
1321 else if (cmdval == const_STOU) /* "store unique" */ 1321 else if (cmdval == const_STOU) /* "store unique" */
1322 handle_stou(); 1322 handle_stou();
1323 else
1324 goto bad_cmd;
1323 } 1325 }
1324#endif 1326#endif
1325#if 0 1327#if 0
@@ -1340,6 +1342,9 @@ int ftpd_main(int argc UNUSED_PARAM, char **argv)
1340 * (doesn't necessarily mean "we must support them") 1342 * (doesn't necessarily mean "we must support them")
1341 * foo 1.2.3: XXXX - comment 1343 * foo 1.2.3: XXXX - comment
1342 */ 1344 */
1345#if ENABLE_FEATURE_FTP_WRITE
1346 bad_cmd:
1347#endif
1343 cmdio_write_raw(STR(FTP_BADCMD)" Unknown command\r\n"); 1348 cmdio_write_raw(STR(FTP_BADCMD)" Unknown command\r\n");
1344 } 1349 }
1345 } 1350 }
diff --git a/networking/httpd.c b/networking/httpd.c
index 6bf103c56..de4fb9b39 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -32,7 +32,7 @@
32 * foo=`httpd -d $foo` # decode "Hello%20World" as "Hello World" 32 * foo=`httpd -d $foo` # decode "Hello%20World" as "Hello World"
33 * bar=`httpd -e "<Hello World>"` # encode as "&#60Hello&#32World&#62" 33 * bar=`httpd -e "<Hello World>"` # encode as "&#60Hello&#32World&#62"
34 * Note that url encoding for arguments is not the same as html encoding for 34 * Note that url encoding for arguments is not the same as html encoding for
35 * presentation. -d decodes a url-encoded argument while -e encodes in html 35 * presentation. -d decodes an url-encoded argument while -e encodes in html
36 * for page display. 36 * for page display.
37 * 37 *
38 * httpd.conf has the following format: 38 * httpd.conf has the following format:
@@ -54,7 +54,7 @@
54 * /adm:admin:setup # Require user admin, pwd setup on urls starting with /adm/ 54 * /adm:admin:setup # Require user admin, pwd setup on urls starting with /adm/
55 * /adm:toor:PaSsWd # or user toor, pwd PaSsWd on urls starting with /adm/ 55 * /adm:toor:PaSsWd # or user toor, pwd PaSsWd on urls starting with /adm/
56 * .au:audio/basic # additional mime type for audio.au files 56 * .au:audio/basic # additional mime type for audio.au files
57 * *.php:/path/php # running cgi.php scripts through an interpreter 57 * *.php:/path/php # run xxx.php through an interpreter
58 * 58 *
59 * A/D may be as a/d or allow/deny - only first char matters. 59 * A/D may be as a/d or allow/deny - only first char matters.
60 * Deny/Allow IP logic: 60 * Deny/Allow IP logic:
@@ -94,13 +94,13 @@
94 * server exits with an error. 94 * server exits with an error.
95 * 95 *
96 */ 96 */
97 /* TODO: use TCP_CORK, parse_config() */
97 98
98#include "libbb.h" 99#include "libbb.h"
99#if ENABLE_FEATURE_HTTPD_USE_SENDFILE 100#if ENABLE_FEATURE_HTTPD_USE_SENDFILE
100# include <sys/sendfile.h> 101# include <sys/sendfile.h>
101#endif 102#endif
102 103
103//#define DEBUG 1
104#define DEBUG 0 104#define DEBUG 0
105 105
106#define IOBUF_SIZE 8192 /* IO buffer */ 106#define IOBUF_SIZE 8192 /* IO buffer */
@@ -115,8 +115,8 @@
115 115
116#define HEADER_READ_TIMEOUT 60 116#define HEADER_READ_TIMEOUT 60
117 117
118static const char default_path_httpd_conf[] ALIGN1 = "/etc"; 118static const char DEFAULT_PATH_HTTPD_CONF[] ALIGN1 = "/etc";
119static const char httpd_conf[] ALIGN1 = "httpd.conf"; 119static const char HTTPD_CONF[] ALIGN1 = "httpd.conf";
120static const char HTTP_200[] ALIGN1 = "HTTP/1.0 200 OK\r\n"; 120static const char HTTP_200[] ALIGN1 = "HTTP/1.0 200 OK\r\n";
121 121
122typedef struct has_next_ptr { 122typedef struct has_next_ptr {
@@ -242,7 +242,7 @@ struct globals {
242 const char *bind_addr_or_port; 242 const char *bind_addr_or_port;
243 243
244 const char *g_query; 244 const char *g_query;
245 const char *configFile; 245 const char *opt_c_configFile;
246 const char *home_httpd; 246 const char *home_httpd;
247 const char *index_page; 247 const char *index_page;
248 248
@@ -289,7 +289,7 @@ struct globals {
289#define rmt_ip (G.rmt_ip ) 289#define rmt_ip (G.rmt_ip )
290#define bind_addr_or_port (G.bind_addr_or_port) 290#define bind_addr_or_port (G.bind_addr_or_port)
291#define g_query (G.g_query ) 291#define g_query (G.g_query )
292#define configFile (G.configFile ) 292#define opt_c_configFile (G.opt_c_configFile )
293#define home_httpd (G.home_httpd ) 293#define home_httpd (G.home_httpd )
294#define index_page (G.index_page ) 294#define index_page (G.index_page )
295#define found_mime_type (G.found_mime_type ) 295#define found_mime_type (G.found_mime_type )
@@ -452,14 +452,6 @@ static int scan_ip_mask(const char *str, unsigned *ipp, unsigned *maskp)
452/* 452/*
453 * Parse configuration file into in-memory linked list. 453 * Parse configuration file into in-memory linked list.
454 * 454 *
455 * The first non-white character is examined to determine if the config line
456 * is one of the following:
457 * .ext:mime/type # new mime type not compiled into httpd
458 * [adAD]:from # ip address allow/deny, * for wildcard
459 * /path:user:pass # username/password
460 * Ennn:error.html # error page for status nnn
461 * P:/url:[http://]hostname[:port]/new/path # reverse proxy
462 *
463 * Any previous IP rules are discarded. 455 * Any previous IP rules are discarded.
464 * If the flag argument is not SUBDIR_PARSE then all /path and mime rules 456 * If the flag argument is not SUBDIR_PARSE then all /path and mime rules
465 * are also discarded. That is, previous settings are retained if flag is 457 * are also discarded. That is, previous settings are retained if flag is
@@ -469,99 +461,150 @@ static int scan_ip_mask(const char *str, unsigned *ipp, unsigned *maskp)
469 * path Path where to look for httpd.conf (without filename). 461 * path Path where to look for httpd.conf (without filename).
470 * flag Type of the parse request. 462 * flag Type of the parse request.
471 */ 463 */
472/* flag */ 464/* flag param: */
473#define FIRST_PARSE 0 465enum {
474#define SUBDIR_PARSE 1 466 FIRST_PARSE = 0, /* path will be "/etc" */
475#define SIGNALED_PARSE 2 467 SIGNALED_PARSE = 1, /* path will be "/etc" */
476#define FIND_FROM_HTTPD_ROOT 3 468 SUBDIR_PARSE = 2, /* path will be derived from URL */
469};
477static void parse_conf(const char *path, int flag) 470static void parse_conf(const char *path, int flag)
478{ 471{
472 /* internally used extra flag state */
473 enum { TRY_CURDIR_PARSE = 3 };
474
479 FILE *f; 475 FILE *f;
480#if ENABLE_FEATURE_HTTPD_BASIC_AUTH 476 const char *filename;
481 Htaccess *prev;
482#endif
483 Htaccess *cur;
484 const char *filename = configFile;
485 char buf[160]; 477 char buf[160];
486 char *p, *p0;
487 char *after_colon;
488 Htaccess_IP *pip;
489 478
490 /* discard old rules */ 479 /* discard old rules */
491 free_Htaccess_IP_list(&ip_a_d); 480 free_Htaccess_IP_list(&ip_a_d);
492 flg_deny_all = 0; 481 flg_deny_all = 0;
493 /* retain previous auth and mime config only for subdir parse */ 482 /* retain previous auth and mime config only for subdir parse */
494 if (flag != SUBDIR_PARSE) { 483 if (flag != SUBDIR_PARSE) {
484 free_Htaccess_list(&mime_a);
495#if ENABLE_FEATURE_HTTPD_BASIC_AUTH 485#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
496 free_Htaccess_list(&g_auth); 486 free_Htaccess_list(&g_auth);
497#endif 487#endif
498 free_Htaccess_list(&mime_a);
499#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR 488#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
500 free_Htaccess_list(&script_i); 489 free_Htaccess_list(&script_i);
501#endif 490#endif
502 } 491 }
503 492
493 filename = opt_c_configFile;
504 if (flag == SUBDIR_PARSE || filename == NULL) { 494 if (flag == SUBDIR_PARSE || filename == NULL) {
505 filename = alloca(strlen(path) + sizeof(httpd_conf) + 2); 495 filename = alloca(strlen(path) + sizeof(HTTPD_CONF) + 2);
506 sprintf((char *)filename, "%s/%s", path, httpd_conf); 496 sprintf((char *)filename, "%s/%s", path, HTTPD_CONF);
507 } 497 }
508 498
509 while ((f = fopen_for_read(filename)) == NULL) { 499 while ((f = fopen_for_read(filename)) == NULL) {
510 if (flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) { 500 if (flag >= SUBDIR_PARSE) { /* SUBDIR or TRY_CURDIR */
511 /* config file not found, no changes to config */ 501 /* config file not found, no changes to config */
512 return; 502 return;
513 } 503 }
514 if (configFile && flag == FIRST_PARSE) /* if -c option given */ 504 if (flag == FIRST_PARSE) {
515 bb_simple_perror_msg_and_die(filename); 505 /* -c CONFFILE given, but CONFFILE doesn't exist? */
516 flag = FIND_FROM_HTTPD_ROOT; 506 if (opt_c_configFile)
517 filename = httpd_conf; 507 bb_simple_perror_msg_and_die(opt_c_configFile);
508 /* else: no -c, thus we looked at /etc/httpd.conf,
509 * and it's not there. try ./httpd.conf: */
510 }
511 flag = TRY_CURDIR_PARSE;
512 filename = HTTPD_CONF;
518 } 513 }
519 514
520#if ENABLE_FEATURE_HTTPD_BASIC_AUTH 515#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
521 prev = g_auth; 516 /* in "/file:user:pass" lines, we prepend path in subdirs */
522#endif 517 if (flag != SUBDIR_PARSE)
523 /* This could stand some work */ 518 path = "";
524 while ((p0 = fgets(buf, sizeof(buf), f)) != NULL) { 519#endif
525 after_colon = NULL; 520 /* The lines can be:
526 for (p = p0; *p0 != '\0' && *p0 != '#'; p0++) { 521 *
527 if (!isspace(*p0)) { 522 * I:default_index_file
528 *p++ = *p0; 523 * H:http_home
529 if (*p0 == ':' && after_colon == NULL) 524 * [AD]:IP[/mask] # allow/deny, * for wildcard
530 after_colon = p; 525 * Ennn:error.html # error page for status nnn
526 * P:/url:[http://]hostname[:port]/new/path # reverse proxy
527 * .ext:mime/type # mime type
528 * *.php:/path/php # run xxx.php through an interpreter
529 * /file:user:pass # username and password
530 */
531 while (fgets(buf, sizeof(buf), f) != NULL) {
532 unsigned strlen_buf;
533 unsigned char ch;
534 char *after_colon;
535
536 { /* remove all whitespace, and # comments */
537 char *p, *p0;
538
539 p0 = buf;
540 /* skip non-whitespace beginning. Often the whole line
541 * is non-whitespace. We want this case to work fast,
542 * without needless copying, therefore we don't merge
543 * this operation into next while loop. */
544 while ((ch = *p0) != '\0' && ch != '\n' && ch != '#'
545 && ch != ' ' && ch != '\t'
546 ) {
547 p0++;
548 }
549 p = p0;
550 /* if we enter this loop, we have some whitespace.
551 * discard it */
552 while (ch != '\0' && ch != '\n' && ch != '#') {
553 if (ch != ' ' && ch != '\t') {
554 *p++ = ch;
555 }
556 ch = *++p0;
531 } 557 }
558 *p = '\0';
559 strlen_buf = p - buf;
560 if (strlen_buf == 0)
561 continue; /* empty line */
532 } 562 }
533 *p = '\0';
534 563
535 /* test for empty or strange line */ 564 after_colon = strchr(buf, ':');
536 if (after_colon == NULL || *after_colon == '\0') 565 /* strange line? */
566 if (after_colon == NULL || *++after_colon == '\0')
567 goto config_error;
568
569 ch = (buf[0] & ~0x20); /* toupper if it's a letter */
570
571 if (ch == 'I') {
572 index_page = xstrdup(after_colon);
537 continue; 573 continue;
538 p0 = buf; 574 }
539 if (*p0 == 'd' || *p0 == 'a') 575
540 *p0 -= 0x20; /* a/d -> A/D */ 576 /* do not allow jumping around using H in subdir's configs */
541 if (*after_colon == '*') { 577 if (flag == FIRST_PARSE && ch == 'H') {
542 if (*p0 == 'D') { 578 home_httpd = xstrdup(after_colon);
543 /* memorize "deny all" */ 579 xchdir(home_httpd);
544 flg_deny_all = 1;
545 }
546 /* skip assumed "A:*", it is a default anyway */
547 continue; 580 continue;
548 } 581 }
549 582
550 if (*p0 == 'A' || *p0 == 'D') { 583 if (ch == 'A' || ch == 'D') {
551 /* storing current config IP line */ 584 Htaccess_IP *pip;
552 pip = xzalloc(sizeof(Htaccess_IP)); 585
553 if (scan_ip_mask(after_colon, &(pip->ip), &(pip->mask))) { 586 if (*after_colon == '*') {
587 if (ch == 'D') {
588 /* memorize "deny all" */
589 flg_deny_all = 1;
590 }
591 /* skip assumed "A:*", it is a default anyway */
592 continue;
593 }
594 /* store "allow/deny IP/mask" line */
595 pip = xzalloc(sizeof(*pip));
596 if (scan_ip_mask(after_colon, &pip->ip, &pip->mask)) {
554 /* IP{/mask} syntax error detected, protect all */ 597 /* IP{/mask} syntax error detected, protect all */
555 *p0 = 'D'; 598 ch = 'D';
556 pip->mask = 0; 599 pip->mask = 0;
557 } 600 }
558 pip->allow_deny = *p0; 601 pip->allow_deny = ch;
559 if (*p0 == 'D') { 602 if (ch == 'D') {
560 /* Deny:from_IP - prepend */ 603 /* Deny:from_IP - prepend */
561 pip->next = ip_a_d; 604 pip->next = ip_a_d;
562 ip_a_d = pip; 605 ip_a_d = pip;
563 } else { 606 } else {
564 /* A:from_IP - append (thus D precedes A) */ 607 /* A:from_IP - append (thus all D's precedes A's) */
565 Htaccess_IP *prev_IP = ip_a_d; 608 Htaccess_IP *prev_IP = ip_a_d;
566 if (prev_IP == NULL) { 609 if (prev_IP == NULL) {
567 ip_a_d = pip; 610 ip_a_d = pip;
@@ -575,12 +618,12 @@ static void parse_conf(const char *path, int flag)
575 } 618 }
576 619
577#if ENABLE_FEATURE_HTTPD_ERROR_PAGES 620#if ENABLE_FEATURE_HTTPD_ERROR_PAGES
578 if (flag == FIRST_PARSE && *p0 == 'E') { 621 if (flag == FIRST_PARSE && ch == 'E') {
579 unsigned i; 622 unsigned i;
580 int status = atoi(++p0); /* error status code */ 623 int status = atoi(buf + 1); /* error status code */
624
581 if (status < HTTP_CONTINUE) { 625 if (status < HTTP_CONTINUE) {
582 bb_error_msg("config error '%s' in '%s'", buf, filename); 626 goto config_error;
583 continue;
584 } 627 }
585 /* then error page; find matching status */ 628 /* then error page; find matching status */
586 for (i = 0; i < ARRAY_SIZE(http_response_type); i++) { 629 for (i = 0; i < ARRAY_SIZE(http_response_type); i++) {
@@ -597,7 +640,7 @@ static void parse_conf(const char *path, int flag)
597#endif 640#endif
598 641
599#if ENABLE_FEATURE_HTTPD_PROXY 642#if ENABLE_FEATURE_HTTPD_PROXY
600 if (flag == FIRST_PARSE && *p0 == 'P') { 643 if (flag == FIRST_PARSE && ch == 'P') {
601 /* P:/url:[http://]hostname[:port]/new/path */ 644 /* P:/url:[http://]hostname[:port]/new/path */
602 char *url_from, *host_port, *url_to; 645 char *url_from, *host_port, *url_to;
603 Htaccess_Proxy *proxy_entry; 646 Htaccess_Proxy *proxy_entry;
@@ -605,23 +648,20 @@ static void parse_conf(const char *path, int flag)
605 url_from = after_colon; 648 url_from = after_colon;
606 host_port = strchr(after_colon, ':'); 649 host_port = strchr(after_colon, ':');
607 if (host_port == NULL) { 650 if (host_port == NULL) {
608 bb_error_msg("config error '%s' in '%s'", buf, filename); 651 goto config_error;
609 continue;
610 } 652 }
611 *host_port++ = '\0'; 653 *host_port++ = '\0';
612 if (strncmp(host_port, "http://", 7) == 0) 654 if (strncmp(host_port, "http://", 7) == 0)
613 host_port += 7; 655 host_port += 7;
614 if (*host_port == '\0') { 656 if (*host_port == '\0') {
615 bb_error_msg("config error '%s' in '%s'", buf, filename); 657 goto config_error;
616 continue;
617 } 658 }
618 url_to = strchr(host_port, '/'); 659 url_to = strchr(host_port, '/');
619 if (url_to == NULL) { 660 if (url_to == NULL) {
620 bb_error_msg("config error '%s' in '%s'", buf, filename); 661 goto config_error;
621 continue;
622 } 662 }
623 *url_to = '\0'; 663 *url_to = '\0';
624 proxy_entry = xzalloc(sizeof(Htaccess_Proxy)); 664 proxy_entry = xzalloc(sizeof(*proxy_entry));
625 proxy_entry->url_from = xstrdup(url_from); 665 proxy_entry->url_from = xstrdup(url_from);
626 proxy_entry->host_port = xstrdup(host_port); 666 proxy_entry->host_port = xstrdup(host_port);
627 *url_to = '/'; 667 *url_to = '/';
@@ -631,115 +671,87 @@ static void parse_conf(const char *path, int flag)
631 continue; 671 continue;
632 } 672 }
633#endif 673#endif
674 /* the rest of directives are non-alphabetic,
675 * must avoid using "toupper'ed" ch */
676 ch = buf[0];
634 677
635#if ENABLE_FEATURE_HTTPD_BASIC_AUTH 678 if (ch == '.' /* ".ext:mime/type" */
636 if (*p0 == '/') { 679#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
637 /* make full path from httpd root / current_path / config_line_path */ 680 || (ch == '*' && buf[1] == '.') /* "*.php:/path/php" */
638 const char *tp = (flag == SUBDIR_PARSE ? path : ""); 681#endif
639 p0 = xmalloc(strlen(tp) + (after_colon - buf) + 2 + strlen(after_colon)); 682 ) {
640 after_colon[-1] = '\0'; 683 char *p;
641 sprintf(p0, "/%s%s", tp, buf); 684 Htaccess *cur;
642
643 /* looks like bb_simplify_path... */
644 tp = p = p0;
645 do {
646 if (*p == '/') {
647 if (*tp == '/') { /* skip duplicate (or initial) slash */
648 continue;
649 }
650 if (*tp == '.') {
651 if (tp[1] == '/' || tp[1] == '\0') { /* remove extra '.' */
652 continue;
653 }
654 if ((tp[1] == '.') && (tp[2] == '/' || tp[2] == '\0')) {
655 ++tp;
656 if (p > p0) {
657 while (*--p != '/') /* omit previous dir */
658 continue;
659 }
660 continue;
661 }
662 }
663 }
664 *++p = *tp;
665 } while (*++tp);
666 685
667 if ((p == p0) || (*p != '/')) { /* not a trailing slash */ 686 cur = xzalloc(sizeof(*cur) /* includes space for NUL */ + strlen_buf);
668 ++p; /* so keep last character */ 687 strcpy(cur->before_colon, buf);
688 p = cur->before_colon + (after_colon - buf);
689 p[-1] = '\0';
690 cur->after_colon = p;
691 if (ch == '.') {
692 /* .mime line: prepend to mime_a list */
693 cur->next = mime_a;
694 mime_a = cur;
695 }
696#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
697 else {
698 /* script interpreter line: prepend to script_i list */
699 cur->next = script_i;
700 script_i = cur;
669 } 701 }
670 *p = ':';
671 strcpy(p + 1, after_colon);
672 }
673#endif 702#endif
674 if (*p0 == 'I') {
675 index_page = xstrdup(after_colon);
676 continue; 703 continue;
677 } 704 }
678 705
679 /* Do not allow jumping around using H in subdir's configs */
680 if (flag == FIRST_PARSE && *p0 == 'H') {
681 home_httpd = xstrdup(after_colon);
682 xchdir(home_httpd);
683 continue;
684 }
685
686 /* storing current config line */
687 cur = xzalloc(sizeof(Htaccess) + strlen(p0));
688 strcpy(cur->before_colon, p0);
689#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
690 if (*p0 == '/') /* was malloced - see above */
691 free(p0);
692#endif
693 cur->after_colon = strchr(cur->before_colon, ':');
694 *cur->after_colon++ = '\0';
695 if (cur->before_colon[0] == '.') {
696 /* .mime line: prepend to mime_a list */
697 cur->next = mime_a;
698 mime_a = cur;
699 continue;
700 }
701#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
702 if (cur->before_colon[0] == '*' && cur->before_colon[1] == '.') {
703 /* script interpreter line: prepend to script_i list */
704 cur->next = script_i;
705 script_i = cur;
706 continue;
707 }
708#endif
709#if ENABLE_FEATURE_HTTPD_BASIC_AUTH 706#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
710//TODO: we do not test for leading "/"?? 707 if (ch == '/') { /* "/file:user:pass" */
711//also, do we leak cur if BASIC_AUTH is off? 708 char *p;
712 if (prev == NULL) { 709 Htaccess *cur;
713 /* first line */ 710 unsigned file_len;
714 g_auth = prev = cur; 711
715 } else { 712 /* note: path is "" unless we are in SUBDIR parse,
716 /* sort path, if current length eq or bigger then move up */ 713 * otherwise it does NOT start with "/" */
717 Htaccess *prev_hti = g_auth; 714 cur = xzalloc(sizeof(*cur) /* includes space for NUL */
718 size_t l = strlen(cur->before_colon); 715 + 1 + strlen(path)
719 Htaccess *hti; 716 + strlen_buf
720 717 );
721 for (hti = prev_hti; hti; hti = hti->next) { 718 /* form "/path/file" */
722 if (l >= strlen(hti->before_colon)) { 719 sprintf(cur->before_colon, "/%s%.*s",
723 /* insert before hti */ 720 path,
724 cur->next = hti; 721 (int) (after_colon - buf - 1), /* includes "/", but not ":" */
725 if (prev_hti != hti) { 722 buf);
726 prev_hti->next = cur; 723 /* canonicalize it */
727 } else { 724 p = bb_simplify_abs_path_inplace(cur->before_colon);
728 /* insert as top */ 725 file_len = p - cur->before_colon;
729 g_auth = cur; 726 /* add "user:pass" after NUL */
727 strcpy(++p, after_colon);
728 cur->after_colon = p;
729
730 /* insert cur into g_auth */
731 /* g_auth is sorted by decreased filename length */
732 {
733 Htaccess *auth, **authp;
734
735 authp = &g_auth;
736 while ((auth = *authp) != NULL) {
737 if (file_len >= strlen(auth->before_colon)) {
738 /* insert cur before auth */
739 cur->next = auth;
740 break;
730 } 741 }
731 break; 742 authp = &auth->next;
732 } 743 }
733 if (prev_hti != hti) 744 *authp = cur;
734 prev_hti = prev_hti->next;
735 }
736 if (!hti) { /* not inserted, add to bottom */
737 prev->next = cur;
738 prev = cur;
739 } 745 }
746 continue;
740 } 747 }
741#endif /* BASIC_AUTH */ 748#endif /* BASIC_AUTH */
749
750 /* the line is not recognized */
751 config_error:
752 bb_error_msg("config error '%s' in '%s'", buf, filename);
742 } /* while (fgets) */ 753 } /* while (fgets) */
754
743 fclose(f); 755 fclose(f);
744} 756}
745 757
@@ -1527,11 +1539,6 @@ static NOINLINE void send_file_and_exit(const char *url, int what)
1527 send_headers_and_exit(HTTP_NOT_FOUND); 1539 send_headers_and_exit(HTTP_NOT_FOUND);
1528 log_and_exit(); 1540 log_and_exit();
1529 } 1541 }
1530
1531 if (DEBUG)
1532 bb_error_msg("sending file '%s' content-type: %s",
1533 url, found_mime_type);
1534
1535 /* If you want to know about EPIPE below 1542 /* If you want to know about EPIPE below
1536 * (happens if you abort downloads from local httpd): */ 1543 * (happens if you abort downloads from local httpd): */
1537 signal(SIGPIPE, SIG_IGN); 1544 signal(SIGPIPE, SIG_IGN);
@@ -1559,6 +1566,11 @@ static NOINLINE void send_file_and_exit(const char *url, int what)
1559 } 1566 }
1560 } 1567 }
1561 } 1568 }
1569
1570 if (DEBUG)
1571 bb_error_msg("sending file '%s' content-type: %s",
1572 url, found_mime_type);
1573
1562#if ENABLE_FEATURE_HTTPD_RANGES 1574#if ENABLE_FEATURE_HTTPD_RANGES
1563 if (what == SEND_BODY) 1575 if (what == SEND_BODY)
1564 range_start = 0; /* err pages and ranges don't mix */ 1576 range_start = 0; /* err pages and ranges don't mix */
@@ -2031,8 +2043,8 @@ static void handle_incoming_and_exit(const len_and_sockaddr *fromAddr)
2031 /* We are done reading headers, disable peer timeout */ 2043 /* We are done reading headers, disable peer timeout */
2032 alarm(0); 2044 alarm(0);
2033 2045
2034 if (strcmp(bb_basename(urlcopy), httpd_conf) == 0 || !ip_allowed) { 2046 if (strcmp(bb_basename(urlcopy), HTTPD_CONF) == 0 || !ip_allowed) {
2035 /* protect listing [/path]/httpd_conf or IP deny */ 2047 /* protect listing [/path]/httpd.conf or IP deny */
2036 send_headers_and_exit(HTTP_FORBIDDEN); 2048 send_headers_and_exit(HTTP_FORBIDDEN);
2037 } 2049 }
2038 2050
@@ -2074,7 +2086,7 @@ static void handle_incoming_and_exit(const len_and_sockaddr *fromAddr)
2074 header_ptr += 2; 2086 header_ptr += 2;
2075 write(proxy_fd, header_buf, header_ptr - header_buf); 2087 write(proxy_fd, header_buf, header_ptr - header_buf);
2076 free(header_buf); /* on the order of 8k, free it */ 2088 free(header_buf); /* on the order of 8k, free it */
2077 /* cgi_io_loop_and_exit needs to have two disctinct fds */ 2089 /* cgi_io_loop_and_exit needs to have two distinct fds */
2078 cgi_io_loop_and_exit(proxy_fd, dup(proxy_fd), length); 2090 cgi_io_loop_and_exit(proxy_fd, dup(proxy_fd), length);
2079 } 2091 }
2080#endif 2092#endif
@@ -2245,7 +2257,7 @@ static void mini_httpd_inetd(void)
2245 2257
2246static void sighup_handler(int sig UNUSED_PARAM) 2258static void sighup_handler(int sig UNUSED_PARAM)
2247{ 2259{
2248 parse_conf(default_path_httpd_conf, SIGNALED_PARSE); 2260 parse_conf(DEFAULT_PATH_HTTPD_CONF, SIGNALED_PARSE);
2249} 2261}
2250 2262
2251enum { 2263enum {
@@ -2304,7 +2316,7 @@ int httpd_main(int argc UNUSED_PARAM, char **argv)
2304 USE_FEATURE_HTTPD_AUTH_MD5("m:") 2316 USE_FEATURE_HTTPD_AUTH_MD5("m:")
2305 USE_FEATURE_HTTPD_SETUID("u:") 2317 USE_FEATURE_HTTPD_SETUID("u:")
2306 "p:ifv", 2318 "p:ifv",
2307 &configFile, &url_for_decode, &home_httpd 2319 &opt_c_configFile, &url_for_decode, &home_httpd
2308 USE_FEATURE_HTTPD_ENCODE_URL_STR(, &url_for_encode) 2320 USE_FEATURE_HTTPD_ENCODE_URL_STR(, &url_for_encode)
2309 USE_FEATURE_HTTPD_BASIC_AUTH(, &g_realm) 2321 USE_FEATURE_HTTPD_BASIC_AUTH(, &g_realm)
2310 USE_FEATURE_HTTPD_AUTH_MD5(, &pass) 2322 USE_FEATURE_HTTPD_AUTH_MD5(, &pass)
@@ -2375,7 +2387,7 @@ int httpd_main(int argc UNUSED_PARAM, char **argv)
2375 } 2387 }
2376#endif 2388#endif
2377 2389
2378 parse_conf(default_path_httpd_conf, FIRST_PARSE); 2390 parse_conf(DEFAULT_PATH_HTTPD_CONF, FIRST_PARSE);
2379 if (!(opt & OPT_INETD)) 2391 if (!(opt & OPT_INETD))
2380 signal(SIGHUP, sighup_handler); 2392 signal(SIGHUP, sighup_handler);
2381 2393
diff --git a/networking/telnetd.c b/networking/telnetd.c
index ccf328925..4c5ea3ab3 100644
--- a/networking/telnetd.c
+++ b/networking/telnetd.c
@@ -199,9 +199,17 @@ static size_t iac_safe_write(int fd, const char *buf, size_t count)
199 return total + rc; 199 return total + rc;
200} 200}
201 201
202/* Must match getopt32 string */
203enum {
204 OPT_WATCHCHILD = (1 << 2), /* -K */
205 OPT_INETD = (1 << 3) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -i */
206 OPT_PORT = (1 << 4) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -p */
207 OPT_FOREGROUND = (1 << 6) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -F */
208};
209
202static struct tsession * 210static struct tsession *
203make_new_session( 211make_new_session(
204 USE_FEATURE_TELNETD_STANDALONE(int sock) 212 USE_FEATURE_TELNETD_STANDALONE(int master_fd, int sock)
205 SKIP_FEATURE_TELNETD_STANDALONE(void) 213 SKIP_FEATURE_TELNETD_STANDALONE(void)
206) { 214) {
207 const char *login_argv[2]; 215 const char *login_argv[2];
@@ -288,9 +296,29 @@ make_new_session(
288 /* Restore default signal handling ASAP */ 296 /* Restore default signal handling ASAP */
289 bb_signals((1 << SIGCHLD) + (1 << SIGPIPE), SIG_DFL); 297 bb_signals((1 << SIGCHLD) + (1 << SIGPIPE), SIG_DFL);
290 298
299#if ENABLE_FEATURE_TELNETD_STANDALONE
300 if (!(option_mask32 & OPT_INETD)) {
301 struct tsession *tp = sessions;
302 while (tp) {
303 close(tp->ptyfd);
304 close(tp->sockfd_read);
305 /* sockfd_write == sockfd_read for standalone telnetd */
306 /*close(tp->sockfd_write);*/
307 tp = tp->next;
308 }
309 }
310#endif
311
291 /* Make new session and process group */ 312 /* Make new session and process group */
292 setsid(); 313 setsid();
293 314
315 close(fd);
316#if ENABLE_FEATURE_TELNETD_STANDALONE
317 close(sock);
318 if (master_fd >= 0)
319 close(master_fd);
320#endif
321
294 /* Open the child's side of the tty. */ 322 /* Open the child's side of the tty. */
295 /* NB: setsid() disconnects from any previous ctty's. Therefore 323 /* NB: setsid() disconnects from any previous ctty's. Therefore
296 * we must open child's side of the tty AFTER setsid! */ 324 * we must open child's side of the tty AFTER setsid! */
@@ -329,14 +357,6 @@ make_new_session(
329 _exit(EXIT_FAILURE); /*bb_perror_msg_and_die("execv %s", loginpath);*/ 357 _exit(EXIT_FAILURE); /*bb_perror_msg_and_die("execv %s", loginpath);*/
330} 358}
331 359
332/* Must match getopt32 string */
333enum {
334 OPT_WATCHCHILD = (1 << 2), /* -K */
335 OPT_INETD = (1 << 3) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -i */
336 OPT_PORT = (1 << 4) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -p */
337 OPT_FOREGROUND = (1 << 6) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -F */
338};
339
340#if ENABLE_FEATURE_TELNETD_STANDALONE 360#if ENABLE_FEATURE_TELNETD_STANDALONE
341 361
342static void 362static void
@@ -465,7 +485,7 @@ int telnetd_main(int argc UNUSED_PARAM, char **argv)
465 485
466#if ENABLE_FEATURE_TELNETD_STANDALONE 486#if ENABLE_FEATURE_TELNETD_STANDALONE
467 if (IS_INETD) { 487 if (IS_INETD) {
468 sessions = make_new_session(0); 488 sessions = make_new_session(-1, 0);
469 if (!sessions) /* pty opening or vfork problem, exit */ 489 if (!sessions) /* pty opening or vfork problem, exit */
470 return 1; /* make_new_session prints error message */ 490 return 1; /* make_new_session prints error message */
471 } else { 491 } else {
@@ -553,7 +573,7 @@ int telnetd_main(int argc UNUSED_PARAM, char **argv)
553 if (fd < 0) 573 if (fd < 0)
554 goto again; 574 goto again;
555 /* Create a new session and link it into our active list */ 575 /* Create a new session and link it into our active list */
556 new_ts = make_new_session(fd); 576 new_ts = make_new_session(master_fd, fd);
557 if (new_ts) { 577 if (new_ts) {
558 new_ts->next = sessions; 578 new_ts->next = sessions;
559 sessions = new_ts; 579 sessions = new_ts;