Skip to content

Commit 5eaacf1

Browse files
author
blackhedd
committed
supported LDAP delete
1 parent 5c18a30 commit 5eaacf1

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

lib/net/ber.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ module BERParser
6868
# this can throw TypeErrors and other nasties.
6969
#
7070
def read_ber syntax=nil
71-
eof? and return nil
71+
return nil if eof?
7272

7373
id = getc # don't trash this value, we'll use it later
7474
tag = id & 31

lib/net/ldap.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ class LdapError < Exception; end
256256
14 => :array, # CompareRequest
257257
15 => :array, # CompareResponse
258258
16 => :array, # AbandonRequest
259+
24 => :array, # Unsolicited Notification
259260
}
260261
},
261262
:context_specific => {
@@ -275,6 +276,7 @@ class LdapError < Exception; end
275276
ResultStrings = {
276277
0 => "Success",
277278
1 => "Operations Error",
279+
2 => "Protocol Error",
278280
16 => "No Such Attribute",
279281
17 => "Undefined Attribute Type",
280282
20 => "Attribute or Value Exists",
@@ -629,6 +631,31 @@ def modify_rdn args
629631
rename args
630632
end
631633

634+
# Delete an entry from the LDAP directory.
635+
# Takes a hash of arguments.
636+
# The only supported argument is :dn, which must
637+
# give the complete DN of the entry to be deleted.
638+
# Returns True or False to indicate whether the delete
639+
# succeeded. Extended status information is available by
640+
# calling #get_operation_result.
641+
#
642+
# dn = "mail=deleteme@example.com,ou=people,dc=example,dc=com"
643+
# ldap.delete :dn => dn
644+
#
645+
def delete args
646+
if @open_connection
647+
@result = @open_connection.delete( args )
648+
else
649+
@result = 0
650+
conn = Connection.new( :host => @host, :port => @port )
651+
if (@result = conn.bind( args[:auth] || @auth )) == 0
652+
@result = conn.delete( args )
653+
end
654+
conn.close
655+
end
656+
@result == 0
657+
end
658+
632659
end # class LDAP
633660

634661

@@ -854,6 +881,22 @@ def rename args
854881
end
855882

856883

884+
#--
885+
# delete
886+
# TODO, need to support a time limit, in case the server fails to respond.
887+
#
888+
def delete args
889+
dn = args[:dn] or raise "Unable to delete empty DN"
890+
891+
request = dn.to_s.to_ber_application_string(10)
892+
pkt = [next_msgid.to_ber, request].to_ber_sequence
893+
@conn.write pkt
894+
895+
(be = @conn.read_ber(AsnSyntax)) && (pdu = LdapPdu.new( be )) && (pdu.app_tag == 11) or raise LdapError.new( "response missing or invalid" )
896+
pdu.result_code
897+
end
898+
899+
857900
end # class Connection
858901
end # class LDAP
859902

lib/net/ldap/pdu.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class LdapPdu
4141
SearchResult = 5
4242
ModifyResponse = 7
4343
AddResponse = 9
44+
DeleteResponse = 11
4445
ModifyRDNResponse = 13
4546

4647
attr_reader :msg_id, :app_tag
@@ -81,6 +82,8 @@ def initialize ber_object
8182
parse_ldap_result ber_object[1]
8283
when AddResponse
8384
parse_ldap_result ber_object[1]
85+
when DeleteResponse
86+
parse_ldap_result ber_object[1]
8487
when ModifyRDNResponse
8588
parse_ldap_result ber_object[1]
8689
else

0 commit comments

Comments
 (0)