注:我的安装方法是以源码编译的方式进行的,以root用户进行安装。  


安装所需软件  


openldap-2.1.29                http://www.openldap.org  


Berkeley DB 4.2.52        http://www.sleepycat.com 




安装步骤 

1、由于openldap需要Berkeley DB来存放数据,所以需先安装Berkeley DB 4.2.52,可到它的网站下载,网址见上面。 

# tar -zxvf db-4.2.52.tar.gz 

解完压后,会生成一个db-4.2.52目录,进行该目录下的build_unix目录。执行以下命令进行配置安装。 

# ../dist/configure  

# make 

# make install 

也是按linux源码安装的三步曲完成,没有什么好说的了。该软件默认是安装在/usr/local/BerkeleyDB.4.2目录下。安装完成后,要把/usr/local/BerkeleyDB.4.2/lib的库路径加到/etc/ld.so.conf文件内,添加完成后执行一次ldconfig,使用配置文件生效。这样编译openldap时才能找到相应的库文件。这样资料库就安装完成了,接下来可以安装openldap了。 

ld.so.conf是什么东西?它就是系统动态链接库的配置文件。此文件内,存放着可被LINUX共享的动态链接库所在目录的名字(系统目录/lib,/usr/lib除外),各个目录名间以空白字符(空格,换行等)或冒号或逗号分隔。一般的LINUX发行版中,此文件均含一个共享目录/usr/X11R6/lib,为X window窗口系统的动态链接库所在的目录。 ldconfig是它的管理命令,具体操作方法可查询man手册,这里就不细讲了。 


2、到openldap官方网站下载最新的稳定版源码,并解压。查看INSTALLT 和README文档,这个很重要,因为安装方法和一些注意事项都在里面有介绍。认真弄明白文档内容能节省你不少的安装调试时间。这也是开源软件的一个特点,给用户提供了最大的灵活性和可配置性。但也增加了系统安装配置的难度,需要有相关的文档配置说明和指导。在官方网站上还有详细的帮助文件,在整个系统配置中需要经常查询。 

# tar -zxvf openldap-stable-20040329.tgz 

解压完成后,会生成一个openldap-2.1.29目录。进行该目录,执行以下命令进行配置安装。 

# env CPPFLAGS="-I/usr/local/BerkeleyDB.4.2/include" LDFLAGS="-L/usr/local/BerkeleyDB.4.2/lib" ./configure --prefix=/usr/local/openldap --enable-ldbm 

注意以上配置语句,要设置资料库的include和lib路径,否则在配置到资料库相关内容时会提示Berkeley DB版本不兼容,并中断配置。如果没有--enable-ldbm选项,在make test时会提示ldbm找不到。为了减少出错,还是加上为好。 

#make depens 

#make 

#make test 

在make test阶段要花费较长时间进行测试,好像有16项吧。你可以放松一下,上上网,聊聊天,听听歌,呵呵,开玩笑了,这个时间应该是最紧张的了。成不成就看这次的了。 

#make install 

通过配置命令可以看出,我们把openldap安装到/usr/local/openldap目录下。建议以源码安装的软件都放到独立的目录下,不要放到软件默认的目录。好处是方便管理和控制,所有文件在统一的目录下,卸载软件只要删除整个目录就可以了。 


3、ok,安装完相关软件后就可以着手配置了。Berkeley DB资料库没什么好配置的。主要是配置openldap 服务。配置文件在软件的安装目录的etc/openldap下,有四个文件,主要的是slapd.conf and ldap.conf,其它两个是backup文件。首先,我们先来配置slapd.conf文档。 

系统默认的slapd.conf文件如下: 

# $OpenLDAP: pkg/ldap/servers/slapd/slapd.conf,v 1.23.2.8 2003/05/24 23:19:14 kurt Exp $ 

# 

# See slapd.conf(5) for details on configuration options. 

# This file should NOT be world readable. 

# 

include         /usr/local/openldap/etc/openldap/schema/core.schema                设置schema配置文档包含 


# Define global ACLs to disable default read access. 


# Do not enable referrals until AFTER you have a working directory 

# service AND an understanding of referrals. 

#referral       ldap://root.openldap.org 


pidfile         /usr/local/openldap/var/slapd.pid                                 设置pid和args文档位置 

argsfile        /usr/local/openldap/var/slapd.args 


# Load dynamic backend modules: 

# modulepath    /usr/local/openldap/libexec/openldap 

# moduleload    back_bdb.la 

# moduleload    back_ldap.la 

# moduleload    back_ldbm.la 

# moduleload    back_passwd.la 

# moduleload    back_shell.la 


# Sample security restrictions 

