Module: LDAP::ConnImplementation

Included in:
Conn, SSLConn
Defined in:
lib/ldap/conn.rb

Instance Method Summary collapse

Instance Method Details

#__jndi_contextObject



54
55
56
# File 'lib/ldap/conn.rb', line 54

def __jndi_context
  @context
end

#add(dn, attrs) ⇒ Object



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
# File 'lib/ldap/conn.rb', line 113

def add(dn, attrs)
  raise LDAP::InvalidDataError, "The LDAP handler has already unbound." unless bound?

  attrs = LDAP::hash2mods(LDAP::LDAP_MOD_ADD, attrs) if attrs.is_a?(Hash)

  begin 
    @context.create_subcontext(dn, LDAP::Mod.to_java_attributes(*attrs))
    @err = 0
  rescue javax.naming.NameNotFoundException => e
    @err = 32
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  rescue javax.naming.InvalidNameException => e
    @err = 34
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  rescue javax.naming.NoPermissionException => e
    @err = 50
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  rescue javax.naming.directory.SchemaViolationException => e
    @err = 65
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  rescue javax.naming.NamingException => e
    @err = 21
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  end
  self
end

#bind(dn = nil, password = nil, method = LDAP_AUTH_SIMPLE) ⇒ Object

Raises:



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
# File 'lib/ldap/conn.rb', line 75

def bind(dn=nil, password=nil, method=LDAP_AUTH_SIMPLE)
  raise LDAP::Error, "already bound" if bound?

  url = @use_ssl ? "ldaps://#@host:#@port/" : "ldap://#@host:#@port/"
  base_env = {javax.naming.Context::PROVIDER_URL => url}
  base_env[javax.naming.Context::SECURITY_PRINCIPAL] = dn if dn
  base_env[javax.naming.Context::SECURITY_CREDENTIALS] = password if password

  @current_env = java.util.Hashtable.new(LDAP::configuration(base_env))

  begin 
    @context = javax.naming.directory.InitialDirContext.new(@current_env)
    @err = 0
  rescue javax.naming.NoPermissionException => e
    @err = 50
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  rescue javax.naming.NamingException => e
    @err = -1
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  end
  
  if !block_given?
    return self
  end

  begin 
    yield self

    return nil
  ensure
    unbind
  end
end

#bound?Boolean

Returns:

  • (Boolean)


242
243
244
# File 'lib/ldap/conn.rb', line 242

def bound?
  !@context.nil?
end

#controls(*args) ⇒ Object



3
4
5
# File 'lib/ldap/conn.rb', line 3

def controls(*args)
  raise "NOT IMPLEMENTED"
end

#delete(dn) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ldap/conn.rb', line 168

def delete(dn)
  raise LDAP::InvalidDataError, "The LDAP handler has already unbound." unless bound?

  begin
    @context.destroy_subcontext(dn)
    @err = 0
  rescue javax.naming.NameNotFoundException => e
    @err = 32
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  rescue javax.naming.InvalidNameException => e
    @err = 34
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  rescue javax.naming.NoPermissionException => e
    @err = 50
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  rescue javax.naming.NamingException => e
    @err = 21
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  end
  self
end

#errObject



63
64
65
# File 'lib/ldap/conn.rb', line 63

def err
  @err || 0
end

#err2string(err) ⇒ Object



67
68
69
# File 'lib/ldap/conn.rb', line 67

def err2string(err)
  LDAP.err2string(err)
end

#get_option(*args) ⇒ Object



7
8
9
# File 'lib/ldap/conn.rb', line 7

def get_option(*args)
  raise "NOT IMPLEMENTED"
end

#initialize(host = 'localhost', port = LDAP_PORT) ⇒ Object



58
59
60
61
# File 'lib/ldap/conn.rb', line 58

def initialize(host='localhost', port=LDAP_PORT)
  @host = host
  @port = port
end

#modify(dn, attrs) ⇒ Object



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
# File 'lib/ldap/conn.rb', line 140

def modify(dn, attrs)
  raise LDAP::InvalidDataError, "The LDAP handler has already unbound." unless bound?

  attrs = LDAP::hash2mods(LDAP::LDAP_MOD_REPLACE, attrs) if attrs.is_a?(Hash)

  begin 
    @context.modify_attributes(dn, LDAP::Mod.to_java_modification_items(*attrs))
    @err = 0
  rescue javax.naming.NameNotFoundException => e
    @err = 32
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  rescue javax.naming.InvalidNameException => e
    @err = 34
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  rescue javax.naming.NoPermissionException => e
    @err = 50
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  rescue javax.naming.directory.SchemaViolationException => e
    @err = 65
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  rescue javax.naming.NamingException => e
    @err = 21
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  end

  self
