我写了一个绑定到LDAP服务器的小脚本,并检索所有用户和用户信息.现在我想写另一个绑定到LDAP服务器然后测试给定登录的那个.我怎样才能做到这一点?
my $ldap = ldapConnect();
my $user = 'user';
my $pwd = 'pwd';
# TEST USER AND PWD BUT HOW?
sub ldapConnect {
my $ldap = Net::LDAP->new('192.168.*.*');
my $password = '***';
$ldap->bind('cn=Administrator,cn=Users,DC=***,DC=***', password=> $password);
return $ldap;
}
Run Code Online (Sandbox Code Playgroud)
my $ldap = ldapConnect(); # Connect
my $search = $ldap->search( # Search for the user
base => 'DC=***,DC=***',
scope => 'sub',
filter => "(&(uid=$user))",
attrs => ['dn']
);
die "not found" if not $search->count;
# Get the user's dn and try to bind:
my $user_dn = $search->entry->dn;
$ldap->bind( $user_dn, password => $pass );
print +($ldap->error ? "Bad credentials" : "Success!"), "\n";
Run Code Online (Sandbox Code Playgroud)