Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

passwd for NT/2K

by idnopheq (Chaplain)
on Apr 23, 2001 at 14:03 UTC ( #74670=sourcecode: print w/ replies, xml ) Need Help??

Category: NT Admin
Author/Contact Info idnopheq
Description: passwd -- change login password.

passwd changes the password or lists password attributes associated with the user's login name (userid) <- Not yet implemented.

When used to change a password, passwd prompts everyone for their old password, if any. It then prompts for the new password twice.

If executed by a user with USER_PRIV_ADMIN() privilege flag (i.e. super-user), the old password prompt does not appear.

Passwords must be constructed to meet the operating system requirements

TODO: Add password attribute listing

#!/usr/local/bin/perl -w
#-*-perl-*-
#

use strict;
use vars qw ( %UserInfo %CallerUserInfo $OPasswd );
use Win32;
use Win32::NetAdmin;
use Term::ReadKey;
use Win32API::Net;
use File::Spec;

my ($VERSION) = '$Revision: 1.0 $' =~ /([.\d]+)/;

my $warnings = 0;

# Print a usuage message on a unknown option.

$SIG {__WARN__} = sub {
    if (substr ($_ [0], 0, 14) eq "Unknown option") {die "Usage"};
    require File::Basename;
    $0 = File::Basename::basename ($0);
    $warnings = 1;
    warn "$0: @_";
};

$SIG {__DIE__} = sub {
    require File::Basename;
    $0 = File::Basename::basename ($0);
    if (substr ($_ [0], 0,  5) eq "Usage") {
        die <<EOF;
$0 (NT Perl bin utils) $VERSION
$0 [ userid ] [-h]
EOF
    }
    die "$0: @_";
};

die "Usage" 
  if ( exists $ARGV[0] && $ARGV[0] eq "-h" );

my $Level           = "1003";
my $UserName        = $ARGV[0] || getlogin;
my $Server          = "";
my $Error           = -1;
my $CallerPriv      = GetCallerPriv() || "0";
my $Executable      = ( File::Spec->splitpath( $0 ) )[2];

CheckUser();

print "Changing password for $UserName\n";

( 
 $OPasswd,
 $UserInfo{password}
)                   = PasswordPrompt();

if ( $CallerPriv < "2" ) {
    Win32API::Net::UserChangePassword (
                       $Server,
                       $UserName,
                       $OPasswd,
                       $UserInfo{password}
                      ) 
    or die "\n$^E\n"; 
}
if ( $CallerPriv == "2" ) {
    Win32API::Net::UserSetInfo ( 
                $Server,
                $UserName,
                $Level,
                \%UserInfo,
                $Error
                   ) 
    or die "\n$^E\n"; 
}

print "\n$Executable: all authentication tokens updated successfully\n
+";

sub GetCallerPriv {
    my $Caller = getlogin;
    Win32API::Net::UserGetInfo (
                $Server,
                $Caller,
                "11",
                \%CallerUserInfo 
                   );

    return $CallerUserInfo{priv};
}

sub PasswordPrompt {
    my (
       $OldPassword,
       $NewPassword1
       );

    unless ( $CallerPriv == 2 ) {
    print "(current) login password: ";
    ReadMode 'noecho';
    $OldPassword = ReadLine 0;
    chomp $OldPassword;
    print "\n";
    }
    for ( my $Accumulator = 0; $Accumulator < 3;  $Accumulator++ ) {
    print "New login password: ";
    ReadMode 'noecho';
    my $NewPassword0 = ReadLine 0;
    chomp $NewPassword0;
    print "\nRetype new login password: ";
    ReadMode 'noecho';
    $NewPassword1 = ReadLine 0;
    chomp $NewPassword1;
    
    last if $NewPassword0 eq $NewPassword1;
    print "\npasswords do not match\n";
    if ( $Accumulator >= "3" ) {
        print "exiting ...\n";
        die;
    }
    }
    return $OldPassword, $NewPassword1;
}

sub CheckUser {
    die "$Executable: unknown username \'$UserName\'\n" 
      if ! Win32::NetAdmin::UsersExist (
                    $Server,
                    $UserName
                       );
}

__END__

=pod

=head1 NAME

B<passwd> -- change login password.

=head1 SYNOPSIS

B<passwd> [ I<userid> ] [ -h ]

=head1 DESCRIPTION

B<passwd> changes the password or lists password attributes associated
+ with the user's login name (userid).

When used to change a password, B<passwd> prompts everyone for their o
+ld password, if any. It then prompts for the new password twice.

If executed by a user with USER_PRIV_ADMIN() privilege flag (i.e. supe
+r-user), the old password prompt does not appear.

Passwords must be constructed to meet the operating system requirement
+s

=head2 OPTIONS

B<passwd> accepts the options described below.

=over 4

=item B<userid>

The userid to perform a password reset.  If null, the current user's p
+assword will be reset.

=item B<-h>

Display syntax.

=back

=head1 ENVIRONMENT

The working of B<passwd> is not influenced by any environment variable
+s.

=head1 BUGS

B<passwd> does not work will in a Samba domain controller environment 
+(so far).

=head1 STANDARDS

It does not make sense to talk about standards in a B<passwd> manual p
+age.

=head1 REVISION HISTORY

    passwd
    Revision 1.0  2000/06/22 09:00:34  idnopheq
    Initial revision

=head1 AUTHOR

The Perl implementation of B<passwd> was written by Dexter Coffin, I<i
+dnopheq@home.com>.

=head1 COPYRIGHT and LICENSE

This program is copyright by Dexter Coffin 2000.

This program is free and open software. You may use, copy, modify, dis
+tribute,
and sell this program (and any modified variants) in any way you wish,
provided you do not restrict others from doing the same.

=head1 SEE ALSO

=head1 NEXT TOPIC

=cut

Comment on passwd for NT/2K
Download Code

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://74670]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2015-07-04 23:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls