rldap 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) 2010 Henrik Hodne
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/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ desc "Push a new version to Gemcutter"
2
+ task :publish do
3
+ require 'ldap/version'
4
+
5
+ sh "gem build rldap.gemspec"
6
+ sh "gem push rldap-#{LDAP::Version}.gem"
7
+ sh "git tag v#{LDAP::Version}"
8
+ sh "git push origin v#{LDAP::Version}"
9
+ sh "git push origin master"
10
+ sh "git clean -fd"
11
+ end
data/ext/constants.h ADDED
@@ -0,0 +1,25 @@
1
+ #define DEFINE_CONST(s) rb_define_const(cLDAP, #s, INT2NUM(s))
2
+
3
+ /* options */
4
+ DEFINE_CONST(LDAP_OPT_API_FEATURE_INFO);
5
+ DEFINE_CONST(LDAP_OPT_API_INFO);
6
+ DEFINE_CONST(LDAP_OPT_CLIENT_CONTROLS);
7
+ DEFINE_CONST(LDAP_OPT_CONNECT_ASYNC);
8
+ DEFINE_CONST(LDAP_OPT_CONNECT_CB);
9
+ DEFINE_CONST(LDAP_OPT_DEBUG_LEVEL);
10
+ DEFINE_CONST(LDAP_OPT_DEFBASE);
11
+ DEFINE_CONST(LDAP_OPT_DEREF);
12
+ DEFINE_CONST(LDAP_OPT_DESC);
13
+ DEFINE_CONST(LDAP_OPT_DIAGNOSTIC_MESSAGE);
14
+ DEFINE_CONST(LDAP_OPT_HOST_NAME);
15
+ DEFINE_CONST(LDAP_OPT_MATCHED_DN);
16
+ DEFINE_CONST(LDAP_OPT_NETWORK_TIMEOUT);
17
+ DEFINE_CONST(LDAP_OPT_PROTOCOL_VERSION);
18
+ DEFINE_CONST(LDAP_OPT_X_TLS_CACERTDIR);
19
+
20
+ /* errors */
21
+ DEFINE_CONST(LDAP_SUCCESS);
22
+ DEFINE_CONST(LDAP_OPERATIONS_ERROR);
23
+ DEFINE_CONST(LDAP_PROTOCOL_ERROR);
24
+ DEFINE_CONST(LDAP_TIMELIMIT_EXCEEDED);
25
+ DEFINE_CONST(LDAP_SIZELIMIT_EXCEEDED);
data/ext/extconf.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'mkmf'
2
+
3
+ have_library('ldap')
4
+ have_library('lber')
5
+
6
+ have_header('ldap.h')
7
+ have_header('lber.h')
8
+
9
+ create_makefile('ldap')
data/ext/ldap.c ADDED
@@ -0,0 +1,310 @@
1
+ #include <ruby.h>
2
+ #ifndef RSTRING_PTR
3
+ #define RSTRING_PTR(str) RSTRING(str)->ptr
4
+ #endif
5
+ #ifndef RSTRING_LEN
6
+ #define RSTRING_LEN(str) RSTRING(str)->len
7
+ #endif
8
+
9
+ #include <ldap.h>
10
+ #include <lber.h>
11
+ #include <stdlib.h>
12
+
13
+ static VALUE cLDAP;
14
+ static VALUE cLDAP_Message;
15
+ static VALUE eLDAP;
16
+
17
+ typedef struct {
18
+ LDAP* ld;
19
+ char connection;
20
+ } RLDAP_WRAP;
21
+
22
+ typedef struct {
23
+ LDAP* ld;
24
+ LDAPMessage* mesg;
25
+ char freed;
26
+ } RLDAP_MSG_WRAP;
27
+
28
+ static RLDAP_WRAP* get_wrapper(VALUE obj)
29
+ {
30
+ RLDAP_WRAP* wrapper;
31
+ Data_Get_Struct(obj, RLDAP_WRAP, wrapper);
32
+ return wrapper;
33
+ }
34
+
35
+ static RLDAP_MSG_WRAP* get_msg_wrapper(VALUE obj)
36
+ {
37
+ RLDAP_MSG_WRAP* wrapper;
38
+ Data_Get_Struct(obj, RLDAP_MSG_WRAP, wrapper);
39
+ return wrapper;
40
+ }
41
+
42
+ static void free_wrapper(RLDAP_WRAP* wrapper)
43
+ {
44
+ ldap_memfree(wrapper->ld);
45
+ xfree(wrapper);
46
+ }
47
+
48
+ static void free_msg_wrapper(RLDAP_MSG_WRAP* wrapper)
49
+ {
50
+ if (wrapper->freed == Qfalse)
51
+ ldap_msgfree(wrapper->mesg);
52
+ xfree(wrapper);
53
+ }
54
+
55
+ static void rldap_raise(int errno)
56
+ {
57
+ VALUE e = rb_exc_new2(eLDAP, ldap_err2string(errno));
58
+ rb_iv_set(e, "@errno", INT2FIX(errno));
59
+ rb_exc_raise(e);
60
+ }
61
+
62
+ static VALUE ldapmessage2obj(LDAP* ld, LDAPMessage* msg);
63
+
64
+ /* class LDAP */
65
+
66
+ static VALUE rldap_err2string(VALUE klass, VALUE rerrno)
67
+ {
68
+ int errno;
69
+ errno = FIX2INT(rerrno);
70
+ return rb_str_new2(ldap_err2string(errno));
71
+ }
72
+
73
+ static VALUE rldap_alloc(VALUE klass)
74
+ {
75
+ RLDAP_WRAP* wrapper;
76
+ VALUE obj;
77
+
78
+ obj = Data_Make_Struct(klass, RLDAP_WRAP, 0, free_wrapper, wrapper);
79
+ wrapper->connection = Qfalse;
80
+
81
+ return obj;
82
+ }
83
+
84
+ static VALUE rldap_initialize(int argc, VALUE* argv, VALUE obj)
85
+ {
86
+ VALUE rhost, rport;
87
+ char* host;
88
+ int port;
89
+ RLDAP_WRAP* wrapper;
90
+
91
+ rb_scan_args(argc, argv, "11", &rhost, &rport);
92
+
93
+ if (NIL_P(rport))
94
+ rport = INT2FIX(LDAP_PORT);
95
+
96
+ wrapper = get_wrapper(obj);
97
+ host = StringValuePtr(rhost);
98
+ port = FIX2INT(rport);
99
+
100
+ wrapper->ld = ldap_open(host, port);
101
+
102
+ return obj;
103
+ }
104
+
105
+ static VALUE rldap_start_tls(VALUE obj)
106
+ {
107
+ RLDAP_WRAP* wrapper;
108
+ int retval;
109
+
110
+ wrapper = get_wrapper(obj);
111
+ retval = ldap_start_tls_s(wrapper->ld, NULL, NULL);
112
+ if (retval == LDAP_SUCCESS)
113
+ return Qtrue;
114
+ else
115
+ rldap_raise(retval);
116
+ }
117
+
118
+ static VALUE rldap_search(VALUE obj, VALUE rbase, VALUE rfilter)
119
+ {
120
+ RLDAP_WRAP* wrapper;
121
+ char* base;
122
+ char* filter;
123
+ int retval, count, i;
124
+ LDAPMessage* res;
125
+ LDAPMessage* msg;
126
+ VALUE ary;
127
+
128
+ wrapper = get_wrapper(obj);
129
+ base = StringValuePtr(rbase);
130
+ filter = StringValuePtr(rfilter);
131
+
132
+ retval = ldap_search_ext_s(wrapper->ld, base, LDAP_SCOPE_SUBTREE, filter, NULL, 0, NULL, NULL, NULL, 0, &res);
133
+
134
+ if (retval != LDAP_SUCCESS)
135
+ rldap_raise(retval);
136
+
137
+ count = ldap_count_entries(wrapper->ld, res);
138
+
139
+ if (count == -1) {
140
+ int errno;
141
+ ldap_get_option(wrapper->ld, LDAP_OPT_RESULT_CODE, &errno);
142
+ rldap_raise(errno);
143
+ }
144
+
145
+ ary = rb_ary_new2((long)count);
146
+
147
+ msg = ldap_first_entry(wrapper->ld, res);
148
+
149
+ for (i=0; i<count; i++) {
150
+ rb_ary_store(ary, (long)i, ldapmessage2obj(wrapper->ld, msg));
151
+ msg = ldap_next_entry(wrapper->ld, msg);
152
+ }
153
+
154
+ return ary;
155
+ }
156
+
157
+ static VALUE rldap_set_option(VALUE obj, VALUE roption, VALUE rvalue)
158
+ {
159
+ RLDAP_WRAP* wrapper;
160
+ int retval;
161
+ int option;
162
+ int ival;
163
+ char* sval;
164
+ void* val;
165
+
166
+ wrapper = get_wrapper(obj);
167
+ option = FIX2INT(roption);
168
+
169
+ if (TYPE(rvalue) == T_STRING) {
170
+ sval = StringValuePtr(rvalue);
171
+ val = &sval;
172
+ } else {
173
+ ival = FIX2INT(rvalue);
174
+ val = &ival;
175
+ }
176
+
177
+ retval = ldap_set_option(wrapper->ld, option, val);
178
+
179
+ if (retval == LDAP_OPT_SUCCESS)
180
+ return Qtrue;
181
+ else
182
+ return Qfalse;
183
+ }
184
+
185
+ static VALUE rldap_errno(VALUE obj)
186
+ {
187
+ RLDAP_WRAP* wrapper;
188
+ int errno;
189
+
190
+ wrapper = get_wrapper(obj);
191
+
192
+ ldap_get_option(wrapper->ld, LDAP_OPT_RESULT_CODE, &errno);
193
+
194
+ return INT2NUM(errno);
195
+ }
196
+
197
+ int rldap_errno_c(VALUE obj)
198
+ {
199
+ RLDAP_WRAP* wrapper;
200
+ int errno;
201
+
202
+ wrapper = get_wrapper(obj);
203
+ ldap_get_option(wrapper->ld, LDAP_OPT_RESULT_CODE, &errno);
204
+ return errno;
205
+ }
206
+
207
+
208
+ /* class LDAP::Message */
209
+
210
+ static VALUE ldapmessage2obj(LDAP* ld, LDAPMessage* msg)
211
+ {
212
+ VALUE obj;
213
+ RLDAP_MSG_WRAP* wrapper;
214
+
215
+ obj = Data_Make_Struct(cLDAP_Message, RLDAP_MSG_WRAP, 0, free_msg_wrapper, wrapper);
216
+ wrapper->mesg = msg;
217
+ wrapper->freed = Qfalse;
218
+ wrapper->ld = ld;
219
+
220
+ return obj;
221
+ }
222
+
223
+ static VALUE rldap_msg_dn(VALUE obj)
224
+ {
225
+ RLDAP_MSG_WRAP* wrapper;
226
+ char* dn;
227
+
228
+ wrapper = get_msg_wrapper(obj);
229
+
230
+ dn = ldap_get_dn(wrapper->ld, wrapper->mesg);
231
+
232
+ return rb_str_new2(dn);
233
+ }
234
+
235
+ static VALUE rldap_msg_get_val(VALUE obj, VALUE key)
236
+ {
237
+ RLDAP_MSG_WRAP* wrapper;
238
+ char* attr;
239
+ BerValue** values;
240
+ VALUE ary;
241
+ int i, length;
242
+ BerValue* value;
243
+ char* strval;
244
+ VALUE str;
245
+
246
+ wrapper = get_msg_wrapper(obj);
247
+ attr = StringValuePtr(key);
248
+
249
+ values = ldap_get_values_len(wrapper->ld, wrapper->mesg, attr);
250
+
251
+ if (values == NULL) {
252
+ rldap_raise(rldap_errno_c(obj));
253
+ }
254
+
255
+ ary = rb_ary_new();
256
+ length = ldap_count_values_len(values);
257
+
258
+ for (i=0; i<length; i++) {
259
+ value = values[i];
260
+ str = rb_str_new(value->bv_val, value->bv_len);
261
+ rb_ary_push(ary, str);
262
+ }
263
+
264
+ ldap_value_free_len(values);
265
+
266
+ return ary;
267
+ }
268
+
269
+ static VALUE rldap_msg_keys(VALUE obj)
270
+ {
271
+ RLDAP_MSG_WRAP* wrapper;
272
+ char* attr;
273
+ BerElement* ber;
274
+ VALUE ary;
275
+
276
+ wrapper = get_msg_wrapper(obj);
277
+ ary = rb_ary_new();
278
+
279
+ attr = ldap_first_attribute(wrapper->ld, wrapper->mesg, &ber);
280
+ do {
281
+ rb_ary_push(ary, rb_str_new2(attr));
282
+ ldap_memfree(attr);
283
+ } while (attr = ldap_next_attribute(wrapper->ld, wrapper->mesg, ber));
284
+
285
+ ber_free(ber, 0);
286
+
287
+ return ary;
288
+ }
289
+
290
+
291
+ void Init_ldap()
292
+ {
293
+ cLDAP = rb_define_class("LDAP", rb_cObject);
294
+ cLDAP_Message = rb_define_class_under(cLDAP, "Message", rb_cObject);
295
+ eLDAP = rb_define_class_under(cLDAP, "Error", rb_eStandardError);
296
+
297
+ rb_define_alloc_func(cLDAP, rldap_alloc);
298
+ rb_define_singleton_method(cLDAP, "err2string", rldap_err2string, 1);
299
+ rb_define_method(cLDAP, "initialize", rldap_initialize, -1);
300
+ rb_define_method(cLDAP, "start_tls", rldap_start_tls, 0);
301
+ rb_define_method(cLDAP, "search", rldap_search, 2);
302
+ rb_define_method(cLDAP, "set_option", rldap_set_option, 2);
303
+ rb_define_method(cLDAP, "errno", rldap_errno, 0);
304
+
305
+ rb_define_method(cLDAP_Message, "dn", rldap_msg_dn, 0);
306
+ rb_define_method(cLDAP_Message, "[]", rldap_msg_get_val, 1);
307
+ rb_define_method(cLDAP_Message, "keys", rldap_msg_keys, 0);
308
+
309
+ #include "constants.h"
310
+ }
@@ -0,0 +1,3 @@
1
+ class LDAP
2
+ Version = VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rldap
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Henrik Hodne
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-05 00:00:00 +00:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: dvyjones@binaryhex.com
23
+ executables: []
24
+
25
+ extensions:
26
+ - ext/extconf.rb
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - Rakefile
31
+ - LICENSE
32
+ - lib/ldap/version.rb
33
+ - ext/constants.h
34
+ - ext/extconf.rb
35
+ - ext/ldap.c
36
+ has_rdoc: true
37
+ homepage: http://github.com/dvyjones/rldap
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.7
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Ruby LDAP wrapper
68
+ test_files: []
69
+