用ACL控制受权编程
咱们在LDAP中建立目录树后,最感兴趣的就是如何控制用户在目录树中的权限(读写)。谁在什么条件下有记录权限,咱们有权限看到哪些信息。ACL(Access Control List)访问控制列表就是解决用户权限问题的。服务器
咱们要把ACL写在哪里?编程语言
ACL写在OpenLDAP的服务端全局配置文件slapd.conf中,以下这段即为其指令:ide
# access to dn.base="" by * readui
# access to dn.base="cn=Subschema" by * readthis
# access to *spa
#by self write.net
#by users readrest
#by anonymous authxml
也能够写在一个单独的文件中,如access.conf,而后在全局配置文件slapd.conf中
调用,在配置文件中引入这个文件便可,以下:
include /etc/openldap/access.conf
include后面的路径为该文件的放置地址。
ACL语法基础
怎么看懂ACL指令?
首先看下ACL访问指令的格式:
################################################
access to [resources]
by [who] [type of access granted] [control]
by [who] [type of access granted] [control]
# More 'by' clauses, if necessary....
################################################
指令中包含1个to语句,多个by语句。
这个指令的大致意思是,经过access to约束咱们访问的范围(resources),
经过by设定哪一个用户(who)获取对这个约束范围有什么权限(type of access granted),
并控制(control)这个by语句完成后是否继续执行下一个by语句或者下一个ACL指令。
若是对ACL指令很熟悉的话,能够没必要继续往下看,由于如下为详细的指令基础介绍。
如今对ACL指令分解成两大部分进行详细说明,一个是access to指令,一个是by指令。
先看看access to吧。
以上内容意思是,
dn.base:约束这个特定DN的访问。他和dn.exact和dn.baselevel是相同的意思。
dn.one:约束这个特定的DN第一级子树的访问。dn.onelevel是同义词。
dn.children:这个和dn.subtree相似,都是对其如下的子树访问权的约束。不一样点在于,
这个的约束是不包含本身自己DN。而subtree包含了自己的DN。
#######################################################################
对于dn的约束条件还能够利用模糊约束,以下:
access to dn.regex="uid=[^,]+,ou=Users,dc=example,dc=com"
by * none
dn.regex是用来作匹配(match)用的。
这个指令将约束全部uid=(任何值),ou=Users,dc=example,dc=com的DN,其中的任何值是用[^,]+这个符号组合来表示的,他能够表明任何至少有1个字符,且字符当中没有逗号(,)的值。
更明确点说,意思就是在ou=Users,dc=example,dc=com这个DN下的全部以uid为属性的一级子树都属于这个约束的范围。
2.经过约束attrs访问
对于DN的约束大多用在对某个层级的约束,而用attrs的话,就能够跨层级(或者说跨越父类树),经过属性来约束访问的范围。
access to attrs=homePhone
by * none
这个例子意思是,任何人都没有权限访问属性为homePhone的信息。
在attrs后面的值能够多个,如
access to attrs=homePhone,homePostalAddress
若是想约束某个对象类(Object class)的全部属性,咱们或许能够有这样的形式:
access to attrs = title, registeredAddress, destinationIndicator,……
但这个方法太耗时,也难以阅读,显得笨重,如下给出一个好的方法:
access to attrs=@organizationalPerson
by * none
用@的方法必须谨慎,这段指令不单单约束了organizationalPerson里的属性,也约束
了person对象类的属性。为何?由于organizationalPerson对象类是person的子类,
所以,全部person中的属性就固然也是organizationalPerson的属性了。
若是想作除了organizationalPerson的其余对象类的约束,能够用!来表示:
access to attrs=!organizationalPerson
也能够加入属性的值,具体约束某个值:
access to attrs=givenName val="Matt"
这个指令也能够用模糊约束的方法,以下:
access to attrs=givenName val.regex="M.*"
最后给个通常状况下用到的利用属性约束的例子:
access to attrs=member val.children="ou=Users,dc=example,dc=com"
by * none
1.经过Filters访问
Filters提供一种支持条目记录匹配的方法,以下:
access to filter="(objectClass=simpleSecurityObject)"
by * none
这表示咱们能够约束全部记录中包含对象类为simpleSecurityObject的信息。
与编程语言相似, ACL指令也有相似与或的条件判断,以下:
access to
filter="(|(|(givenName=Matt)(givenName=Barbara))(sn=Kant))"
by * none
这段代码过滤出givenName为Matt或者Barbara,或者surname为Kant的信息。
Access to [resources]
resources能够有多种形式,如DN,attrs, Filters.
如下即详细说明。
1.经过约束DN进行访问
以下所示,
access to dn="uid=matt,ou=Users,dc=example,dc=com"
by * none
这个指令是指访问uid=matt,ou=Users,dc=example,dc=com这个DN,即把访问的
范围约束在这个DN中。
by * none是指对于任何人的访问都是拒绝的。
整体的意思就是,任何人都没有权限访问uid=matt,ou=Users,dc=example,dc=com这个DN,固然,服务器管理员是能够访问的,否则它没法维护这个OpenLDAP中的用户信息。
再来看一个,
access to dn.subtree="ou=Users,dc=example,dc=com"
by * none
在这个例子中,咱们用了dn.subtree。在咱们的目录信息树中,在ou=Users子树下可能有多个用户。举例来讲,DN为uid=matt,ou=Users,dc=example,dc=com就是ou=Users, dc=example,dc=com的子树,当要试图访问他时,这个ACL指令就起了做用。
整体的意思是,任何人都没有权限访问ou=Users,dc=example,dc=com以及其子树的信息。
#######################################################################
※ 此处插播1个知识点
dn.base:Restrict access to this particular DN. This is the default, and
dn.exactand dn.baselevel are synonyms of dn.base.
dn.one: Restrict access to any entries immediately below this DN.
dn.onelevelis a synonym.
dn.children:Restrict access to the children (subordinate) entries of this DN.
This is similar to subtree, except that the given DN itself is not restricted by the rule.