[zz]我的openldap经历

转自:http://blog.csdn.net/porcupinefinal/article/details/640523

我的openldap经历 

----by hefish@cz8.net 转载请注明出处。

大致叙述一下。

如今网上的ldap的文档,多半是相互抄袭的。全他 *** 狗屁不通,千篇一律。其实ldap主要应用在大容量邮件系统上。几乎所有的文档对这个问题避而不谈,总是在地址簿上兜圈子,可恶!

ldap 其实就是一个数据库,不是数据库怎么存放资料呢?不过跟我们平常的关系数据库有所不同。关系数据库是有一张一张的二维表格来存放数据的。ldap类似我们 的dns系统,是树状的。用节点来存放数据。当然一个树枝可以有n个节点,每个节点上存放的数据,都是以key => value的形式。就像我们的dns系统。 .是根,下面是com,org,net,cn等等一些树枝,这些树枝下面还有abc.com, bcd.com等等树枝。在每个树枝下面都可以放节点,其实就是域名下面的主机:www,ftp,mail等等。所有的这些内容,组成了一个dns树,在 ldap里面叫数据库。这下应该很形象了吧。

下面说说我的ldap的实现。 
我主要用ldap来存放邮件系统里面的用户信息,包括username,clearpw,maildir,等一些,这样postfix在投递邮件的过程中可以通过检索ldap来知道把邮件投递到硬盘的什么位置。

先去把berkeley-DB的源代码拉过来(http://www.sleepycat.com/) ,我的是4.1.25,openldap需要这东西,这东西其实也没什么不好的,所以我就把它装到/usr下去了。 
# tar zxvf db-4.1.25.tar.gz 
# cd db-4.1.25 
# cd dist 
# ./configure --prefix=/usr 
# make 
# make install

然后就可以开始安装openldap了,我用的是2.1.22,原码是从http://www.openldap.org/上拉的。 
# tar zxvf openldap-2.1.22.tgz 
# cd openldap-2.1.22 
# ./configure --prefix=/usr/local/openldap 
# make depend 
# make 
# make test 
# make install

这部分安装应该没什么问题,有问题的话就去看看网上的文档,虽然说不到重点,这方面说的还算详细。接下来就是配置。


schema

schema 类似关系数据库的字段说明,包括字段名,数据类型,数据长度等等。系统有一些默认的schema,我的默认schema文件在 /usr/local/openldap/etc/openldap/schema下面,最重要的是core.schema。它定义了一些最基本的字段。

为了适应我们的应用,我们要创建自己的schema文件。我创建的shema文件如下:(文件名:kunmail.schema)


# kunmail-ldap v3 directory schema 

# written by hefish@cz8.net 

# Attribute Type Definitions

attributetype ( 1.3.6.1.4.1.7914.1.2.1.1 NAME 'username' 
DESC 'name of the user on the mailsystem' 
EQUALITY caseIgnoreIA5Match 
SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 
SINGLE-value )

attributetype ( 1.3.6.1.4.1.7914.1.2.1.2 NAME 'vuid' 
DESC 'UID of the user on the mailsystem' 
EQUALITY integerMatch 
SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 
SINGLE-value )

attributetype ( 1.3.6.1.4.1.7914.1.2.1.3 NAME 'vgid' 
DESC 'GID of the user on the mailsystem' 
EQUALITY integerMatch 
SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 
SINGLE-value )

attributetype ( 1.3.6.1.4.1.7914.1.2.1.4 NAME 'maildir' 
DESC 'Path to the maildir/mbox on the mail system' 
EQUALITY caseExactMatch 
SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 
SINGLE-value )

attributetype ( 1.3.6.1.4.1.7914.1.2.1.5 NAME 'forwardAddr' 
SUBSTR caseIgnoreSubstringsMatch 
DESC 'Forward mail address' 
EQUALITY caseIgnoreIA5Match 
SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )

attributetype ( 1.3.6.1.4.1.7914.1.2.1.6 NAME 'quota' 
DESC 'The amount of space the user can use until all further messages get bounced.' 
SYNTAX 1.3.6.1.4.1.1466.115.121.1.44 
SINGLE-value )

attributetype ( 1.3.6.1.4.1.7914.1.2.1.7 NAME 'storeHost' 
DESC 'On which kunmail server the messagestore of this user is located.' 
EQUALITY caseIgnoreIA5Match 
SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 
SINGLE-value )

attributetype ( 1.3.6.1.4.1.7914.1.2.1.8 NAME 'delivery' 
DESC 'Program to execute for all incoming mails.' 
SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 
SINGLE-value )

attributetype ( 1.3.6.1.4.1.7914.1.2.1.9 NAME 'clearpw' 
DESC 'name of the user on the mailsystem' 
EQUALITY caseIgnoreIA5Match 
SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 
SINGLE-value )

attributetype ( 1.3.6.1.4.1.7914.1.2.1.10 NAME 'home' 
DESC 'Program to execute for all incoming mails.' 
SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 
SINGLE-value )

attributetype ( 1.3.6.1.4.1.7914.1.2.1.11 NAME 'mailReplyText' 
DESC 'A reply text for every incoming message' 
SUBSTR caseIgnoreSubstringsMatch 
SYNTAX 1.3.6.1.4.1.1466.115.121.1.44{4096} 
SINGLE-value )

attributetype ( 1.3.6.1.4.1.7914.1.2.1.12 NAME 'active' 
DESC 'The status of a user account: active, nopop, disabled' 
EQUALITY integerMatch 
SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 
SINGLE-value )

# Object Class Definitions

objectclass ( 1.3.6.1.4.1.7914.1.2.2.1 NAME 'kunmailUser' 
DESC 'KunMail-LDAP User' SUP top STRUCTURAL 
MUST ( username $ cn $ vuid $ vgid ) 
MAY ( maildir $ home $ clearpw $ 
forwardAddr $ quota $ 
storeHost $ delivery $ 
mailReplyText $ active ) )

现在来说说这个schema文件。 
开始部分是attributeType的定义,相当于字段定义。最后的objectclass是定义数据所包含的属性。 
这 里kunmailUser这种数据,要包含maildir $ home $ clearpw $ forwardAddr $ quota $ storeHost $ delivery $ mailReplyText $ active 等可选项,还要包括username $ cn $ vuid $ vgid 必选项。 可选项用MAY()来包含,必选项用MUST()来包含。DESC是说明项。SUP表示父类(有点像面向对象编程啊)top表示没有父类,他自己是顶级。 STRUCTURAL是存储方式,不管他(我也说不清楚) 
接下来解释attributeType的说明项。 
第一个数字是表示序号,至少我是怎么认为的,也许不对,不过。。。管他。 
NAME是表示属性的名字 
DESC是说明 
下面表示的是匹配的方式,SUBSTR是字符串匹配,EQUALITY是相等性匹配,这些在openldap的admin guide里面有,不难看懂 
SYNTAX是表示字段的数据类型。这个admin guide里面也有说明。 
SINGLE-value表示这个属性只有一个值,有些属性可以有多个值,比如联系地址等。默认的话,是多值的。

schema准备好之后,我们要在配置文件中,把这个schema包含进去,让这个schema生效。 
在配置文件slapd.conf中间的开始部分加入这样的一句: 
include /usr/local/openldap/etc/openldap/schema/kunmail.schema

应该注意,上面这句话之前应确保有一句: 
include /usr/local/openldap/etc/openldap/schema/core.schema 
因为kunmail.schema里面有些东西是依赖core.schema的。 
然后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 
include /usr/local/openldap/etc/openldap/schema/kunmail.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

####################################################################### 
# ldbm database definitions 
#######################################################################

database bdb 
suffix "dc=cz8,dc=net" 
rootdn "cn=Manager,dc=cz8,dc=net" 
# Cleartext passwords, especially for the rootdn, should 
# be avoid. See slappasswd(8) and slapd.conf(5) for details. 
# Use of strong authentication encouraged. 
rootpw abcd1234

# 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


配置文件没什么好解释的了吧,主要是我配置了一个数据库"dc=cz8,dc=net",如果需要,还可以配置多个数据库,每个都是以database开始的。具体的方法看admin guide。(一般说来,一个公司用一个数据库)

可以启动ldap服务了! 
# cd /usr/local/openldap/libexec 
# ./slapd

接下来的事情就是向数据库添加数据。ldap不支持sql,所以要用ldapadd这个程序来添加,添加的时候要用一个ldif文件,把要添加的东西写在里面,ldif的格式在admin guide里面有说明,我在这里只是简要说一下。

首先要添加根,这一步是必要的,不然任何数据都添加不进去。我们编辑一个root.ldif文件:

dn: dc=cz8,dc=net 
dc: cz8 
description: YuCa Network Studio 
objectClass: dcObject 
objectClass: organization 
o: YuCa Network

然后用 ldapadd命令将它加入ldap。 
# ./ldapadd -f root.ldif -x -D "cn=Manager,dc=cz8,dc=net" -w abcd1234

然后我准备在数据库里添加一个分支,专门用来存放邮件系统的用户数据,这种分支,据说要做ou(organizational unit),一般来说,不同的分支,存放不同类型的数据,我给这个ou起名叫mail。且看ldif文件(mail.ldif)

dn: ou=mail,dc=cz8,dc=net 
objectClass: organizationalUnit 
ou: mail 
description: Mail Directory

继续用ldapadd加入。

然后在ou=mail,cd=cz8,dc=net分支下,加入用户数据:(user.ldif) 
dn: cn=heyu, ou=mail, dc=cz8, dc=net 
objectClass: kunmailUser 
cn: heyu 
username: heyu@cz8.net 
vuid: 1000 
vgid: 1000 
clearpw: tmd8427 
home: /exports/mailhome/system/heyu 
maildir: /exports/mailhome/system/heyu/Maildir/ 
forwardAddr: hefish@czlib.net 
quota: 10000000 
active: 1

主要解释一下上面的ldif文件,主要是objectClass这一行,意思是这个节点存放kunmailUser这个类型的数据记录。其他的都是kunmailUser这个类型的属性。

运行ldapadd,将其加入。 
现在去下在一个ldap browser/editor,登录到我们的ldap server看看,应该可以看见ldap的树状结构了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值