1- #----------------------------------------------------------------------------
2- #
3- # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
4- #
5- # Gmail: garbagecat10
6- #
7- # This program is free software; you can redistribute it and/or modify
8- # it under the terms of the GNU General Public License as published by
9- # the Free Software Foundation; either version 2 of the License, or
10- # (at your option) any later version.
11- #
12- # This program is distributed in the hope that it will be useful,
13- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15- # GNU General Public License for more details.
16- #
17- # You should have received a copy of the GNU General Public License
18- # along with this program; if not, write to the Free Software
19- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20- #
21- #---------------------------------------------------------------------------
22-
231##
242# An LDAP Dataset. Used primarily as an intermediate format for converting
253# to and from LDIF strings and Net::LDAP::Entry objects.
@@ -28,78 +6,7 @@ class Net::LDAP::Dataset < Hash
286 # Dataset object comments.
297 attr_reader :comments
308
31- class << self
32- class ChompedIO #:nodoc:
33- def initialize ( io )
34- @io = io
35- end
36- def gets
37- s = @io . gets
38- s . chomp if s
39- end
40- end
41-
42- ##
43- # Reads an object that returns data line-wise (using #gets) and parses
44- # LDIF data into a Dataset object.
45- def read_ldif ( io ) #:yields: entry-type, value Used mostly for debugging.
46- ds = Net ::LDAP ::Dataset . new
47- io = ChompedIO . new ( io )
48-
49- line = io . gets
50- dn = nil
51-
52- while line
53- new_line = io . gets
54-
55- if new_line =~ /^[\s ]+/
56- line << " " << $'
57- else
58- nextline = new_line
59-
60- if line =~ /^#/
61- ds . comments << line
62- yield :comment , line if block_given?
63- elsif line =~ /^dn:[\s ]*/i
64- dn = $'
65- ds [ dn ] = Hash . new { |k , v | k [ v ] = [ ] }
66- yield :dn , dn if block_given?
67- elsif line . empty?
68- dn = nil
69- yield :end , nil if block_given?
70- elsif line =~ /^([^:]+):([\: ]?)[\s ]*/
71- # $1 is the attribute name
72- # $2 is a colon iff the attr-value is base-64 encoded
73- # $' is the attr-value
74- # Avoid the Base64 class because not all Ruby versions have it.
75- attrvalue = ( $2 == ":" ) ? $'. unpack ( 'm' ) . shift : $'
76- ds [ dn ] [ $1. downcase . to_sym ] << attrvalue
77- yield :attr , [ $1. downcase . to_sym , attrvalue ] if block_given?
78- end
79-
80- line = nextline
81- end
82- end
83-
84- ds
85- end
86-
87- ##
88- # Creates a Dataset object from an Entry object. Used mostly to assist
89- # with the conversion of
90- def from_entry ( entry )
91- dataset = Net ::LDAP ::Dataset . new
92- hash = { }
93- entry . each_attribute do |attribute , value |
94- next if attribute == :dn
95- hash [ attribute ] = value
96- end
97- dataset [ entry . dn ] = hash
98- dataset
99- end
100- end
101-
102- def initialize ( *args , &block ) #:nodoc:
9+ def initialize ( *args , &block ) # :nodoc:
10310 super
10411 @comments = [ ]
10512 end
@@ -152,6 +59,7 @@ def to_entries
15259 ary
15360 end
15461
62+ ##
15563 # This is an internal convenience method to determine if a value requires
15664 # base64-encoding before conversion to LDIF output. The standard approach
15765 # in most LDAP tools is to check whether the value is a password, or if
@@ -162,13 +70,84 @@ def to_entries
16270 # why we handle the simplest cases first. Ideally, we would also test the
16371 # first/last byte, but it's a bit harder to do this in a way that's
16472 # compatible with both 1.8.6 and 1.8.7.
165- def value_is_binary? ( value )
73+ def value_is_binary? ( value ) # :nodoc:
16674 value = value . to_s
16775 return true if value [ 0 ] == ?: or value [ 0 ] == ?<
16876 value . each_byte { |byte | return true if ( byte < 32 ) || ( byte > 126 ) }
16977 false
17078 end
17179 private :value_is_binary?
80+
81+ class << self
82+ class ChompedIO # :nodoc:
83+ def initialize ( io )
84+ @io = io
85+ end
86+ def gets
87+ s = @io . gets
88+ s . chomp if s
89+ end
90+ end
91+
92+ ##
93+ # Creates a Dataset object from an Entry object. Used mostly to assist
94+ # with the conversion of
95+ def from_entry ( entry )
96+ dataset = Net ::LDAP ::Dataset . new
97+ hash = { }
98+ entry . each_attribute do |attribute , value |
99+ next if attribute == :dn
100+ hash [ attribute ] = value
101+ end
102+ dataset [ entry . dn ] = hash
103+ dataset
104+ end
105+
106+ ##
107+ # Reads an object that returns data line-wise (using #gets) and parses
108+ # LDIF data into a Dataset object.
109+ def read_ldif ( io )
110+ ds = Net ::LDAP ::Dataset . new
111+ io = ChompedIO . new ( io )
112+
113+ line = io . gets
114+ dn = nil
115+
116+ while line
117+ new_line = io . gets
118+
119+ if new_line =~ /^[\s ]+/
120+ line << " " << $'
121+ else
122+ nextline = new_line
123+
124+ if line =~ /^#/
125+ ds . comments << line
126+ yield :comment , line if block_given?
127+ elsif line =~ /^dn:[\s ]*/i
128+ dn = $'
129+ ds [ dn ] = Hash . new { |k , v | k [ v ] = [ ] }
130+ yield :dn , dn if block_given?
131+ elsif line . empty?
132+ dn = nil
133+ yield :end , nil if block_given?
134+ elsif line =~ /^([^:]+):([\: ]?)[\s ]*/
135+ # $1 is the attribute name
136+ # $2 is a colon iff the attr-value is base-64 encoded
137+ # $' is the attr-value
138+ # Avoid the Base64 class because not all Ruby versions have it.
139+ attrvalue = ( $2 == ":" ) ? $'. unpack ( 'm' ) . shift : $'
140+ ds [ dn ] [ $1. downcase . to_sym ] << attrvalue
141+ yield :attr , [ $1. downcase . to_sym , attrvalue ] if block_given?
142+ end
143+
144+ line = nextline
145+ end
146+ end
147+
148+ ds
149+ end
150+ end
172151end
173152
174153require 'net/ldap/entry' unless defined? Net ::LDAP ::Entry
0 commit comments