#       Require integrity protection (prevent hijacking) 

#       Require 112-bit (3DES or better) encryption for updates 

#       Require 63-bit encryption for simple bind 

# security ssf=1 update_ssf=112 simple_bind=64 


# Sample access control policy: 

#       Root DSE: allow anyone to read it 

#       Subschema (sub)entry DSE: allow anyone to read it 

#       Other DSEs: 

#       Subschema (sub)entry DSE: allow anyone to read it 

#       Other DSEs: 

#               Allow self write access 

#               Allow authenticated users read access 

#               Allow anonymous users to authenticate 

#       Directives needed to implement policy: 

# access to dn.base="" by * read 

# access to dn.base="cn=Subschema" by * read 

# access to * 

#       by self write 

#       by users read 

#       by anonymous auth 

# 

# if no access controls are present, the default policy is: 

#       Allow read by all 

# 

# rootdn can always write! 


####################################################################### 

# ldbm database definitions 

####################################################################### 


database        bdb                                        设置使用的资料库,也可用lbdm。 

suffix          "dc=my-domain,dc=com"                        设置目录后缀 

rootdn          "cn=Manager,dc=my-domain,dc=com"        设置目录管理员 

# Cleartext passwords, especially for the rootdn, should 

# be avoid.  See slappasswd(8) and slapd.conf(5) for details. 

# Use of strong authentication encouraged. 

rootpw          secret                                        设置管理密码,这里用了明文的secret密码。 

# The database directory MUST exist prior to running slapd AND 

# should only be accessible by the slapd and slap tools. 

# Mode 700 recommended. 

directory       /usr/local/openldap/var/openldap-data        设置资料库路径 

# Indices to maintain 

index   objectClass     eq                                设置目录项索引 


要服务器正常动作,要修改一些始初参数和设置,修改后的配置文档如下: 


# $OpenLDAP: pkg/ldap/servers/slapd/slapd.conf,v 1.23.2.8 2003/05/24 23:19:14 kurt Exp $ 

# 

# See slapd.conf(5) for details on configuration options. 

# This file should NOT be world readable. 

# 

include         /usr/local/openldap/etc/openldap/schema/core.schema                为了有效使用目录服务,包含相关的文件。 

include         /usr/local/openldap/etc/openldap/schema/corba.schema                注意,在包含文件时是要按一定顺序的,因为文件 

include         /usr/local/openldap/etc/openldap/schema/cosine.schema                里的属性存在依赖关系。如果顺序不对,服务器启 

include         /usr/local/openldap/etc/openldap/schema/inetorgperson.schema    动不了,文档间的依赖关系在文档中都有说明,请 

include         /usr/local/openldap/etc/openldap/schema/misc.schema             仔细查看一个。如果懒得看也可以按我的顺序。 

include         /usr/local/openldap/etc/openldap/schema/openldap.schema 

include         /usr/local/openldap/etc/openldap/schema/nis.schema 

include         /usr/local/openldap/etc/openldap/schema/samba.schema 

# Define global ACLs to disable default read access. 


# Do not enable referrals until AFTER you have a working directory 

# service AND an understanding of referrals. 

#referral       ldap://root.openldap.org 


pidfile         /usr/local/openldap/var/slapd.pid 

argsfile        /usr/local/openldap/var/slapd.args 


loglevel 1                        增加了日志功能,需修改syslog配置文件,在文件中增加一项:local4.*         /var/log/ldap.log 

                                日志级别定义可查相官方网站的文档。1级记录的信息很多。可用于调试。 

# Load dynamic backend modules: 

# modulepath    /usr/local/openldap/libexec/openldap 

# moduleload    back_bdb.la 

# moduleload    back_ldap.la 

# moduleload    back_ldbm.la 

# moduleload    back_passwd.la 

# moduleload    back_shell.la 


# Sample security restrictions 

#       Require integrity protection (prevent hijacking) 

#       Require 112-bit (3DES or better) encryption for updates 

#       Require 63-bit encryption for simple bind 

# security ssf=1 update_ssf=112 simple_bind=64 


# Sample access control policy: 

#       Root DSE: allow anyone to read it 

#       Subschema (sub)entry DSE: allow anyone to read it 

#       Other DSEs: 

#               Allow self write access 

#               Allow authenticated users read access 

#               Allow anonymous users to authenticate 

#       Directives needed to implement policy: 

# access to dn.base="" by * read 

# access to dn.base="cn=Subschema" by * read 

# access to * 

#       by self write 

#       by users read 

#       by anonymous auth 

# 

# if no access controls are present, the default policy is: 

#       Allow read by all 

# 

# rootdn can always write! 