end

#modrdn(dn, new_rdn, delete_old_rdn) ⇒ Object

Modify the RDN of the entry with DN, dn, giving it the new RDN, new_rdn. If delete_old_rdn is true, the old RDN value will be deleted from the entry.



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
# File 'lib/ldap/conn.rb', line 14

def modrdn(dn, new_rdn, delete_old_rdn)
  begin 
    if delete_old_rdn
      @context.rename(dn, new_rdn)
    else
      obj = @context.lookup(dn)
      @context.bind(new_rdn, obj)
    end
    @err = 0
  rescue javax.naming.NameAlreadyBoundException => e
    @err = 68
  rescue javax.naming.InvalidNameException => e
    @err = 34
  rescue javax.naming.NoPermissionException => e
    @err = 50
  rescue javax.naming.directory.SchemaViolationException => e
    @err = 65
  rescue javax.naming.NamingException => e
    @err = 21
  rescue javax.naming.NoPermissionException => e
    @err = 50
  rescue javax.naming.NamingException => e
    @err = -1
  end
  raise LDAP::ResultError.wrap(LDAP::err2string(@err), e) if @err != 0
  self
end

#perror(*args) ⇒ Object



42
43
44
# File 'lib/ldap/conn.rb', line 42

def perror(*args)
  raise "NOT IMPLEMENTED"
end

#referrals(*args) ⇒ Object



46
47
48
# File 'lib/ldap/conn.rb', line 46

def referrals(*args)
  raise "NOT IMPLEMENTED"
end

#result2error(*args) ⇒ Object



50
51
52
# File 'lib/ldap/conn.rb', line 50

def result2error(*args)
  raise "NOT IMPLEMENTED"
end

#search(base_dn, scope, filter, attrs = nil, attrsonly = nil, sec = 0, usec = 0, s_attr = nil, s_proc = nil) ⇒ Object



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
# File 'lib/ldap/conn.rb', line 190

def search(base_dn, scope, filter, attrs=nil, attrsonly=nil, sec=0, usec=0, s_attr=nil, s_proc=nil)
  raise LDAP::InvalidDataError, "The LDAP handler has already unbound." unless bound?

  controls = javax.naming.directory.SearchControls.new
  controls.search_scope = scope

  if attrs && !attrs.empty?
    controls.returning_attributes = attrs.to_java(:string)
  end
  if attrsonly
    controls.returning_obj_flag = true
  end

  if sec != 0 || usec != 0
    controls.time_limit = usec/1000 + sec*1000
  end

  begin 
    @context.search(base_dn, filter, controls).each do |val|
      yield LDAP::Entry.create_from_search_result(val)
    end

    @err = 0
  rescue javax.naming.NameNotFoundException => e
    @err = 32
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  rescue javax.naming.InvalidNameException => e
    @err = 34
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  rescue javax.naming.NoPermissionException => e
    @err = 50
    raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
  end

  self
end

#search2(base_dn, scope, filter, attrs = nil, attrsonly = nil, sec = 0, usec = 0, s_attr = nil, s_proc = nil) ⇒ Object



227
228
229
230
231
232
233
# File 'lib/ldap/conn.rb', line 227

def search2(base_dn, scope, filter, attrs=nil, attrsonly=nil, sec=0, usec=0, s_attr=nil, s_proc=nil)
  arr = []
  search(base_dn, scope, filter, attrs, attrsonly, sec, usec, s_attr, s_proc) do |val|
    arr << LDAP::entry2hash(val)
  end
  arr
end

#set_option(opt, value) ⇒ Object



109
110
111
# File 'lib/ldap/conn.rb', line 109

def set_option(opt, value)
  @err = 0
end

#simple_bind(dn = nil, password = nil, &block) ⇒ Object



71
72
73
# File 'lib/ldap/conn.rb', line 71

def simple_bind(dn=nil, password=nil, &block)
  bind(dn, password, LDAP_AUTH_SIMPLE, &block)
end

#unbindObject



235
236
237
238
239
240
# File 'lib/ldap/conn.rb', line 235

def unbind
  raise LDAP::InvalidDataError, "The LDAP handler has already unbound." unless bound?
  @context.close
  @err = 0
  @context = nil
end