aboutsummaryrefslogtreecommitdiff
path: root/networking/wget.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2017-01-23 01:08:16 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2017-01-23 01:08:16 +0100
commit9a647c326a41e8160d53e6cb5470161a44c0e8cf (patch)
tree1fef73df291e5c5897aef1bb32b65206caacf879 /networking/wget.c
parente1f90d13fa07d2974908470ce818ef956b7740f2 (diff)
downloadbusybox-w32-9a647c326a41e8160d53e6cb5470161a44c0e8cf.tar.gz
busybox-w32-9a647c326a41e8160d53e6cb5470161a44c0e8cf.tar.bz2
busybox-w32-9a647c326a41e8160d53e6cb5470161a44c0e8cf.zip
separate TLS code into a library, use in in wget
A new applet, ssl_client, is the TLS debug thing now. It doubles as wget's NOMMU helper. In MMU mode, wget still forks, but then directly calls TLS code, without execing. This can also be applied to sendmail/popmail (SMTPS / SMTP+starttls support) and nc --ssl (ncat, nmap's nc clone, has such option). function old new delta tls_handshake - 1691 +1691 tls_run_copy_loop - 443 +443 ssl_client_main - 128 +128 packed_usage 30978 31007 +29 wget_main 2508 2535 +27 applet_names 2553 2560 +7 ... xwrite_encrypted 360 342 -18 tls_main 2127 - -2127 ------------------------------------------------------------------------------ (add/remove: 4/1 grow/shrink: 13/8 up/down: 2351/-2195) Total: 156 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/wget.c')
-rw-r--r--networking/wget.c102
1 files changed, 54 insertions, 48 deletions
diff --git a/networking/wget.c b/networking/wget.c
index 58ead4c96..a448acdae 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -47,18 +47,26 @@
47//config: FEATURE_WGET_LONG_OPTIONS is also enabled, the --timeout option 47//config: FEATURE_WGET_LONG_OPTIONS is also enabled, the --timeout option
48//config: will work in addition to -T. 48//config: will work in addition to -T.
49//config: 49//config:
50//config:config FEATURE_WGET_HTTPS
51//config: bool "Support HTTPS using internal TLS code"
52//config: default y
53//config: depends on WGET
54//config: select TLS
55//config: help
56//config: wget will use internal TLS code to connect to https:// URLs.
57//config: Note:
58//config: On NOMMU machines, ssl_helper applet should be available
59//config: in the $PATH for this to work. Make sure to select that applet.
60//config:
50//config:config FEATURE_WGET_OPENSSL 61//config:config FEATURE_WGET_OPENSSL
51//config: bool "Try to connect to HTTPS using openssl" 62//config: bool "Try to connect to HTTPS using openssl"
52//config: default y 63//config: default y
53//config: depends on WGET 64//config: depends on WGET
54//config: help 65//config: help
55//config: Choose how wget establishes SSL connection for https:// URLs. 66//config: Try to use openssl to handle HTTPS.
56//config:
57//config: Busybox itself contains no SSL code. wget will spawn
58//config: a helper program to talk over HTTPS.
59//config: 67//config:
60//config: OpenSSL has a simple SSL client for debug purposes. 68//config: OpenSSL has a simple SSL client for debug purposes.
61//config: If you select "openssl" helper, wget will effectively run: 69//config: If you select this option, wget will effectively run:
62//config: "openssl s_client -quiet -connect hostname:443 70//config: "openssl s_client -quiet -connect hostname:443
63//config: -servername hostname 2>/dev/null" and pipe its data 71//config: -servername hostname 2>/dev/null" and pipe its data
64//config: through it. -servername is not used if hostname is numeric. 72//config: through it. -servername is not used if hostname is numeric.
@@ -71,24 +79,9 @@
71//config: openssl is also a big binary, often dynamically linked 79//config: openssl is also a big binary, often dynamically linked
72//config: against ~15 libraries. 80//config: against ~15 libraries.
73//config: 81//config:
74//config:config FEATURE_WGET_SSL_HELPER 82//config: If openssl can't be executed, internal TLS code will be used
75//config: bool "Try to connect to HTTPS using ssl_helper" 83//config: (if you enabled it); if openssl can be executed but fails later,
76//config: default y 84//config: wget can't detect this, and download will fail.
77//config: depends on WGET
78//config: help
79//config: Choose how wget establishes SSL connection for https:// URLs.
80//config:
81//config: Busybox itself contains no SSL code. wget will spawn
82//config: a helper program to talk over HTTPS.
83//config:
84//config: ssl_helper is a tool which can be built statically
85//config: from busybox sources against a small embedded SSL library.
86//config: Please see networking/ssl_helper/README.
87//config: It does not require double host resolution and emits
88//config: error messages to stderr.
89//config:
90//config: Precompiled static binary may be available at
91//config: http://busybox.net/downloads/binaries/
92 85
93//applet:IF_WGET(APPLET(wget, BB_DIR_USR_BIN, BB_SUID_DROP)) 86//applet:IF_WGET(APPLET(wget, BB_DIR_USR_BIN, BB_SUID_DROP))
94 87
@@ -137,7 +130,7 @@
137#endif 130#endif
138 131
139 132
140#define SSL_SUPPORTED (ENABLE_FEATURE_WGET_OPENSSL || ENABLE_FEATURE_WGET_SSL_HELPER) 133#define SSL_SUPPORTED (ENABLE_FEATURE_WGET_OPENSSL || ENABLE_FEATURE_WGET_HTTPS)
141 134
142struct host_info { 135struct host_info {
143 char *allocated; 136 char *allocated;
@@ -657,7 +650,7 @@ static int spawn_https_helper_openssl(const char *host, unsigned port)
657 char *servername; 650 char *servername;
658 int sp[2]; 651 int sp[2];
659 int pid; 652 int pid;
660 IF_FEATURE_WGET_SSL_HELPER(volatile int child_failed = 0;) 653 IF_FEATURE_WGET_HTTPS(volatile int child_failed = 0;)
661 654
662 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) != 0) 655 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) != 0)
663 /* Kernel can have AF_UNIX support disabled */ 656 /* Kernel can have AF_UNIX support disabled */
@@ -702,7 +695,7 @@ static int spawn_https_helper_openssl(const char *host, unsigned port)
702 695
703 BB_EXECVP(argv[0], argv); 696 BB_EXECVP(argv[0], argv);
704 xmove_fd(3, 2); 697 xmove_fd(3, 2);
705# if ENABLE_FEATURE_WGET_SSL_HELPER 698# if ENABLE_FEATURE_WGET_HTTPS
706 child_failed = 1; 699 child_failed = 1;
707 xfunc_die(); 700 xfunc_die();
708# else 701# else
@@ -715,7 +708,7 @@ static int spawn_https_helper_openssl(const char *host, unsigned port)
715 free(servername); 708 free(servername);
716 free(allocated); 709 free(allocated);
717 close(sp[1]); 710 close(sp[1]);
718# if ENABLE_FEATURE_WGET_SSL_HELPER 711# if ENABLE_FEATURE_WGET_HTTPS
719 if (child_failed) { 712 if (child_failed) {
720 close(sp[0]); 713 close(sp[0]);
721 return -1; 714 return -1;
@@ -725,38 +718,51 @@ static int spawn_https_helper_openssl(const char *host, unsigned port)
725} 718}
726#endif 719#endif
727 720
728/* See networking/ssl_helper/README how to build one */ 721#if ENABLE_FEATURE_WGET_HTTPS
729#if ENABLE_FEATURE_WGET_SSL_HELPER 722static void spawn_ssl_client(const char *host, int network_fd)
730static void spawn_https_helper_small(int network_fd)
731{ 723{
732 int sp[2]; 724 int sp[2];
733 int pid; 725 int pid;
726 char *servername, *p;
727
728 servername = xstrdup(host);
729 p = strrchr(servername, ':');
730 if (p) *p = '\0';
734 731
735 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) != 0) 732 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) != 0)
736 /* Kernel can have AF_UNIX support disabled */ 733 /* Kernel can have AF_UNIX support disabled */
737 bb_perror_msg_and_die("socketpair"); 734 bb_perror_msg_and_die("socketpair");
738 735
736 fflush_all();
739 pid = BB_MMU ? xfork() : xvfork(); 737 pid = BB_MMU ? xfork() : xvfork();
740 if (pid == 0) { 738 if (pid == 0) {
741 /* Child */ 739 /* Child */
742 char *argv[3];
743
744 close(sp[0]); 740 close(sp[0]);
745 xmove_fd(sp[1], 0); 741 xmove_fd(sp[1], 0);
746 xdup2(0, 1); 742 xdup2(0, 1);
747 xmove_fd(network_fd, 3); 743 if (BB_MMU) {
748 /* 744 tls_state_t *tls = new_tls_state();
749 * A simple ssl/tls helper 745 tls->ifd = tls->ofd = network_fd;
750 */ 746 tls_handshake(tls, servername);
751 argv[0] = (char*)"ssl_helper"; 747 tls_run_copy_loop(tls);
752 argv[1] = (char*)"-d3"; 748 exit(0);
753 argv[2] = NULL; 749 } else {
754 BB_EXECVP(argv[0], argv); 750 char *argv[5];
755 bb_perror_msg_and_die("can't execute '%s'", argv[0]); 751 xmove_fd(network_fd, 3);
752 argv[0] = (char*)"ssl_client";
753 argv[1] = (char*)"-s3";
754 //TODO: if (!is_ip_address(servername))...
755 argv[2] = (char*)"-n";
756 argv[3] = servername;
757 argv[4] = NULL;
758 BB_EXECVP(argv[0], argv);
759 bb_perror_msg_and_die("can't execute '%s'", argv[0]);
760 }
756 /* notreached */ 761 /* notreached */
757 } 762 }
758 763
759 /* Parent */ 764 /* Parent */
765 free(servername);
760 close(sp[1]); 766 close(sp[1]);
761 xmove_fd(sp[0], network_fd); 767 xmove_fd(sp[0], network_fd);
762} 768}
@@ -1005,16 +1011,16 @@ static void download_one_url(const char *url)
1005 1011
1006 /* Open socket to http(s) server */ 1012 /* Open socket to http(s) server */
1007#if ENABLE_FEATURE_WGET_OPENSSL 1013#if ENABLE_FEATURE_WGET_OPENSSL
1008 /* openssl (and maybe ssl_helper) support is configured */ 1014 /* openssl (and maybe internal TLS) support is configured */
1009 if (target.protocol == P_HTTPS) { 1015 if (target.protocol == P_HTTPS) {
1010 /* openssl-based helper 1016 /* openssl-based helper
1011 * Inconvenient API since we can't give it an open fd 1017 * Inconvenient API since we can't give it an open fd
1012 */ 1018 */
1013 int fd = spawn_https_helper_openssl(server.host, server.port); 1019 int fd = spawn_https_helper_openssl(server.host, server.port);
1014# if ENABLE_FEATURE_WGET_SSL_HELPER 1020# if ENABLE_FEATURE_WGET_HTTPS
1015 if (fd < 0) { /* no openssl? try ssl_helper */ 1021 if (fd < 0) { /* no openssl? try internal */
1016 sfp = open_socket(lsa); 1022 sfp = open_socket(lsa);
1017 spawn_https_helper_small(fileno(sfp)); 1023 spawn_ssl_client(server.host, fileno(sfp));
1018 goto socket_opened; 1024 goto socket_opened;
1019 } 1025 }
1020# else 1026# else
@@ -1027,11 +1033,11 @@ static void download_one_url(const char *url)
1027 } 1033 }
1028 sfp = open_socket(lsa); 1034 sfp = open_socket(lsa);
1029 socket_opened: 1035 socket_opened:
1030#elif ENABLE_FEATURE_WGET_SSL_HELPER 1036#elif ENABLE_FEATURE_WGET_HTTPS
1031 /* Only ssl_helper support is configured */ 1037 /* Only internal TLS support is configured */
1032 sfp = open_socket(lsa); 1038 sfp = open_socket(lsa);
1033 if (target.protocol == P_HTTPS) 1039 if (target.protocol == P_HTTPS)
1034 spawn_https_helper_small(fileno(sfp)); 1040 spawn_ssl_client(server.host, fileno(sfp));
1035#else 1041#else
1036 /* ssl (https) support is not configured */ 1042 /* ssl (https) support is not configured */
1037 sfp = open_socket(lsa); 1043 sfp = open_socket(lsa);