使用 Net::LDAPS 的 Perl 中特定用户的组成员身份
Group membership of a specific user in Perl using Net::LDAPS
我一直在搜索各种站点以寻求这方面的帮助,但进展不大。我的目标是在登录成功后检查组成员身份。登录部分工作正常,但我想获取已登录用户组的列表,以将其与特定用户组进行比较。如果他们不是该组的成员,他们将被注销,会话被删除并返回到登录页面,否则他们可以继续。
代码如下:
my $base = "ou=Groups,ou=Services,dc=example,cd=com";
my $mesg = $ldap->search(
filter=>"(uid=$userid)",
base=>"$base",
scope=>'children',
);
my @entries = $mesg->entries;
print("mesg = <BR>");
print(Dumper($mesg));
这里是 $mesg 的输出:
$VAR1 = bless( {
'parent' => bless( {
'net_ldap_version' => 3,
'net_ldap_scheme' => 'ldaps',
'net_ldap_debug' => 0,
'net_ldap_socket' => bless( \*Symbol::GEN0, 'IO::Socket::SSL' ),
'net_ldap_host' => 'ldap.example.com',
'net_ldap_uri' => 'ldap.example.com',
'net_ldap_resp' => {},
'net_ldap_mesg' => {},
'net_ldap_async' => 0,
'net_ldap_port' => '636',
'net_ldap_refcnt' => 1
}, 'Net::LDAPS' ),
'errorMessage' => 'NDS error: no such entry (-601)',
'ctrl_hash' => undef,
'resultCode' => 32,
'callback' => undef,
'mesgid' => 2,
'matchedDN' => '',
'controls' => undef,
'raw' => undef
}, 'Net::LDAP::Search' );
我可以看到 NDS 消息说没有这样的条目,所以我假设我的搜索查询是错误所在,但错误正在逃避我。有什么建议么?我也试过没有 scope=>'children'
行,没有任何改变。
谢谢。
更新:
这就是我最终所做的,并且是成功的..不得不稍微调整另一个人的答案之一,但最终得到了我想要的东西。
my $mesg = $ldap->search(
filter=>"(&(objectClass=group)(memberUid=$userid)(member=$userdn))",
base=>"$base",
scope=>'subtree',
attrs=>[qw(memberUid)],
);
my $searchresults = $mesg->as_struct;
my %searchhash = %$searchresults; # dereference the hash
my @members = @ { $searchhash{$base}{memberuid} }; # pick out just the memberuid array from the hash
然后我基本上遍历了该组 (@members) 中的用户,如果登录用户在其中,
他们通过了身份验证,如果没有,他们的会话信息将被删除并退出脚本。
与下面给出的答案不完全相同,但我们的树中没有唯一成员。无论哪种方式,希望这对某人有所帮助。
当您登录用户时,我假设您正在使用他们的专有名称执行成功绑定。
我会在组中搜索用户,而不是搜索所有用户组并查找特定组。我还假设您正在使用 groupOfUniqueNames
组对象类来对您的用户进行分组。我会做这样的事情...
# assuming you already have this value in a variable from the authN step
my $user_dn = "cn=dave,ou=people,dc=example,dc=com"
my $base = "cn=GroupToCheck,ou=Groups,ou=Services,dc=example,cd=com";
my $mesg = $ldap->search(
filter=>"(uniquemember=$user_dn)",
base=>"$base",
scope=>'base'
);
如果您得到结果,则该用户在该组中。如果您没有得到任何结果,则该用户不在该组中。
如果您确实想要获取所有组,那么这将 return 用户所属的所有组的组名。
# assuming you already have this value in a variable from the authN step
my $user_dn = "cn=dave,ou=people,dc=example,dc=com"
my $base = "ou=Groups,ou=Services,dc=example,cd=com";
my $mesg = $ldap->search(
filter=>"(uniquemember=$user_dn)",
base=>"$base",
scope=>'sub',
attrs => ['cn']
);
我一直在搜索各种站点以寻求这方面的帮助,但进展不大。我的目标是在登录成功后检查组成员身份。登录部分工作正常,但我想获取已登录用户组的列表,以将其与特定用户组进行比较。如果他们不是该组的成员,他们将被注销,会话被删除并返回到登录页面,否则他们可以继续。
代码如下:
my $base = "ou=Groups,ou=Services,dc=example,cd=com";
my $mesg = $ldap->search(
filter=>"(uid=$userid)",
base=>"$base",
scope=>'children',
);
my @entries = $mesg->entries;
print("mesg = <BR>");
print(Dumper($mesg));
这里是 $mesg 的输出:
$VAR1 = bless( {
'parent' => bless( {
'net_ldap_version' => 3,
'net_ldap_scheme' => 'ldaps',
'net_ldap_debug' => 0,
'net_ldap_socket' => bless( \*Symbol::GEN0, 'IO::Socket::SSL' ),
'net_ldap_host' => 'ldap.example.com',
'net_ldap_uri' => 'ldap.example.com',
'net_ldap_resp' => {},
'net_ldap_mesg' => {},
'net_ldap_async' => 0,
'net_ldap_port' => '636',
'net_ldap_refcnt' => 1
}, 'Net::LDAPS' ),
'errorMessage' => 'NDS error: no such entry (-601)',
'ctrl_hash' => undef,
'resultCode' => 32,
'callback' => undef,
'mesgid' => 2,
'matchedDN' => '',
'controls' => undef,
'raw' => undef
}, 'Net::LDAP::Search' );
我可以看到 NDS 消息说没有这样的条目,所以我假设我的搜索查询是错误所在,但错误正在逃避我。有什么建议么?我也试过没有 scope=>'children'
行,没有任何改变。
谢谢。
更新:
这就是我最终所做的,并且是成功的..不得不稍微调整另一个人的答案之一,但最终得到了我想要的东西。
my $mesg = $ldap->search(
filter=>"(&(objectClass=group)(memberUid=$userid)(member=$userdn))",
base=>"$base",
scope=>'subtree',
attrs=>[qw(memberUid)],
);
my $searchresults = $mesg->as_struct;
my %searchhash = %$searchresults; # dereference the hash
my @members = @ { $searchhash{$base}{memberuid} }; # pick out just the memberuid array from the hash
然后我基本上遍历了该组 (@members) 中的用户,如果登录用户在其中, 他们通过了身份验证,如果没有,他们的会话信息将被删除并退出脚本。
与下面给出的答案不完全相同,但我们的树中没有唯一成员。无论哪种方式,希望这对某人有所帮助。
当您登录用户时,我假设您正在使用他们的专有名称执行成功绑定。
我会在组中搜索用户,而不是搜索所有用户组并查找特定组。我还假设您正在使用 groupOfUniqueNames
组对象类来对您的用户进行分组。我会做这样的事情...
# assuming you already have this value in a variable from the authN step
my $user_dn = "cn=dave,ou=people,dc=example,dc=com"
my $base = "cn=GroupToCheck,ou=Groups,ou=Services,dc=example,cd=com";
my $mesg = $ldap->search(
filter=>"(uniquemember=$user_dn)",
base=>"$base",
scope=>'base'
);
如果您得到结果,则该用户在该组中。如果您没有得到任何结果,则该用户不在该组中。
如果您确实想要获取所有组,那么这将 return 用户所属的所有组的组名。
# assuming you already have this value in a variable from the authN step
my $user_dn = "cn=dave,ou=people,dc=example,dc=com"
my $base = "ou=Groups,ou=Services,dc=example,cd=com";
my $mesg = $ldap->search(
filter=>"(uniquemember=$user_dn)",
base=>"$base",
scope=>'sub',
attrs => ['cn']
);