Skip to content

Commit

Permalink
e-smith-util: remove unused password function
Browse files Browse the repository at this point in the history
- Remove also perl-Crypt-Cracklib dependency
  • Loading branch information
gsanchietti committed May 16, 2016
1 parent 67fdde2 commit 609fc8b
Showing 1 changed file with 0 additions and 293 deletions.
293 changes: 0 additions & 293 deletions lib/perl/esmith/util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -530,299 +530,6 @@ sub backgroundCommand ($@)

=pod
=head1 PASSWORD UTILITIES
Low-level password-changing utilities. These utilities each
change passwords for a single underlying password database,
for example /etc/passwd, /etc/samba/smbpasswd, etc.
=head2 validatePassword($password, $strength)
Validate Unix password.
=cut

sub validatePassword($$)
{
my ( $password, $strength ) = @_;
use Crypt::Cracklib;

$strength ||= 'normal';

my $reason = 'ok';
$reason = 'it is too short' unless (length($password) > 6);
return $reason if ($reason ne 'ok' || $strength eq 'none');

$reason = 'it does not contain numbers' if (not $password =~ /\d/);
$reason = 'it does not contain uppercase characters' if (not $password =~ /[A-Z]/);
$reason = 'it does not contain lowercase characters' if (not $password =~ /[a-z]/);
$reason = 'it does not contain special characters' if (not $password =~ /\W|_/);
return $reason if ($reason ne 'ok' && $strength eq 'strong');

if ( -f '/usr/lib64/cracklib_dict.pwd' ) {
$reason = fascist_check($password, '/usr/lib64/cracklib_dict');
} else {
$reason = fascist_check($password, '/usr/lib/cracklib_dict');
}
$reason ||= 'the password check failed';

return 'ok' if (lc($reason) eq 'ok');
return $reason;
}

=pod
=head2 setUnixPassword($username, $password)
Set Unix password
=cut

sub setUnixPassword($$)
{
my ( $username, $password ) = @_;
setUnixPasswordRequirePrevious( $username, undef, $password );
}

=pod
=head2 authenticateUnixPassword ($username, $password)
Check if the given username/password pair is correct.
Return 1 if they are correct, return 0 otherwise.
=cut

sub authenticateUnixPassword ($$)
{
my ( $username, $password ) = @_;

my $pam_auth_func = sub {
return ( PAM_SUCCESS(), $password, PAM_SUCCESS() );
};
my $pamh = new Authen::PAM( 'passwd', $username, $pam_auth_func );

unless ( ref($pamh) )
{
warn "WARN: Couldn't open Authen::PAM handle for user $username";
return 0;
}
my $res = $pamh->pam_authenticate();
return ( $res == PAM_SUCCESS() ) || 0;
}

=pod
=head2 setUnixPasswordRequirePrevious($username, $oldpassword, $newpassword)
Set Unix password but require previous password for authentication.
=cut

# setUnixPasswordRequirePrevious is left as an exercise for the reader :-)
sub setUnixPasswordRequirePrevious ($$$)
{
my ( $username, $oldpassword, $newpassword ) = @_;
use Authen::PAM;
my $state;

my $my_conv_func = sub {
my @res;
while (@_)
{
my $code = shift;
my $msg = shift;
my $ans = "";

$ans = $username if ( $code == PAM_PROMPT_ECHO_ON() );
if ( $code == PAM_PROMPT_ECHO_OFF() )
{
if ( $< == 0 || $state >= 1 )
{
# are we asked for a new password
$ans = $newpassword;
}
else
{
# asked for old password before we can set a new one.
$ans = $oldpassword;
}
$state++;
}

#print("code is $code, ans is $ans, msg is $msg, state is $state\n");
push @res, ( PAM_SUCCESS(), $ans );
}
push @res, PAM_SUCCESS();
return @res;
};

my $pamh = new Authen::PAM( "passwd", $username, $my_conv_func );
unless ( ref($pamh) )
{
warn "Autopasswd: error code $pamh during PAM init!";
warn "Failed to set Unix password for account $username.\n";
return 0;
}

# Require the old password to be correct before proceeding to set a new
# one.
# This does that, except if you're already root, such as from the
# bootstrap-console
$state = 0;
unless ( $< == 0 or $pamh->pam_authenticate == 0 )
{
warn
"PAM authentication failed for user \"$username\", old password invalid!\n";
return 0;
}

$state = 0;
my $res = $pamh->pam_chauthtok;
unless ( $res == PAM_SUCCESS() )
{
my $err = $pamh->pam_strerror($res);
warn "Failed to set Unix password for account $username: $err\n";
return 0;
}
return 1; # success
}



=pod
=head2 genRandomHash()
Returns a random generated sha1 hash using urandom.
Returns undef if the hash could not be generated/retrieved.
DEPRECATED see NethServer::Password module
=cut

sub genRandomHash
{
return NethServer::Password->new(undef, {'symbols' => ['a'..'f', '0'..'9'], 'length' => 40})->getAscii();
}

=pod
=head2 genRandomPassword($store_file="")
Returns the a random generated password using urandom.
If $store_file is not empty, try to read the password from the file.
If $store_file not exists, generate a new random password and save it on the file.
Returns undef if the password could not be generated/retrieved.
DEPRECATED see NethServer::Password module
=cut

sub genRandomPassword
{
my $store_file = shift;
return NethServer::Password::store($store_file);
}

=pod
=head1 HIGH LEVEL PASSWORD UTILITIES
High-level password-changing utilities. These utilities
each change passwords for a single e-smith entity (system,
user or ibay). Each one works by calling the appropriate
low-level password changing utilities.
=head2 setUnixSystemPassword($password)
Set the root's password
=cut

sub setUnixSystemPassword ($)
{
my ($password) = @_;

setUnixPassword( "root", $password );
}

=pod
=head2 setUserPassword($username, $password)
Set e-smith user password
=cut

sub setUserPassword ($$)
{
my ( $username, $password ) = @_;

setUnixPassword( $username, $password );
}

=pod
=head2 setUserPasswordRequirePrevious($username, $oldpassword, $newpassword)
Set e-smith user password - require previous password
=cut

sub setUserPasswordRequirePrevious ($$$)
{
my ( $username, $oldpassword, $newpassword ) = @_;

# We need to suid to the user, instead of root, so that PAM will
# prompt us for the old password.
my @pwent = getpwnam($username);
return 0 unless ( $pwent[2] > 0 ); # uid must be non-zero
my $uid = $<;
$< = $pwent[2];

# Return if this function call fails, we didn't change passwords
# successfully.
my $ret =
setUnixPasswordRequirePrevious( $username, $oldpassword, $newpassword );
$< = $uid;
return 0 unless $ret;
}

=pod
=head2 cancelUserPassword
Cancel user password. This is called when a user is deleted from the
system. We assume that the Unix "useradd/userdel" programs are
called separately. Since "userdel" automatically removes the
/etc/passwd entry, we only need to worry about the /etc/samba/smbpasswd
entry.
=cut

sub cancelUserPassword ($)
{
my ($username) = @_;
}

=pod
=head2 setIbayPassword($ibayname, $password)
Set ibay password
=cut

sub setIbayPassword ($$)
{
my ( $ibayname, $password ) = @_;

setUnixPassword( $ibayname, $password );
}

=pod
=head1 SERVICE MANAGEMENT UTILITIES
=head2 serviceControl()
Expand Down

0 comments on commit 609fc8b

Please sign in to comment.