用LDAP实现Apache的授权与认证

试验了一天,网上各种文档操作(基本上都是一样的。。。),然后偶得一份英文的文档,上手做了下,我了个擦,竟然都调通了。但是最后的调试还是不能做好(调试和部署)

本人openldap文档首页

下面是一条典型的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

wKioL1PHdfPhk6WQAAKUfrP-1tk736.jpg


这里我使用的Apacheyum安装的(包括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的身份验证语法,sorryGoogle 可以帮助人(不要怕,其实我写这个的时候,我也不太懂)。

<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)),然后参照最开始yanzonglidf记录来配置相关(如下)。 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 处理(ID10001)”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  下面文档已做适当修改


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章