[new applet] Cryptpw.

Thomas Lundquist lists at zelow.no
Sun Jun 11 03:56:39 PDT 2006


I've submitted this one before but got a "no, port mkpasswd" but later
at least Tito agreed that it was bloated.

So, i've remade the patch for Tito's redo of passwd and here it is. very
simple and used for:

CRYPTED=`cryptpw -a md5 $CLEAR` 

in a startup script which means it can't really bomb out so it does not
use obscure since it prints errors to STDOUT (is that really a good /
right thing?)


Thomas
-------------- next part --------------
diff -urN busybox/include/applets.h busybox-cryptpw/include/applets.h
--- busybox/include/applets.h	2006-06-08 01:20:48.000000000 -0600
+++ busybox-cryptpw/include/applets.h	2006-06-08 14:25:47.000000000 -0600
@@ -78,6 +78,7 @@
 USE_CPIO(APPLET(cpio, _BB_DIR_BIN, _BB_SUID_NEVER))
 USE_CROND(APPLET(crond, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
 USE_CRONTAB(APPLET(crontab, _BB_DIR_USR_BIN, _BB_SUID_ALWAYS))
+USE_CRYPTPW(APPLET(cryptpw, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
 USE_CUT(APPLET(cut, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
 USE_DATE(APPLET(date, _BB_DIR_BIN, _BB_SUID_NEVER))
 USE_DC(APPLET(dc, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
diff -urN busybox/include/libbb.h busybox-cryptpw/include/libbb.h
--- busybox/include/libbb.h	2006-06-08 01:20:48.000000000 -0600
+++ busybox-cryptpw/include/libbb.h	2006-06-09 03:01:27.000000000 -0600
@@ -460,6 +460,8 @@
 extern char *pw_encrypt(const char *clear, const char *salt);
 extern struct spwd *pwd_to_spwd(const struct passwd *pw);
 extern int obscure(const char *old, const char *newval, const struct passwd *pwdp);
+extern int get_algo(char *a);
+extern char *crypt_make_salt(void);
 
 extern int bb_xopen(const char *pathname, int flags);
 extern int bb_xopen3(const char *pathname, int flags, int mode);
diff -urN busybox/include/usage.h busybox-cryptpw/include/usage.h
--- busybox/include/usage.h	2006-06-08 01:20:48.000000000 -0600
+++ busybox-cryptpw/include/usage.h	2006-06-08 14:16:20.000000000 -0600
@@ -313,6 +313,20 @@
 	"\t-d [user]    delete crontab for user\n" \
 	"\t-c dir       specify crontab directory"
 
+#ifdef CONFIG_FEATURE_SHA1_PASSWORDS
+  #define CRYPTPW_ALG_TYPES(a) a
+#else
+  #define CRYPTPW_ALG_TYPES(a)
+#endif
+#define cryptpw_trivial_usage \
+	"[OPTION] string"
+#define cryptpw_full_usage \
+	"Outputs a crypted version of the string.\n" \
+	"Options:\n" \
+	"\t-a\tDefine which algorithm shall be used for the password.\n" \
+	"\t\t\t(Choices: des, md5" \
+	CRYPTPW_ALG_TYPES(", sha1") \
+	". Default is md5.)"
 
 #define cut_trivial_usage \
 	"[OPTION]... [FILE]..."
diff -urN busybox/libbb/Makefile.in busybox-cryptpw/libbb/Makefile.in
--- busybox/libbb/Makefile.in	2006-06-08 01:20:41.000000000 -0600
+++ busybox-cryptpw/libbb/Makefile.in	2006-06-09 03:30:33.000000000 -0600
@@ -35,13 +35,14 @@
 	getopt_ulflags.c default_error_retval.c wfopen_input.c speed_table.c \
 	perror_nomsg_and_die.c perror_nomsg.c skip_whitespace.c bb_askpass.c \
 	warn_ignoring_args.c concat_subpath_file.c vfork_daemon_rexec.c \
-	bb_do_delay.c
+	bb_do_delay.c crypt_make_salt.c get_algo.c
 
 # conditionally compiled objects:
 LIBBB-$(CONFIG_FEATURE_SHADOWPASSWDS)+=pwd2spwd.c
 LIBBB-$(CONFIG_FEATURE_MOUNT_LOOP)+= loop.c
 LIBBB-$(CONFIG_LOSETUP)+= loop.c
 LIBBB-$(CONFIG_FEATURE_MTAB_SUPPORT)+= mtab.c
+LIBBB-$(CONFIG_CRYPTPW)+= pw_encrypt.c
 LIBBB-$(CONFIG_PASSWD)+= pw_encrypt.c
 LIBBB-$(CONFIG_SULOGIN)+= pw_encrypt.c
 LIBBB-$(CONFIG_FEATURE_HTTPD_AUTH_MD5)+= pw_encrypt.c
diff -urN busybox/libbb/crypt_make_salt.c busybox-cryptpw/libbb/crypt_make_salt.c
--- busybox/libbb/crypt_make_salt.c	1969-12-31 17:00:00.000000000 -0700
+++ busybox-cryptpw/libbb/crypt_make_salt.c	2006-06-08 14:16:20.000000000 -0600
@@ -0,0 +1,43 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * crypt_make_salt 
+ *
+ * i64c was also put here, this is the only function that uses it.
+ *
+ * Lifted from loginutils/passwd.c by Thomas Lundquist <thomasez at zelow.no>
+ *
+ */
+
+#include <unistd.h>
+#include <time.h>
+
+#include <libbb.h>
+
+static int i64c(int i)
+{
+	if (i <= 0)
+		return ('.');
+	if (i == 1)
+		return ('/');
+	if (i >= 2 && i < 12)
+		return ('0' - 2 + i);
+	if (i >= 12 && i < 38)
+		return ('A' - 12 + i);
+	if (i >= 38 && i < 63)
+		return ('a' - 38 + i);
+	return ('z');
+}
+
+extern char *crypt_make_salt(void)
+{
+	time_t now;
+	static unsigned long x;
+	static char result[3];
+
+	time(&now);
+	x += now + getpid() + clock();
+	result[0] = i64c(((x >> 18) ^ (x >> 6)) & 077);
+	result[1] = i64c(((x >> 12) ^ x) & 077);
+	result[2] = '\0';
+	return result;
+}
diff -urN busybox/libbb/get_algo.c busybox-cryptpw/libbb/get_algo.c
--- busybox/libbb/get_algo.c	1969-12-31 17:00:00.000000000 -0700
+++ busybox-cryptpw/libbb/get_algo.c	2006-06-08 14:16:20.000000000 -0600
@@ -0,0 +1,20 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * get_algo 
+ *
+ * Lifted from loginutils/passwd.c
+ *
+ */
+
+#include <string.h>
+
+#include <libbb.h>
+
+extern int get_algo(char *a)
+{
+	int x = 1;					/* standard: MD5 */
+
+	if (strcasecmp(a, "des") == 0)
+		x = 0;
+	return x;
+}
Files busybox/loginutils/.cryptpw.c.swp and busybox-cryptpw/loginutils/.cryptpw.c.swp differ
Files busybox/loginutils/.passwd.c.swp and busybox-cryptpw/loginutils/.passwd.c.swp differ
diff -urN busybox/loginutils/Config.in busybox-cryptpw/loginutils/Config.in
--- busybox/loginutils/Config.in	2006-06-08 01:20:48.000000000 -0600
+++ busybox-cryptpw/loginutils/Config.in	2006-06-08 14:16:20.000000000 -0600
@@ -131,6 +131,12 @@
 	  Note that Busybox binary must be setuid root for this applet to
 	  work properly.
 
+config CONFIG_CRYPTPW
+	bool "cryptpw"
+	default n
+	help
+	  Utility for crypting a string.
+
 config CONFIG_SU
 	bool "su"
 	default n
diff -urN busybox/loginutils/Makefile.in busybox-cryptpw/loginutils/Makefile.in
--- busybox/loginutils/Makefile.in	2006-06-08 01:20:48.000000000 -0600
+++ busybox-cryptpw/loginutils/Makefile.in	2006-06-09 03:30:06.000000000 -0600
@@ -13,6 +13,7 @@
 LOGINUTILS-y:=
 LOGINUTILS-$(CONFIG_ADDGROUP)	+= addgroup.o
 LOGINUTILS-$(CONFIG_ADDUSER)	+= adduser.o
+LOGINUTILS-$(CONFIG_CRYPTPW)	+= cryptpw.o
 LOGINUTILS-$(CONFIG_GETTY)	+= getty.o
 LOGINUTILS-$(CONFIG_LOGIN)	+= login.o
 LOGINUTILS-$(CONFIG_PASSWD)	+= passwd.o
@@ -36,6 +37,7 @@
 needcrypt-y:=
 needcrypt-$(CONFIG_LOGIN)	:= y
 needcrypt-$(CONFIG_PASSWD)	:= y
+needcrypt-$(CONFIG_CRYPTPW)	:= y
 needcrypt-$(CONFIG_SU)		:= y
 needcrypt-$(CONFIG_SULOGIN)	:= y
 needcrypt-$(CONFIG_VLOCK)	:= y
diff -urN busybox/loginutils/cryptpw.c busybox-cryptpw/loginutils/cryptpw.c
--- busybox/loginutils/cryptpw.c	1969-12-31 17:00:00.000000000 -0700
+++ busybox-cryptpw/loginutils/cryptpw.c	2006-06-08 14:51:06.000000000 -0600
@@ -0,0 +1,73 @@
+/* vi: set sw=4 ts=4: */
+
+/*
+ * cryptpw.c
+ * 
+ * Cooked from passwd.c by Thomas Lundquist <thomasez at zelow.no>
+ * 
+ */
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "busybox.h"
+
+
+extern int cryptpw_main(int argc, char **argv)
+{
+	char *clear;
+	char *crypted;
+	char salt[12]; /* "$N$XXXXXXXX" or "XX" */
+
+	int flag;
+	int algo = 1;				/* -a - password algorithm */
+
+	while ((flag = getopt(argc, argv, "a:")) != EOF) {
+		switch (flag) {
+		case 'a':
+			algo = get_algo(optarg);
+			break;
+		default:
+			bb_show_usage();
+		}
+	}
+
+	if (optind < argc) {
+		clear = argv[optind];
+	} else {
+		bb_show_usage();
+	}
+
+	/*
+	
+	These two used to be in obscure.c but have now been removed.
+
+	if (palindrome(clear) || simple(clear)) {
+		fprintf(stderr, "Warning: weak password (continuing).\n");
+	}
+
+	The function I need to use is:
+
+	if (obscure(orig, pass, pw)) {
+
+    but I do not have neither orig nor pw so I may have to hack obscure
+	somehow or provide it. It also prints errors to STDOUT and that's 
+	not something I can use, even if it's true that the password are weak.
+	
+	*/
+
+	if (algo == 1) {
+		strcpy(salt, "$1$");
+		strcat(salt, crypt_make_salt());
+		strcat(salt, crypt_make_salt());
+		strcat(salt, crypt_make_salt());
+	}
+
+	strcat(salt, crypt_make_salt());
+	crypted = pw_encrypt(clear, salt);
+
+	printf("%s\n", crypted);
+
+	return (0);
+
+}
diff -urN busybox/loginutils/passwd.c busybox-cryptpw/loginutils/passwd.c
--- busybox/loginutils/passwd.c	2006-06-08 01:20:48.000000000 -0600
+++ busybox-cryptpw/loginutils/passwd.c	2006-06-08 14:16:20.000000000 -0600
@@ -21,16 +21,6 @@
 static void set_filesize_limit(int blocks);
 
 
-static int get_algo(char *a)
-{
-	int x = 1;					/* standard: MD5 */
-
-	if (strcasecmp(a, "des") == 0)
-		x = 0;
-	return x;
-}
-
-
 static int update_passwd(const struct passwd *pw, const char *crypt_pw)
 {
 	char filename[1024];
@@ -287,35 +277,6 @@
 	return 0;
 }
 
-static int i64c(int i)
-{
-	if (i <= 0)
-		return ('.');
-	if (i == 1)
-		return ('/');
-	if (i >= 2 && i < 12)
-		return ('0' - 2 + i);
-	if (i >= 12 && i < 38)
-		return ('A' - 12 + i);
-	if (i >= 38 && i < 63)
-		return ('a' - 38 + i);
-	return ('z');
-}
-
-static char *crypt_make_salt(void)
-{
-	time_t now;
-	static unsigned long x;
-	static char result[3];
-
-	time(&now);
-	x += now + getpid() + clock();
-	result[0] = i64c(((x >> 18) ^ (x >> 6)) & 077);
-	result[1] = i64c(((x >> 12) ^ x) & 077);
-	result[2] = '\0';
-	return result;
-}
-
 
 static int new_password(const struct passwd *pw, int amroot, int algo)
 {


More information about the busybox mailing list