Perl script - changing passwords

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script - changing passwords
# 1  
Old 05-02-2002
Perl script - changing passwords

Just wanted options of this - first 'real' Perl script and I'm not positive of all the quirks in Perl. Any suggestions are welcome.
Especially since I'm messing with /etc/shadow! Running Solaris 2.6, Perl 5.005.03

#  Change the user's old password to the new in /etc/shadow 
#  Expects userID, old password, new password
#  Will only change ftp users passwords - 
#       HOG 04/25/02 It's Alive!
# ====================================================================
# Set up variables ------------
$user = "$ARGV[0]";
$oldpass = "$ARGV[1]";
$newpass = "$ARGV[2]";
$oldshadow = "/etc/shadow";
$newshadow = "/etc/.newshadow";
# ==== Check that there is only one of user =====
$useramount = `/usr/bin/grep -c $user /etc/shadow`;
if ($useramount != 1) { die "More or less than one"; }
$userinfo = `/usr/bin/grep $user /etc/shadow`;
($user1, $passwd, $epoch, $passextra) = split(/:/, $userinfo, 4);
$salt = substr($passwd,0,2);
$newsalt = join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64];
# Put testing junk here (print variables)
    if (crypt($oldpass, $salt) ne $passwd) {
        # =========== FAILED - write to messages file - return error =========
        system("/usr/bin/echo \" progserver chgpwd: ERROR changing $user passwor
d\" >> /var/adm/messages");
        die "";
    } else {
        $newcrypt = crypt($newpass, $newsalt);
        $nowepoch = (time () /60 /60 /24 ) + 35;
        ($newepoch, $junk) = split(/\./, $nowepoch, 2);
        chomp($userinfo1 = "$user1:$newcrypt:$newepoch:$passextra");
        # Make a backup copy if none exists - done nightly in another script
        if (!-e "/etc/.oldshadow") { `/usr/bin/cp /etc/shadow /etc/.oldshadow` }
        `/usr/bin/cp $newshadow $oldshadow`;
sub create_newfile {
        open NEWSHAD, ">$newshadow" or die "Can not open new shadow\n";
        open OLDSHAD, "<$oldshadow" or die "Can not open old shadow\n";
        while (<OLDSHAD>) {
                ($usertmp, $everythingelse) = split(/:/, $_, 2);
                if ("$usertmp" eq "$user") {
                        print NEWSHAD "$userinfo1\n";
                } else {
                        print NEWSHAD "$_";
                close OLDSHAD;
                close NEWSHAD;

Last edited by Yogesh Sawant; 08-11-2010 at 01:45 AM.. Reason: added code tags
# 2  
Old 05-02-2002
Is not necesary to do the job with perl, there are a post to change password with expect.

Script to change UNIX password (In shell programming and scripting).

Regards. Hugo.
# 3  
Old 05-02-2002
I think that you are writing this for SunOS. I don't know SunOS super well, but I think you missed an issue.

With the versions of UNIX that I do know, programs like passwd use some kind of locking scheme. This prevents two or more users from stepping on each other if they to change their password at the same time.

With early versions of Unix, the trick was to invoke the link system call to link /etc/passwd to /etc/ptmp. The link system call is atomic. And it will fail, even for root, if the target exists. If the link was successful, the program could proceed. If not, it would back off. Sysadmins would get a call from a user sometimes about the password file being locked. The trick was then to remove /etc/ptmp. HP-UX was still using this method a few years ago, but I haven't checked since HP-UX 9.0.

Beats me what Sun is using. I just ran the passwd command on a Sun. A "lt -ltc /etc | head" showed that oshadow had popped into existence. It is listed without comment on Sun's man page for passwd.

If you going to run the script in single user mode you can ignore this issue. Otherwise, you might want to do some research....
# 4  
Old 05-03-2002

I don't have expect loaded on any system but since Perl was there...thanks thou.


/etc/oshadow is a link to /etc/shadow on Solaris. If you do a checksum against both you will see it.
The password lock is done with /etc/.pwd.lock - thanks for the tip, I'll look into what I need to change.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

perl :Changing script to only find the group

Hi scripting guru's I found this script on IBM's website and it seems to be really good only thing it gives off more info than i need. I was wondering if someone could help me modify it to only find a group instead of every user. (group is support) I believe i know how to add the line so it... (2 Replies)
Discussion started by: vpundit
2 Replies

2. UNIX for Advanced & Expert Users

When did UNIX start using encrypted passwords, and not displaying passwords when you type them in?

I've been using various versions of UNIX and Linux since 1993, and I've never run across one that showed your password as you type it in when you log in, or one that stored passwords in plain text rather than encrypted. I'm writing a script for work for a security audit, and two of the... (5 Replies)
Discussion started by: Anne Neville
5 Replies

3. Solaris

Changing Passwords with a script.

We are real strict when it comes to passwords. Every 60 days the admins have to change passwords on all of the accounts. And there is pretty strict enforcement of the type of passwords chosen. This is a tedious and monotonous job. Ww don't use NIS or LDAP, so this has to be done on each machine. ... (5 Replies)
Discussion started by: brownwrap
5 Replies

4. Shell Programming and Scripting

script for changing passwords

Hello, We are running aix 5.3. We're looking for a script that can change passwords, taking 2 arguments ( old password, new password ). I am wondering if this can be done with a here document, or some generic scripting method. Or, if I would have to download expect. Alternatively I wonder... (3 Replies)
Discussion started by: fwellers
3 Replies

5. Shell Programming and Scripting

changing from command line to perl script

I had posted previously about this problem I had. I have multiple text files with hundreds of lines of the following type: 2000001 34 54 234 2000001 32 545 2000001 -2000001 77 2000001 44 2000001 998 2000001 77 32 2000001 45 23 111 89 98 75 23 34 999 . . . etc... What I wanted was... (2 Replies)
Discussion started by: xchen89x
2 Replies

6. UNIX for Advanced & Expert Users

Monitoring the changing of passwords

What is the best way to monitor who changes passwords, or what passwords get changed? Is there a way to send that over to Syslog? An example would be someone logs in as themselves, changes to root (which I capture by loging auth and and then changes a password. Do I need to put an... (1 Reply)
Discussion started by: AW12
1 Replies

7. Shell Programming and Scripting

changing passwords remotely on sun boxes

now, for reasons i really cant begin to delve into, i have to find a way to be able to rmeotely create user accounts and also assign them passwords. unfortunately, it appears Sun boxes frowns upon this. sun boxes will let u create a user account remotely but will never let u assign the useraccount... (0 Replies)
Discussion started by: Terrible
0 Replies

8. UNIX for Advanced & Expert Users

Changing Users Passwords Via Script?

I am the administrator for a large network of HP/UX servers, about 100, this will be growing to over 200 in the next 18 months, part of my duties are to change the root passwords on these machines once month... which is a pain. I have written a script that will generate random passwords for me and... (3 Replies)
Discussion started by: PJolliffe
3 Replies

9. UNIX for Dummies Questions & Answers

passwords changing

Hello everyone let me start off by saying happy new year to all I am new to this board. I am running a multipurpose server (web/ftp/email) it runs apache 1.3.20 i think it is and Qmail would I would like to do is find/create a script that will allow my users to change there unix password... (1 Reply)
Discussion started by: viperws
1 Replies
Login or Register to Ask a Question