####################################################################### 

# ldbm database definitions 

####################################################################### 


database        bdb 

suffix          "dc=it,dc=com"                改成你自已的目录后缀, 

rootdn          "cn=root,dc=it,dc=com"        设置root为管理员,与linux的root没有什么关系。 

# Cleartext passwords, especially for the rootdn, should 

# be avoid.  See slappasswd(8) and slapd.conf(5) for details. 

# Use of strong authentication encouraged. 

rootpw          {MD5}mjkiuPt0wXhpxxkdiOOO+0000000AKq0by        设置root密码,用MD5加密。密码串用slappasswd -h {MD5}指令 

# The database directory MUST exist prior to running slapd AND 

# should only be accessible by the slapd and slap tools. 

# Mode 700 recommended. 

directory       /usr/local/openldap/var/openldap-data    

# Indices to maintain 

index   objectClass     eq        这里可根据你的需要设置相关索引,以加快查询速度。具体内容可查询官方网站管理手册。        


#ACL configure                        以下内容定义访问控制 

access to  attr=userPassword                                        userPassword只能由自已修改,有效验证用户查询。 

        by self write 

        by anonymous auth 

access to attr=mail 

        by dn="cn=root,dc=it,dc=tigerhead" write                mail只能由自已修改,有效验证用户查询。 

        by self write 

        by anonymous auth 

access to dn=".*,dc=it,dc=tigerhead"                                允许所有人查询没受控制访问限制的信息。 

        by self write 

        by * read 

ok,到现在为止,服务器基本就配置完成了,可以启动了,服务器程序是位于安装目录的libexec下的slapd程序。注意,不是sldap哦。 

# ./slapd 

如果没有提示什么出错信息,直接返回shell状态,就说明服务器正常启动了,你可以查询日志或用ps -aux查看。或用以下命令查询服务器。 

ldapsearch -x -b '' -s base '(objectclass=*)' namingContexts 

如果命令执行成功,返回一些信息,则说明服务器正常运行了。如果启动不成功,它会提示一些出错信息,多数是slapd.conf配置出错。回头仔细核查一下配置文档。 


4、客户端配置文档是ldap.conf。该文档相当简单,其实不和配置也能正常操作。 

# $OpenLDAP: pkg/ldap/libraries/libldap/ldap.conf,v 1.9 2000/09/04 19:57:01 kurt Exp $ 

# 

# LDAP Defaults 

# 


# See ldap.conf(5) for details 

# This file should be world readable but not world writable. 


BASE    dc=it, dc=com                设置目录起点 

#URI    ldap://ldap.example.com ldap://ldap-master.example.com:666 


#SIZELIMIT      12 

#TIMELIMIT      15 

#DEREF          never 


5、ok,服务器正常运作后,就可以录入信息了。信息的录入有三种方法,一种是手工录入,一种是.ldif文件格式录入,一种是脚本自动录入。我们先从最基础的手工录入方式开始介绍,了解录入信息的格式。明白了手工录入的格式,其它两种方式都很容易明白。信息录入用到ldapadd这个程序。可在安装目录的bin目录下找到。具体用法如下: 

第一步是要建立DN: 

# ldapadd -x -D 'cn=root,dc=it,dc=com' -W 

dn: dc=it,dc=com 

objectClass: dcObject 

objectClass: organization 

dc: it 

o: Corporation 

description: d Corporation 

注意:如果你用复制/粘贴功能把以上内容拷贝过去,一定要注意每行后面不要有空格。 

第二步是建立RDN: 

# ldapadd -x -D 'cn=root,dc=it,dc=com' -W        -x表示用简单验证,-D表示指定目录,-W表示弹出密码输入提示 

dn: uid=qq,dc=it,dc=com 

objectClass: person 

objectClass: organizationalPerson 

objectClass: inetOrgPerson 

uid: qq 

cn: qq 

sn: qq 

telephoneNumber: 138888888 

description: openldap test 

telexNumber: tex-8888888 

street: my street 

postOfficeBox: postofficebox 

displayName: qqdisplay 

homePhone: home1111111 

mobile: mobile99999 

mail:qq@qq.com 

输入完所有信息后,按Ctrl+d结束存盘。如果出现出错信息,请查一下对象类和属性的对应关系有没有错或输入失误。初学者就容易出错的地方是对象类和属性的对应关系没有处理好。对象类和属性是在schema文档中定义的。它们之间的关系是这样的,对象类中有些属性是必选的,有些属性是可选的。录入信息的属性必须在对象类中有定义才能用。 


输入以下命令可查询到刚才输入的信息。 

