ldap-shell-utils 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/lsu CHANGED
@@ -1,11 +1,36 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+
3
4
  $:.unshift( File.join( File.dirname( __FILE__ ), %w[.. lib] ) )
4
5
 
5
- require 'loader'
6
6
 
7
- LdapShellUtils::CliApplication.run(
8
- Choice.choices[:config],
9
- Choice.choices[:filter],
10
- Choice.choices[:attributes]
11
- )
7
+ require 'rubygems'
8
+ require 'slop'
9
+ require 'ldap-shell-utils'
10
+
11
+
12
+ def validate_options( options )
13
+
14
+ options[:config] && options[:filter]
15
+ end
16
+
17
+
18
+ slop = Slop.new( :strict => true, :help=> true ) do
19
+
20
+ banner "Usage:\n\t#{$0} [options]\n\nOptions:"
21
+
22
+ on :a, :audit, 'Audit result entries'
23
+ on :c, :config, 'The configuration file', :optional => false
24
+ on :f, :filter, 'Query', :optional => false
25
+ on :attributes, 'Entry attribute list', :optional => true, :as => Array, :delimiter => ','
26
+ end
27
+
28
+
29
+ begin
30
+ slop.parse( )
31
+ options = slop.to_hash( true )
32
+ LdapShellUtils::CliApplication.run( options ) if validate_options( options )
33
+ rescue Slop::InvalidOptionError => e
34
+ $stderr.puts( e.message )
35
+ exit
36
+ end
@@ -1,5 +1,5 @@
1
1
  begin
2
- Dir.glob( File.join( File.dirname( __FILE__ ), "**", "*.rb" ) ).each { |library| require library }
2
+ Dir.glob( File.join( File.dirname( __FILE__ ), "**", "*.rb" ) ).each { |l| require l }
3
3
  rescue LoadError => e
4
4
  $sdterr.puts( e.message )
5
- end
5
+ end
@@ -6,18 +6,17 @@ module LdapShellUtils
6
6
  module CliApplication
7
7
  extend self
8
8
 
9
- def run( config, filter, attributes )
9
+ def print_entry( entry )
10
10
 
11
- ConfigContext.load( File.expand_path( config ) )
12
- results = LdapConnection.new( ConfigContext.url, ConfigContext.all ).search( filter, attributes )
13
-
14
- if( results )
15
- results.each do |entry|
16
-
17
- puts "dn: #{entry.dn}"
18
- entry.each { |a,v| puts " - #{a}: #{v.length == 1 ? v[0]:v.inspect}" unless a == :dn }
19
- end
20
- end
11
+ puts "dn: #{entry.dn}"
12
+ entry.each { |a,v| puts "#{a}: #{(v.size == 1) ? v[0]:v.inspect}" unless a == :dn }
13
+ end
14
+
15
+ def run( options )
16
+
17
+ ConfigContext.load( File.expand_path( options[:config] ) )
18
+ results = LdapConnection.new( ConfigContext.url, ConfigContext.all ).search( options[:filter], options[:attributes], options[:audit] )
19
+ results.each { |e| print_entry( e ) } if results
21
20
  rescue Exception => e
22
21
  $stderr.puts( e.message )
23
22
  end
@@ -6,8 +6,14 @@ require 'net/ldap'
6
6
  module LdapShellUtils
7
7
  class LdapConnection
8
8
 
9
- attr_reader :connection
9
+ OPERATIONAL_ATTRIBUTES = [
10
+ :creatorsname,
11
+ :createtimestamp,
12
+ :modifiersname,
13
+ :modifytimestamp
14
+ ]
10
15
 
16
+ public
11
17
  def initialize( url, config )
12
18
 
13
19
  @uri = URI.parse( url )
@@ -21,22 +27,26 @@ module LdapShellUtils
21
27
  :password => config[:password]
22
28
  }
23
29
  }
24
-
30
+
25
31
  @configuration[:encryption] = { :method => :simple_tls } if( @uri.scheme.to_sym == :ldaps )
26
- @connection = Net::LDAP.new( @configuration )
32
+
33
+ @connection = Net::LDAP.new( @configuration )
27
34
 
28
35
  self
29
36
  rescue Exception => e
30
37
  raise ArgumentError.new( e.message )
31
38
  end
32
39
 
33
- def search( filter, attributes=[] )
34
-
35
- @connection.search( :filter=>Net::LDAP::Filter.from_rfc2254( filter ), :attributes=>attributes, :result=>false )
36
- end
37
40
 
38
- def to_s
39
- "#{@configuration.uri}"
40
- end
41
+ def search( filter, attributes, audit=false )
42
+
43
+ final_attributes = attributes ? attributes : []
44
+ final_attributes += OPERATIONAL_ATTRIBUTES if audit
45
+ final_filter = Net::LDAP::Filter.construct( filter )
46
+
47
+ @connection.search( :filter=>Net::LDAP::Filter.from_rfc2254( filter ),
48
+ :attributes=>final_attributes,
49
+ :return_result=>true )
50
+ end
41
51
  end
42
52
  end
@@ -2,7 +2,7 @@ module Version
2
2
  INFO = {
3
3
  :major =>0,
4
4
  :minor =>0,
5
- :patch =>1
5
+ :patch =>2
6
6
  }
7
7
 
8
8
  NAME = 'ldap-shell-utils'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ldap-shell-utils
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Javier Juarez
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-07 00:00:00 +02:00
18
+ date: 2011-05-09 00:00:00 +02:00
19
19
  default_executable: lsu
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -29,7 +29,7 @@ dependencies:
29
29
  segments:
30
30
  - 0
31
31
  version: "0"
32
- name: choice
32
+ name: slop
33
33
  version_requirements: *id001
34
34
  prerelease: false
35
35
  - !ruby/object:Gem::Dependency
@@ -145,7 +145,7 @@ dependencies:
145
145
  segments:
146
146
  - 0
147
147
  version: "0"
148
- name: choice
148
+ name: slop
149
149
  version_requirements: *id009
150
150
  prerelease: false
151
151
  - !ruby/object:Gem::Dependency
@@ -171,10 +171,9 @@ extensions: []
171
171
  extra_rdoc_files:
172
172
  - README.rdoc
173
173
  files:
174
+ - lib/ldap-shell-utils.rb
174
175
  - lib/ldap-shell-utils/cli_application.rb
175
176
  - lib/ldap-shell-utils/ldap_connection.rb
176
- - lib/ldap-shell-utils/options_parser.rb
177
- - lib/loader.rb
178
177
  - lib/version.rb
179
178
  - README.rdoc
180
179
  - bin/lsu
@@ -1,38 +0,0 @@
1
- require 'rubygems'
2
- require 'choice'
3
-
4
-
5
- Choice.options do
6
-
7
- header ''
8
- header ' options:'
9
-
10
- option :config, :required=>true do
11
- short '-c'
12
- long '--config'
13
- desc 'The Yaml config file'
14
- end
15
-
16
- option :filter, :required=>true do
17
- short '-f'
18
- long '--filter'
19
- desc 'The LDAP query'
20
- end
21
-
22
- option :attributes, :required=>false do
23
- short '-a'
24
- long '--attributes *ATTRIBUTES'
25
- desc 'The attributes list'
26
- end
27
-
28
- separator ''
29
- separator ' help:'
30
-
31
- option :help do
32
- short '-h'
33
- long '--help'
34
- desc 'Show this help screen'
35
- end
36
-
37
- separator ''
38
- end