File: syntaxes.rb

package info (click to toggle)
ruby-activeldap 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,588 kB
  • sloc: ruby: 18,143; sh: 12; makefile: 5
file content (477 lines) | stat: -rw-r--r-- 13,025 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
module ActiveLdap
  class Schema
    module Syntaxes
      class << self
        def [](id)
          syntax = Base::SYNTAXES[id]
          if syntax
            syntax.new
          else
            nil
          end
        end
      end

      class Base
        include GetTextSupport
        SYNTAXES = {}

        printable_character_source = "a-zA-Z\\d\"()+,\\-.\\/:? "
        PRINTABLE_CHARACTER = /[#{printable_character_source}]/ #
        UNPRINTABLE_CHARACTER = /[^#{printable_character_source}]/ #

        def binary?
          false
        end

        def type_cast(value)
          value
        end

        def valid?(value)
          validate(value).nil?
        end

        def validate(value)
          validate_normalized_value(normalize_value(value), value)
        end

        def normalize_value(value)
          value
        end
      end

      class BitString < Base
        SYNTAXES["1.3.6.1.4.1.1466.115.121.1.6"] = self

        def type_cast(value)
          return nil if value.nil?
          if /\A'([01]*)'B\z/ =~ value.to_s
            $1
          else
            value
          end
        end

        def normalize_value(value)
          if value.is_a?(String) and /\A[01]*\z/ =~ value
            "'#{value}'B"
          else
            value
          end
        end

        private
        def validate_normalized_value(value, original_value)
          if /\A'/ !~ value
            return _("%s doesn't have the first \"'\"") % original_value.inspect
          end

          if /'B\z/ !~ value
            return _("%s doesn't have the last \"'B\"") % original_value.inspect
          end

          if /([^01])/ =~ value[1..-3]
            return _("%s has invalid character '%s'") % [value.inspect, $1]
          end

          nil
        end
      end

      class Boolean < Base
        SYNTAXES["1.3.6.1.4.1.1466.115.121.1.7"] = self

        def type_cast(value)
          case value
          when "TRUE"
            true
          when "FALSE"
            false
          else
            value
          end
        end

        def normalize_value(value)
          case value
          when true, "1"
            "TRUE"
          when false, "0"
            "FALSE"
          else
            value
          end
        end

        private
        def validate_normalized_value(value, original_value)
          if %w(TRUE FALSE).include?(value)
            nil
          else
            _("%s should be TRUE or FALSE") % original_value.inspect
          end
        end
      end

      class CountryString < Base
        SYNTAXES["1.3.6.1.4.1.1466.115.121.1.11"] = self

        private
        def validate_normalized_value(value, original_value)
          if /\A#{PRINTABLE_CHARACTER}{2,2}\z/i =~ value
            nil
          else
            format = _("%s should be just 2 printable characters")
            format % original_value.inspect
          end
        end
      end

      class DistinguishedName < Base
        SYNTAXES["1.3.6.1.4.1.1466.115.121.1.12"] = self

        def type_cast(value)
          return nil if value.nil?
          DN.parse(value)
        rescue DistinguishedNameInvalid
          value
        end

        def normalize_value(value)
          if value.is_a?(DN)
            value.to_s
          else
            value
          end
        end

        private
        def validate_normalized_value(value, original_value)
          DN.parse(value)
          nil
        rescue DistinguishedNameInvalid
          $!.message
        end
      end

      class DirectoryString < Base
        SYNTAXES["1.3.6.1.4.1.1466.115.121.1.15"] = self

        private
        def validate_normalized_value(value, original_value)
          value.unpack("U*")
          nil
        rescue ArgumentError
          _("%s has invalid UTF-8 character") % original_value.inspect
        end
      end

      class GeneralizedTime < Base
        SYNTAXES["1.3.6.1.4.1.1466.115.121.1.24"] = self
        FORMAT = /\A
                  (\d{4,4})?
                  (\d{2,2})?
                  (\d{2,2})?
                  (\d{2,2})?
                  (\d{2,2})?
                  (\d{2,2})?
                  ([,.]\d+)?
                  ([+-]\d{4,4}|Z)?
                 \z/x

        def type_cast(value)
          return value if value.nil? or value.is_a?(Time)
          match_data = FORMAT.match(value)
          if match_data
            required_components = match_data.to_a[1, 5]
            return value if required_components.any?(&:nil?)
            year, month, day, hour, minute = required_components.collect(&:to_i)
            second = match_data[-3].to_i
            fraction = match_data[-2]
            fraction = fraction.to_f if fraction
            time_zone = match_data[-1]
            arguments = [
              value, year, month, day, hour, minute, second, fraction, time_zone,
              Time.now,
            ]
            if Time.method(:make_time).arity == 11
              arguments[2, 0] = nil
            end
            begin
              Time.send(:make_time, *arguments)
            rescue ArgumentError
              raise if year >= 1700
              out_of_range_messages = ["argument out of range",
                                       "time out of range"]
              raise unless out_of_range_messages.include?($!.message)
              Time.at(0)
            rescue RangeError
              raise if year >= 1700
              raise if $!.message != "bignum too big to convert into `long'"
              Time.at(0)
            end
          else
            value
          end
        end

        def normalize_value(value)
          if value.is_a?(Time)
            normalized_value = value.strftime("%Y%m%d%H%M%S")
            if value.gmt?
              normalized_value + "Z"
            else
              # for timezones with non-zero minutes, such as IST which is +0530,
              # divmod(3600) will give wrong value of 1800

              offset = value.gmtoff / 60 # in minutes
              normalized_value + ("%+03d%02d" % offset.divmod(60))
            end
          else
            value
          end
        end

        private
        def validate_normalized_value(value, original_value)
          match_data = FORMAT.match(value)
          if match_data
            date_data = match_data.to_a[1..-1]
            missing_components = []
            required_components = %w(year month day hour minute)
            required_components.each_with_index do |component, i|
              missing_components << component unless date_data[i]
            end
            if missing_components.empty?
              nil
            else
              params = [original_value.inspect, missing_components.join(", ")]
              _("%s has missing components: %s") % params
            end
          else
            _("%s is invalid time format") % original_value.inspect
          end
        end
      end

      class Integer < Base
        SYNTAXES["1.3.6.1.4.1.1466.115.121.1.27"] = self

        def type_cast(value)
          return value if value.nil?
          begin
            Integer(value)
          rescue ArgumentError
            value
          end
        end

        def normalize_value(value)
          if value.is_a?(::Integer)
            value.to_s
          else
            value
          end
        end

        private
        def validate_normalized_value(value, original_value)
          Integer(value)
          nil
        rescue ArgumentError
          _("%s is invalid integer format") % original_value.inspect
        end
      end

      class JPEG < Base
        SYNTAXES["1.3.6.1.4.1.1466.115.121.1.28"] = self

        def binary?
          true
        end

        private
        def validate_normalized_value(value, original_value)
          if value.unpack("n")[0] == 0xffd8
            nil
          else
            _("invalid JPEG format")
          end
        end
      end

      class NameAndOptionalUID < Base
        SYNTAXES["1.3.6.1.4.1.1466.115.121.1.34"] = self

        private
        def validate_normalized_value(value, original_value)
          separator_index = value.rindex("#")
          if separator_index
            dn = value[0, separator_index]
            bit_string = value[(separator_index + 1)..-1]
            bit_string_reason = BitString.new.validate(bit_string)
            dn_reason = DistinguishedName.new.validate(dn)
            if bit_string_reason
              if dn_reason
                value_reason = DistinguishedName.new.validate(value)
                return nil unless value_reason
                dn_reason
              else
                bit_string_reason
              end
            else
              dn_reason
            end
          else
            DistinguishedName.new.validate(value)
          end
        end
      end

      class NumericString < Base
        SYNTAXES["1.3.6.1.4.1.1466.115.121.1.36"] = self

        private
        def validate_normalized_value(value, original_value)
          if /\A\d+\z/ =~ value
            nil
          else
            _("%s is invalid numeric format") % original_value.inspect
          end
        end
      end

      class OID < Base
        SYNTAXES["1.3.6.1.4.1.1466.115.121.1.38"] = self

        private
        def validate_normalized_value(value, original_value)
          DN.parse("#{value}=dummy")
          nil
        rescue DistinguishedNameInvalid
          reason = $!.reason
          if reason
            _("%s is invalid OID format: %s") % [original_value.inspect, reason]
          else
            _("%s is invalid OID format") % original_value.inspect
          end
        end
      end

      class OtherMailbox < Base
        SYNTAXES["1.3.6.1.4.1.1466.115.121.1.39"] = self

        private
        def validate_normalized_value(value, original_value)
          type, mailbox = value.split('$', 2)

          if type.empty?
            return _("%s has no mailbox type") % original_value.inspect
          end

          if /(#{UNPRINTABLE_CHARACTER})/i =~ type
            format = _("%s has unprintable character in mailbox type: '%s'")
            return format % [original_value.inspect, $1]
          end

          if mailbox.blank?
            return _("%s has no mailbox") % original_value.inspect
          end

          nil
        end
      end

      class OctetString < Base
        SYNTAXES["1.3.6.1.4.1.1466.115.121.1.40"] = self

        def binary?
          true
        end

        private
        def validate_normalized_value(value, original_value)
          nil
        end
      end

      class PostalAddress < Base
        SYNTAXES["1.3.6.1.4.1.1466.115.121.1.41"] = self

        private
        def validate_normalized_value(value, original_value)
          if value.blank?
            return _("empty string")
          end

          begin
            value.unpack("U*")
          rescue ArgumentError
            return _("%s has invalid UTF-8 character") % original_value.inspect
          end

          nil
        end
      end

      class PrintableString < Base
        SYNTAXES["1.3.6.1.4.1.1466.115.121.1.44"] = self

        private
        def validate_normalized_value(value, original_value)
          if value.blank?
            return _("empty string")
          end

          if /(#{UNPRINTABLE_CHARACTER})/i =~ value
            format = _("%s has unprintable character: '%s'")
            return format % [original_value.inspect, $1]
          end

          nil
        end
      end

      class TelephoneNumber < PrintableString
        SYNTAXES["1.3.6.1.4.1.1466.115.121.1.50"] = self

        private
        def validate_normalized_value(value, original_value)
          return nil if value.blank?
          super
        end
      end

      class ObjectSecurityDescriptor < OctetString
        # @see http://tools.ietf.org/html/draft-armijo-ldap-syntax-00
        #   Object-Security-Descriptor: 1.2.840.113556.1.4.907
        #
        #   Encoded as an Octet-String (OID 1.3.6.1.4.1.1466.115.121.1.40)
        #
        # @see http://msdn.microsoft.com/en-us/library/cc223229.aspx
        #   String(NT-Sec-Desc) 1.2.840.113556.1.4.907
        SYNTAXES["1.2.840.113556.1.4.907"] = self
      end

      class UnicodePwd < OctetString
        # @see http://msdn.microsoft.com/en-us/library/cc220961.aspx
        #   cn: Unicode-Pwd
        #   ldapDisplayName: unicodePwd
        #   attributeId: 1.2.840.113556.1.4.90
        #   attributeSyntax: 2.5.5.10
        #   omSyntax: 4
        #   isSingleValued: TRUE
        #   schemaIdGuid: bf9679e1-0de6-11d0-a285-00aa003049e2
        #   systemOnly: FALSE
        #   searchFlags: 0
        #   systemFlags: FLAG_SCHEMA_BASE_OBJECT
        #   schemaFlagsEx: FLAG_ATTR_IS_CRITICAL
        #
        # @see http://msdn.microsoft.com/en-us/library/cc223177.aspx
        #   String(Octet) 2.5.5.10
        SYNTAXES["1.2.840.113556.1.4.90"] = self
      end
    end
  end
end