SUMMARY:script to change password.

From: Haim Stotsky (stots@elbit.co.il)
Date: Sun Jul 28 1996 - 07:37:56 CDT


Hello
I got several good answers to my problem and i choose the one that fixed the
problem , i got it from judyh@nikko.com .
I'm sure that most of the other answers were also good as well but i didn't have the
time to check them,if time wasn't a problem ....i might change every password manually ;-)

thanks to everyone!

The Original question was :

>Hello,
>I need very urgent a script that will change password for a user on
>Solaris 2.4 and 2.5.
>the script will get the user name and the passwd as a parameter.
>i need it to add a lot of users to a server,
>So if someone have the solution i'll be glad to hear about.
>i tried this one and it does not worked. . .

>
>#!/bin/csh
>su $1 <<EOF
>passwd $2
>$2
>EOF
>
>
>Sincerely Yours
>Stots
>

The Answer from judyh@nikko.com :

#!/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.



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:11:06 CDT