試驗了一天,網上各種文檔操作(基本上都是一樣的。。。),然後偶得一份英文的文檔,上手做了下,我了個擦,竟然都調通了。但是最後的調試還是不能做好(調試和部署)
下面是一條典型的LDAP用戶信息(ldif格式):
PS:該實驗環境爲yum所裝。
# yanzong chen,People, shuyun.com dn: cn=yanzongchen,ou=People,dc=shuyun,dc=com objectClass:posixAccount objectClass: inetOrgPerson objectClass:organizationalPerson objectClass: person homeDirectory:/home/yanzong.chen loginShell: /bin/bash uid: yanzong.chen cn: yanzong chen uidNumber: 10000 gidNumber: 10001 userPassword:: e1NTSEF9ampCT0JTc0d3d2llN1lySVdzMHZQZUlIREV4SU5sQTU= sn: chen givenName: yanzong
你可以使用一些命令行工具(ldapsearch)來查詢LDAP數據,但是如果你和我一樣,剛入行,那麼可能你需要抽出點時間學習下LDAP搜索語法!
配置Apache 2.2
Apache 對於LDAP的相關模塊在1.3的時候已經出現。這裏用的是2.2版本的Apache。
這裏我使用的Apache爲yum安裝的(包括ldap),所以默認的模塊安裝、加載的都比較全,這裏就不在演示如何加載ldap模塊,你可以查看httpd.conf file 來確認他們是否被加載:
LoadModuleldap_module /path/to/mod_ldap.so
LoadModuleauthnz_ldap_module /path/to/mod_authnz_ldap.so
當模塊被加載,你可以通過ldap驗證來訪問特定的目錄。 AuthLDAPUrl 指向LDAP服務器,它一般是這樣來寫的:
AuthLDAPUrl ldap://ldap.shuyun.com/ou=People,dc=shuyun,dc=com?uid
他定義了LDAP服務器的地址,其他的不在着了說,複雜的定義策略、搜索策略可以通過理解LDAP樹形結構來定義。。。
接下來的幾節如何配置各種APACHE的訪問策略(通過ldap),這些配置文件爲Apache.conf,放在你習慣放的位置。
允許任何有效用戶
這個配置首先會去查找是否爲ldap的有效用戶,如果通過則可訪問。當你訪問的時候,Apache會要求用戶輸入用戶名、密碼,然後去檢驗。如果你熟悉Apache的基本身份驗證,這裏很容易看懂。Or 你不太懂Apache的身份驗證語法,sorry,Google 可以幫助人(不要怕,其實我寫這個的時候,我也不太懂)。
<Location /> Order deny,allow Deny from All AuthName "Welcometo shuyun ldap . Use your info to pass in !" AuthType Basic AuthBasicProvider ldap AuthzLDAPAuthoritative off AuthLDAPUrlldap://ldap.shuyun.com/ou=People,dc=shuyun,dc=com?uid Require valid-user Satisfy any </Location>
AuthBasicProvider ldap 是必須滴,以便讓Apache種地查詢的是其他地址的LDAP服務器,而不是本地的。
AuthzLDAPAuthoritative off 必須在此處配置出來,因爲這玩意兒默認是“開”。如果你要求驗證的用戶必須爲LDAP用戶,就設置此項爲“ON”。例如:Require ldap-user, 需要設置爲 "on." 設置爲 off 則可以使用混合用戶驗證。
Satisfy any (off時,2套驗證機制)這個指令不在此處使用。
用戶列表(認證用戶)
Require ldap-user 這個指令作用是:ldap-user列表中的用戶可以通過此認證
Order deny,allow Deny from All AuthName " Welcometo shuyun ldap . Use your info to pass in !" AuthType Basic AuthBasicProvider ldap AuthzLDAPAuthoritative on AuthLDAPUrlldap://ldap.shuyun.com/ou=People,dc=shuyun,dc=com?uid Require ldap-user yanzong.chen Satisfy any
AuthzLDAPAuthoritative on 默認爲”ON”,這裏寫出來是爲了更清晰。
注意: AuthLDAPUrl 沒有更改,和前邊例子一樣,他在目錄中搜索匹配到的UID
組成員
通過 Require ldap-group 來按照組劃分認證.
組配置可能是由目錄的架構設計(從NIS轉換(as mine was)),然後參照最開始yanzong的lidf記錄來配置相關(如下)。 gidNumber 和 memberUid 請仔細看
dn: cn=SysOps,ou=group,dc=shuyun,dc=com objectClass:posixGroup gidNumber: 10001 cn: SysOps memberUid: yanzong.chen memberUid: … memberUid: … ...
我們進行另一個測試, Require ldap-attribute, 去匹配組中的用戶下面是Apache配置:
Order deny,allow Deny from All AuthName " Welcometo shuyun ldap . Use your info to pass in !" AuthType Basic AuthBasicProvider ldap AuthzLDAPAuthoritative on AuthLDAPUrlldap://ldap.shuyun.com/ou=People,dc=shuyun,dc=com?uid AuthLDAPGroupAttribute memberUid AuthLDAPGroupAttributeIsDN off Require ldap-group cn=SysOps,ou=Group,dc=company,dc=com Require ldap-attribute gidNumber=10001 Satisfy any
AuthzLDAPAuthoritative on 默認爲”ON”,這裏寫出來是爲了更清晰。
AuthLDAPGroupAttribute memberUid 表示LDAP組記錄有哪些屬性,以配合UID。在這種情況下,一個memberUid 包含其組中每一個成員。
AuthLDAPGroupAttributeIsDN off 告訴Apache驗證用戶時,使用客戶端的distinguished name(標示名比如:cn=yanzong.chen,ou=SysOps,dc=shuyun,dc=com)。否則,用戶名會被使用。在我的LDAP目錄中,用戶名來自NIS,所以需要關閉(默認爲ON)。LDAP目錄中可以存儲整個轉有名稱,因此你可以根據你的需求更改此選項。
Require ldap-group 授予獲得”SysOps”的組成員,對於多個組,添加一個額外的命令”each(也可能是for each)”。
Require ldap-attribute gidNumber=10001 處理(ID:10001)、”SysOps”組,如果沒有此選項,用戶訪問將被拒絕。對於多個組,添加額外命令“each”。
Satisfy any 這個指令是必須的。因爲我們正在測試多個條件,並希望任何條件授予訪問權限都能成功。
用戶和組的結合
例子如下,各種選項你都能看懂的應該是,所以你看看吧然後測試測試。
Order deny,allow Deny from All AuthName " Welcometo shuyun ldap . Use your info to pass in !" AuthType Basic AuthBasicProvider ldap AuthzLDAPAuthoritative on AuthLDAPUrl ldap://ldap.shuyun.com/ou=People,dc=shuyun,dc=com?uid AuthLDAPGroupAttribute memberUid AuthLDAPGroupAttributeIsDN off Require ldap-group cn=SysOps,ou=Group,dc=company,dc=com Require ldap-attribute gidNumber=10001 Require ldap-user yanzong.chen Satisfy any
調試和部署
Testing LDAP authentication from a Web browser can be frustrating, becausethe only thing you know is whether access was granted or not. You don't get anykind of feedback on why something did not work. For verbose information on eachstep in the process, set theLogLeveldebug option inApache. With debugging active, Apache will record the connection status to theLDAP server, what attributes and values were requested, what was returned, andwhy conditions were met or not met. This information can be invaluable infine-tuning LDAP access controls.
我保留了這段話,因爲這段話我雖然看懂了,但是實話是不知道咋操作。因爲我的LDAP目前爲止除了終端啓動模式,我並不知道如何輸出日誌到文件(我係統的原因貌似。)我猜想可能是要將apache日誌的loglevel調整下,然後觀察Apache的日誌吧。這個大家可以試一下。也可以留言給我。
PS:源英文地址:http://www.opendigest.org/article.php/490 下面文檔已做適當修改