[PATCH] chpasswd: support -c argument and respect DEFAULT_PASSWD_ALGO

Pascal Bach pascal.bach at siemens.com
Mon Nov 30 08:14:01 UTC 2015


---
 loginutils/chpasswd.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)

diff --git a/loginutils/chpasswd.c b/loginutils/chpasswd.c
index 6c41d17..50b3ccf 100644
--- a/loginutils/chpasswd.c
+++ b/loginutils/chpasswd.c
@@ -24,26 +24,27 @@
 //kbuild:lib-$(CONFIG_CHPASSWD) += chpasswd.o
 
 //usage:#define chpasswd_trivial_usage
-//usage:	IF_LONG_OPTS("[--md5|--encrypted]") IF_NOT_LONG_OPTS("[-m|-e]")
+//usage:	IF_LONG_OPTS("[--md5|--encrypted|--crypt-method]") IF_NOT_LONG_OPTS("[-m|-e|-c]")
 //usage:#define chpasswd_full_usage "\n\n"
 //usage:       "Read user:password from stdin and update /etc/passwd\n"
 //usage:	IF_LONG_OPTS(
-//usage:     "\n	-e,--encrypted	Supplied passwords are in encrypted form"
-//usage:     "\n	-m,--md5	Use MD5 encryption instead of DES"
+//usage:     "\n	-e,--encrypted		Supplied passwords are in encrypted form"
+//usage:     "\n	-m,--md5		Use MD5 encryption instead of DES"
+//usage:     "\n	-c,--crypt-method	Use the specified method to encrypt the passwords"
 //usage:	)
 //usage:	IF_NOT_LONG_OPTS(
 //usage:     "\n	-e	Supplied passwords are in encrypted form"
 //usage:     "\n	-m	Use MD5 encryption instead of DES"
+//usage:     "\n	-c	Use the specified method to encrypt the passwords"
 //usage:	)
 
-//TODO: implement -c ALGO
-
 #include "libbb.h"
 
 #if ENABLE_LONG_OPTS
 static const char chpasswd_longopts[] ALIGN1 =
-	"encrypted\0" No_argument "e"
-	"md5\0"       No_argument "m"
+	"encrypted\0"    No_argument       "e"
+	"md5\0"          No_argument       "m"
+	"crypt-method\0" Required_argument "c"
 	;
 #endif
 
@@ -54,14 +55,15 @@ int chpasswd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int chpasswd_main(int argc UNUSED_PARAM, char **argv)
 {
 	char *name;
+	char *algo = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO;
 	int opt;
 
 	if (getuid() != 0)
 		bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
 
-	opt_complementary = "m--e:e--m";
+	opt_complementary = "m--ec:e--mc:c--em";
 	IF_LONG_OPTS(applet_long_options = chpasswd_longopts;)
-	opt = getopt32(argv, "em");
+	opt = getopt32(argv, "emc:", &algo);
 
 	while ((name = xmalloc_fgetline(stdin)) != NULL) {
 		char *free_me;
@@ -77,15 +79,14 @@ int chpasswd_main(int argc UNUSED_PARAM, char **argv)
 
 		free_me = NULL;
 		if (!(opt & OPT_ENC)) {
-			char salt[sizeof("$N$XXXXXXXX")];
+			char salt[MAX_PW_SALT_LEN];
 
-			crypt_make_salt(salt, 1);
 			if (opt & OPT_MD5) {
-				salt[0] = '$';
-				salt[1] = '1';
-				salt[2] = '$';
-				crypt_make_salt(salt + 3, 4);
+				/* Force MD5 if the -m flag is set */
+				algo = "md5";
 			}
+
+			crypt_make_pw_salt(salt, algo);
 			free_me = pass = pw_encrypt(pass, salt, 0);
 		}
 
-- 
2.1.4



More information about the busybox mailing list