Book: Perlを使ったLDAPアクセス -Net::LDAPモジュールの設定-

LDAPサーバーにアクセス可能なAPIは色々ありそうですが、まずは、PerlからLDAPにアクセスするための方法を見ていきます。サーバーはOpenDSのみならず、Sun Java System Directory Server, OpenLDAP, Fedora Directory Server 等などLDAPに準拠したものであればすべて同じようにアクセスできるはずです。(できる点がRDBMSと違う点なはずです)

1998年頃、ずいぶん前になりますが、LDAPサーバー実装の先駆けであるNetscape Directory Serverがリリースされたころ、PerLDAPというPerlからLDAPを操作できるライブラリが提供されていました。当初、これを使ってみようと思ったのですが、Mozilla.orgに移管された後、開発のスピードがスローダウンしてしまったようです。

よって、PerlのCPAN ( Comprehensive Perl Archive Network http://www.cpan.org )にあるNet::LDAPモジュールを使用したいと思います。

Net::LDAPモジュール
http://search.cpan.org/~gbarr/perl-ldap-0.39/lib/Net/LDAP.pod

今回はLinux( Fedora 9 )を使って検証しています。
デフォルトでNet::LDAPモジュールが入っているかどうか確認する方法を「Perlのモジュール一覧」に記録しておいたので参考にしてください。Net::Perlモジュールがなければモジュールをインストールする必要があります。
yumで簡単にインストールすこともできそうですが、あえて、ディストリビューションに依存しない方法で設定してみたいと思います。
ちなみにyumでインストールする際は、perl-LDAP というパッケージ名のようです。

# yum info perl-LDAP
Loaded plugins: refresh-packagekit
Available Packages
Name : perl-LDAP
Arch : noarch
Epoch : 1
Version : 0.34
Release : 4.fc9
Size : 328 k
Repo : fedora
Summary : LDAP Perl モジュール
URL : http://search.cpan.org/dist/perl-ldap/
License : GPL+ or Artistic
Description: Net::LDAP is a collection of modules that implements a LDAP services API for Perl programs. The module may be used
: to search directories or perform maintenance functions such as adding, deleting or modifying entries.

CPANのNet::LDAPモジュール( http://search.cpan.org/~gbarr/perl-ldap-0.39/lib/Net/LDAP.pod )の"Dependencies"を見ると、Convert::ASN1 (http://search.cpan.org/~gbarr/Convert-ASN1-0.22/lib/Convert/ASN1.pod) に依存している事が分かります。

よって、この2つのアーカイブをダウンロードします。ファイルは以下のとおりです。

perl-ldap-0.39.tar.gz
Convert-ASN1-0.22.tar.gz

Convert::ASN1のインストール

#tar xvf Convert-ASN1-0.22.tar.gz
Convert-ASN1-0.22/
Convert-ASN1-0.22/.gitignore
Convert-ASN1-0.22/ChangeLog
...
Convert-ASN1-0.22/t/14any.t
Convert-ASN1-0.22/t/funcs.pl

# cd Convert-ASN1-0.22

# perl Makefile.PL
Checking if your kit is complete...
Looks good
Writing Makefile for Convert::ASN1

# make
cp lib/Convert/ASN1/parser.pm blib/lib/Convert/ASN1/parser.pm
cp lib/Convert/ASN1.pm blib/lib/Convert/ASN1.pm
cp lib/Convert/ASN1.pod blib/lib/Convert/ASN1.pod
cp lib/Convert/ASN1/_decode.pm blib/lib/Convert/ASN1/_decode.pm
cp lib/Convert/ASN1/IO.pm blib/lib/Convert/ASN1/IO.pm
cp lib/Convert/ASN1/Debug.pm blib/lib/Convert/ASN1/Debug.pm
cp lib/Convert/ASN1/_encode.pm blib/lib/Convert/ASN1/_encode.pm
Manifying blib/man3/Convert::ASN1.3pm

# make test
...
All tests successful.
Files=15, Tests=457, 2 wallclock secs ( 0.99 cusr + 0.17 csys = 1.16 CPU)

# make install

以上で、Convert::ASN1がインストールされているはずです。

Net::LDAPのインストール

# tar xvf perl-ldap-0.39.tar.gz
# cd perl-ldap-0.39

# perl Makefile.PL

・・・いくつかのモジュールを自動インストールするかの確認が出ますが、すべてnでも正しくセットアップできます。

*** Module::AutoInstall version 1.03
*** Checking for Perl dependencies...
[Core Features]
- Convert::ASN1 ...loaded. (0.22 >= 0.07)
[SASL authentication]
- Authen::SASL ...missing. (would need 2.00)
==> Auto-install the 1 optional module(s) from CPAN? [n]
[LDAP URLs]
- URI::ldap ...loaded. (1.11 >= 1.1)
[LDAPS]
- IO::Socket::SSL ...missing. (would need 0.81)
==> Auto-install the 1 optional module(s) from CPAN? [n]
[Read/Write DSML files]
- MIME::Base64 ...loaded. (3.07_01)
- XML::SAX::Writer ...missing.
==> Auto-install the 1 optional module(s) from CPAN? [n]
[Read/Write LDIF files]
- MIME::Base64 ...loaded. (3.07_01)
*** Module::AutoInstall configuration finished.
Checking if your kit is complete...
Looks good
Writing Makefile for Net::LDAP

# make
...
Manifying blib/man3/Net::LDAP.3pm
Manifying blib/man3/Net::LDAP::Constant.3pm
Manifying blib/man3/Net::LDAP::Search.3pm

# make test
...
All tests successful, 10 tests skipped.
Files=16, Tests=521, 5 wallclock secs ( 2.10 cusr + 0.30 csys = 2.40 CPU)

# make install

以上で、Net::LDAPモジュールがインストールされます。

簡単なサンプルのプログラムや使用方法が、http://search.cpan.org/~gbarr/perl-ldap-0.39/lib/Net/LDAP.podに掲載されていますので、それを元に簡単なプログラムを作成しています。

簡単なNet::LDAPを使用したプログラム例 test-ldap.pl )

use Net::LDAP;

$ldap = Net::LDAP->;new( "localhost" );
$ldap->bind;
$result=$ldap->search( base=>"dc=example,dc=com", filter=>"uid=*" );

foreach $entry ($result->entries){
        $entry->dump;
}

$ldap->unbind;

上記を実行します。
# perl test-ldap.pl ... ------------------------------------------------------------------------ dn:uid=user.1000,ou=People,dc=example,dc=com objectClass: person organizationalperson inetorgperson top postalAddress: Bonnee Bilodeau$73145 Washington Street$Salem, FL 28158 postalCode: 28158 uid: user.1000 description: This is the description for Bonnee Bilodeau. employeeNumber: 1000 initials: BEB givenName: Bonnee pager: +1 064 558 4984 mobile: +1 028 848 2878 cn: Bonnee Bilodeau telephoneNumber: +1 919 066 9369 sn: Bilodeau street: 73145 Washington Street homePhone: +1 787 127 0313 mail: user.1000@maildomain.net l: Salem st: FL #
検索結果が表示されました。 上記のプログラムは簡単なものですが、接続でき、検索可能であることが確認できます。