- 論壇徽章:
- 0
|
創(chuàng)建基于ldap的apache目錄驗證
軟件:openldap
ldapclient/server
apache:添加mod_authz_ldap支持
1:設置ldap和創(chuàng)建ldap數(shù)據(jù)庫
apache的authz_ldap module通過匹配在ldap server中的attributetype:usePassword來驗證密碼,所以在ldap存儲設計中一定要使用這個attribute。而這個attribute是openldap所預先定義好的,它屬于inetOrgPerson這樣一個objectclass,我們設計的數(shù)據(jù)的objectClass就應該是inetOrgPerson或者它的一個子類。
這里有必要講一下ldap中的objectClass和attributetype。在我看來,objectClass就相當于普通數(shù)據(jù)庫中的一個table,attributetype就是table中的一個coloum。與數(shù)據(jù)庫中的table,coloum不同的是,ldap中的objectClass呈現(xiàn)一種類似與面向?qū)ο缶幊讨械睦^承關系,比如 organization->dcObject->top。top就是頂層objectClass,而 dcObject,organization都是“繼承”它而來。我們需要使用inetOrgPerson中的userPassword,因此可以直接用這個objectClass,也可以定義一個通過繼承它而來的objectClass。objectClass可以定義must和may兩種attributetype,意思是必須和不是必須的attributetype,在inetOrgPerson中sn和cn是必須的(我不知道這兩個具體代表什么意思),但我們并不需要它,因此我懷疑應該有其他不通過繼承inetOrgPerson來使用userPassword的方法,如果哪位高手明白的話,請不吝賜教。
好,說了一堆,現(xiàn)在正式來創(chuàng)建ldap數(shù)據(jù)項。
- attributetype (1.1.2.2.2.2
- NAME 'userName'
- DESC 'Employee User Name'
- EQUALITY caseIgnoreMatch
- SUBSTR caseIgnoreSubstringsMatch
- SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 SINGLE-VALUE )
- objectclass (1.1.2.2.1
- NAME 'Employee'
- SUP inetOrgPerson
- STRUCTURAL
- MUST ( userName )
- )
復制代碼
在這里我創(chuàng)建了一個叫rdpsUser的objectClass,SUP inetOrgPerson表示它是繼承inetOrgPerson而來,并且有一個必須的userName。這個userName使用來存儲網(wǎng)頁驗證時輸入的用戶名的,當然我們也可是使用inetOrgPerson已有的如sn來存儲,而且那樣的話都沒有必要建立一個新的objectClass,但在這里為了演示如何定義schema,并為了數(shù)據(jù)庫的易讀性,還是創(chuàng)建一個新的。
把它另存為/.../openldap/schema/what_ever_you_call_it.schema,然后在slapd.conf里將它include進去。重啟ldap以驗證schema的正確性。
現(xiàn)在來創(chuàng)建ldap的存儲結(jié)構,相當于創(chuàng)建數(shù)據(jù)庫的table
創(chuàng)建ldap init文件initial_create.ldif
- dn: dc=dps,c=us
- objectClass: organization
- objectClass: dcObject
- objectClass: top
- o: dps-us
- dc: dps
- dn: o=dps-us,dc=dps,c=us
- objectClass: organization
- objectClass: dcObject
- o: dps-us
- dc: dps
- dn: ou=employees,o=dps,dc=dps,c=us
- objectClass: organizationalUnit
- ou: employees
復制代碼
這里假設了slapd.conf里設置是這樣的
- suffix "dc=dps,c=us"
- rootdn "cn=Manager,dc=dps,c=us"
- rootpw secret
復制代碼
注:這段使用來設置ldap服務器的root權限,不知道是否一定需要把ldap init文件的root dn設置為和suffix一樣,呼喚明白人
關閉ldap
將數(shù)據(jù)庫定義導入ldap
- slapadd -v -l initial_create.ldif
- /etc/init.d/ldap start
復制代碼
注:我通過yum install安裝的ldap,第一次導入這些的時候出現(xiàn)了數(shù)據(jù)文件的owner不是ldap用戶的錯誤信息,使用chown改成ldap即可
導入數(shù)據(jù):
創(chuàng)建內(nèi)容為如下的data.ldif
- dn: userName=fduan,ou=employees,o=dps,dc=dps,c=us
- objectClass: top
- objectClass: inetOrgPerson
- objectClass: rdpsEmployee
- userPassword: {crypt}$1$d5TsJc7e$1qEaguOMwU9.89opV3BH1.
- userName: fduan
- sn: fduan
- cn: fduan
復制代碼
- ldapadd -x -D 'cn=Manager,dc=dps,c=us' -w secret -f data.ldif
復制代碼
注:userPassword可以使用明文和加密方式存儲,可以使用slappasswd來生成加密的字段,crypt是linux /etc/shadow的加密方式,所以可以使用ldap來存儲服務器用戶的密碼
- slappasswd -h{MD5} -s secret
- {MD5}Xr4ilOzQ4PCOq3aQ0qbuaQ==
復制代碼
2:為http服務目錄添加使用ldap驗證的功能
需要為apache安裝mod_authz_ldap,還有一個mod_auth_ldap的模塊,別搞混了.
假設需要添加驗證的目錄是/var/www/elog/
首先打開目錄.htaccess的override功能
- <Directory "/var/www/elog">
- AllowOverride all
- Order allow,deny
- Allow from all
- </Directory>
復制代碼
在/var/www/elog里面創(chuàng)建.htaccess,添加如下內(nèi)容
- AuthzLDAPMethod ldap
- AuthName AuthzLDAP
- AuthType Basic
- AuthzLDAPServer "serverName"
- AuthzLDAPUserBase ou=employees,o=dps-us,dc=dps,c=us
- #這是存數(shù)據(jù)的DN
- AuthzLDAPUserKey userName
- #這是用來驗證的attributetype,所以說不一定要用userName
- AuthzLDAPLogLevel info
- Order deny,allow
- Deny from all
- require valid-user
- satisfy any
復制代碼
這樣應該通過ldap來做網(wǎng)頁的驗證了。
另:打開ldap的log
openldap的log默認是不打開的,要打開在/etc/syslog.conf或rsyslog.conf里面加入
- local4.* -/var/log/ldap/ldap.log
復制代碼
[ 本帖最后由 redicaps 于 2009-2-5 14:08 編輯 ] |
|