open_directory 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rake"
4
+ # Specify your gem's dependencies in open_directory.gemspec
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Mauro Morales
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # OpenDirectory
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'open_directory'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install open_directory
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,10 @@
1
+ require "net/ssh"
2
+
3
+ require "open_directory/version"
4
+ require "open_directory/configuration"
5
+ require "open_directory/user"
6
+ require "open_directory/dscl"
7
+
8
+ module OpenDirectory
9
+ extend Configuration
10
+ end
@@ -0,0 +1,31 @@
1
+ module OpenDirectory
2
+ module Configuration
3
+ VALID_OPTIONS_KEYS = %w(host_name host_username host_password od_username od_password od_datasource).freeze
4
+
5
+ HOST_NAME = nil
6
+ HOST_USERNAME = nil
7
+ HOST_PASSWORD = nil
8
+ OD_USERNAME = nil
9
+ OD_PASSWORD = nil
10
+ OD_DATASOURCE = '/LDAPv3/127.0.0.1/'
11
+
12
+ attr_accessor *VALID_OPTIONS_KEYS
13
+
14
+ def self.extended(base)
15
+ base.reset
16
+ end
17
+
18
+ def reset
19
+ self.host_name = HOST_NAME
20
+ self.host_username = HOST_USERNAME
21
+ self.host_password = HOST_PASSWORD
22
+ self.od_username = OD_USERNAME
23
+ self.od_password = OD_PASSWORD
24
+ self.od_datasource = OD_DATASOURCE
25
+ end
26
+
27
+ def configure
28
+ yield self
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,48 @@
1
+ require "plist"
2
+
3
+ module OpenDirectory
4
+ class Dscl
5
+ @@commands = []
6
+
7
+ def self.generate(command, path, params="")
8
+ datasource ||= OpenDirectory.od_datasource
9
+ params = params.join(" ") unless params.empty?
10
+
11
+ @@commands << "dscl -plist -u #{OpenDirectory.od_username} -P #{OpenDirectory.od_password} #{datasource} -#{command} #{path} #{params}"
12
+ end
13
+
14
+ def self.parse_output(string)
15
+ begin
16
+ parsed_xml = Plist::parse_xml( string )
17
+ if parsed_xml.nil?
18
+ string.split("\n")
19
+ else
20
+ parsed_xml
21
+ end
22
+ rescue RuntimeError => e
23
+ string.split("\n")
24
+ rescue TypeError => e
25
+ if e.nil?
26
+ ""
27
+ else
28
+ e.to_s
29
+ end
30
+ end
31
+ end
32
+
33
+ def self.run
34
+ response = []
35
+ $ssh ||= Net::SSH.start(OpenDirectory.host_name, OpenDirectory.host_username, :password => OpenDirectory.host_password)
36
+ @@commands.each do |command|
37
+ output = $ssh.exec!(command)
38
+ response << {:parsed_out => parse_output(output), :output => output, :command => command} unless output.nil?
39
+ end
40
+ @@commands.clear
41
+ if response.size == 1
42
+ response[0][:parsed_out]
43
+ else
44
+ response
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,73 @@
1
+ module OpenDirectory
2
+ class User
3
+ def self.all
4
+ Dscl.generate("list","/Users")
5
+ Dscl.run
6
+ end
7
+
8
+ def self.exists?(username)
9
+ users = all
10
+ users.include?(username)
11
+ end
12
+
13
+ def self.read(username, params="")
14
+ if exists?(username)
15
+ Dscl.generate("read", "/Users/#{username}",params)
16
+ Dscl.run
17
+ else
18
+ false
19
+ end
20
+ end
21
+
22
+ def self.auth(username, password)
23
+ params = [password]
24
+ Dscl.generate("authonly", username, params)
25
+ output = Dscl.run
26
+ if output.empty?
27
+ true
28
+ else
29
+ !output[0].include?("eDSAuthFailed")
30
+ end
31
+ end
32
+
33
+ def self.create(username, password, params)
34
+ Dscl.generate("create", "/Users/#{username}")
35
+ Dscl.generate("passwd", "/Users/#{username}", ["'#{password}'"])
36
+ Dscl.generate("create", "/Users/#{username}", ["NFSHomeDirectory", "/Users/#{username}"])
37
+ params.each do |key, value|
38
+ if key == :Keywords
39
+ value.each do |keyword|
40
+ Dscl.generate("append", "/Users/#{username}", [key, "'#{keyword}'"])
41
+ end
42
+ else
43
+ Dscl.generate("create", "/Users/#{username}", [key, "'#{value}'"])
44
+ end
45
+ end
46
+ Dscl.run
47
+ end
48
+
49
+ def self.delete(username)
50
+ Dscl.generate("delete", "/Users/#{username}")
51
+ Dscl.run
52
+ end
53
+
54
+ def self.active?(username)
55
+ record = read(username, ["AuthenticationAuthority"])
56
+ !record["dsAttrTypeStandard:AuthenticationAuthority"].include? ";DisabledUser;"
57
+ end
58
+
59
+ def self.disable(username)
60
+ params = %w(AuthenticationAuthority ';DisabledUser;')
61
+ Dscl.generate("append", "/Users/#{username}", params)
62
+ Dscl.run
63
+ end
64
+
65
+ def self.enable(username)
66
+ response = read(username, ["AuthenticationAuthority"])
67
+ response["dsAttrTypeStandard:AuthenticationAuthority"].delete(";DisabledUser;")
68
+ params = ["dsAttrTypeStandard:AuthenticationAuthority", "'" + response["dsAttrTypeStandard:AuthenticationAuthority"].join(" ") + "'"]
69
+ Dscl.generate("create", "/Users/#{username}", params)
70
+ Dscl.run
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ module OpenDirectory
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'open_directory/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "open_directory"
8
+ gem.version = OpenDirectory::VERSION
9
+ gem.authors = ["Mauro Morales"]
10
+ gem.email = ["contact@mauromorales.com"]
11
+ gem.description = "Interact with Open Directory"
12
+ gem.summary = "Generate dscl commands and run them on OD"
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "plist"
21
+ gem.add_dependency "net-ssh"
22
+
23
+ gem.add_development_dependency "rspec", "~> 2.6"
24
+ end
@@ -0,0 +1,71 @@
1
+ require 'open_directory'
2
+
3
+ describe OpenDirectory do
4
+ before(:all) do
5
+ OpenDirectory.configure do |config|
6
+ config.host_name = 'yourhost.com'
7
+ config.host_username = 'deploy'
8
+ config.host_password = 'd3pl0y'
9
+ config.od_username = 'admin'
10
+ config.od_password = '4dm1n'
11
+ end
12
+
13
+ @username = 'jdoe'
14
+ @password = 'super5ecret!'
15
+ end
16
+
17
+ it "should list all users" do
18
+ users = OpenDirectory::User.all
19
+ users.class.should == Array
20
+ users.size.should_not == 0
21
+ end
22
+
23
+ it "should create a new user" do
24
+ params = { :UniqueID => '9999',
25
+ :LastName => 'Doe',
26
+ :FirstName => 'John',
27
+ :EMailAddress => 'jdoe@johndoe.com',
28
+ :Keywords => ['test', 'ruby']
29
+ }
30
+ OpenDirectory::User.create(@username, @password, params)
31
+ OpenDirectory::User.exists?(@username).should == true
32
+ end
33
+
34
+ it "should know if a user exists" do
35
+ OpenDirectory::User.exists?(@username).should == true
36
+ username = "idontexists"
37
+ OpenDirectory::User.exists?(username).should == false
38
+ end
39
+
40
+ it "should read an existing user" do
41
+ record = OpenDirectory::User.read(@username)
42
+ record["dsAttrTypeStandard:RecordName"][0].should == @username
43
+ username = "idontexists"
44
+ OpenDirectory::User.read(username).should == false
45
+ end
46
+
47
+ it "should know if a user is active" do
48
+ OpenDirectory::User.active?(@username).should == true
49
+ end
50
+
51
+ it "should be able to disable an active user" do
52
+ OpenDirectory::User.disable(@username)
53
+ OpenDirectory::User.active?(@username).should_not == true
54
+ end
55
+
56
+ it "should be able to enable an inactive user" do
57
+ OpenDirectory::User.enable(@username)
58
+ OpenDirectory::User.active?(@username).should == true
59
+ end
60
+
61
+ it "should check user authentication" do
62
+ OpenDirectory::User.auth(@username, @password).should == true
63
+ password = "badpass"
64
+ OpenDirectory::User.auth(@username, password).should == false
65
+ end
66
+
67
+ it "should delete an existing user" do
68
+ OpenDirectory::User.delete(@username)
69
+ OpenDirectory::User.exists?(@username).should == false
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_directory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mauro Morales
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: plist
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: net-ssh
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.6'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.6'
62
+ description: Interact with Open Directory
63
+ email:
64
+ - contact@mauromorales.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - lib/open_directory.rb
76
+ - lib/open_directory/configuration.rb
77
+ - lib/open_directory/dscl.rb
78
+ - lib/open_directory/user.rb
79
+ - lib/open_directory/version.rb
80
+ - open_directory.gemspec
81
+ - spec/open_directory_spec.rb
82
+ homepage: ''
83
+ licenses: []
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ segments:
95
+ - 0
96
+ hash: -4132442276101860958
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ segments:
104
+ - 0
105
+ hash: -4132442276101860958
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 1.8.24
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Generate dscl commands and run them on OD
112
+ test_files:
113
+ - spec/open_directory_spec.rb