rldap 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/ext/constants.h +5 -0
  2. data/ext/ldap.c +71 -22
  3. data/lib/ldap/version.rb +1 -1
  4. metadata +2 -2
data/ext/constants.h CHANGED
@@ -24,3 +24,8 @@ DEFINE_CONST(LDAP_OPERATIONS_ERROR);
24
24
  DEFINE_CONST(LDAP_PROTOCOL_ERROR);
25
25
  DEFINE_CONST(LDAP_TIMELIMIT_EXCEEDED);
26
26
  DEFINE_CONST(LDAP_SIZELIMIT_EXCEEDED);
27
+
28
+ /* scopes */
29
+ DEFINE_CONST(LDAP_SCOPE_SUBTREE);
30
+ DEFINE_CONST(LDAP_SCOPE_ONE);
31
+ DEFINE_CONST(LDAP_SCOPE_BASE);
data/ext/ldap.c CHANGED
@@ -16,13 +16,11 @@ static VALUE eLDAP;
16
16
 
17
17
  typedef struct {
18
18
  LDAP *ld;
19
- char connection;
20
19
  } RLDAP_WRAP;
21
20
 
22
21
  typedef struct {
23
22
  LDAP *ld;
24
23
  LDAPMessage *mesg;
25
- char freed;
26
24
  } RLDAP_MSG_WRAP;
27
25
 
28
26
  static RLDAP_WRAP *get_wrapper(VALUE obj)
@@ -42,14 +40,15 @@ static RLDAP_MSG_WRAP *get_msg_wrapper(VALUE obj)
42
40
  static void free_wrapper(RLDAP_WRAP *wrapper)
43
41
  {
44
42
  ldap_memfree(wrapper->ld);
45
- xfree(wrapper);
43
+ free(wrapper);
46
44
  }
47
45
 
48
46
  static void free_msg_wrapper(RLDAP_MSG_WRAP *wrapper)
49
47
  {
50
- if (wrapper->freed == Qfalse)
48
+ if (wrapper->mesg != NULL)
51
49
  ldap_msgfree(wrapper->mesg);
52
- xfree(wrapper);
50
+ wrapper->ld = NULL;
51
+ free(wrapper);
53
52
  }
54
53
 
55
54
  static void rldap_raise(int errno)
@@ -76,7 +75,6 @@ static VALUE rldap_alloc(VALUE klass)
76
75
  VALUE obj;
77
76
 
78
77
  obj = Data_Make_Struct(klass, RLDAP_WRAP, 0, free_wrapper, wrapper);
79
- wrapper->connection = Qfalse;
80
78
 
81
79
  return obj;
82
80
  }
@@ -115,19 +113,44 @@ static VALUE rldap_start_tls(VALUE obj)
115
113
  rldap_raise(retval);
116
114
  }
117
115
 
118
- static VALUE rldap_search(VALUE obj, VALUE rbase, VALUE rfilter)
116
+ static VALUE rldap_search(int argc, VALUE *argv, VALUE obj)
119
117
  {
120
118
  RLDAP_WRAP *wrapper;
121
119
  char *base, *filter;
122
- int retval, count, i;
120
+ int retval, count, i, scope;
123
121
  LDAPMessage *res, *msg;
124
- VALUE ary;
125
-
122
+ VALUE ary, rbase, rfilter, rscope;
123
+ ID iscope;
124
+
125
+ rb_scan_args(argc, argv, "21", &rbase, &rfilter, &rscope);
126
+
127
+ switch(TYPE(rscope)) {
128
+ case T_NIL:
129
+ scope = LDAP_SCOPE_SUBTREE;
130
+ break;
131
+ case T_FIXNUM:
132
+ scope = FIX2INT(rscope);
133
+ break;
134
+ case T_SYMBOL:
135
+ case T_STRING:
136
+ iscope = rb_to_id(rscope);
137
+ if (iscope == rb_intern("subtree"))
138
+ scope = LDAP_SCOPE_SUBTREE;
139
+ if (iscope == rb_intern("base"))
140
+ scope = LDAP_SCOPE_BASE;
141
+ if (iscope == rb_intern("one"))
142
+ scope = LDAP_SCOPE_ONE;
143
+ break;
144
+ default:
145
+ rb_raise(rb_eTypeError, "not a valid scope");
146
+ break;
147
+ }
148
+
126
149
  wrapper = get_wrapper(obj);
127
150
  base = StringValuePtr(rbase);
128
151
  filter = StringValuePtr(rfilter);
129
152
 
130
- retval = ldap_search_ext_s(wrapper->ld, base, LDAP_SCOPE_SUBTREE, filter, NULL, 0, NULL, NULL, NULL, 0, &res);
153
+ retval = ldap_search_ext_s(wrapper->ld, base, scope, filter, NULL, 0, NULL, NULL, NULL, 0, &res);
131
154
 
132
155
  if (retval != LDAP_SUCCESS)
133
156
  rldap_raise(retval);
@@ -149,6 +172,8 @@ static VALUE rldap_search(VALUE obj, VALUE rbase, VALUE rfilter)
149
172
  msg = ldap_next_entry(wrapper->ld, msg);
150
173
  }
