[PATCH] wget: Support --user and --password options

Sergey Ponomarev stokito at gmail.com
Sun May 8 20:15:18 UTC 2022


GNU wget and OpenWrt uclient allows to pass user/pass via dedicated options.
For BB wget we must adjust a URL.
The patch adds the options to improve compatibility and experience.

function                                             old     new   delta
usage_messages                                      3914    3977     +63
parse_url                                            347     397     +50
static.wget_longopts                                 185     203     +18
wget_main                                           2680    2693     +13
retrieve_file_data                                   623     629      +6
get_sanitized_hdr                                    176     179      +3
fgets_trim_sanitize                                  166     169      +3
base64enc                                             56      59      +3
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 8/0 up/down: 159/0)             Total: 159 bytes
   text	   data	    bss	    dec	    hex	filename
 164088	   3747	   1688	 169523	  29633	busybox_old
 164247	   3747	   1688	 169682	  296d2	busybox_unstripped

Signed-off-by: Sergey Ponomarev <stokito at gmail.com>
---
 networking/wget.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/networking/wget.c b/networking/wget.c
index 36461e489..797c66b47 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -152,6 +152,10 @@
 //usage:     "\n	--header STR	Add STR (of form 'header: value') to headers"
 //usage:     "\n	--post-data STR	Send STR using POST method"
 //usage:     "\n	--post-file FILE	Send FILE using POST method"
+//usage:	IF_FEATURE_WGET_AUTHENTICATION(
+//usage:     "\n	--user=USER	User (Basic Auth / FTP)"
+//usage:     "\n	--password=PASS	Password"
+//usage:	)
 //usage:	IF_FEATURE_WGET_OPENSSL(
 //usage:     "\n	--no-check-certificate	Don't validate the server's certificate"
 //usage:	)
@@ -255,6 +259,10 @@ struct globals {
 	char *post_file;
 	char *extra_headers;
 	unsigned char user_headers; /* Headers mentioned by the user */
+#if ENABLE_FEATURE_WGET_AUTHENTICATION
+	char *user;
+	char *password;
+#endif
 #endif
 	char *fname_out;        /* where to direct output (-O) */
 	char *fname_log;        /* where to direct log (-o) */
@@ -302,6 +310,8 @@ enum {
 	WGET_OPT_SPIDER     = (1 << 13) * ENABLE_FEATURE_WGET_LONG_OPTIONS,
 	WGET_OPT_NO_CHECK_CERT = (1 << 14) * ENABLE_FEATURE_WGET_LONG_OPTIONS,
 	WGET_OPT_POST_FILE  = (1 << 15) * ENABLE_FEATURE_WGET_LONG_OPTIONS,
+	WGET_OPT_USER       = (1 << 16) * ENABLE_FEATURE_WGET_LONG_OPTIONS,
+	WGET_OPT_PASSWORD   = (1 << 17) * ENABLE_FEATURE_WGET_LONG_OPTIONS,
 	/* hijack this bit for other than opts purposes: */
 	WGET_NO_FTRUNCATE   = (1 << 31)
 };
@@ -617,6 +627,11 @@ static void parse_url(const char *src_url, struct host_info *h)
 		h->user = xstrdup(percent_decode_in_place(h->host, /*strict:*/ 0));
 		h->host = sp + 1;
 	}
+#if ENABLE_FEATURE_WGET_LONG_OPTIONS && ENABLE_FEATURE_WGET_AUTHENTICATION
+	if (h->user == NULL && G.user != NULL && G.password != NULL) {
+		h->user = xasprintf("%s:%s", G.user, G.password);
+	}
+#endif
 	/* else: h->user remains NULL, or as set by original request
 	 * before redirect (if we are here after a redirect).
 	 */
@@ -1521,6 +1536,9 @@ IF_DESKTOP(	"no-verbose\0"       No_argument       "\xf0")
 IF_DESKTOP(	"no-clobber\0"       No_argument       "\xf0")
 IF_DESKTOP(	"no-host-directories\0" No_argument    "\xf0")
 IF_DESKTOP(	"no-parent\0"        No_argument       "\xf0")
+IF_FEATURE_WGET_AUTHENTICATION(
+		"user\0"             Required_argument     "\xf1"
+		"password\0"         Required_argument     "\xf2")
 		;
 # define GETOPT32 getopt32long
 # define LONGOPTS ,wget_longopts
@@ -1569,6 +1587,8 @@ IF_DESKTOP(	"no-parent\0"        No_argument       "\xf0")
 		IF_FEATURE_WGET_LONG_OPTIONS(, &headers_llist)
 		IF_FEATURE_WGET_LONG_OPTIONS(, &G.post_data)
 		IF_FEATURE_WGET_LONG_OPTIONS(, &G.post_file)
+		IF_FEATURE_WGET_LONG_OPTIONS(IF_FEATURE_WGET_AUTHENTICATION(, &G.user))
+		IF_FEATURE_WGET_LONG_OPTIONS(IF_FEATURE_WGET_AUTHENTICATION(, &G.password))
 	);
 #if 0 /* option bits debug */
 	if (option_mask32 & WGET_OPT_RETRIES) bb_error_msg("-t NUM");
-- 
2.34.1



More information about the busybox mailing list