📜  珀尔 | LDAP 服务器

📅  最后修改于: 2022-05-13 01:55:44.741000             🧑  作者: Mango

珀尔 | LDAP 服务器

轻量级目录访问协议 (LDAP)是一种基于 TCP/IP 的 Internet 协议,用于访问目录中的信息。 LDAP 协议通常用于访问活动目录。它允许我们保存项目目录和有关它们的信息。 LDAP 以包含一组属性的记录的形式存储数据。

Perl – LDAP 服务器

Perl-LDAP 发行版是 Perl 模块的集合,它为我们提供了一个面向对象的 LDAP 服务器接口。 Perl-LDAP 服务器的一些特性如下:

  1. 通过使用 Perl 对象接口,Perl-LDAP 模块提供了一个接口,该接口允许使用少量代码对 LDAP 目录进行复杂搜索。
  2. 所有 Perl-LDAP 模块都完全用 Perl 编写,这使得它的库真正跨平台兼容。
  3. 它正在积极开发中。

使用 LDAP 服务器的四个步骤是连接、验证、交互和注销。交互包括搜索、添加、删除和更改记录。为此,我们需要一个负责管理 LDAP 会话的 Perl 模块。

Net::LDAP就是其中之一。它是一组允许我们为 Perl 程序实现 LDAP 服务 API 的模块。该模块可用于搜索目录和执行维护功能,例如添加、删除或修改条目。

要安装Net::LDAP服务器,请使用以下命令:

perl -MCPAN -e shell
install Net::LDAP

从服务器获取数据

基于 LDAP 的目录服务将信息存储在条目中。每个条目都属于一个或多个对象类,这些对象类指定存储在目录中的条目类型。属性是包含条目中数据片段的属性。
基于 LDAP 的目录服务中的每个条目都有一个与之关联的唯一名称。此“专有名称”(DN) 由逗号分隔的“相对专有名称”(RDN)字符串组成,它们共同指定目录树中条目的位置和名称。相对专有名称由一个或多个属性/值对组成,它们在目录树中的级别上是唯一的。

当我们使用 search 方法时,它返回一个包含一组条目(数据)的对象。
搜索方法的基本组件是基础和过滤器。基数标记正在搜索的树的顶部,过滤器指示我们感兴趣的记录。
有两种获取条目的方法:

  • 获取整个条目集
Perl
foreach $result ($mesg->all_entries)
{
  # Perform some operation on the data
}


Perl
$num_entries = $mesg->count( ); 
for ($i = 0; $i < $num_entries; $i++)
{ 
  my $entry = $mesg->entry($i);
  # Perform some operation on the data
}


Perl
use strict;
use warnings;
use Net::LDAP; # Package Definition
  
# Initialization
$ldap = Net::LDAP->new("ldap.example.com") or die $@; 
  
# Binding
$ldap->bind( ); 
  
$mesg = $ldap->search(base => $base_dn, 
                       filter => $FILTER); 
$mesg->code( ) && die $mesg->error; 
foreach $result ($mesg->all_entries)
{
  # We can perform any operation on the entries 
  # like adding, removing, modifying the data etc
  print $result->get_value(''), "\n",
} 
  
$ldap->unbind( );


Perl
use Net::LDAP; # Package Definition
  
# Initialization
$ldap = Net::LDAP->new("mumbaiuniversity.com") or die $@; 
  
# Binding
$ldap->bind( ); 
  
my $result = $ldap->search(  # Searching
    base   => "",
    filter => "(&(cn=Thomas*) (sn=S*))",
);
die $result->error if $result->code;
   
printf "COUNT: %s\n", $result->count;
  
$ldap->unbind;



  • 一个一个地获取条目

Perl

$num_entries = $mesg->count( ); 
for ($i = 0; $i < $num_entries; $i++)
{ 
  my $entry = $mesg->entry($i);
  # Perform some operation on the data
}

下面给出了一个从 LDAP 服务器获取信息并打印的基本程序:

Perl

use strict;
use warnings;
use Net::LDAP; # Package Definition
  
# Initialization
$ldap = Net::LDAP->new("ldap.example.com") or die $@; 
  
# Binding
$ldap->bind( ); 
  
$mesg = $ldap->search(base => $base_dn, 
                       filter => $FILTER); 
$mesg->code( ) && die $mesg->error; 
foreach $result ($mesg->all_entries)
{
  # We can perform any operation on the entries 
  # like adding, removing, modifying the data etc
  print $result->get_value(''), "\n",
} 
  
$ldap->unbind( );

超出管理员限制错误

有时,服务器日志中的错误通常与内部 LDAP 问题有关,从而导致 LDAP 相关的错误消息。即使错误不是致命的,它们也表明有问题需要调查。

超出管理限制错误表示已超出管理机构设置的 LDAP 服务器限制。
假设进行的 LDAP 搜索大于允许的目录服务器的nsslapd-sizelimit 属性,则它不会返回全部信息而是部分信息。

有几种方法可以避免出现该错误。

  1. 通过增加nsslapd-sizelimit属性的值。
  2. 为失败的搜索实施 VL V 索引。

示例 –假设我们从一所大学的服务器获取数据,限制为 50。我们搜索姓氏为“Shelby”的名为“Thomas”的人。它会给我们带来很少的结果。
但是如果我们搜索“Thomas”并且我们不知道完整的姓氏,那么我们只搜索“S”。然后这个数字可能会超过限制,并给我们带来超出管理员限制错误”。

Perl

use Net::LDAP; # Package Definition
  
# Initialization
$ldap = Net::LDAP->new("mumbaiuniversity.com") or die $@; 
  
# Binding
$ldap->bind( ); 
  
my $result = $ldap->search(  # Searching
    base   => "",
    filter => "(&(cn=Thomas*) (sn=S*))",
);
die $result->error if $result->code;
   
printf "COUNT: %s\n", $result->count;
  
$ldap->unbind;