@@ -10,7 +10,7 @@ def initialize(server)
10
10
@instrumentation_service = server [ :instrumentation_service ]
11
11
12
12
begin
13
- @conn = TCPSocket . new ( server [ :host ] , server [ :port ] )
13
+ @conn = server [ :socket ] || TCPSocket . new ( server [ :host ] , server [ :port ] )
14
14
rescue SocketError
15
15
raise Net ::LDAP ::LdapError , "No such address or other socket error."
16
16
rescue Errno ::ECONNREFUSED
@@ -87,13 +87,9 @@ def setup_encryption(args)
87
87
# additional branches requiring server validation and peer certs, etc.
88
88
# go here.
89
89
when :start_tls
90
- msgid = next_msgid . to_ber
91
90
request = [ Net ::LDAP ::StartTlsOid . to_ber_contextspecific ( 0 ) ] . to_ber_appsequence ( Net ::LDAP ::PDU ::ExtendedRequest )
92
- request_pkt = [ msgid , request ] . to_ber_sequence
93
- write request_pkt
94
- be = read
95
- raise Net ::LDAP ::LdapError , "no start_tls result" if be . nil?
96
- pdu = Net ::LDAP ::PDU . new ( be )
91
+ write ( request )
92
+ pdu = read
97
93
raise Net ::LDAP ::LdapError , "no start_tls result" if pdu . nil?
98
94
if pdu . result_code . zero?
99
95
@conn = self . class . wrap_with_ssl ( @conn )
@@ -119,25 +115,40 @@ def close
119
115
#
120
116
# - syntax: the BER syntax to use to parse the read data with
121
117
#
122
- # Returns basic BER objects .
118
+ # Returns parsed Net::LDAP::PDU object .
123
119
def read ( syntax = Net ::LDAP ::AsnSyntax )
124
- instrument "read.net_ldap_connection" , :syntax => syntax do |payload |
125
- @conn . read_ber ( syntax ) do |id , content_length |
126
- payload [ :object_type_id ] = id
127
- payload [ :content_length ] = content_length
120
+ ber_object =
121
+ instrument "read.net_ldap_connection" , :syntax => syntax do |payload |
122
+ @conn . read_ber ( syntax ) do |id , content_length |
123
+ payload [ :object_type_id ] = id
124
+ payload [ :content_length ] = content_length
125
+ end
128
126
end
127
+
128
+ return unless ber_object
129
+
130
+ instrument "parse_pdu.net_ldap_connection" do |payload |
131
+ pdu = payload [ :pdu ] = Net ::LDAP ::PDU . new ( ber_object )
132
+
133
+ payload [ :message_id ] = pdu . message_id
134
+ payload [ :app_tag ] = pdu . app_tag
135
+
136
+ pdu
129
137
end
130
138
end
131
139
private :read
132
140
133
- # Internal: Writes the given packet to the configured connection.
141
+ # Internal: Write a BER formatted packet with the next message id to the
142
+ # configured connection.
134
143
#
135
- # - packet: the BER data packet to write on the socket.
144
+ # - request: required BER formatted request
145
+ # - controls: optional BER formatted controls
136
146
#
137
147
# Returns the return value from writing to the connection, which in some
138
148
# cases is the Integer number of bytes written to the socket.
139
- def write ( packet )
149
+ def write ( request , controls = nil )
140
150
instrument "write.net_ldap_connection" do |payload |
151
+ packet = [ next_msgid . to_ber , request , controls ] . compact . to_ber_sequence
141
152
payload [ :content_length ] = @conn . write ( packet )
142
153
end
143
154
end
@@ -176,13 +187,12 @@ def bind_simple(auth)
176
187
177
188
raise Net ::LDAP ::LdapError , "Invalid binding information" unless ( user && psw )
178
189
179
- msgid = next_msgid . to_ber
180
190
request = [ LdapVersion . to_ber , user . to_ber ,
181
191
psw . to_ber_contextspecific ( 0 ) ] . to_ber_appsequence ( 0 )
182
- request_pkt = [ msgid , request ] . to_ber_sequence
183
- write request_pkt
192
+ write ( request )
184
193
185
- ( be = read and pdu = Net ::LDAP ::PDU . new ( be ) ) or raise Net ::LDAP ::LdapError , "no bind result"
194
+ pdu = read
195
+ raise Net ::LDAP ::LdapError , "no bind result" unless pdu
186
196
187
197
pdu
188
198
end
@@ -215,13 +225,13 @@ def bind_sasl(auth)
215
225
216
226
n = 0
217
227
loop {
218
- msgid = next_msgid . to_ber
219
228
sasl = [ mech . to_ber , cred . to_ber ] . to_ber_contextspecific ( 3 )
220
229
request = [ LdapVersion . to_ber , "" . to_ber , sasl ] . to_ber_appsequence ( 0 )
221
- request_pkt = [ msgid , request ] . to_ber_sequence
222
- write request_pkt
230
+ write ( request )
231
+
232
+ pdu = read
233
+ raise Net ::LDAP ::LdapError , "no bind result" unless pdu
223
234
224
- ( be = read and pdu = Net ::LDAP ::PDU . new ( be ) ) or raise Net ::LDAP ::LdapError , "no bind result"
225
235
return pdu unless pdu . result_code == 14 # saslBindInProgress
226
236
raise Net ::LDAP ::LdapError , "sasl-challenge overflow" if ( ( n += 1 ) > MaxSaslChallenges )
227
237
@@ -393,13 +403,12 @@ def search(args = {})
393
403
controls << sort_control if sort_control
394
404
controls = controls . empty? ? nil : controls . to_ber_contextspecific ( 0 )
395
405
396
- pkt = [ next_msgid . to_ber , request , controls ] . compact . to_ber_sequence
397
- write pkt
406
+ write ( request , controls )
398
407
399
408
result_pdu = nil
400
409
controls = [ ]
401
410
402
- while ( be = read ) && ( pdu = Net :: LDAP :: PDU . new ( be ) )
411
+ while pdu = read
403
412
case pdu . app_tag
404
413
when Net ::LDAP ::PDU ::SearchReturnedData
405
414
n_results += 1
@@ -502,10 +511,13 @@ def modify(args)
502
511
ops = self . class . modify_ops args [ :operations ]
503
512
request = [ modify_dn . to_ber ,
504
513
ops . to_ber_sequence ] . to_ber_appsequence ( 6 )
505
- pkt = [ next_msgid . to_ber , request ] . to_ber_sequence
506
- write pkt
514
+ write ( request )
507
515
508
- ( be = read ) && ( pdu = Net ::LDAP ::PDU . new ( be ) ) && ( pdu . app_tag == Net ::LDAP ::PDU ::ModifyResponse ) or raise Net ::LDAP ::LdapError , "response missing or invalid"
516
+ pdu = read
517
+
518
+ if !pdu || pdu . app_tag != Net ::LDAP ::PDU ::ModifyResponse
519
+ raise Net ::LDAP ::LdapError , "response missing or invalid"
520
+ end
509
521
510
522
pdu
511
523
end
@@ -525,13 +537,13 @@ def add(args)
525
537
}
526
538
527
539
request = [ add_dn . to_ber , add_attrs . to_ber_sequence ] . to_ber_appsequence ( 8 )
528
- pkt = [ next_msgid . to_ber , request ] . to_ber_sequence
529
- write pkt
540
+ write ( request )
541
+
542
+ pdu = read
530
543
531
- ( be = read ) &&
532
- ( pdu = Net ::LDAP ::PDU . new ( be ) ) &&
533
- ( pdu . app_tag == Net ::LDAP ::PDU ::AddResponse ) or
544
+ if !pdu || pdu . app_tag != Net ::LDAP ::PDU ::AddResponse
534
545
raise Net ::LDAP ::LdapError , "response missing or invalid"
546
+ end
535
547
536
548
pdu
537
549
end
@@ -548,12 +560,13 @@ def rename(args)
548
560
request = [ old_dn . to_ber , new_rdn . to_ber , delete_attrs . to_ber ]
549
561
request << new_superior . to_ber_contextspecific ( 0 ) unless new_superior == nil
550
562
551
- pkt = [ next_msgid . to_ber , request . to_ber_appsequence ( 12 ) ] . to_ber_sequence
552
- write pkt
563
+ write ( request . to_ber_appsequence ( 12 ) )
564
+
565
+ pdu = read
553
566
554
- ( be = read ) &&
555
- ( pdu = Net ::LDAP ::PDU . new ( be ) ) && ( pdu . app_tag == Net :: LDAP :: PDU :: ModifyRDNResponse ) or
556
- raise Net :: LDAP :: LdapError . new ( "response missing or invalid" )
567
+ if ! pdu || pdu . app_tag != Net :: LDAP :: PDU :: ModifyRDNResponse
568
+ raise Net ::LDAP ::LdapError . new "response missing or invalid"
569
+ end
557
570
558
571
pdu
559
572
end
@@ -565,10 +578,13 @@ def delete(args)
565
578
dn = args [ :dn ] or raise "Unable to delete empty DN"
566
579
controls = args . include? ( :control_codes ) ? args [ :control_codes ] . to_ber_control : nil #use nil so we can compact later
567
580
request = dn . to_s . to_ber_application_string ( 10 )
568
- pkt = [ next_msgid . to_ber , request , controls ] . compact . to_ber_sequence
569
- write pkt
581
+ write ( request , controls )
582
+
583
+ pdu = read
570
584
571
- ( be = read ) && ( pdu = Net ::LDAP ::PDU . new ( be ) ) && ( pdu . app_tag == Net ::LDAP ::PDU ::DeleteResponse ) or raise Net ::LDAP ::LdapError , "response missing or invalid"
585
+ if !pdu || pdu . app_tag != Net ::LDAP ::PDU ::DeleteResponse
586
+ raise Net ::LDAP ::LdapError , "response missing or invalid"
587
+ end
572
588
573
589
pdu
574
590
end
0 commit comments