[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [BusyBox] [PATCH] Tinylogin: set password from command line
From:       "Torrey Hoffman" <Torrey.Hoffman () myrio ! com>
Date:       2002-03-20 12:13:03
[Download RAW message or body]

This small patch to tinylogin 1.0 adds the ability to set 
passwords from the command line.  It is improved from my 
original version against 0.8, as now I understand how 
tinylogin uses getopt(). :-)

usage:
passwd -p darkstar

This can be very useful for scripting.
The patch is set up to only allow root to use this feature, 
as there may be security implications in the general case.

Enjoy...

Torrey Hoffman

thoffman@arnor.net
torrey.hoffman@myrio.com


["tinylogin-1.00.passwd-p.diff" (application/octet-stream)]

diff -urN tinylogin-1.00/passwd.c tinylogin-1.00.torrey/passwd.c
--- tinylogin-1.00/passwd.c	Thu Jan  3 02:39:44 2002
+++ tinylogin-1.00.torrey/passwd.c	Wed Mar 20 10:39:35 2002
@@ -19,6 +19,7 @@
 
 static int create_backup(const char *backup, FILE * fp);
 static int new_password(const struct passwd *pw, int amroot, int algo);
+static int encrypt_password(char *pass, int algo);
 static void set_filesize_limit();
 
 
@@ -48,6 +49,8 @@
 	int lflg = 0;				/* -l - lock account */
 	int uflg = 0;				/* -u - unlock account */
 	int dflg = 0;				/* -d - delete password */
+	int pflg = 0;                           /* -p - set password from argument */
+	char pass[200];
 	const struct passwd *pw;
 	unsigned short ruid;
 
@@ -56,7 +59,7 @@
 #endif							/* TLG_FEATURE_SHADOWPASSWDS */
 	amroot = (getuid() == 0);
 	openlog("passwd", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
-	while ((flag = getopt(argc, argv, "a:dlu")) != EOF) {
+	while ((flag = getopt(argc, argv, "a:dlup:")) != EOF) {
 		switch (flag) {
 		case 'a':
 			algo = get_algo(optarg);
@@ -70,6 +73,10 @@
 		case 'u':
 			uflg++;
 			break;
+		case 'p':
+			pflg++;
+			STRFCPY(pass,optarg);
+			break;
 		default:
 			usage(passwd_usage);
 		}
@@ -109,7 +116,7 @@
 #endif							/* TLG_FEATURE_SHADOWPASSWDS */
 
 	STRFCPY(crypt_passwd, cp);
-	if (!(dflg || lflg || uflg)) {
+	if (!(dflg || lflg || uflg || pflg)) {
 		if (!amroot) {
 			if (cp[0] == '!') {
 				syslog(LOG_WARNING, "password locked for `%s'", np);
@@ -120,6 +127,11 @@
 		if (new_password(pw, amroot, algo)) {
 			error_msg_and_die( "The password for %s is unchanged.\n", name);
 		}
+	} else if (pflg) {
+		if (!amroot)
+			error_msg_and_die("Only root can set passwords through argument.\n");
+		printf("Setting password for %s\n", name);
+		encrypt_password(pass, algo);
 	} else if (lflg) {
 		if (crypt_passwd[0] != '!') {
 			memmove(&crypt_passwd[1], crypt_passwd,
@@ -144,7 +156,7 @@
 		syslog(LOG_ERR, "can't setuid(0)");
 		error_msg_and_die( "Cannot change ID to root.\n");
 	}
-	/* if (update_passwd(pw, crypt_passwd)) { */
+
 	if (!update_passwd(pw, crypt_passwd)) {
 		syslog(LOG_INFO, "password for `%s' changed by user `%s'", name,
 			   myname);
@@ -394,6 +406,13 @@
 	}
 	bzero(cp, strlen(cp));
 	bzero(orig, sizeof(orig));
+
+	return encrypt_password(pass, algo);
+}
+
+static int encrypt_password(char *pass, int algo)
+{
+	char *cp;
 
 #ifdef TLG_FEATURE_SHA1_PASSWORDS
 	if (algo == 2) {


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic