歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> 我的openldap經歷

我的openldap經歷

日期:2017/2/27 14:21:58   编辑:更多Linux
  大致敘述一下。 如今網上的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 [email protected] # # 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: [email protected] vuid: 1000 vgid: 1000 clearpw: tmd8427 home: /exports/mailhome/system/heyu maildir: /exports/mailhome/system/heyu/Maildir/ forwardAddr: [email protected] quota: 10000000 active: 1 主要解釋一下上面的ldif文件,主要是objectClass這一行,意思是這個節點存放kunmailUser這個類型的數據記錄。其他的都是kunmailUser這個類型的屬性。 運行ldapadd,將其加入。 現在去下在一個ldap browser/editor,登錄到我們的ldap server看看,應該可以看見ldap的樹狀結構了。



# 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: [email protected] vuid: 1000 vgid: 1000 clearpw: tmd8427 home: /exports/mailhome/system/heyu maildir: /exports/mailhome/system/heyu/Maildir/ forwardAddr: [email protected] quota: 10000000 active: 1 主要解釋一下上面的ldif文件,主要是objectClass這一行,意思是這個節點存放kunmailUser這個類型的數據記錄。其他的都是kunmailUser這個類型的屬性。 運行ldapadd,將其加入。 現在去下在一個ldap browser/editor,登錄到我們的ldap server看看,應該可以看見ldap的樹狀結構了。



Copyright © Linux教程網 All Rights Reserved