如同这个问题 https://stackoverflow.com/questions/140439/authenticating-against-active-directory-using-python-ldap,我尝试使用 python ldap (CentOS 6.2 x86_64、Python 2.6.6、python-ldap 2.3.10)对 2003 Active Directory 执行简单身份验证。
尽管遵循了 init 中的所有常规步骤,包括
conn.set_option(ldap.OPT_REFERRALS, 0)
如果我通过了正确的凭据我always get a (97, [])
回到:
>>> import ldap
>>> conn = ldap.initialize('ldap://ad.server.domain.com')
>>> conn.protocol_version = 3
>>> conn.set_option(ldap.OPT_REFERRALS, 0)
>>> conn.simple_bind_s('[email protected] /cdn-cgi/l/email-protection', 'WrongPassword')
ldap.INVALID_CREDENTIALS: {'info': '80090308: LdapErr: DSID-0C090334, comment: AcceptSecurityContext error, data 52e, vece', 'desc': 'Invalid credentials'}
>>> conn.simple_bind_s('[email protected] /cdn-cgi/l/email-protection', 'CorrectPassword')
(97, [])
错误代码97表示未成功;这是LDAP_REFERRAL_LIMIT_EXCEEDED
从 AD 返回错误。我也不能用它作为de facto成功指标,因为:
>>> conn.simple_bind_s('', 'CorrectPassword')
(97, [])
>>> conn.simple_bind_s('', '')
(97, [])
更令人沮丧的是,该脚本是使用 Net::LDAP 从旧 Perl 脚本迁移而来的,它does如果成功通过身份验证绑定到同一 AD 和服务器,则返回 0。
我在 python-ldap 上找到的所有信息都表明我所做的应该只是工作;我倾向于认为 AD 服务器有问题,但 Perl 脚本确实在成功绑定时返回正确的 LDAP 代码。
我已经在我闲置的旧 CentOS 5.5 机器上测试了 python-ldap 2.2.0 和 python 2.4.4,但它以完全相同的方式“失败”。
有谁知道我缺少什么?
编辑:根据请求,这里是有效的 Perl 脚本。Net::LDAP
从 LDAP 服务器返回返回码,AD 服务器返回 0x00,“请求成功”,AFAICT。
#!/usr/bin/perl -w
use strict;
use Net::LDAP;
## Corporate subdomains
my @domains = ("americas", "asia", "europe");
# AD connect timeout
my $timeout = 10;
# Set AD server info.
my $port = "389";
my $host = "server.domain.com";
my $user = shift @ARGV;
chomp $user;
my $password = <STDIN>;
$password =~ s/\r\n//;
chomp $password;
my $ldap = Net::LDAP->new($host, port => $port, timeout => $timeout ) ||
die "Unable to connect to LDAP server";
my $bind_return = 1;
foreach (@domains) {
my $result = $ldap->bind( "$user\@$_.domain.com", password => $password );
if( $result->code == 0) {
$bind_return = 0;
last;
}
}
## Unbind and return
$ldap->unbind;
if ($bind_return) { print "Authentication Failed. Access Denied\n" }
exit $bind_return;