Skip to content

Commit

Permalink
Add LdapConnection class
Browse files Browse the repository at this point in the history
* The LdapConnection class provides storage for a single LDAP session
  between the server and client. The operations performed from the
  connection over its life are aggregated into this connection, along
  with an id, which should be unique and used to reference an
  LdapConnection object in a collection or database.

* Added parseops3.rb to test LdapConnection class.
  • Loading branch information
John Lamb committed Feb 15, 2013
1 parent 814eb3b commit 48c4c6f
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
28 changes: 28 additions & 0 deletions lib/ldapreplay/ldapconnection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'ldapreplay'

class LdapReplay::LdapConnection

attr_accessor :id, :op_ary, :op_hsh, :from_ip, :from_port, :to_ip, :to_port, :ldap_conn

def initialize from_ip, from_port, to_ip, to_port
@from_ip = from_ip
@from_port = from_port
@to_ip = to_ip
@to_port = to_port
@op_ary = []
@op_hsh = {}
@ldap_conn = nil
end

def add_op op_time, op_conn, op_id, op_type, *op_args
unless op_hsh[op_id]
new_op = LdapReplay::LdapOperation.new(op_time, op_conn, op_id, op_type, *op_args)
@op_hsh[op_id] = new_op
@op_ary.push new_op
return new_op
else
@op_hsh[op_id].add_args(*op_args)
return nil
end
end
end
4 changes: 3 additions & 1 deletion lib/ldapreplay/ldapoperation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class LdapReplay::LdapOperation

attr_accessor :op_time, :op_conn, :op_id, :op_type, :op_args

def initialize *args
# puts args[4]
@op_time = args[0]
Expand All @@ -23,7 +25,7 @@ def add_args op_args
def to_s
oa = @op_args ? @op_args.to_s : 'nil'
"LdapReplay::LdapOperation[{ :op_time => " + @op_time.to_s + ", :op_conn => \"" + @op_conn + "\", :op_id => \"" + @op_id + "\", :op_type => \"" + @op_type + "\", :op_args => " + oa + " }]"
end
end
end

class LdapReplay
Expand Down
60 changes: 60 additions & 0 deletions parseops2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/ruby -I ./lib

require 'ldapreplay/parsers/openldap'
require 'ldapreplay/ldapoperation'
require 'pp'
require 'yaml'

parser = LdapReplay::Parsers::OpenLDAP.new( *ARGV )

conn_hsh = {}
conn_ary = []
op_hsh = Hash.new { |hh, kk| hh[kk] = [] }

parser.emit { |args|
begin
op_time, op_conn, op_id, op_type, op_args = *args
# puts "args: #{args.join(' ')}"
unless op_type == 'RESULT'
if conn_hsh.has_key?(op_conn)
connection = conn_hsh[op_conn]
if connection.has_key?(op_id)
connection[op_id].add_args(op_args)
else
new_op = LdapReplay::LdapOperation.new(*args)
connection[op_id] = new_op
unless new_op.op_type == 'ACCEPT'
op_hsh[new_op.op_time].push(new_op)
end
end
else
conn_hsh[op_conn] = {op_id => LdapReplay::LdapOperation.new(*args)}
end
if op_type == 'closed'
conn_ary.push conn_hsh.delete op_conn
# puts "--------------------"
# puts "conn_hsh: %s" % conn_hsh.pretty_inspect
# puts "===================="
# puts "op_hsh: %s" % op_hsh.pretty_inspect
# puts "conn_ary: %s" % conn_ary.pretty_inspect
# # puts "conn_ary: #{conn_ary}"
# puts "^^^^^^^^^^^^^^^^^^^^"
end
end
rescue Errno::EPIPE
break
end
}

ckeys = conn_hsh.keys
ckeys.each {|kk| conn_ary.push conn_hsh.delete kk}

# puts "--------------------"
# puts "conn_hsh: %s" % conn_hsh.pretty_inspect
# puts "===================="
# puts "conn_ary: %s" % conn_ary.pretty_inspect
# # puts "conn_ary: #{conn_ary}"
# puts "^^^^^^^^^^^^^^^^^^^^"

# puts YAML.dump conn_ary
puts "op_hsh: %s" % op_hsh.pretty_inspect
49 changes: 49 additions & 0 deletions parseops3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/ruby -I ./lib

require 'ldapreplay/parsers/openldap'
require 'ldapreplay/ldapoperation'
require 'ldapreplay/ldapconnection'
require 'pp'
require 'yaml'

parser = LdapReplay::Parsers::OpenLDAP.new( *ARGV )

conn_hsh = {}
conn_ary = []
op_hsh = Hash.new { |hh, kk| hh[kk] = [] }

parser.emit { |args|
begin
op_time, op_conn, op_id, op_type, op_args = *args
# puts "args: #{args.join(' ')}"
case op_type
when 'ACCEPT'
nc = LdapReplay::LdapConnection.new( op_args[:from_ip], op_args[:from_port], op_args[:to_ip], op_args[:to_port] )
nc.id = (conn_ary.push(nc).size) - 1
conn_hsh[op_conn] = nc.id
when 'closed'
conn_hsh.delete op_conn
when 'RESULT'

else
if conn_hsh[op_conn] and conn = conn_ary[conn_hsh[op_conn]]
if new_op = conn.add_op(*args)
op_hsh[new_op.op_time].push(new_op)
end
end
end
rescue Errno::EPIPE
break
end
}


puts "--------------------"
puts "conn_hsh: %s" % conn_hsh.pretty_inspect
puts "===================="
puts "conn_ary: %s" % conn_ary.pretty_inspect
# puts "conn_ary: #{conn_ary}"
puts "^^^^^^^^^^^^^^^^^^^^"

# puts YAML.dump conn_ary
puts "op_hsh: %s" % op_hsh.pretty_inspect

0 comments on commit 48c4c6f

Please sign in to comment.