151
174
 
175
+
176
+
152
177
  return ary;
153
178
  }
154
179
 
@@ -180,16 +205,9 @@ static VALUE rldap_set_option(VALUE obj, VALUE roption, VALUE rvalue)
180
205
  return Qfalse;
181
206
  }
182
207
 
183
- static VALUE rldap_errno(VALUE obj)
208
+ static VALUE rldap_set_version(VALUE obj, VALUE version)
184
209
  {
185
- RLDAP_WRAP *wrapper;
186
- int errno;
187
-
188
- wrapper = get_wrapper(obj);
189
-
190
- ldap_get_option(wrapper->ld, LDAP_OPT_RESULT_CODE, &errno);
191
-
192
- return INT2NUM(errno);
210
+ return rldap_set_option(obj, INT2FIX(LDAP_OPT_PROTOCOL_VERSION), version);
193
211
  }
194
212
 
195
213
  int rldap_errno_c(VALUE obj)
@@ -202,6 +220,35 @@ int rldap_errno_c(VALUE obj)
202
220
  return errno;
203
221
  }
204
222
 
223
+ static VALUE rldap_errno(VALUE obj)
224
+ {
225
+ return INT2NUM(rldap_errno_c(obj));
226
+ }
227
+
228
+ static VALUE rldap_uri(VALUE obj)
229
+ {
230
+ RLDAP_WRAP *wrapper;
231
+ char *uri;
232
+ VALUE ruri;
233
+
234
+ wrapper = get_wrapper(obj);
235
+ ldap_get_option(wrapper->ld, LDAP_OPT_URI, &uri);
236
+
237
+ return rb_str_new2(uri);
238
+ }
239
+
240
+ static VALUE rldap_inspect(VALUE obj)
241
+ {
242
+ VALUE ruri, ret;
243
+
244
+ ruri = rb_funcall(rldap_uri(obj), rb_intern("dump"), 0);
245
+ ret = rb_str_new2("#<LDAP @uri=");
246
+ rb_str_cat2(ret, StringValuePtr(ruri));
247
+ rb_str_cat2(ret, ">");
248
+
249
+ return ret;
250
+ }
251
+
205
252
 
206
253
  /* class LDAP::Message */
207
254
 
@@ -212,7 +259,6 @@ static VALUE ldapmessage2obj(LDAP* ld, LDAPMessage* msg)
212
259
 
213
260
  obj = Data_Make_Struct(cLDAP_Message, RLDAP_MSG_WRAP, 0, free_msg_wrapper, wrapper);
214
261
  wrapper->mesg = msg;
215
- wrapper->freed = Qfalse;
216
262
  wrapper->ld = ld;
217
263
 
218
264
  return obj;
@@ -304,9 +350,12 @@ void Init_ldap()
304
350
  rb_define_singleton_method(cLDAP, "err2string", rldap_err2string, 1);
305
351
  rb_define_method(cLDAP, "initialize", rldap_initialize, -1);
306
352
  rb_define_method(cLDAP, "start_tls", rldap_start_tls, 0);
307
- rb_define_method(cLDAP, "search", rldap_search, 2);
353
+ rb_define_method(cLDAP, "search", rldap_search, -1);
308
354
  rb_define_method(cLDAP, "set_option", rldap_set_option, 2);
355
+ rb_define_method(cLDAP, "version=", rldap_set_version, 1);
309
356
  rb_define_method(cLDAP, "errno", rldap_errno, 0);
357
+ rb_define_method(cLDAP, "uri", rldap_uri, 0);
358
+ rb_define_method(cLDAP, "inspect", rldap_inspect, 0);
310
359
 
311
360
  rb_define_method(cLDAP_Message, "dn", rldap_msg_dn, 0);
312
361
  rb_define_method(cLDAP_Message, "[]", rldap_msg_get_val, 1);
data/lib/ldap/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class LDAP
2
- Version = VERSION = '0.0.2'
2
+ Version = VERSION = '0.0.3'
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Henrik Hodne