1- # LDAP DN support classes
2- #
1+ # -*- ruby encoding: utf-8 -*-
32
43##
5- # Objects of this class represent an LDAP DN.
6- #
7- # In LDAP-land, a DN ("Distinguished Name") is a unique identifier for an
8- # entry within an LDAP directory. It is made up of a number of other
9- # attributes strung together, to identify the entry in the tree.
4+ # Objects of this class represent an LDAP DN ("Distinguished Name"). A DN
5+ # ("Distinguished Name") is a unique identifier for an entry within an LDAP
6+ # directory. It is made up of a number of other attributes strung together,
7+ # to identify the entry in the tree.
108#
119# Each attribute that makes up a DN needs to have its value escaped so that
1210# the DN is valid. This class helps take care of that.
@@ -18,10 +16,10 @@ class Net::LDAP::DN
1816 # Initialize a DN, escaping as required. Pass in attributes in name/value
1917 # pairs. If there is a left over argument, it will be appended to the dn
2018 # without escaping (useful for a base string).
21- #
19+ #
2220 # Most uses of this class will be to escape a DN, rather than to parse it,
23- # so storing the dn as an escaped String and parsing parts as required with
24- # a state machine seems sensible.
21+ # so storing the dn as an escaped String and parsing parts as required
22+ # with a state machine seems sensible.
2523 def initialize ( *args )
2624 buffer = StringIO . new
2725
@@ -42,7 +40,6 @@ def initialize(*args)
4240 ##
4341 # Parse a DN into key value pairs using ASN from
4442 # http://tools.ietf.org/html/rfc2253 section 3.
45- #
4643 def each_pair
4744 state = :key
4845 key = StringIO . new
@@ -51,118 +48,126 @@ def each_pair
5148
5249 @dn . each_char do |char |
5350 case state
54-
55- when :key then case char
56- when 'a' ..'z' , 'A' ..'Z' then
57- state = :key_normal
58- key << char
59- when '0' ..'9' then
60- state = :key_oid
61- key << char
62- when ' ' then state = :key
63- else raise "DN badly formed"
51+ when :key then
52+ case char
53+ when 'a' ..'z' , 'A' ..'Z' then
54+ state = :key_normal
55+ key << char
56+ when '0' ..'9' then
57+ state = :key_oid
58+ key << char
59+ when ' ' then state = :key
60+ else raise "DN badly formed"
6461 end
65- when :key_normal then case char
66- when '=' then state = :value
67- when 'a' ..'z' , 'A' ..'Z' , '0' ..'9' , '-' , ' ' then key << char
68- else raise "DN badly formed"
62+ when :key_normal then
63+ case char
64+ when '=' then state = :value
65+ when 'a' ..'z' , 'A' ..'Z' , '0' ..'9' , '-' , ' ' then key << char
66+ else raise "DN badly formed"
6967 end
70- when :key_oid then case char
71- when '=' then state = :value
72- when '0' ..'9' , '.' , ' ' then key << char
73- else raise "DN badly formed"
68+ when :key_oid then
69+ case char
70+ when '=' then state = :value
71+ when '0' ..'9' , '.' , ' ' then key << char
72+ else raise "DN badly formed"
7473 end
75-
76- when :value then case char
77- when '\\' then state = :value_normal_escape
78- when '"' then state = :value_quoted
79- when ' ' then state = :value
80- when '#' then
81- state = :value_hexstring
82- value << char
83- when ',' then
84- state = :key
85- yield key . string . strip , value . string . rstrip
86- key = StringIO . new
87- value = StringIO . new ;
88- else
89- state = :value_normal
90- value << char
74+ when :value then
75+ case char
76+ when '\\' then state = :value_normal_escape
77+ when '"' then state = :value_quoted
78+ when ' ' then state = :value
79+ when '#' then
80+ state = :value_hexstring
81+ value << char
82+ when ',' then
83+ state = :key
84+ yield key . string . strip , value . string . rstrip
85+ key = StringIO . new
86+ value = StringIO . new ;
87+ else
88+ state = :value_normal
89+ value << char
9190 end
92-
93- when :value_normal then case char
94- when '\\' then state = :value_normal_escape
95- when ',' then
96- state = :key
97- yield key . string . strip , value . string . rstrip
98- key = StringIO . new
99- value = StringIO . new ;
100- else value << char
91+ when :value_normal then
92+ case char
93+ when '\\' then state = :value_normal_escape
94+ when ',' then
95+ state = :key
96+ yield key . string . strip , value . string . rstrip
97+ key = StringIO . new
98+ value = StringIO . new ;
99+ else value << char
101100 end
102- when :value_normal_escape then case char
103- when '0' ..'9' , 'a' ..'f' , 'A' ..'F' then
104- state = :value_normal_escape_hex
105- hex_buffer = char
106- else state = :value_normal ; value << char
101+ when :value_normal_escape then
102+ case char
103+ when '0' ..'9' , 'a' ..'f' , 'A' ..'F' then
104+ state = :value_normal_escape_hex
105+ hex_buffer = char
106+ else state = :value_normal ; value << char
107107 end
108- when :value_normal_escape_hex then case char
109- when '0' ..'9' , 'a' ..'f' , 'A' ..'F' then
110- state = :value_normal
111- value << "#{ hex_buffer } #{ char } " . to_i ( 16 ) . chr
112- else raise "DN badly formed"
108+ when :value_normal_escape_hex then
109+ case char
110+ when '0' ..'9' , 'a' ..'f' , 'A' ..'F' then
111+ state = :value_normal
112+ value << "#{ hex_buffer } #{ char } " . to_i ( 16 ) . chr
113+ else raise "DN badly formed"
113114 end
114-
115- when :value_quoted then case char
116- when '\\' then state = :value_quoted_escape
117- when '"' then state = :value_end
118- else value << char
115+ when :value_quoted then
116+ case char
117+ when '\\' then state = :value_quoted_escape
118+ when '"' then state = :value_end
119+ else value << char
119120 end
120- when :value_quoted_escape then case char
121- when '0' ..'9' , 'a' ..'f' , 'A' ..'F' then
122- state = :value_quoted_escape_hex
123- hex_buffer = char
124- else state = :value_quoted ; value << char
121+ when :value_quoted_escape then
122+ case char
123+ when '0' ..'9' , 'a' ..'f' , 'A' ..'F' then
124+ state = :value_quoted_escape_hex
125+ hex_buffer = char
126+ else
127+ state = :value_quoted ;
128+ value << char
125129 end
126- when :value_quoted_escape_hex then case char
127- when '0' ..'9' , 'a' ..'f' , 'A' ..'F' then
128- state = :value_quoted
129- value << "#{ hex_buffer } #{ char } " . to_i ( 16 ) . chr
130- else raise "DN badly formed"
130+ when :value_quoted_escape_hex then
131+ case char
132+ when '0' ..'9' , 'a' ..'f' , 'A' ..'F' then
133+ state = :value_quoted
134+ value << "#{ hex_buffer } #{ char } " . to_i ( 16 ) . chr
135+ else raise "DN badly formed"
131136 end
132-
133- when :value_hexstring then case char
134- when '0' ..'9' , 'a' ..'f' , 'A' ..'F' then
135- state = :value_hexstring_hex
136- value << char
137- when ' ' then state = :value_end
138- when ',' then
139- state = :key
140- yield key . string . strip , value . string . rstrip
141- key = StringIO . new
142- value = StringIO . new ;
143- else raise "DN badly formed"
137+ when :value_hexstring then
138+ case char
139+ when '0' ..'9' , 'a' ..'f' , 'A' ..'F' then
140+ state = :value_hexstring_hex
141+ value << char
142+ when ' ' then state = :value_end
143+ when ',' then
144+ state = :key
145+ yield key . string . strip , value . string . rstrip
146+ key = StringIO . new
147+ value = StringIO . new ;
148+ else raise "DN badly formed"
144149 end
145- when :value_hexstring_hex then case char
146- when '0' ..'9' , 'a' ..'f' , 'A' ..'F' then
147- state = :value_hexstring
148- value << char
149- else raise "DN badly formed"
150+ when :value_hexstring_hex then
151+ case char
152+ when '0' ..'9' , 'a' ..'f' , 'A' ..'F' then
153+ state = :value_hexstring
154+ value << char
155+ else raise "DN badly formed"
150156 end
151-
152- when :value_end then case char
153- when ' ' then state = :value_end
154- when ',' then
155- state = :key
156- yield key . string . strip , value . string . rstrip
157- key = StringIO . new
158- value = StringIO . new ;
159- else raise "DN badly formed"
157+ when :value_end then
158+ case char
159+ when ' ' then state = :value_end
160+ when ',' then
161+ state = :key
162+ yield key . string . strip , value . string . rstrip
163+ key = StringIO . new
164+ value = StringIO . new ;
165+ else raise "DN badly formed"
160166 end
161-
162- else raise "Fell out of state machine"
167+ else raise "Fell out of state machine"
163168 end
164169 end
165-
170+
166171 # Last pair
167172 if [ :value , :value_normal , :value_hexstring , :value_end ] . include? state
168173 yield key . string . strip , value . string . rstrip
@@ -186,9 +191,8 @@ def to_s
186191 end
187192
188193 # http://tools.ietf.org/html/rfc2253 section 2.4 lists these exceptions
189- # for dn values. All of the following must be escaped in any normal
190- # string using a single backslash ('\') as escape.
191- #
194+ # for dn values. All of the following must be escaped in any normal string
195+ # using a single backslash ('\') as escape.
192196 ESCAPES = {
193197 ',' => ',' ,
194198 '+' => '+' ,
@@ -198,13 +202,13 @@ def to_s
198202 '>' => '>' ,
199203 ';' => ';' ,
200204 }
205+
201206 # Compiled character class regexp using the keys from the above hash, and
202207 # checking for a space or # at the start, or space at the end, of the
203208 # string.
204- ESCAPE_RE = Regexp . new (
205- "(^ |^#| $|[" +
206- ESCAPES . keys . map { |e | Regexp . escape ( e ) } . join +
207- "])" )
209+ ESCAPE_RE = Regexp . new ( "(^ |^#| $|[" +
210+ ESCAPES . keys . map { |e | Regexp . escape ( e ) } . join +
211+ "])" )
208212
209213 ##
210214 # Escape a string for use in a DN value
0 commit comments