ldap-shell-utils 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/lsu CHANGED
@@ -1,36 +1,12 @@
1
- #!/usr/bin/env ruby
1
+ #!/usr/bin/env ruby -W0
2
2
 
3
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
3
4
 
4
- $:.unshift( File.join( File.dirname( __FILE__ ), %w[.. lib] ) )
5
-
6
-
7
- require 'rubygems'
8
- require 'slop'
9
5
  require 'ldap-shell-utils'
10
6
 
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
7
+ LdapShellUtils::CliApplication.run( {
8
+ :config =>Choice.choices[:config],
9
+ :filter =>Choice.choices[:filter],
10
+ :attributes =>Choice.choices[:attributes],
11
+ :audit =>Choice.choices[:audit]
12
+ })
@@ -6,20 +6,21 @@ module LdapShellUtils
6
6
  module CliApplication
7
7
  extend self
8
8
 
9
- def print_entry( entry )
9
+ def self.run(options)
10
10
 
11
- puts "#{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.configure( File.expand_path( options[:config] ) )
18
-
19
- results = LdapConnection.new( ConfigContext.url, ConfigContext.to_hash ).search( options[:filter], options[:attributes], options[:audit] )
20
- results.each { |e| print_entry( e ) } if results
21
- rescue Exception => e
22
- $stderr.puts( e.message )
11
+ ConfigContext.configure(File.expand_path(options[:config]))
12
+
13
+ options = { :audit=>false }.merge(options)
14
+ results = LdapConnection.new(ConfigContext.to_hash).search(options)
15
+
16
+ results.each do |entry|
17
+ puts "#{entry.dn}"
18
+ entry.each do |attr,values|
19
+ puts "#{attr}: #{(values.size == 1) ? values[0] : values.inspect}" unless attr == :dn
20
+ end
21
+ end if results
22
+ rescue Exception=>e
23
+ $stderr.puts(e.message)
23
24
  end
24
25
  end
25
26
  end
@@ -6,46 +6,43 @@ require 'net/ldap'
6
6
  module LdapShellUtils
7
7
  class LdapConnection
8
8
 
9
- OPERATIONAL_ATTRIBUTES = [
10
- :creatorsname,
11
- :createtimestamp,
12
- :modifiersname,
13
- :modifytimestamp
14
- ]
15
-
16
9
  public
17
- def initialize( url, config )
10
+ def initialize(config)
18
11
 
