File: ldif.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 (936 lines) | stat: -rw-r--r-- 24,789 bytes parent folder | download | duplicates (3)
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
require "strscan"
require "uri"
require "open-uri"

module ActiveLdap
  class Ldif
    module Attributes
      module_function
      def encode(attributes)
        return "" if attributes.empty?

        result = ""
        normalize(attributes).sort_by {|name,| name}.each do |name, values|
          values.each do |options, value|
            result << Attribute.encode([name, *options].join(";"), value)
          end
        end
        result
      end

      def normalize(attributes)
        result = {}
        attributes.each do |name, values|
          result[name] = Attribute.normalize_value(values).sort
        end
        result
      end
    end

    module Attribute
      SIZE = 75

      module_function
      def binary_value?(value)
        if value.respond_to?(:encoding)
          return true if value.encoding == Encoding.find("ascii-8bit")
        end
        if /\A#{Parser::SAFE_STRING}\z/ =~ value
          false
        else
          true
        end
      end

      def encode(name, value)
        return "#{name}:\n" if value.blank?
        result = "#{name}:"

        value = value.to_s unless value.is_a?(String)
        if value[-1, 1] == ' ' or binary_value?(value)
          result << ":"
          value = [value].pack("m").gsub(/\n/, '')
        end
        result << " "

        first_line_value_size = SIZE - result.size
        if value.size > first_line_value_size
          first_line_value = value[0, first_line_value_size]
          rest_value = value[first_line_value_size..-1]
        else
          first_line_value = value
          rest_value = nil
        end

        result << "#{first_line_value}\n"
        return result if rest_value.nil?

        rest_value.scan(/.{1,#{SIZE - 1}}/).each do |line| # FIXME
          result << " #{line}\n"
        end
        result
      end

      def normalize_value(value, result=[])
        case value
        when Array
          value.each {|val| normalize_value(val, result)}
        when Hash
          value.each do |option, val|
            normalize_value(val).each do |options, v|
              result << [[option] + options, v]
            end
          end
          result
        else
          result << [[], value]
        end
        result
      end
    end

    class Parser
      include GetTextSupport

      attr_reader :ldif
      def initialize(source)
        @ldif = nil
        source = source.to_s if source.is_a?(LDIF)
        @source = source
      end

      ATTRIBUTE_TYPE_CHARS = /[a-zA-Z][a-zA-Z0-9\-]*/
      SAFE_CHAR = /[\x01-\x09\x0B-\x0C\x0E-\x7F]/
      SAFE_INIT_CHAR = /[\x01-\x09\x0B-\x0C\x0E-\x1F\x21-\x39\x3B\x3D-\x7F]/
      SAFE_STRING = /#{SAFE_INIT_CHAR}#{SAFE_CHAR}*/
      FILL = / */
      def parse
        return @ldif if @ldif

        @scanner = Scanner.new(@source)
        raise version_spec_is_missing unless @scanner.scan(/version:/)
        @scanner.scan(FILL)

        version = @scanner.scan(/\d+/)
        raise version_number_is_missing if version.nil?

        version = Integer(version)
        raise unsupported_version(version) if version != 1

        raise separator_is_missing unless @scanner.scan_separators

        records = parse_records

        @ldif = LDIF.new(records)
      end

      private
      def read_base64_value
        value = @scanner.scan(/[a-zA-Z0-9\+\/=]+/)
        return nil if value.nil?
        encoding = value.encoding if value.respond_to?(:encoding)
        value = value.unpack("m")[0].chomp
        if value.respond_to?(:force_encoding)
          value.force_encoding(encoding)
          value.force_encoding("ascii-8bit") unless value.valid_encoding?
        end
        value
      end

      def read_external_file
        uri_string = @scanner.scan(URI::ABS_URI)
        raise uri_is_missing if uri_string.nil?
        uri_string.chomp!

        uri = nil
        begin
          uri = URI.parse(uri_string)
        rescue URI::Error
          raise invalid_uri(uri_string, $!.message)
        end

        if uri.scheme == "file"
          File.open(uri.path, "rb") {|file| file.read}
        else
          uri.read
        end
      end

      def parse_dn(dn_string)
        DN.parse(dn_string).to_s
      rescue DistinguishedNameInvalid
        raise invalid_dn(dn_string, $!.reason)
      end

      def parse_attributes(least=0, &block)
        i = 0
        attributes = {}
        block ||= Proc.new {@scanner.check_separator}
        loop do
          i += 1
          if i >= least
            break if block.call or @scanner.eos?
          end
          type, options, value = parse_attribute
          if @scanner.scan_separator.nil? and !@scanner.eos?
            raise separator_is_missing
          end
          attributes[type] ||= []
          container = attributes[type]
          options.each do |option|
            parent = container.find do |val|
              val.is_a?(Hash) and val.has_key?(option)
            end
            if parent.nil?
              parent = {option => []}
              container << parent
            end
            container = parent[option]
          end
          container << value
        end
        raise attribute_spec_is_missing if attributes.size < least
        attributes
      end

      def parse_attribute_description
        type = @scanner.scan(ATTRIBUTE_TYPE_CHARS)
        raise attribute_type_is_missing if type.nil?
        options = parse_options
        [type, options]
      end

      def parse_attribute
        type, options = parse_attribute_description
        value = parse_attribute_value
        [type, options, value]
      end

      def parse_options
        options = []
        while @scanner.scan(/;/)
          option = @scanner.scan(ATTRIBUTE_TYPE_CHARS)
          raise option_is_missing if option.nil?
          options << option
        end
        options
      end

      def parse_attribute_value(accept_external_file=true)
        raise attribute_value_separator_is_missing if @scanner.scan(/:/).nil?
        if @scanner.scan(/:/)
          @scanner.scan(FILL)
          read_base64_value
        elsif accept_external_file and @scanner.scan(/</)
          @scanner.scan(FILL)
          read_external_file
        else
          @scanner.scan(FILL)
          @scanner.scan(SAFE_STRING)
        end
      end

      def parse_control
        return nil if @scanner.scan(/control:/).nil?
        @scanner.scan(FILL)
        type = @scanner.scan(/\d+(?:\.\d+)*/)
        raise control_type_is_missing if type.nil?
        criticality = nil
        if @scanner.scan(/ +/)
          criticality = @scanner.scan(/true|false/)
          raise criticality_is_missing if criticality.nil?
        end
        value = parse_attribute_value if @scanner.check(/:/)
        raise separator_is_missing unless @scanner.scan_separator
        ChangeRecord::Control.new(type, criticality, value)
      end

      def parse_controls
        controls = []
        loop do
          control = parse_control
          break if control.nil?
          controls << control
        end
        controls
      end

      def parse_change_type
        return nil unless @scanner.scan(/changetype:/)
        @scanner.scan(FILL)
        type = @scanner.check(ATTRIBUTE_TYPE_CHARS)
        raise change_type_value_is_missing if type.nil?
        unless @scanner.scan(/add|delete|modrdn|moddn|modify/)
          raise unknown_change_type(type)
        end

        raise separator_is_missing unless @scanner.scan_separator
        type
      end

      def parse_modify_name_record(klass, dn, controls)
        raise new_rdn_mark_is_missing unless @scanner.scan(/newrdn\b/)
        new_rdn = parse_attribute_value(false)
        raise new_rdn_value_is_missing if new_rdn.nil?
        raise separator_is_missing unless @scanner.scan_separator

        unless @scanner.scan(/deleteoldrdn:/)
          raise delete_old_rdn_mark_is_missing
        end
        @scanner.scan(FILL)
        delete_old_rdn = @scanner.scan(/[01]/)
        raise delete_old_rdn_value_is_missing if delete_old_rdn.nil?
        raise separator_is_missing unless @scanner.scan_separator

        if @scanner.scan(/newsuperior\b/)
          @scanner.scan(FILL)
          new_superior = parse_attribute_value(false)
          raise new_superior_value_is_missing if new_superior.nil?
          new_superior = parse_dn(new_superior)
          raise separator_is_missing unless @scanner.scan_separator
        end
        klass.new(dn, controls, new_rdn, delete_old_rdn, new_superior)
      end

      def parse_modify_spec
        return nil unless @scanner.check(/(#{ATTRIBUTE_TYPE_CHARS}):/)
        type = @scanner[1]
        unless @scanner.scan(/(?:add|delete|replace):/)
          raise unknown_modify_type(type)
        end
        @scanner.scan(FILL)
        attribute, options = parse_attribute_description
        raise separator_is_missing unless @scanner.scan_separator
        attributes = parse_attributes {@scanner.check(/-/)}
        raise modify_spec_separator_is_missing unless @scanner.scan(/-/)
        raise separator_is_missing unless @scanner.scan_separator
        [type, attribute, options, attributes]
      end

      def parse_modify_record(dn, controls)
        operations = []
        loop do
          spec = parse_modify_spec
          break if spec.nil?
          type, attribute, options, attributes = spec
          case type
          when "add"
            klass = ModifyRecord::AddOperation
          when "delete"
            klass = ModifyRecord::DeleteOperation
          when "replace"
            klass = ModifyRecord::ReplaceOperation
          else
            unknown_modify_type(type)
          end
          operations << klass.new(attribute, options, attributes)
        end
        ModifyRecord.new(dn, controls, operations)
      end

      def parse_change_type_record(dn, controls, change_type)
        case change_type
        when "add"
          attributes = parse_attributes(1)
          AddRecord.new(dn, controls, attributes)
        when "delete"
          DeleteRecord.new(dn, controls)
        when "moddn"
          parse_modify_name_record(ModifyDNRecord, dn, controls)
        when "modrdn"
          parse_modify_name_record(ModifyRDNRecord, dn, controls)
        when "modify"
          parse_modify_record(dn, controls)
        else
          raise unknown_change_type(change_type)
        end
      end

      def parse_record
        raise dn_mark_is_missing unless @scanner.scan(/dn:/)
        if @scanner.scan(/:/)
          @scanner.scan(FILL)
          dn = read_base64_value
          raise dn_is_missing if dn.nil?
          dn = parse_dn(dn)
        else
          @scanner.scan(FILL)
          dn = @scanner.scan(/#{SAFE_STRING}$/)
          if dn.nil?
            partial_dn = @scanner.scan(SAFE_STRING)
            raise dn_has_invalid_character(@scanner.check(/./)) if partial_dn
            raise dn_is_missing
          end
          dn = parse_dn(dn)
        end

        raise separator_is_missing unless @scanner.scan_separator

        controls = parse_controls
        change_type = parse_change_type
        raise change_type_is_missing if change_type.nil? and !controls.empty?

        if change_type
          parse_change_type_record(dn, controls, change_type)
        else
          attributes = parse_attributes(1)
          ContentRecord.new(dn, attributes)
        end
      end

      def parse_records
        records = []
        loop do
          records << parse_record
          break if @scanner.eos?
          raise separator_is_missing if @scanner.scan_separator.nil?

          break if @scanner.eos?
          break if @scanner.scan_separators and @scanner.eos?
        end
        records
      end

      def invalid_ldif(reason)
        LdifInvalid.new(@source, reason, @scanner.line, @scanner.column)
      end

      def version_spec_is_missing
        invalid_ldif(_("version spec is missing"))
      end

      def version_number_is_missing
        invalid_ldif(_("version number is missing"))
      end

      def unsupported_version(version)
        invalid_ldif(_("unsupported version: %d") % version)
      end

      def separator_is_missing
        invalid_ldif(_("separator is missing"))
      end

      def dn_mark_is_missing
        invalid_ldif(_("'dn:' is missing"))
      end

      def dn_is_missing
        invalid_ldif(_("DN is missing"))
      end

      def invalid_dn(dn_string, reason)
        invalid_ldif(_("DN is invalid: %s: %s") % [dn_string, reason])
      end

      def dn_has_invalid_character(character)
        invalid_ldif(_("DN has an invalid character: %s") % character)
      end

      def attribute_type_is_missing
        invalid_ldif(_("attribute type is missing"))
      end

      def option_is_missing
        invalid_ldif(_("option is missing"))
      end

      def attribute_value_separator_is_missing
        invalid_ldif(_("':' is missing"))
      end

      def invalid_uri(uri_string, message)
        invalid_ldif(_("URI is invalid: %s: %s") % [uri_string, message])
      end

      def uri_is_missing
        invalid_ldif(_("URI is missing"))
      end

      def modify_spec_separator_is_missing
        invalid_ldif(_("'-' is missing"))
      end

      def unknown_change_type(change_type)
        invalid_ldif(_("unknown change type: %s") % change_type)
      end

      def change_type_is_missing
        invalid_ldif(_("change type is missing"))
      end

      def control_type_is_missing
        invalid_ldif(_("control type is missing"))
      end

      def criticality_is_missing
        invalid_ldif(_("criticality is missing"))
      end

      def change_type_value_is_missing
        invalid_ldif(_("change type value is missing"))
      end

      def attribute_spec_is_missing
        invalid_ldif(_("attribute spec is missing"))
      end

      def new_rdn_mark_is_missing
        invalid_ldif(_("'newrdn:' is missing"))
      end

      def new_rdn_value_is_missing
        invalid_ldif(_("new RDN value is missing"))
      end

      def delete_old_rdn_mark_is_missing
        invalid_ldif(_("'deleteoldrdn:' is missing"))
      end

      def delete_old_rdn_value_is_missing
        invalid_ldif(_("delete old RDN value is missing"))
      end

      def new_superior_value_is_missing
        invalid_ldif(_("new superior value is missing"))
      end

      def unknown_modify_type(type)
        invalid_ldif(_("unknown modify type: %s") % type)
      end
    end

    class Scanner
      SEPARATOR = /(?:\r\n|\n)/
      SEPARATORS = /(?:(?:^#.*)?#{SEPARATOR})+/

      def initialize(source)
        @source = source
        @scanner = StringScanner.new(@source)
        @sub_scanner = nil
        @sub_scanner = next_segment || StringScanner.new("")
      end

      def scan(regexp)
        @sub_scanner = next_segment if @sub_scanner.eos?
        @sub_scanner.scan(regexp)
      end

      def check(regexp)
        @sub_scanner = next_segment if @sub_scanner.eos?
        @sub_scanner.check(regexp)
      end

      def scan_separator
        return @scanner.scan(SEPARATOR) if @sub_scanner.eos?

        scan(SEPARATOR)
      end

      def check_separator
        return @scanner.check(SEPARATOR) if @sub_scanner.eos?

        check(SEPARATOR)
      end

      def scan_separators
        return @scanner.scan(SEPARATORS) if @sub_scanner.eos?

        sub_result = scan(SEPARATORS)
        return nil if sub_result.nil?
        return sub_result unless @sub_scanner.eos?

        result = @scanner.scan(SEPARATORS)
        return sub_result if result.nil?

        sub_result + result
      end

      def [](*args)
        @sub_scanner[*args]
      end

      def eos?
        @sub_scanner = next_segment if @sub_scanner.eos?
        @sub_scanner.eos? and @scanner.eos?
      end

      def line
        _consumed_source = consumed_source
        return 1 if _consumed_source.empty?

        n = Compatible.string_to_lines(_consumed_source).size
        n += 1 if _consumed_source[-1, 1] == "\n"
        n
      end

      def column
        _consumed_source = consumed_source
        return 1 if _consumed_source.empty?

        position - (_consumed_source.rindex("\n") || -1)
      end

      def position
        sub_scanner_string = @sub_scanner.string
        if sub_scanner_string.respond_to?(:bytesize)
          sub_scanner_string_size = sub_scanner_string.bytesize
        else
          sub_scanner_string_size = sub_scanner_string.size
        end
        @scanner.pos - (sub_scanner_string_size - @sub_scanner.pos)
      end

      private
      def next_segment
        loop do
          segment = @scanner.scan(/.+(?:#{SEPARATOR} .*)*#{SEPARATOR}?/)
          return @sub_scanner if segment.nil?
          next if segment[0, 1] == "#"
          return StringScanner.new(segment.gsub(/\r?\n /, ''))
        end
      end

      def consumed_source
        @source[0, position]
      end
    end

    class << self
      def parse(ldif)
        Parser.new(ldif).parse
      end
    end

    include Enumerable

    attr_reader :version, :records
    def initialize(records=[])
      @version = 1
      @records = records
    end

    def <<(record)
      @records << record
    end

    def each(&block)
      @records.each(&block)
    end

    def to_s
      result = "version: #{@version}\n"
      result << @records.collect do |record|
        record.to_s
      end.join("\n")
      result
    end

    def ==(other)
      other.is_a?(self.class) and
        @version == other.version and @records == other.records
    end

    class Record
      include GetTextSupport

      attr_reader :dn, :attributes
      def initialize(dn, attributes)
        @dn = dn
        @attributes = attributes
      end

      def to_hash
        attributes.merge({"dn" => dn})
      end

      def to_s
        result = to_s_prelude
        result << to_s_content
        result
      end

      def ==(other)
        other.is_a?(self.class) and
          @dn == other.dn and
          Attributes.normalize(@attributes) ==
          Attributes.normalize(other.attributes)
      end

      private
      def to_s_prelude
        Attribute.encode("dn", dn)
      end

      def to_s_content
        Attributes.encode(@attributes)
      end
    end

    class ContentRecord < Record
    end

    class ChangeRecord < Record
      attr_reader :controls, :change_type
      def initialize(dn, attributes, controls, change_type)
        super(dn, attributes)
        @controls = controls
        @change_type = change_type
      end

      def add?
        @change_type == "add"
      end

      def delete?
        @change_type == "delete"
      end

      def modify?
        @change_type == "modify"
      end

      def modify_dn?
        @change_type == "moddn"
      end

      def modify_rdn?
        @change_type == "modrdn"
      end

      def ==(other)
        super(other) and
          @controls = other.controls and
          @change_type == other.change_type
      end

      private
      def to_s_prelude
        result = super
        @controls.each do |control|
          result << control.to_s
        end
        result
      end

      def to_s_content
        result = "changetype: #{@change_type}\n"
        result << super
        result
      end

      class Control
        attr_reader :type, :value
        def initialize(type, criticality, value)
          @type = type
          @criticality = normalize_criticality(criticality)
          @value = value
        end

        def criticality?
          @criticality
        end

        def to_a
          [@type, @criticality, @value]
        end

        def to_hash
          {
            :type => @type,
            :criticality => @criticality,
            :value => @value,
          }
        end

        def to_s
          result = "control: #{@type}"
          result << " #{@criticality}" unless @criticality.nil?
          result << @value if @value
          result << "\n"
          result
        end

        def ==(other)
          other.is_a?(self.class) and
            @type == other.type and
            @criticality = other.criticality and
            @value == other.value
        end

        private
        def normalize_criticality(criticality)
          case criticality
          when "true", true
            true
          when "false", false
            false
          when nil
            nil
          else
            raise ArgumentError,
                  _("invalid criticality value: %s") % criticality.inspect
          end
        end
      end
    end

    class AddRecord < ChangeRecord
      def initialize(dn, controls=[], attributes={})
        super(dn, attributes, controls, "add")
      end
    end

    class DeleteRecord < ChangeRecord
      def initialize(dn, controls=[])
        super(dn, {}, controls, "delete")
      end
    end

    class ModifyNameRecord < ChangeRecord
      attr_reader :new_rdn, :new_superior
      def initialize(dn, controls, change_type,
                     new_rdn, delete_old_rdn, new_superior)
        super(dn, {}, controls, change_type)
        @new_rdn = new_rdn
        @delete_old_rdn = normalize_delete_old_rdn(delete_old_rdn)
        @new_superior = new_superior
      end

      def delete_old_rdn?
        @delete_old_rdn
      end

      private
      def normalize_delete_old_rdn(delete_old_rdn)
        case delete_old_rdn
        when "1", true
          true
        when "0", false
          false
        when nil
          nil
        else
          raise ArgumentError,
                _("invalid deleteoldrdn value: %s") % delete_old_rdn.inspect
        end
      end

      def to_s_content
        result = super
        result << "newrdn: #{@new_rdn}\n"
        result << "deleteoldrdn: #{@delete_old_rdn ? 1 : 0}\n"
        result << Attribute.encode("newsuperior", @new_superior) if @new_superior
        result
      end
    end

    class ModifyDNRecord < ModifyNameRecord
      def initialize(dn, controls, new_rdn, delete_old_rdn, new_superior=nil)
        super(dn, controls, "moddn", new_rdn, delete_old_rdn, new_superior)
      end
    end

    class ModifyRDNRecord < ModifyNameRecord
      def initialize(dn, controls, new_rdn, delete_old_rdn, new_superior=nil)
        super(dn, controls, "modrdn", new_rdn, delete_old_rdn, new_superior)
      end
    end

    class ModifyRecord < ChangeRecord
      include Enumerable

      attr_reader :operations
      def initialize(dn, controls=[], operations=[])
        super(dn, {}, controls, "modify")
        @operations = operations
      end

      def each(&block)
        @operations.each(&block)
      end

      def <<(operation)
        @operations << operation
      end

      def add_operation(type, attribute, options, attributes)
        klass = self.class.const_get("#{type.to_s.capitalize}Operation")
        self << klass.new(attribute, options, attributes)
      end

      def ==(other)
        super(other) and @operations == other.operations
      end

      private
      def to_s_content
        result = super
        return result if @operations.empty?
        @operations.collect do |operation|
          result << "#{operation}-\n"
        end
        result
      end

      class Operation
        attr_reader :type, :attribute, :options, :attributes
        def initialize(type, attribute, options, attributes)
          @type = type
          @attribute = attribute
          @options = options
          @attributes = attributes
        end

        def full_attribute_name
          [@attribute, *@options].join(";")
        end

        def add?
          @type == "add"
        end

        def delete?
          @type == "delete"
        end

        def replace?
          @type == "replace"
        end

        def to_s
          Attribute.encode(@type, full_attribute_name) +
            Attributes.encode(@attributes)
        end

        def ==(other)
          other.is_a?(self.class) and
            @type == other.type and
            full_attribute_name == other.full_attribute_name and
            Attributes.normalize(@attributes) ==
            Attributes.normalize(other.attributes)
        end
      end

      class AddOperation < Operation
        def initialize(attribute, options, attributes)
          super("add", attribute, options, attributes)
        end
      end

      class DeleteOperation < Operation
        def initialize(attribute, options, attributes)
          super("delete", attribute, options, attributes)
        end
      end

      class ReplaceOperation < Operation
        def initialize(attribute, options, attributes)
          super("replace", attribute, options, attributes)
        end
      end
    end
  end

  LDIF = Ldif
end