nis-ffi 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ gem "ffi", "~> 1.0.0"
4
+
5
+ # Add dependencies to develop your gem here.
6
+ # Include everything needed to run rake, tests, features, etc.
7
+ group :development do
8
+ gem "bundler", "~> 1.0.0"
9
+ gem "jeweler", "~> 1.5.2"
10
+ gem "shoulda", ">= 0"
11
+ gem "rcov", ">= 0"
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ ffi (1.0.5)
5
+ rake (>= 0.8.7)
6
+ ffi (1.0.5-java)
7
+ git (1.2.5)
8
+ jeweler (1.5.2)
9
+ bundler (~> 1.0.0)
10
+ git (>= 1.2.5)
11
+ rake
12
+ rake (0.8.7)
13
+ rcov (0.9.9)
14
+ rcov (0.9.9-java)
15
+ shoulda (2.11.3)
16
+
17
+ PLATFORMS
18
+ java
19
+ ruby
20
+
21
+ DEPENDENCIES
22
+ bundler (~> 1.0.0)
23
+ ffi (~> 1.0.0)
24
+ jeweler (~> 1.5.2)
25
+ rcov
26
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Robin Stocker
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,32 @@
1
+ = nis-ffi
2
+
3
+ NIS (YP) library using libc's libnsl through ruby-ffi. It is known to work on
4
+ Ruby 1.8.7, 1.9.2 and JRuby 1.6.0.
5
+
6
+ Please note that only a few functions have been implemented, so please fork and
7
+ add the ones you need. It's really easy thanks to ffi!
8
+
9
+ == Usage
10
+
11
+ >> require 'nis-ffi'
12
+ >> NIS.yp_match("nis.example.org", "passwd.byname", "username")
13
+ => "username:42pwdhashBEEF:1000:1111:Full Name:/home/username:/bin/zsh"
14
+
15
+ == Contributing to nis-ffi
16
+
17
+ * Check out the latest master to make sure the feature hasn't been implemented
18
+ or the bug hasn't been fixed yet
19
+ * Check out the issue tracker to make sure someone already hasn't requested it
20
+ and/or contributed it
21
+ * Fork the project
22
+ * Start a feature/bugfix branch
23
+ * Commit and push until you are happy with your contribution
24
+ * Make sure to add tests for it. This is important so I don't break it in a
25
+ future version unintentionally.
26
+ * Please try not to mess with the Rakefile, version, or history. If you want to
27
+ have your own version, or is otherwise necessary, that is fine, but please
28
+ isolate to its own commit so I can cherry-pick around it.
29
+
30
+ == Copyright
31
+
32
+ Copyright (c) 2011 Robin Stocker. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ RDOC_OPTIONS = ['--main', 'README.rdoc']
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ gem.name = "nis-ffi"
17
+ gem.homepage = "http://github.com/robinst/nis-ffi"
18
+ gem.license = "MIT"
19
+ gem.summary = %Q{NIS (YP) library using libc's libnsl through ruby-ffi}
20
+ gem.description = %Q{Library for NIS (YP) queries using the libnsl library of libc through ruby-ffi.}
21
+ gem.email = "robin@nibor.org"
22
+ gem.authors = ["Robin Stocker"]
23
+ gem.add_dependency 'ffi', '~> 1.0.0'
24
+ gem.rdoc_options = RDOC_OPTIONS
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ end
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "nis-ffi #{version}"
50
+ rdoc.options = RDOC_OPTIONS
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.1
data/lib/nis-ffi.rb ADDED
@@ -0,0 +1,94 @@
1
+ require 'ffi'
2
+
3
+ # NIS module providing the yp functions.
4
+ module NIS
5
+
6
+ YPERR_SUCCESS = 0
7
+ YPERR_BADARGS = 1
8
+ YPERR_RPC = 2
9
+ YPERR_DOMAIN = 3
10
+ YPERR_MAP = 4
11
+ YPERR_KEY = 5
12
+ YPERR_YPERR = 6
13
+ YPERR_RESRC = 7
14
+ YPERR_NOMORE = 8
15
+ YPERR_PMAP = 9
16
+ YPERR_YPBIND = 10
17
+ YPERR_YPSERV = 11
18
+ YPERR_NODOM = 12
19
+ YPERR_BADDB = 13
20
+ YPERR_VERS = 14
21
+ YPERR_ACCESS = 15
22
+ YPERR_BUSY = 16
23
+
24
+ # Runtime error for yp functions.
25
+ #
26
+ # Can be raised from each function when something goes wrong (when the return
27
+ # code isn't 0). Includes the error code and a readable message. The code can
28
+ # be checked against the YPERR constants (e.g. NIS::YPERR_BADARGS).
29
+ class YPError < RuntimeError
30
+ attr_reader :code
31
+
32
+ def initialize(code, message)
33
+ super(message)
34
+ @code = code
35
+ end
36
+ end
37
+
38
+ module Library
39
+ extend FFI::Library
40
+ begin
41
+ ffi_lib 'nsl'
42
+ rescue LoadError
43
+ begin
44
+ # fall back to libc (Mac OS X is different)
45
+ ffi_lib 'c'
46
+ rescue LoadError
47
+ raise LoadError, "nis-ffi: Neither libnsl nor libc could be loaded, giving up."
48
+ end
49
+ end
50
+ attach_function 'yp_get_default_domain', [:pointer], :int
51
+ attach_function 'yp_match', [:string, :string, :string, :int, :pointer, :pointer], :int
52
+ attach_function 'yperr_string', [:int], :string
53
+ end
54
+
55
+ # Get the default domain.
56
+ def self.yp_get_default_domain
57
+ domain_ptr = FFI::MemoryPointer.new(:pointer)
58
+ code = Library.yp_get_default_domain(domain_ptr)
59
+ raise_on_error(code)
60
+ str_ptr = domain_ptr.read_pointer
61
+ str_ptr.read_string
62
+ end
63
+
64
+ # Look up a value with a specified map and key.
65
+ #
66
+ # Returns the string value or raises an YPError (even when it's just a key
67
+ # that doesn't exist).
68
+ def self.yp_match(domain, map, key)
69
+ value = FFI::MemoryPointer.new(:pointer)
70
+ value_len = FFI::MemoryPointer.new(:int)
71
+
72
+ code = Library.yp_match(domain, map, key, key.bytesize, value, value_len)
73
+ raise_on_error(code)
74
+
75
+ str_ptr = value.read_pointer
76
+ len = value_len.read_int
77
+ result = str_ptr.read_string(len)
78
+ result
79
+ end
80
+
81
+ # Returns an error string which describes the error code.
82
+ def self.yperr_string(code)
83
+ Library.yperr_string(code)
84
+ end
85
+
86
+ private
87
+
88
+ def self.raise_on_error(code)
89
+ if code != YPERR_SUCCESS
90
+ message = yperr_string(code)
91
+ raise YPError.new(code, message)
92
+ end
93
+ end
94
+ end
data/nis-ffi.gemspec ADDED
@@ -0,0 +1,73 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{nis-ffi}
8
+ s.version = "0.2.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Robin Stocker"]
12
+ s.date = %q{2011-02-18}
13
+ s.description = %q{Library for NIS (YP) queries using the libnsl library of libc through ruby-ffi.}
14
+ s.email = %q{robin@nibor.org}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/nis-ffi.rb",
28
+ "nis-ffi.gemspec",
29
+ "test/helper.rb",
30
+ "test/test_errors.rb",
31
+ "test/test_server.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/robinst/nis-ffi}
34
+ s.licenses = ["MIT"]
35
+ s.rdoc_options = ["--main", "README.rdoc"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.7}
38
+ s.summary = %q{NIS (YP) library using libc's libnsl through ruby-ffi}
39
+ s.test_files = [
40
+ "test/helper.rb",
41
+ "test/test_errors.rb",
42
+ "test/test_server.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
50
+ s.add_runtime_dependency(%q<ffi>, ["~> 1.0.0"])
51
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
52
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
53
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
54
+ s.add_development_dependency(%q<rcov>, [">= 0"])
55
+ s.add_runtime_dependency(%q<ffi>, ["~> 1.0.0"])
56
+ else
57
+ s.add_dependency(%q<ffi>, ["~> 1.0.0"])
58
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
59
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
60
+ s.add_dependency(%q<shoulda>, [">= 0"])
61
+ s.add_dependency(%q<rcov>, [">= 0"])
62
+ s.add_dependency(%q<ffi>, ["~> 1.0.0"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<ffi>, ["~> 1.0.0"])
66
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
67
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
68
+ s.add_dependency(%q<shoulda>, [">= 0"])
69
+ s.add_dependency(%q<rcov>, [">= 0"])
70
+ s.add_dependency(%q<ffi>, ["~> 1.0.0"])
71
+ end
72
+ end
73
+
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'nis-ffi'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,14 @@
1
+ require 'helper'
2
+
3
+ class TestErrors < Test::Unit::TestCase
4
+ should "return error string with yperr_string" do
5
+ assert_match /^RPC failure/, NIS::yperr_string(NIS::YPERR_RPC)
6
+ assert_match /error/, NIS::yperr_string(NIS::YPERR_YPERR)
7
+ end
8
+
9
+ should "raise YPError when server doesn't exist" do
10
+ exception = assert_raise(NIS::YPError) { NIS::yp_match("foo.example.org", "passwd.byname", "foo") }
11
+ assert [NIS::YPERR_YPERR, NIS::YPERR_DOMAIN].include?(exception.code)
12
+ assert_equal NIS::yperr_string(exception.code), exception.message
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ require 'helper'
2
+
3
+ class TestServer < Test::Unit::TestCase
4
+ should "return the default domain" do
5
+ assert_not_nil NIS::yp_get_default_domain()
6
+ end
7
+
8
+ context "The NIS server (default domain)" do
9
+ setup do
10
+ @domain = NIS::yp_get_default_domain()
11
+ end
12
+
13
+ should "return a result on yp_match with the current user" do
14
+ user = ENV['USER']
15
+ result = NIS::yp_match(@domain, "passwd.byname", user)
16
+ assert_not_nil result
17
+ values = result.split(":")
18
+ assert_equal user, values[0]
19
+ assert values.length > 1
20
+ end
21
+
22
+ should "not return a result on yp_match with an inexisting user" do
23
+ user = "thisisreallynotausernameihope"
24
+ exception = assert_raise(NIS::YPError) { result = NIS::yp_match(@domain, "passwd.byname", user) }
25
+ assert_equal NIS::YPERR_KEY, exception.code
26
+ end
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nis-ffi
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 1
10
+ version: 0.2.1
11
+ platform: ruby
12
+ authors:
13
+ - Robin Stocker
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-18 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: ffi
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 23
29
+ segments:
30
+ - 1
31
+ - 0
32
+ - 0
33
+ version: 1.0.0
34
+ prerelease: false
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 23
45
+ segments:
46
+ - 1
47
+ - 0
48
+ - 0
49
+ version: 1.0.0
50
+ prerelease: false
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: jeweler
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 7
61
+ segments:
62
+ - 1
63
+ - 5
64
+ - 2
65
+ version: 1.5.2
66
+ prerelease: false
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: shoulda
71
+ requirement: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ prerelease: false
81
+ type: :development
82
+ version_requirements: *id004
83
+ - !ruby/object:Gem::Dependency
84
+ name: rcov
85
+ requirement: &id005 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ prerelease: false
95
+ type: :development
96
+ version_requirements: *id005
97
+ - !ruby/object:Gem::Dependency
98
+ name: ffi
99
+ requirement: &id006 !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ~>
103
+ - !ruby/object:Gem::Version
104
+ hash: 23
105
+ segments:
106
+ - 1
107
+ - 0
108
+ - 0
109
+ version: 1.0.0
110
+ prerelease: false
111
+ type: :runtime
112
+ version_requirements: *id006
113
+ description: Library for NIS (YP) queries using the libnsl library of libc through ruby-ffi.
114
+ email: robin@nibor.org
115
+ executables: []
116
+
117
+ extensions: []
118
+
119
+ extra_rdoc_files:
120
+ - LICENSE.txt
121
+ - README.rdoc
122
+ files:
123
+ - .document
124
+ - Gemfile
125
+ - Gemfile.lock
126
+ - LICENSE.txt
127
+ - README.rdoc
128
+ - Rakefile
129
+ - VERSION
130
+ - lib/nis-ffi.rb
131
+ - nis-ffi.gemspec
132
+ - test/helper.rb
133
+ - test/test_errors.rb
134
+ - test/test_server.rb
135
+ has_rdoc: true
136
+ homepage: http://github.com/robinst/nis-ffi
137
+ licenses:
138
+ - MIT
139
+ post_install_message:
140
+ rdoc_options:
141
+ - --main
142
+ - README.rdoc
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ hash: 3
151
+ segments:
152
+ - 0
153
+ version: "0"
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ none: false
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ hash: 3
160
+ segments:
161
+ - 0
162
+ version: "0"
163
+ requirements: []
164
+
165
+ rubyforge_project:
166
+ rubygems_version: 1.3.7
167
+ signing_key:
168
+ specification_version: 3
169
+ summary: NIS (YP) library using libc's libnsl through ruby-ffi
170
+ test_files:
171
+ - test/helper.rb
172
+ - test/test_errors.rb
173
+ - test/test_server.rb