Module: LDAP::ConnImplementation

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

Instance Method Summary collapse

Instance Method Details

#__jndi_contextObject



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

def __jndi_context
  @context
end

#add(dn, attrs) ⇒ Object



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

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:



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

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)


254
255
256
# File 'lib/ldap/conn.rb', line 254

def bound?
  !@context.nil?
end

#controls(*args) ⇒ Object



15
16
17
# File 'lib/ldap/conn.rb', line 15

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

#delete(dn) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/ldap/conn.rb', line 180

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



75
76
77
# File 'lib/ldap/conn.rb', line 75

def err
  @err || 0
end

#err2string(err) ⇒ Object



79
80
81
# File 'lib/ldap/conn.rb', line 79

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

#get_option(*args) ⇒ Object



19
20
21
# File 'lib/ldap/conn.rb', line 19

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

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



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

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

#modify(dn, attrs) ⇒ Object



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

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.



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

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



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

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

#referrals(*args) ⇒ Object



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

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

#result2error(*args) ⇒ Object



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

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



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

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



239
240
241
242
243
244
245
# File 'lib/ldap/conn.rb', line 239

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



121
122
123
# File 'lib/ldap/conn.rb', line 121

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

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



83
84
85
# File 'lib/ldap/conn.rb', line 83

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

#unbindObject



247
248
249
250
251
252
# File 'lib/ldap/conn.rb', line 247

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