3. 字符串搜索过滤器定义 (String Search Filter Definition)
LDAP 搜索过滤器的字符串表示是由 UTF-8 [RFC3629] 编码的 Unicode 字符 [Unicode] 组成的字符串, 由以下语法定义. 该语法采用 [RFC4234] 定义的 ABNF 记法. 除非另有说明, 此处使用但未在此定义的产生式均定义于 [RFC4512] 第 1.4 节 (通用 ABNF 产生式). 过滤器格式采用前缀记法.
filter = LPAREN filtercomp RPAREN
filtercomp = and / or / not / item
and = AMPERSAND filterlist
or = VERTBAR filterlist
not = EXCLAMATION filter
filterlist = 1*filter
item = simple / present / substring / extensible
simple = attr filtertype assertionvalue
filtertype = equal / approx / greaterorequal / lessorequal
equal = EQUALS
approx = TILDE EQUALS
greaterorequal = RANGLE EQUALS
lessorequal = LANGLE EQUALS
extensible = ( attr [dnattrs]
[matchingrule] COLON EQUALS assertionvalue )
/ ( [dnattrs]
matchingrule COLON EQUALS assertionvalue )
present = attr EQUALS ASTERISK
substring = attr EQUALS [initial] any [final]
initial = assertionvalue
any = ASTERISK *(assertionvalue ASTERISK)
final = assertionvalue
attr = attributedescription
; The attributedescription rule is defined in
; Section 2.5 of [RFC4512].
dnattrs = COLON "dn"
matchingrule = COLON oid
assertionvalue = valueencoding
; The <valueencoding> rule is used to encode an <AssertionValue>
; from Section 4.1.6 of [RFC4511].
valueencoding = 0*(normal / escaped)
normal = UTF1SUBSET / UTFMB
escaped = ESC HEX HEX
UTF1SUBSET = %x01-27 / %x2B-5B / %x5D-7F
; UTF1SUBSET excludes 0x00 (NUL), LPAREN,
; RPAREN, ASTERISK, and ESC.
EXCLAMATION = %x21 ; exclamation mark ("!")
AMPERSAND = %x26 ; ampersand (or AND symbol) ("&")
ASTERISK = %x2A ; asterisk ("*")
COLON = %x3A ; colon (":")
VERTBAR = %x7C ; vertical bar (or pipe) ("|")
TILDE = %x7E ; tilde ("~")
请注意, 尽管上述语法中的 <substring> 和 <present> 产生式都可以生成 attr=* 结构, 但该结构仅用于表示存在性过滤器.
<valueencoding> 规则确保整个过滤器字符串是有效的 UTF-8 字符串, 并规定表示 ASCII 字符 * (ASCII 0x2a)、( (ASCII 0x28)、) (ASCII 0x29)、\ (ASCII 0x5c) 和 NUL (ASCII 0x00) 的八位组必须表示为反斜杠 \ (ASCII 0x5c), 后跟两个表示所编码八位组值的十六进制数字.
这种简单的转义机制消除了过滤器解析的歧义, 并允许任何可用 LDAP 表示的过滤器表示为以 NUL 结尾的字符串. <normal> 集合中的其他八位组也可以使用这种机制转义, 例如不可打印的 ASCII 字符.
对于包含 UTF-8 字符数据的 AssertionValue, 待转义字符的每个八位组都由一个反斜杠和两个十六进制数字替换, 它们在该字符的编码中构成单个八位组. 例如, 检查 cn 属性值中任意位置是否包含字符 * 的过滤器应表示为 (cn=*\2a*).
如 <valueencoding> 规则所示, 实现生成搜索过滤器的字符串表示时, MUST 转义所有大于 0x7F 且不属于有效 UTF-8 编码序列的八位组. 实现 SHOULD 接受并非有效 UTF-8 字符串的输入字符串. 这是必要的, 因为 RFC 2254 并未清晰定义术语 "字符串表示" (尤其没有提到 LDAP 搜索过滤器的字符串表示是由 UTF-8 编码的 Unicode 字符组成的字符串).