LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-13-2008, 11:02 AM   #1
RaelOM
Member
 
Registered: Dec 2004
Posts: 110

Rep: Reputation: 16
Script to change password in initially set as *LK*


So this script fails because of the *LK* entry in /etc/shadow (Or in this case /etc/ptmp)

If it's any other value, then this works fine.

How can I correct this?

Code:
#!/usr/bin/perl

@saltset = ('a' .. 'z', 'A' .. 'Z', '0' .. '9', '.', '/');
$ptmp = '/etc/ptmp';
$passwd = '/etc/shadow';
$perl = '/usr/bin/perl' ; # Used in system() later

srand(time|$$);

if ($#ARGV != 1) {
    print "Usage: $0 username newpassword\n" ;
    exit 1;
}

$user = shift;
$newpass = shift;

open(PASSWD,'<' . $passwd);
@entry = grep(/^$user:/,<PASSWD>);
close PASSWD;
if ($#entry == -1) {
    print "User \"$user\" does not exist!\n";
    exit 1;
}
if ($#entry != 0) {
    $accts = $#entry + 1;
    print "There are $accts accounts for \"$user\". Password not changed.";
    exit 1;
}
$entry = shift(@entry);
($name, $oldcrypt, @rest) = split(/:/, $entry);

if ( -f $ptmp ) {
    print "The password file is busy, $ptmp exists. Try again later.\n";
    exit 1;
}

$salt = $saltset[rand(64)] . $saltset[rand(64)]; # form new salt
$newcrypt = crypt($newpass,$salt); # form new encrypted password

open(PW,'<' . $passwd) || die "Couldn't read password file\n";
open(PTMP,'>' . $ptmp) || die "Coulnd't make temp password file.\n";
print PTMP <PW>; # copy passwd to ptmp
close(PTMP);

# Here is where we actually change the password. We let perl do the dirty work.
$ok = system("$perl -pi.bak -e 's:$oldcrypt:$newcrypt: if m/^$user:/;' $ptmp");
if ($ok / 256 ) {
    print "Change failed! Couldn't update the password in $ptmp.\n";
    unlink($ptmp);
    exit 1;
}

# Here we update the real password file by renaming the ptmp file to passwd.
unless (rename($ptmp,$passwd)) { # make new password file from ptmp
    print "Change failed! Couldn't update $passwd. Backup file is $ptmp.bak\n";
    unlink($ptmp);
    exit 1
}

unlink($ptmp); # remove ptmp file
exit 0; # return success.
 
Old 08-13-2008, 12:10 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You should not edit the passwd file manually. Use usermod with the --password option. (you will need to pre-hash the password).
 
Old 08-13-2008, 12:13 PM   #3
RaelOM
Member
 
Registered: Dec 2004
Posts: 110

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by matthewg42 View Post
You should not edit the passwd file manually. Use usermod with the --password option. (you will need to pre-hash the password).
That's based on the premise that usermod on Solaris would have such an option, which it doesn't.

I would also just use something like this for Linux although the usermod function would work just as well.

echo $PASSWORD | passwd --stdin $user
 
Old 08-13-2008, 12:26 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
If you don't mind keeping the password in cleartext, you could use expect.
 
Old 08-13-2008, 12:33 PM   #5
RaelOM
Member
 
Registered: Dec 2004
Posts: 110

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by matthewg42 View Post
If you don't mind keeping the password in cleartext, you could use expect.

Yea, was looking into that route as well and decided against it since it seems expect doesn't exist on every box and rather than pulling a pre-compiled binary around with me, I do know perl is on each host.

I actually figured this out BTW:

This string:
$ok = system("$perl -pi.bak -e 's:$oldcrypt:$newcrypt: if m/^$user:/;' $ptmp");

Should read:
$ok = system("$perl -pi.bak -e 's:\Q$oldcrypt\E:$newcrypt: if m/^$user:/;' $ptmp");

so as to escape the boolean's
 
Old 08-13-2008, 03:25 PM   #6
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
perl module - Passwd::Solaris
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Set up Red Hat Linux Fedora Core 4 fine, initially but. . . Henry777 Linux - Newbie 5 07-08-2008 02:47 PM
change Root Password even if the password in the grub is also set sheelnidhi Linux - General 6 08-30-2006 07:27 AM
Change Colors and set password on Lilo thenowherekid Linux - Newbie 3 08-26-2005 02:22 AM
Where does $PATH initially&subsequently get set? drobson Linux - General 1 07-01-2001 06:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration