line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyright (c) 1997-2004 Graham Barr . All rights reserved. |
2
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or |
3
|
|
|
|
|
|
|
# modify it under the same terms as Perl itself. |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Net::LDAP::Filter; |
6
|
|
|
|
|
|
|
|
7
|
4
|
|
|
4
|
|
65015
|
use strict; |
|
4
|
|
|
|
|
15
|
|
|
4
|
|
|
|
|
3841
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.20'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# filter = "(" filtercomp ")" |
12
|
|
|
|
|
|
|
# filtercomp = and / or / not / item |
13
|
|
|
|
|
|
|
# and = "&" filterlist |
14
|
|
|
|
|
|
|
# or = "|" filterlist |
15
|
|
|
|
|
|
|
# not = "!" filter |
16
|
|
|
|
|
|
|
# filterlist = 1*filter |
17
|
|
|
|
|
|
|
# item = simple / present / substring / extensible |
18
|
|
|
|
|
|
|
# simple = attr filtertype value |
19
|
|
|
|
|
|
|
# filtertype = equal / approx / greater / less |
20
|
|
|
|
|
|
|
# equal = "=" |
21
|
|
|
|
|
|
|
# approx = "~=" |
22
|
|
|
|
|
|
|
# greater = ">=" |
23
|
|
|
|
|
|
|
# less = "<=" |
24
|
|
|
|
|
|
|
# extensible = attr [":dn"] [":" matchingrule] ":=" value |
25
|
|
|
|
|
|
|
# / [":dn"] ":" matchingrule ":=" value |
26
|
|
|
|
|
|
|
# present = attr "=*" |
27
|
|
|
|
|
|
|
# substring = attr "=" [initial] any [final] |
28
|
|
|
|
|
|
|
# initial = value |
29
|
|
|
|
|
|
|
# any = "*" *(value "*") |
30
|
|
|
|
|
|
|
# final = value |
31
|
|
|
|
|
|
|
# attr = AttributeDescription from Section 2.5 of RFC 4512 |
32
|
|
|
|
|
|
|
# matchingrule = MatchingRuleId from Section 4.1.8 of RFC 4511 |
33
|
|
|
|
|
|
|
# value = AttributeValue from Section 4.1.6 of RFC 4511 |
34
|
|
|
|
|
|
|
# with some characters encoded, see below. |
35
|
|
|
|
|
|
|
# |
36
|
|
|
|
|
|
|
# Special Character encodings |
37
|
|
|
|
|
|
|
# --------------------------- |
38
|
|
|
|
|
|
|
# * \2a, \* |
39
|
|
|
|
|
|
|
# ( \28, \( |
40
|
|
|
|
|
|
|
# ) \29, \) |
41
|
|
|
|
|
|
|
# \ \5c, \\ |
42
|
|
|
|
|
|
|
# NUL \00 |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my $ErrStr; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub new { |
47
|
142
|
|
|
142
|
1
|
55122
|
my $self = shift; |
48
|
142
|
|
33
|
|
|
483
|
my $class = ref($self) || $self; |
49
|
|
|
|
|
|
|
|
50
|
142
|
|
|
|
|
250
|
my $me = bless {}, $class; |
51
|
|
|
|
|
|
|
|
52
|
142
|
50
|
|
|
|
273
|
if (@_) { |
53
|
142
|
50
|
|
|
|
290
|
$me->parse(shift) or |
54
|
|
|
|
|
|
|
return undef; |
55
|
|
|
|
|
|
|
} |
56
|
142
|
|
|
|
|
282
|
$me; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
my $Attr = '[-;.:\d\w]*[-;\d\w]'; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my %Op = qw( |
62
|
|
|
|
|
|
|
& and |
63
|
|
|
|
|
|
|
| or |
64
|
|
|
|
|
|
|
! not |
65
|
|
|
|
|
|
|
= equalityMatch |
66
|
|
|
|
|
|
|
~= approxMatch |
67
|
|
|
|
|
|
|
>= greaterOrEqual |
68
|
|
|
|
|
|
|
<= lessOrEqual |
69
|
|
|
|
|
|
|
:= extensibleMatch |
70
|
|
|
|
|
|
|
); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
my %Rop = reverse %Op; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
0
|
0
|
0
|
sub errstr { $ErrStr } |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# Unescape |
78
|
|
|
|
|
|
|
# \xx where xx is a 2-digit hex number |
79
|
|
|
|
|
|
|
# \y where y is one of ( ) \ * |
80
|
|
|
|
|
|
|
sub _unescape { |
81
|
225
|
|
|
225
|
|
335
|
$_[0] =~ s/ |
82
|
|
|
|
|
|
|
\\([\da-fA-F]{2}|[()\\*]) |
83
|
|
|
|
|
|
|
/ |
84
|
20
|
100
|
|
|
|
77
|
length($1) == 1 |
85
|
|
|
|
|
|
|
? $1 |
86
|
|
|
|
|
|
|
: chr(hex($1)) |
87
|
|
|
|
|
|
|
/soxeg; |
88
|
225
|
|
|
|
|
714
|
$_[0]; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
168
|
|
|
168
|
|
332
|
sub _escape { (my $t = $_[0]) =~ s/([\\\(\)\*\0-\37\177-\377])/sprintf('\\%02x', ord($1))/sge; $t } |
|
23
|
|
|
|
|
94
|
|
|
168
|
|
|
|
|
505
|
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# encode a triplet ($attr,$op,$val) representing a single filter item |
94
|
|
|
|
|
|
|
sub _encode { |
95
|
206
|
|
|
206
|
|
606
|
my($attr, $op, $val) = @_; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# extensible match |
98
|
206
|
100
|
|
|
|
410
|
if ($op eq ':=') { |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# attr must be in the form type:dn:1.2.3.4 |
101
|
21
|
50
|
|
|
|
96
|
unless ($attr =~ /^([-;\d\w]*)(:dn)?(:(\w+|[.\d]+))?$/) { |
102
|
0
|
|
|
|
|
0
|
$ErrStr = "Bad attribute $attr"; |
103
|
0
|
|
|
|
|
0
|
return undef; |
104
|
|
|
|
|
|
|
} |
105
|
21
|
|
|
|
|
64
|
my($type, $dn, $rule) = ($1, $2, $4); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
return ( { |
108
|
21
|
100
|
|
|
|
55
|
extensibleMatch => { |
|
|
100
|
|
|
|
|
|
109
|
|
|
|
|
|
|
matchingRule => $rule, |
110
|
|
|
|
|
|
|
type => length($type) ? $type : undef, |
111
|
|
|
|
|
|
|
matchValue => _unescape($val), |
112
|
|
|
|
|
|
|
dnAttributes => $dn ? 1 : undef |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
}); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# special cases: present / substring match |
118
|
185
|
100
|
|
|
|
265
|
if ($op eq '=') { |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# present match |
121
|
159
|
100
|
|
|
|
387
|
if ($val eq '*') { |
|
|
100
|
|
|
|
|
|
122
|
10
|
|
|
|
|
32
|
return ({ present => $attr }); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# if val contains unescaped *, then we have substring match |
126
|
|
|
|
|
|
|
elsif ( $val =~ /^(\\.|[^\\*]+)*\*/o ) { |
127
|
|
|
|
|
|
|
|
128
|
33
|
|
|
|
|
50
|
my $n = []; |
129
|
33
|
|
|
|
|
46
|
my $type = 'initial'; |
130
|
|
|
|
|
|
|
|
131
|
33
|
|
|
|
|
130
|
while ($val =~ s/^((\\.|[^\\*]+)*)\*//) { |
132
|
57
|
100
|
100
|
|
|
211
|
push(@$n, { $type, _unescape("$1") }) # $1 is readonly, copy it |
133
|
|
|
|
|
|
|
if length($1) or $type eq 'any'; |
134
|
|
|
|
|
|
|
|
135
|
57
|
|
|
|
|
166
|
$type = 'any'; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
33
|
100
|
|
|
|
65
|
push(@$n, { 'final', _unescape($val) }) |
139
|
|
|
|
|
|
|
if length $val; |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
return ({ |
142
|
33
|
|
|
|
|
116
|
substrings => { |
143
|
|
|
|
|
|
|
type => $attr, |
144
|
|
|
|
|
|
|
substrings => $n |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
}); |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# in all other cases we must have an operator and no un-escaped *'s on the RHS |
151
|
|
|
|
|
|
|
return { |
152
|
142
|
|
|
|
|
256
|
$Op{$op} => { |
153
|
|
|
|
|
|
|
attributeDesc => $attr, assertionValue => _unescape($val) |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
}; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# parse & encode a filter string |
159
|
|
|
|
|
|
|
sub parse { |
160
|
142
|
|
|
142
|
1
|
193
|
my $self = shift; |
161
|
142
|
|
|
|
|
179
|
my $filter = shift; |
162
|
|
|
|
|
|
|
|
163
|
142
|
|
|
|
|
197
|
my @stack = (); # stack |
164
|
142
|
|
|
|
|
177
|
my $cur = []; |
165
|
142
|
|
|
|
|
157
|
my $op; |
166
|
|
|
|
|
|
|
|
167
|
142
|
|
|
|
|
194
|
undef $ErrStr; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
# a filter is required |
170
|
142
|
50
|
|
|
|
257
|
if (!defined $filter) { |
171
|
0
|
|
|
|
|
0
|
$ErrStr = 'Undefined filter'; |
172
|
0
|
|
|
|
|
0
|
return undef; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
# Algorithm depends on /^\(/; |
176
|
142
|
|
|
|
|
498
|
$filter =~ s/^\s*//; |
177
|
|
|
|
|
|
|
|
178
|
142
|
100
|
|
|
|
378
|
$filter = '(' . $filter . ')' |
179
|
|
|
|
|
|
|
unless $filter =~ /^\(/; |
180
|
|
|
|
|
|
|
|
181
|
142
|
|
|
|
|
289
|
while (length($filter)) { |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
# Process the start of ( (...)(...)), with = [&!|] |
184
|
|
|
|
|
|
|
|
185
|
346
|
100
|
|
|
|
2066
|
if ($filter =~ s/^\(\s*([&!|])\s*//) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
186
|
70
|
|
|
|
|
126
|
push @stack, [$op, $cur]; |
187
|
70
|
|
|
|
|
107
|
$op = $1; |
188
|
70
|
|
|
|
|
88
|
$cur = []; |
189
|
70
|
|
|
|
|
119
|
next; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
# Process the end of ( (...)(...)), with = [&!|] |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
elsif ($filter =~ s/^\)\s*//o) { |
195
|
70
|
50
|
|
|
|
125
|
unless (@stack) { |
196
|
0
|
|
|
|
|
0
|
$ErrStr = 'Bad filter, unmatched )'; |
197
|
0
|
|
|
|
|
0
|
return undef; |
198
|
|
|
|
|
|
|
} |
199
|
70
|
|
|
|
|
105
|
my($myop, $mydata) = ($op, $cur); |
200
|
70
|
|
|
|
|
985
|
($op, $cur) = @{ pop @stack }; |
|
70
|
|
|
|
|
111
|
|
201
|
|
|
|
|
|
|
# Need to do more checking here |
202
|
70
|
100
|
|
|
|
194
|
push @$cur, { $Op{$myop} => $myop eq '!' ? $mydata->[0] : $mydata }; |
203
|
70
|
100
|
|
|
|
153
|
next if @stack; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
# process (attr op string) |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
elsif ($filter =~ s/^\(\s* |
209
|
|
|
|
|
|
|
($Attr)\s* |
210
|
|
|
|
|
|
|
([:~<>]?=) |
211
|
|
|
|
|
|
|
((?:\\.|[^\\()]+)*) |
212
|
|
|
|
|
|
|
\)\s* |
213
|
|
|
|
|
|
|
//xo) { |
214
|
206
|
|
|
|
|
404
|
push(@$cur, _encode($1, $2, $3)); |
215
|
206
|
100
|
|
|
|
424
|
next if @stack; |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
# If we get here then there is an error in the filter string |
219
|
|
|
|
|
|
|
# so exit loop with data in $filter |
220
|
142
|
|
|
|
|
188
|
last; |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
|
223
|
142
|
50
|
|
|
|
221
|
if (length $filter) { |
224
|
|
|
|
|
|
|
# If we have anything left in the filter, then there is a problem |
225
|
0
|
|
|
|
|
0
|
$ErrStr = 'Bad filter, error before ' . substr($filter, 0, 20); |
226
|
0
|
|
|
|
|
0
|
return undef; |
227
|
|
|
|
|
|
|
} |
228
|
142
|
50
|
|
|
|
188
|
if (@stack) { |
229
|
0
|
|
|
|
|
0
|
$ErrStr = 'Bad filter, unmatched ('; |
230
|
0
|
|
|
|
|
0
|
return undef; |
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
142
|
|
|
|
|
151
|
%$self = %{$cur->[0]}; |
|
142
|
|
|
|
|
410
|
|
234
|
|
|
|
|
|
|
|
235
|
142
|
|
|
|
|
417
|
$self; |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
sub print { |
239
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
240
|
4
|
|
|
4
|
|
27
|
no strict 'refs'; # select may return a GLOB name |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
3020
|
|
241
|
0
|
0
|
|
|
|
0
|
my $fh = @_ ? shift : select; |
242
|
|
|
|
|
|
|
|
243
|
0
|
|
|
|
|
0
|
print $fh $self->as_string, "\n"; |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
|
246
|
92
|
|
|
92
|
1
|
107511
|
sub as_string { _string(%{$_[0]}) } |
|
92
|
|
|
|
|
276
|
|
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
sub _string { # prints things of the form ( () ... ) |
249
|
223
|
|
|
223
|
|
269
|
my $str = ''; |
250
|
|
|
|
|
|
|
|
251
|
223
|
|
|
|
|
314
|
for ($_[0]) { |
252
|
223
|
100
|
|
|
|
470
|
/^and/ and return '(&' . join('', map { _string(%$_) } @{$_[1]}) . ')'; |
|
65
|
|
|
|
|
135
|
|
|
30
|
|
|
|
|
52
|
|
253
|
193
|
100
|
|
|
|
317
|
/^or/ and return '(|' . join('', map { _string(%$_) } @{$_[1]}) . ')'; |
|
49
|
|
|
|
|
103
|
|
|
20
|
|
|
|
|
39
|
|
254
|
173
|
100
|
|
|
|
288
|
/^not/ and return '(!' . _string(%{$_[1]}) . ')'; |
|
17
|
|
|
|
|
41
|
|
255
|
156
|
100
|
|
|
|
261
|
/^present/ and return "($_[1]=*)"; |
256
|
|
|
|
|
|
|
/^(equalityMatch|greaterOrEqual|lessOrEqual|approxMatch)/ |
257
|
147
|
100
|
|
|
|
554
|
and return '(' . $_[1]->{attributeDesc} . $Rop{$1} . _escape($_[1]->{assertionValue}) .')'; |
258
|
36
|
100
|
|
|
|
102
|
/^substrings/ and do { |
259
|
22
|
|
|
|
|
32
|
my $str = join('*', '', map { _escape($_) } map { values %$_ } @{$_[1]->{substrings}}); |
|
43
|
|
|
|
|
67
|
|
|
43
|
|
|
|
|
96
|
|
|
22
|
|
|
|
|
41
|
|
260
|
22
|
100
|
|
|
|
83
|
$str =~ s/^.// if exists $_[1]->{substrings}[0]{initial}; |
261
|
22
|
100
|
|
|
|
52
|
$str .= '*' unless exists $_[1]->{substrings}[-1]{final}; |
262
|
22
|
|
|
|
|
73
|
return "($_[1]->{type}=$str)"; |
263
|
|
|
|
|
|
|
}; |
264
|
14
|
50
|
|
|
|
41
|
/^extensibleMatch/ and do { |
265
|
14
|
|
|
|
|
23
|
my $str = '('; |
266
|
14
|
100
|
|
|
|
37
|
$str .= $_[1]->{type} if defined $_[1]->{type}; |
267
|
14
|
100
|
|
|
|
30
|
$str .= ':dn' if $_[1]->{dnAttributes}; |
268
|
14
|
100
|
|
|
|
32
|
$str .= ":$_[1]->{matchingRule}" if defined $_[1]->{matchingRule}; |
269
|
14
|
|
|
|
|
28
|
$str .= ':=' . _escape($_[1]->{matchValue}) . ')'; |
270
|
14
|
|
|
|
|
37
|
return $str; |
271
|
|
|
|
|
|
|
}; |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
|
274
|
0
|
|
|
|
|
|
die "Internal error $_[0]"; |
275
|
|
|
|
|
|
|
} |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
sub negate { |
278
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
279
|
|
|
|
|
|
|
|
280
|
0
|
|
|
|
|
|
%{$self} = _negate(%{$self}); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
|
282
|
0
|
|
|
|
|
|
$self; |
283
|
|
|
|
|
|
|
} |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
sub _negate { # negate a filter tree |
286
|
0
|
|
|
0
|
|
|
for ($_[0]) { |
287
|
0
|
0
|
|
|
|
|
/^and/ and return ( 'or' => [ map { { _negate(%$_) }; } @{$_[1]} ] ); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
288
|
0
|
0
|
|
|
|
|
/^or/ and return ( 'and' => [ map { { _negate(%$_) }; } @{$_[1]} ] ); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
289
|
0
|
0
|
|
|
|
|
/^not/ and return %{$_[1]}; |
|
0
|
|
|
|
|
|
|
290
|
0
|
0
|
|
|
|
|
/^(present|equalityMatch|greaterOrEqual|lessOrEqual|approxMatch|substrings|extensibleMatch)/ |
291
|
|
|
|
|
|
|
and do return ( 'not' => { $_[0 ], $_[1] } ); |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
|
294
|
0
|
|
|
|
|
|
die "Internal error $_[0]"; |
295
|
|
|
|
|
|
|
} |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
1; |