19
- @uri = URI.parse( url )
20
- @configuration = {
21
- :host => @uri.host,
22
- :port => @uri.port,
23
- :base => config[:base],
24
- :auth => {
25
- :method => :simple,
26
- :username => config[:username],
27
- :password => config[:password]
28
- }
12
+ uri = URI.parse( config[:url] )
13
+ auth_config = {
14
+ :method =>:simple,
15
+ :username =>config[:username],
16
+ :password =>config[:password]
17
+ }
18
+
19
+ configuration = {
20
+ :host=>uri.host,
21
+ :port=>uri.port,
22
+ :base=>config[:base],
23
+ :auth=>auth_config
29
24
  }
30
25
 
31
- @configuration[:encryption] = { :method => :simple_tls } if( @uri.scheme.to_sym == :ldaps )
32
- @connection = Net::LDAP.new( @configuration )
26
+ configuration[:encryption] = { :method => :simple_tls } if(uri.scheme == 'ldaps')
27
+ @connection = Net::LDAP.new(configuration)
33
28
 
34
29
  self
35
- rescue Exception => e
36
- raise ArgumentError.new( e.message )
30
+ rescue StandardError=>e
31
+ raise ArgumentError.new(e.message)
37
32
  end
38
-
39
-
40
- def search( filter, attributes, audit=false )
33
+
34
+ def search(options)
35
+
36
+ ldap_filter = ""
41
37
 
42
- final_attributes = attributes ? attributes : []
43
- final_attributes += OPERATIONAL_ATTRIBUTES if audit
44
- final_filter = Net::LDAP::Filter.construct( filter )
38
+ begin
39
+ ldap_filter = Net::LDAP::Filter.from_rfc2254(options[:filter])
40
+ rescue StandardError=>e
41
+ raise ArgumentError.new(e.message)
42
+ end
45
43
 
46
- @connection.search( :filter=>Net::LDAP::Filter.from_rfc2254( filter ),
47
- :attributes=>final_attributes,
48
- :return_result=>true )
44
+ options[:attributes] += [:creatorsname, :createtimestamp, :modifiersname, :modifytimestamp] if options[:audit]
45
+ @connection.search( :filter=>ldap_filter, :attributes=>options[:attributes], :return_result=>true )
49
46
  end
50
47
  end
51
48
  end
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'choice'
3
+
4
+ Choice.options do
5
+
6
+ header ''
7
+ header ' options:'
8
+
9
+ option :config, :required=>true do
10
+ short '-c'
11
+ long '--config'
12
+ desc 'The LDAP config file'
13
+ end
14
+
15
+ option :filter, :required=>false do
16
+ short '-f'
17
+ long '--filter'
18
+ desc 'LDAP query filter'
19
+ default "(objectClass=*)"
20
+ end
21
+
22
+ option :attributes, :required=>false do
23
+ short '-a'
24
+ long '--attributes *ATTRIBUTES'
25
+ desc 'The attributes list'
26
+ default [:dn]
27
+ end
28
+
29
+ option :audit, :required=>false do
30
+ long '--audit'
31
+ desc 'Audit attributes'
32
+ default false
33
+ end
34
+
35
+ separator ''
36
+ separator ' help:'
37
+
38
+ option :help do
39
+ short '-h'
40
+ long '--help'
41
+ desc 'Show this help screen'
42
+ end
43
+
44
+ separator ''
45
+ end
@@ -2,8 +2,8 @@ module LdapShellUtils
2
2
  module Version
3
3
  INFO = {
4
4
  :major =>0,
5
- :minor =>0,
6
- :patch =>4
5
+ :minor =>1,
6
+ :patch =>0
7
7
  }
8
8
 
9
9
  NAME = 'ldap-shell-utils'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ldap-shell-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,8 +12,8 @@ cert_chain: []
12
12
  date: 2011-06-11 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: slop
16
- requirement: &2161265180 !ruby/object:Gem::Requirement
15
+ name: choice
16
+ requirement: &2156251920 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2161265180
24
+ version_requirements: *2156251920
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: net-ldap
27
- requirement: &2161238240 !ruby/object:Gem::Requirement
27
+ requirement: &2156251440 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2161238240
35
+ version_requirements: *2156251440
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: config_context
38
- requirement: &2161236000 !ruby/object:Gem::Requirement
38
+ requirement: &2156250960 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2161236000
46
+ version_requirements: *2156250960
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &2161216640 !ruby/object:Gem::Requirement
49
+ requirement: &2156250480 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.0.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2161216640
57
+ version_requirements: *2156250480
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: shoulda
60
- requirement: &2161215240 !ruby/object:Gem::Requirement
60
+ requirement: &2156250000 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2161215240
68
+ version_requirements: *2156250000
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: jeweler
71
- requirement: &2161213560 !ruby/object:Gem::Requirement
71
+ requirement: &2156249520 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 1.5.1
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2161213560
79
+ version_requirements: *2156249520
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rcov
82
- requirement: &2161212200 !ruby/object:Gem::Requirement
82
+ requirement: &2156249040 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *2161212200
90
+ version_requirements: *2156249040
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: net-ldap
93
- requirement: &2161210580 !ruby/object:Gem::Requirement
93
+ requirement: &2156248560 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :runtime
100
100
  prerelease: false
101
- version_requirements: *2161210580
101
+ version_requirements: *2156248560
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: slop
104
- requirement: &2161206140 !ruby/object:Gem::Requirement
104
+ requirement: &2156248080 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :runtime
111
111
  prerelease: false
112
- version_requirements: *2161206140
112
+ version_requirements: *2156248080
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: config_context
115
- requirement: &2161204540 !ruby/object:Gem::Requirement
115
+ requirement: &2156247600 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,7 +120,7 @@ dependencies:
120
120
  version: '0'
121
121
  type: :runtime
122
122
  prerelease: false
123
- version_requirements: *2161204540
123
+ version_requirements: *2156247600
124
124
  description: This is a simple wrapper over ldapsearch util avoiding base64 dn and
125
125
  others gotchas
126
126
  email: javier.juarez@gmail.com
@@ -133,6 +133,7 @@ files:
133
133
  - lib/ldap-shell-utils.rb
134
134
  - lib/ldap-shell-utils/cli_application.rb
135
135
  - lib/ldap-shell-utils/ldap_connection.rb
136
+ - lib/ldap-shell-utils/options-parser.rb
136
137
  - lib/ldap-shell-utils/version.rb
137
138
  - README.rdoc
138
139
  - bin/lsu
@@ -151,7 +152,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
151
152
  version: '0'
152
153
  segments:
153
154
  - 0
154
- hash: 3098725512291486326
155
+ hash: 2177724174337720231
155
156
  required_rubygems_version: !ruby/object:Gem::Requirement
156
157
  none: false
157
158
  requirements: