jruby-ldap 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Ola Bini <ola.bini@gmail.com>
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 ADDED
@@ -0,0 +1,14 @@
1
+ JRuby/LDAP
2
+ -----------
3
+
4
+ This is an interface compatible port of Ruby/LDAP - to allow binary LDAP usage through JRuby. This implementation is pure Ruby, but uses the Java Integration features of JRuby to access the JNDI libraries and through these implement the correct functionality.
5
+
6
+ Many classes are missing right now, but the base functionality should be there.
7
+
8
+
9
+ Usage
10
+ -----
11
+ require 'rubygems'
12
+ require 'ldap'
13
+
14
+ And then use it like all tutorials of Ruby/LDAP show you.
@@ -0,0 +1,42 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ task :default => [:test, :package]
5
+
6
+ desc "Run all tests"
7
+ task :test => [:test_all]
8
+
9
+ Rake::TestTask.new(:test_all) do |t|
10
+ t.test_files = FileList['test/**/test_*.rb']
11
+ t.libs << 'test'
12
+ t.libs.delete("lib") unless defined?(JRUBY_VERSION)
13
+ end
14
+
15
+
16
+ task :filelist do
17
+ puts FileList['pkg/**/*'].inspect
18
+ end
19
+
20
+ MANIFEST = FileList["lib/**/*.rb", "test/**/*.rb", "Rakefile", "LICENSE", "README"]
21
+
22
+ file "Manifest.txt" => :manifest
23
+ task :manifest do
24
+ File.open("Manifest.txt", "w") {|f| MANIFEST.each {|n| f << "#{n}\n"} }
25
+ end
26
+
27
+ Rake::Task['manifest'].invoke # Always regen manifest, so Hoe has up-to-date list of files
28
+
29
+ begin
30
+ require 'hoe'
31
+ Hoe.new("jruby-ldap", "0.0.1") do |p|
32
+ p.rubyforge_name = "jruby-extras"
33
+ p.url = "http://jruby-extras.rubyforge.org/jruby-ldap"
34
+ p.author = "Ola Bini"
35
+ p.email = "ola.bini@gmail.com"
36
+ p.summary = "Port of Ruby/LDAP to JRuby"
37
+ end.spec.dependencies.delete_if { |dep| dep.name == "hoe" }
38
+ rescue LoadError
39
+ puts "You really need Hoe installed to be able to package this gem"
40
+ rescue => e
41
+ puts "ignoring error while loading hoe: #{e.to_s}"
42
+ end
@@ -0,0 +1,83 @@
1
+ require 'java'
2
+
3
+ module LDAP
4
+ def self.err2string(err)
5
+ case err||0
6
+ when -1: "Can't contact LDAP server"
7
+ when 0: "Success"
8
+ when 1: "Operations error"
9
+ when 2: "Protocol error"
10
+ when 3: "Time limit exceeded"
11
+ when 4: "Size limit exceeded"
12
+ when 5: "Compare False"
13
+ when 6: "Compare True"
14
+ when 7: "Authentication method not supported"
15
+ when 8: "Strong(er) authentication required"
16
+ when 9: "Partial results and referral received"
17
+ when 10: "Referral"
18
+ when 11: "Administrative limit exceeded"
19
+ when 12: "Critical extension is unavailable"
20
+ when 13: "Confidentiality required"
21
+ when 14: "SASL bind in progress"
22
+ when 15: "Unknown error"
23
+ when 16: "No such attribute"
24
+ when 17: "Undefined attribute type"
25
+ when 18: "Inappropriate matching"
26
+ when 19: "Constraint violation"
27
+ when 20: "Type or value exists"
28
+ when 21: "Invalid syntax"
29
+ when 32: "No such object"
30
+ when 33: "Alias problem"
31
+ when 34: "Invalid DN syntax"
32
+ when 35: "Entry is a leaf"
33
+ when 36: "Alias dereferencing problem"
34
+ when 47: "Proxy Authorization Failure"
35
+ when 48: "Inappropriate authentication"
36
+ when 49: "Invalid credentials"
37
+ when 50: "Insufficient access"
38
+ when 51: "Server is busy"
39
+ when 52: "Server is unavailable"
40
+ when 53: "Server is unwilling to perform"
41
+ when 54: "Loop detected"
42
+ when 64: "Naming violation"
43
+ when 65: "Object class violation"
44
+ when 66: "Operation not allowed on non-leaf"
45
+ when 67: "Operation not allowed on RDN"
46
+ when 68: "Already exists"
47
+ when 69: "Cannot modify object class"
48
+ when 70: "Results too large"
49
+ when 71: "Operation affects multiple DSAs"
50
+ when 80: "Internal (implementation specific) error"
51
+ else "Unknown error"
52
+ end
53
+ end
54
+
55
+ def self.load_configuration(attrs={})
56
+ env = nil
57
+ env = javax.naming.directory.InitialDirContext.new.environment rescue nil
58
+ default = {'java.naming.factory.initial' => 'com.sun.jndi.ldap.LdapCtxFactory'}
59
+ if env
60
+ env2 = default.dup
61
+ env.each do |k,v|
62
+ env2[k.to_s] = v.to_s
63
+ end
64
+ env = env2
65
+ else
66
+ env = default.dup
67
+ end
68
+ env.merge! attrs
69
+ @environment = env
70
+ end
71
+
72
+ def self.configuration(attrs = { })
73
+ @environment.update attrs
74
+ end
75
+ end
76
+
77
+ require 'ldap/constants'
78
+ require 'ldap/conn'
79
+ require 'ldap/entry'
80
+ require 'ldap/error'
81
+ require 'ldap/mod'
82
+
83
+ LDAP.load_configuration
@@ -0,0 +1,250 @@
1
+ module LDAP
2
+ module ConnImplementation
3
+ def compare(*args)
4
+ raise "NOT IMPLEMENTED"
5
+ end
6
+
7
+ def controls(*args)
8
+ raise "NOT IMPLEMENTED"
9
+ end
10
+
11
+ def get_option(*args)
12
+ raise "NOT IMPLEMENTED"
13
+ end
14
+
15
+ def modrdn(*args)
16
+ raise "NOT IMPLEMENTED"
17
+ end
18
+
19
+ def perror(*args)
20
+ raise "NOT IMPLEMENTED"
21
+ end
22
+
23
+ def referrals(*args)
24
+ raise "NOT IMPLEMENTED"
25
+ end
26
+
27
+ def result2error(*args)
28
+ raise "NOT IMPLEMENTED"
29
+ end
30
+
31
+ def __jndi_context
32
+ @context
33
+ end
34
+
35
+ def initialize(host='localhost', port=LDAP_PORT)
36
+ @host = host
37
+ @port = port
38
+ end
39
+
40
+ def err
41
+ @err || 0
42
+ end
43
+
44
+ def err2string(err)
45
+ LDAP.err2string(err)
46
+ end
47
+
48
+ def simple_bind(dn=nil, password=nil, &block)
49
+ bind(dn, password, LDAP_AUTH_SIMPLE, &block)
50
+ end
51
+
52
+ def bind(dn=nil, password=nil, method=LDAP_AUTH_SIMPLE)
53
+ raise LDAP::Error, "already bound" if bound?
54
+
55
+ url = @use_ssl ? "ldaps://#@host:#@port/" : "ldap://#@host:#@port/"
56
+ base_env = {javax.naming.Context::PROVIDER_URL => url}
57
+ base_env[javax.naming.Context::SECURITY_PRINCIPAL] = dn if dn
58
+ base_env[javax.naming.Context::SECURITY_CREDENTIALS] = password if password
59
+
60
+ @current_env = java.util.Hashtable.new(LDAP::configuration(base_env))
61
+
62
+ begin
63
+ @context = javax.naming.directory.InitialDirContext.new(@current_env)
64
+ @err = 0
65
+ rescue javax.naming.NoPermissionException => e
66
+ @err = 50
67
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
68
+ rescue javax.naming.NamingException => e
69
+ @err = -1
70
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
71
+ end
72
+
73
+ if !block_given?
74
+ return self
75
+ end
76
+
77
+ begin
78
+ yield self
79
+
80
+ return nil
81
+ ensure
82
+ unbind
83
+ end
84
+ end
85
+
86
+ def set_option(opt, value)
87
+ @err = 0
88
+ end
89
+
90
+ def add(dn, attrs)
91
+ raise LDAP::InvalidDataError, "The LDAP handler has already unbound." unless bound?
92
+
93
+ attrs = LDAP::hash2mods(LDAP::LDAP_MOD_ADD, attrs) if attrs.is_a?(Hash)
94
+
95
+ begin
96
+ @context.create_subcontext(dn, LDAP::Mod.to_java_attributes(*attrs))
97
+ @err = 0
98
+ rescue javax.naming.NameNotFoundException => e
99
+ @err = 32
100
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
101
+ rescue javax.naming.InvalidNameException => e
102
+ @err = 34
103
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
104
+ rescue javax.naming.NoPermissionException => e
105
+ @err = 50
106
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
107
+ rescue javax.naming.directory.SchemaViolationException => e
108
+ @err = 65
109
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
110
+ rescue javax.naming.NamingException => e
111
+ @err = 21
112
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
113
+ end
114
+ self
115
+ end
116
+
117
+ def modify(dn, attrs)
118
+ raise LDAP::InvalidDataError, "The LDAP handler has already unbound." unless bound?
119
+
120
+ attrs = LDAP::hash2mods(LDAP::LDAP_MOD_REPLACE, attrs) if attrs.is_a?(Hash)
121
+
122
+ begin
123
+ @context.modify_attributes(dn, LDAP::Mod.to_java_modification_items(*attrs))
124
+ @err = 0
125
+ rescue javax.naming.NameNotFoundException => e
126
+ @err = 32
127
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
128
+ rescue javax.naming.InvalidNameException => e
129
+ @err = 34
130
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
131
+ rescue javax.naming.NoPermissionException => e
132
+ @err = 50
133
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
134
+ rescue javax.naming.directory.SchemaViolationException => e
135
+ @err = 65
136
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
137
+ rescue javax.naming.NamingException => e
138
+ @err = 21
139
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
140
+ end
141
+
142
+ self
143
+ end
144
+
145
+ def delete(dn)
146
+ raise LDAP::InvalidDataError, "The LDAP handler has already unbound." unless bound?
147
+
148
+ begin
149
+ @context.destroy_subcontext(dn)
150
+ @err = 0
151
+ rescue javax.naming.NameNotFoundException => e
152
+ @err = 32
153
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
154
+ rescue javax.naming.InvalidNameException => e
155
+ @err = 34
156
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
157
+ rescue javax.naming.NoPermissionException => e
158
+ @err = 50
159
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
160
+ rescue javax.naming.NamingException => e
161
+ @err = 21
162
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
163
+ end
164
+ self
165
+ end
166
+
167
+ def search(base_dn, scope, filter, attrs=nil, attrsonly=nil, sec=0, usec=0, s_attr=nil, s_proc=nil)
168
+ raise LDAP::InvalidDataError, "The LDAP handler has already unbound." unless bound?
169
+
170
+ controls = javax.naming.directory.SearchControls.new
171
+ controls.search_scope = scope
172
+
173
+ if attrs && !attrs.empty?
174
+ controls.returning_attributes = attrs.to_java(:string)
175
+ end
176
+ if attrsonly
177
+ controls.returning_obj_flag = true
178
+ end
179
+
180
+ if sec != 0 || usec != 0
181
+ controls.time_limit = usec/1000 + sec*1000
182
+ end
183
+
184
+ begin
185
+ @context.search(base_dn, filter, controls).each do |val|
186
+ yield LDAP::Entry.create_from_search_result(val)
187
+ end
188
+
189
+ @err = 0
190
+ rescue javax.naming.NameNotFoundException => e
191
+ @err = 32
192
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
193
+ rescue javax.naming.InvalidNameException => e
194
+ @err = 34
195
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
196
+ rescue javax.naming.NoPermissionException => e
197
+ @err = 50
198
+ raise LDAP::ResultError.wrap(LDAP::err2string(@err), e)
199
+ end
200
+
201
+ self
202
+ end
203
+
204
+ def search2(base_dn, scope, filter, attrs=nil, attrsonly=nil, sec=0, usec=0, s_attr=nil, s_proc=nil)
205
+ arr = []
206
+ search(base_dn, scope, filter, attrs, attrsonly, sec, usec, s_attr, s_proc) do |val|
207
+ arr << LDAP::entry2hash(val)
208
+ end
209
+ arr
210
+ end
211
+
212
+ def unbind
213
+ raise LDAP::InvalidDataError, "The LDAP handler has already unbound." unless bound?
214
+ @context.close
215
+ @err = 0
216
+ @context = nil
217
+ end
218
+
219
+ def bound?
220
+ !@context.nil?
221
+ end
222
+ end
223
+
224
+
225
+ class Conn
226
+ class << self
227
+ alias open new
228
+ end
229
+
230
+ def initialize(host='localhost', port=LDAP_PORT)
231
+ super
232
+ @use_ssl = false
233
+ end
234
+
235
+ include ConnImplementation
236
+ end
237
+
238
+ class SSLConn
239
+ class << self
240
+ alias open new
241
+ end
242
+
243
+ def initialize(host='localhost', port=LDAPS_PORT)
244
+ super
245
+ @use_ssl = true
246
+ end
247
+
248
+ include ConnImplementation
249
+ end
250
+ end
@@ -0,0 +1,126 @@
1
+ module LDAP
2
+ LDAPS_PORT = 636
3
+ LDAP_ADMINLIMIT_EXCEEDED = 11
4
+ LDAP_ALIAS_DEREF_PROBLEM = 36
5
+ LDAP_ALIAS_PROBLEM = 33
6
+ LDAP_ALREADY_EXISTS = 68
7
+ LDAP_API_INFO_VERSION = 1
8
+ LDAP_API_VERSION = 3001
9
+ LDAP_AUTH_KRBV41 = 129
10
+ LDAP_AUTH_KRBV42 = 130
11
+ LDAP_AUTH_METHOD_NOT_SUPPORTED = 7
12
+ LDAP_AUTH_NONE = 0
13
+ LDAP_AUTH_SASL = 163
14
+ LDAP_AUTH_SIMPLE = 128
15
+ LDAP_AUTH_UNKNOWN = -6
16
+ LDAP_BUSY = 51
17
+ LDAP_COMPARE_FALSE = 5
18
+ LDAP_COMPARE_TRUE = 6
19
+ LDAP_CONFIDENTIALITY_REQUIRED = 13
20
+ LDAP_CONSTRAINT_VIOLATION = 19
21
+ LDAP_CONTROL_PAGEDRESULTS = "1.2.840.113556.1.4.319"
22
+ LDAP_DECODING_ERROR = -4
23
+ LDAP_DEREF_ALWAYS = 3
24
+ LDAP_DEREF_FINDING = 2
25
+ LDAP_DEREF_NEVER = 0
26
+ LDAP_DEREF_SEARCHING = 1
27
+ LDAP_ENCODING_ERROR = -3
28
+ LDAP_FILTER_ERROR = -7
29
+ LDAP_INAPPROPRIATE_AUTH = 48
30
+ LDAP_INAPPROPRIATE_MATCHING = 18
31
+ LDAP_INSUFFICIENT_ACCESS = 50
32
+ LDAP_INVALID_CREDENTIALS = 49
33
+ LDAP_INVALID_DN_SYNTAX = 34
34
+ LDAP_INVALID_SYNTAX = 21
35
+ LDAP_IS_LEAF = 35
36
+ LDAP_LOCAL_ERROR = -2
37
+ LDAP_LOOP_DETECT = 54
38
+ LDAP_MOD_ADD = 0
39
+ LDAP_MOD_BVALUES = 128
40
+ LDAP_MOD_DELETE = 1
41
+ LDAP_MOD_INCREMENT = 3
42
+ LDAP_MOD_OP = 7
43
+ LDAP_MOD_REPLACE = 2
44
+ LDAP_NAMING_VIOLATION = 64
45
+ LDAP_NOT_ALLOWED_ON_NONLEAF = 66
46
+ LDAP_NOT_ALLOWED_ON_RDN = 67
47
+ LDAP_NO_MEMORY = -10
48
+ LDAP_NO_OBJECT_CLASS_MODS = 69
49
+ LDAP_NO_SUCH_ATTRIBUTE = 16
50
+ LDAP_NO_SUCH_OBJECT = 32
51
+ LDAP_OBJECT_CLASS_VIOLATION = 65
52
+ LDAP_OPERATIONS_ERROR = 1
53
+ LDAP_OPT_API_FEATURE_INFO = 21
54
+ LDAP_OPT_API_INFO = 0
55
+ LDAP_OPT_CLIENT_CONTROLS = 19
56
+ LDAP_OPT_DEREF = 2
57
+ LDAP_OPT_DESC = 1
58
+ LDAP_OPT_HOST_NAME = 48
59
+ LDAP_OPT_OFF = 0
60
+ LDAP_OPT_ON = 3117220
61
+ LDAP_OPT_PROTOCOL_VERSION = 17
62
+ LDAP_OPT_REFERRALS = 8
63
+ LDAP_OPT_RESTART = 9
64
+ LDAP_OPT_SERVER_CONTROLS = 18
65
+ LDAP_OPT_SIZELIMIT = 3
66
+ LDAP_OPT_TIMELIMIT = 4
67
+ LDAP_OPT_X_SASL_AUTHCID = 24834
68
+ LDAP_OPT_X_SASL_AUTHZID = 24835
69
+ LDAP_OPT_X_SASL_MAXBUFSIZE = 24841
70
+ LDAP_OPT_X_SASL_MECH = 24832
71
+ LDAP_OPT_X_SASL_REALM = 24833
72
+ LDAP_OPT_X_SASL_SECPROPS = 24838
73
+ LDAP_OPT_X_SASL_SSF = 24836
74
+ LDAP_OPT_X_SASL_SSF_EXTERNAL = 24837
75
+ LDAP_OPT_X_SASL_SSF_MAX = 24840
76
+ LDAP_OPT_X_SASL_SSF_MIN = 24839
77
+ LDAP_OPT_X_TLS = 24576
78
+ LDAP_OPT_X_TLS_ALLOW = 3
79
+ LDAP_OPT_X_TLS_CACERTDIR = 24579
80
+ LDAP_OPT_X_TLS_CACERTFILE = 24578
81
+ LDAP_OPT_X_TLS_CERTFILE = 24580
82
+ LDAP_OPT_X_TLS_CIPHER_SUITE = 24584
83
+ LDAP_OPT_X_TLS_DEMAND = 2
84
+ LDAP_OPT_X_TLS_HARD = 1
85
+ LDAP_OPT_X_TLS_KEYFILE = 24581
86
+ LDAP_OPT_X_TLS_NEVER = 0
87
+ LDAP_OPT_X_TLS_RANDOM_FILE = 24585
88
+ LDAP_OPT_X_TLS_REQUIRE_CERT = 24582
89
+ LDAP_OPT_X_TLS_TRY = 4
90
+ LDAP_OTHER = 80
91
+ LDAP_PARAM_ERROR = -9
92
+ LDAP_PARTIAL_RESULTS = 9
93
+ LDAP_PORT = 389
94
+ LDAP_PROTOCOL_ERROR = 2
95
+ LDAP_REFERRAL = 10
96
+ LDAP_RESULTS_TOO_LARGE = 70
97
+ LDAP_SASL_BIND_IN_PROGRESS = 14
98
+ LDAP_SASL_SIMPLE = nil
99
+ LDAP_SCOPE_BASE = 0
100
+ LDAP_SCOPE_ONELEVEL = 1
101
+ LDAP_SCOPE_SUBTREE = 2
102
+ LDAP_SERVER_DOWN = -1
103
+ LDAP_SIZELIMIT_EXCEEDED = 4
104
+ LDAP_STRONG_AUTH_NOT_SUPPORTED = 7
105
+ LDAP_STRONG_AUTH_REQUIRED = 8
106
+ LDAP_SUCCESS = 0
107
+ LDAP_TIMELIMIT_EXCEEDED = 3
108
+ LDAP_TIMEOUT = -5
109
+ LDAP_TYPE_OR_VALUE_EXISTS = 20
110
+ LDAP_UNAVAILABLE = 52
111
+ LDAP_UNAVAILABLE_CRITICAL_EXTENSION = 12
112
+ LDAP_UNDEFINED_TYPE = 17
113
+ LDAP_UNWILLING_TO_PERFORM = 53
114
+ LDAP_USER_CANCELLED = -8
115
+ LDAP_VENDOR_NAME = "OpenLDAP"
116
+ LDAP_VENDOR_VERSION = 20335
117
+ LDAP_VERSION = 2
118
+ LDAP_VERSION1 = 1
119
+ LDAP_VERSION2 = 2
120
+ LDAP_VERSION3 = 3
121
+ LDAP_VERSION_MAX = 3
122
+ MAJOR_VERSION = 0
123
+ MINOR_VERSION = 9
124
+ PATCH_VERSION = 7
125
+ VERSION = "0.9.7"
126
+ end