# ldapsearch -x -b 'dc=it,dc=com'        -b设置目录起点,如果设置了BASE参数,该项可不用。 

如果按以上配置文件设置了acl,用上面的查询命令是查询不到受保护的内容的。如上面的userPassword and mail。要查询到这些受限内容,需要通过验证才可以。 

# ldapsearch -x -LLL -h it.com -b 'dc=it,dc=com' -D 'uid=qq,dc=it,dc=com' -W 'uid=qq' 

接着提示输入密码。输入userPassword的密码回车,所有信息就都出来了。 


.ldif文件方式也就是把以上手工输入的内容先写入一个.ldif文件中,然后,用ldapadd命令的-f参数导入。 

# ldapadd -x -D "cn=root,dc=it,dc=com" -W -f test.ldif 

一个完整的global.ldif文件例子: 

dn: dc=info, dc=net 

objectClass: top 

objectClass: organization 

o: info.net 


dn: ou=People, dc=info, dc=net 

objectClass: top 

objectClass: organizationalUnit 

ou: People 

description: User Info 


dn: cn=Admin, dc=info, dc=net 

objectClass: top 

objectClass: person 

objectClass: organizationalPerson 

cn: Admin 

sn: Admin 

userPassword: Admin 

description: Administrator for info.net 


dn: id=1, ou=people, dc=info, dc=net 

objectclass: top 

objectclass: InfoPerson 

id: 1 

username: 张三 

tel:021-63138990 

card_id:ABC001 

再次提醒,注意每行后面不要留有空格。 


脚本录入方式需要自已编写脚本,或到网上下载。有一个用PHP写的LDAP管理工具不错,叫phpLDAPadmin。可以到以下网址下载: 

http://phpldapadmin.sourceforge.net/。安装方法也很简单,只要解压出来,拷贝到apache的web目录下,按说明配置一下设定文档,就ok了。 

接着为大家介绍一下几个常用的ldap命令,如果你用了phpLDAPadmin程序,其实它已经有一个很好的图形介面帮你完成这些命令了。但了解一下还是对你还是很有益的,因为命令方法才是最根本的。 

1、删除命令ldapdelete 

# ldapdelete -x -D 'cn=root,dc=it,dc=com' -W 'uid=qq1,dc=it,dc=com' 

2、设置使用者密码,当然了,你的用户需要有userPassword项了。 

#ldappasswd -x -D "cn=root,dc=it,dc=com" -W "uid=qq1,dc=it,dc=com" -S 

New password: 

Re-enter new password: 

Enter bind password: 

Result: Success (0) 

注意:"Enter bind password" 是 "cn=root,dc=it,dc=com"管理员的密码。 

3、管理员密码更改 

#slappasswd 

New password 

Re-enter new password 

{SSHA}83DJ4KVwqlk1uh9k2uDb8+NT1U4RgkEs 

接下來再拷到 /path/to/sldap.conf 的 rootpw 即可,重启使用配置文件生效 

4、通过ldapmodify修改目录内容 

ldapmodify -x -D "cn=root,dc=it,dc=com" -W -f modify.ldif 

通过ldif文件修改ldap数据,ldif文件格式如下: 

dn: cn=qq,dc=it,dc=com 

changetype: modify 

replace: mail 

mail: modme@example.com 

- 

add: title 

title: Grand Poobah 

- 

add: jpegPhoto 

jpegPhoto:< file:///tmp/modme.jpeg 

- 

delete: description 

- 


启用sasl验证 

前提是你在系统中安装了sasl认证库,并在编译openldap时支持它,默认就支持了。到http://asg.web.cmu.edu/cyrus/下载。安装方法见我写的sendmail安装笔记。 

安装好之后,需要在sasl中建立相应的帐号,用以下命令可完成。 

# saslpasswd2 -c test 


接着配置slapd.conf文件,加入以下内容。 

sasl-regexp 

        uid=(.*),cn=.*,cn=auth 

        uid=$1,dc=it,dc=com 

ok,重启服务器使配置文件生效。这个配置是最大权限的配置,如果要细化请查阅相关文档。 

用以下命令测试。 

# ldapsearch -U qq  -b 'uid=qq,dc=it,dc=com' -D 'dc=it,dc=com' -Y DIGEST-MD5                采用digest-md5验证 

提示密码,输入saslpasswd2的密码。 

???用sasl作验证的作用还不是很清楚。 


配置复制服务器 

由于没有配置两台服务器,所以还没有测试。





参考网站
http://www.openldap.org/doc/admin22/
http://asg.web.cmu.edu/cyrus/
http://phpldapadmin.sourceforge.net/
http://www.linux.org/docs/ldp/howto/LDAP-HOWTO/index.html