Sponsored Content
Top Forums Shell Programming and Scripting Perl script - changing passwords Post 20740 by thehoghunter on Thursday 2nd of May 2002 05:53:32 PM
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

Code:
#!/u/bin/perl
# 
#  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` }
        &create_newfile;
        `/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
thehoghunter
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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 auth.info) and then changes a password. Do I need to put an... (1 Reply)
Discussion started by: AW12
1 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. 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

7. 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

8. 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

9. 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
d_passwd(4)							   File Formats 						       d_passwd(4)

NAME
d_passwd - dial-up password file SYNOPSIS
/etc/d_passwd DESCRIPTION
A dial-up password is an additional password required of users who access the computer through a modem or dial-up port. The correct pass- word must be entered before the user is granted access to the computer. d_passwd is an ASCII file which contains a list of executable programs (typically shells) that require a dial-up password and the associ- ated encrypted passwords. When a user attempts to log in on any of the ports listed in the dialups file (see dialups(4)), the login program looks at the user's login entry stored in the passwd file (see passwd(4)), and compares the login shell field to the entries in d_passwd. These entries determine whether the user will be required to supply a dial-up password. Each entry in d_passwd is a single line of the form: login-shell:password: where login-shell The name of the login program that will require an additional dial-up password. password An encrypted password. Users accessing the computer through a dial-up port or modem using login-shell will be required to enter this password before gaining access to the computer. d_passwd should be owned by the root user and the root group. The file should have read and write permissions for the owner (root) only. If the user's login program in the passwd file is not found in d_passwd or if the login shell field in passwd is empty, the user must sup- ply the default password. The default password is the entry for /usr/bin/sh. If d_passwd has no entry for /usr/bin/sh, then those users whose login shell field in passwd is empty or does not match any entry in d_passwd will not be prompted for a dial-up password. Dial-up logins are disabled if d_passwd has only the following entry: /usr/bin/sh:*: EXAMPLES
Example 1: Sample d_passwd file. Here is a sample d_passwd file: /usr/lib/uucp/uucico:q.mJzTnu8icF0: /usr/bin/csh:6k/7KCFRPNVXg: /usr/bin/ksh:9df/FDf.4jkRt: /usr/bin/sh:41FuGVzGcDJlw: Generating An Encrypted Password The passwd (see passwd(1)) utility can be used to generate the encrypted password for each login program. passwd generates encrypted pass- words for users and places the password in the shadow (see shadow(4)) file. Passwords for the d_passwd file will need to be generated by first adding a temporary user id using useradd (see useradd(1M)), and then using passwd(1) to generate the desired password in the shadow file. Once the encrypted version of the password has been created, it can be copied to the d_passwd file. For example: 1. Type useradd tempuser and press Return. This creates a user named tempuser. 2. Type passwd tempuser and press Return. This creates an encrypted password for tempuser and places it in the shadow file. 3. Find the entry for tempuser in the shadow file and copy the encrypted password to the desired entry in the d_passwd file. 4. Type userdel tempuser and press Return to delete tempuser. These steps must be executed as the root user. FILES
/etc/d_passwd dial-up password file /etc/dialups list of dial-up ports requiring dial-up passwords /etc/passwd password file /etc/shadow shadow password file SEE ALSO
passwd(1), useradd(1M), dialups(4), passwd(4), shadow(4) WARNINGS
When creating a new dial-up password, be sure to remain logged in on at least one terminal while testing the new password. This ensures that there is an available terminal from which you can correct any mistakes that were made when the new password was added. SunOS 5.10 2 Sep 2004 d_passwd(4)
All times are GMT -4. The time now is 04:44 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy