ruby-ldap 0.9.9

Sign up to get free protection for your applications and to get access to all the features.
data/mod.c ADDED
@@ -0,0 +1,355 @@
1
+ /*
2
+ * mod.c
3
+ * $Id: mod.c,v 1.14 2005/03/07 22:57:34 ianmacd Exp $
4
+ */
5
+
6
+ #include "ruby.h"
7
+ #include "rbldap.h"
8
+
9
+ VALUE rb_cLDAP_Mod;
10
+
11
+
12
+ void
13
+ rb_ldap_mod_free (RB_LDAPMOD_DATA * data)
14
+ {
15
+ if (data->mod)
16
+ {
17
+ struct berval **bvals;
18
+ char **svals;
19
+ int i;
20
+
21
+ if (data->mod->mod_op & LDAP_MOD_BVALUES)
22
+ {
23
+ bvals = data->mod->mod_vals.modv_bvals;
24
+ for (i = 0; bvals[i] != NULL; i++)
25
+ {
26
+ xfree (bvals[i]);
27
+ }
28
+ xfree (bvals);
29
+ }
30
+ else
31
+ {
32
+ svals = data->mod->mod_vals.modv_strvals;
33
+ for (i = 0; svals[i] != NULL; i++)
34
+ {
35
+ xfree (svals[i]);
36
+ }
37
+ xfree (svals);
38
+ }
39
+ xfree (data->mod);
40
+ }
41
+ }
42
+
43
+ static LDAPMod *
44
+ rb_ldap_new_mod (int mod_op, char *mod_type, char **modv_strvals)
45
+ {
46
+ LDAPMod *mod;
47
+
48
+ if (mod_op & LDAP_MOD_BVALUES)
49
+ {
50
+ rb_bug ("rb_ldap_mod_new: illegal mod_op");
51
+ }
52
+
53
+ mod = ALLOC_N (LDAPMod, 1);
54
+ mod->mod_op = mod_op;
55
+ mod->mod_type = mod_type;
56
+ mod->mod_vals.modv_strvals = modv_strvals;
57
+
58
+ return mod;
59
+ }
60
+
61
+ VALUE
62
+ rb_ldap_mod_new (int mod_op, char *mod_type, char **modv_strvals)
63
+ {
64
+ VALUE obj;
65
+ RB_LDAPMOD_DATA *moddata;
66
+
67
+ obj = Data_Make_Struct (rb_cLDAP_Mod, RB_LDAPMOD_DATA,
68
+ 0, rb_ldap_mod_free, moddata);
69
+ moddata->mod = rb_ldap_new_mod (mod_op, mod_type, modv_strvals);
70
+
71
+ return obj;
72
+ }
73
+
74
+ static LDAPMod *
75
+ rb_ldap_new_mod2 (int mod_op, char *mod_type, struct berval **modv_bvals)
76
+ {
77
+ LDAPMod *mod;
78
+
79
+ if (!(mod_op & LDAP_MOD_BVALUES))
80
+ {
81
+ rb_bug ("rb_ldap_mod_new: illegal mod_op");
82
+ }
83
+
84
+ mod = ALLOC_N (LDAPMod, 1);
85
+ mod->mod_op = mod_op;
86
+ mod->mod_type = mod_type;
87
+ mod->mod_vals.modv_bvals = modv_bvals;
88
+
89
+ return mod;
90
+ }
91
+
92
+ VALUE
93
+ rb_ldap_mod_new2 (int mod_op, char *mod_type, struct berval ** modv_bvals)
94
+ {
95
+ VALUE obj;
96
+ RB_LDAPMOD_DATA *moddata;
97
+
98
+ obj = Data_Make_Struct (rb_cLDAP_Mod, RB_LDAPMOD_DATA,
99
+ 0, rb_ldap_mod_free, moddata);
100
+ moddata->mod = rb_ldap_new_mod2 (mod_op, mod_type, modv_bvals);
101
+
102
+ return obj;
103
+ }
104
+
105
+ static VALUE
106
+ rb_ldap_mod_s_allocate (VALUE klass)
107
+ {
108
+ RB_LDAPMOD_DATA *moddata;
109
+ VALUE obj;
110
+
111
+ obj =
112
+ Data_Make_Struct (klass, RB_LDAPMOD_DATA, 0, rb_ldap_mod_free, moddata);
113
+ moddata->mod = NULL;
114
+
115
+ return obj;
116
+ }
117
+
118
+ /*
119
+ * call-seq:
120
+ * Mod.new(mod_type, attr, vals) => LDAP::Mod
121
+ *
122
+ * Create a new LDAP::Mod object of type +mod_type+. This is most commonly
123
+ * *LDAP_MOD_ADD*, *LDAP_MOD_REPLACE* or *LDAP_MOD_DELETE*, although some LDAP
124
+ * servers may offer extension types.
125
+ *
126
+ * +attr+ should be the name of the attribute on which to operate, whilst
127
+ * +vals+ is an array of values pertaining to +attr+. If +vals+ contains
128
+ * binary data, +mod_type+ should be logically OR'ed (|) with
129
+ * *LDAP_MOD_BVALUES*.
130
+ *
131
+ * LDAP::Mod objects can be passed to methods in the LDAP::Conn class, such as
132
+ * Conn#add, Conn#add_ext, Conn#modify and Conn#modify_ext.
133
+ */
134
+ static VALUE
135
+ rb_ldap_mod_initialize (int argc, VALUE argv[], VALUE self)
136
+ {
137
+ struct berval **bvals;
138
+ char **strvals;
139
+ int mod_op;
140
+ char *mod_type;
141
+ int i;
142
+ VALUE op, type, vals;
143
+ RB_LDAPMOD_DATA *moddata;
144
+
145
+ rb_scan_args (argc, argv, "3", &op, &type, &vals);
146
+ Data_Get_Struct (self, RB_LDAPMOD_DATA, moddata);
147
+ if (moddata->mod)
148
+ return Qnil;
149
+
150
+ mod_op = NUM2INT (op);
151
+ mod_type = StringValueCStr (type);
152
+ Check_Type (vals, T_ARRAY);
153
+
154
+ if (mod_op & LDAP_MOD_BVALUES)
155
+ {
156
+ bvals = ALLOC_N (struct berval *, RARRAY_LEN (vals) + 1);
157
+ for (i = 0; i < RARRAY_LEN (vals); i++)
158
+ {
159
+ VALUE str;
160
+ struct berval *bval;
161
+ str = RARRAY_PTR (vals)[i];
162
+ Check_Type (str, T_STRING);
163
+ bval = ALLOC_N (struct berval, 1);
164
+ bval->bv_len = RSTRING_LEN (str);
165
+ RB_LDAP_SET_STR (bval->bv_val, str);
166
+ bvals[i] = bval;
167
+ }
168
+ bvals[i] = NULL;
169
+ moddata->mod = rb_ldap_new_mod2 (mod_op, mod_type, bvals);
170
+ }
171
+ else
172
+ {
173
+ strvals = ALLOC_N (char *, RARRAY_LEN (vals) + 1);
174
+ for (i = 0; i < RARRAY_LEN (vals); i++)
175
+ {
176
+ VALUE str;
177
+ char *sval;
178
+ str = RARRAY_PTR (vals)[i];
179
+ RB_LDAP_SET_STR (sval, str);
180
+ strvals[i] = sval;
181
+ }
182
+ strvals[i] = NULL;
183
+ moddata->mod = rb_ldap_new_mod (mod_op, mod_type, strvals);
184
+ }
185
+
186
+ return Qnil;
187
+ }
188
+
189
+ /*
190
+ * call-seq:
191
+ * mod.mod_op => Fixnum
192
+ *
193
+ * Return the type of modification associated with the LDAP::Mod object.
194
+ * Standard types are *LDAP_MOD_ADD*, *LDAP_MOD_REPLACE* and
195
+ * *LDAP_MOD_DELETE*, although any of these may be logically OR'ed with
196
+ * *LDAP_MOD_BVALUES* to indicate that the values of the Mod object contain
197
+ * binary data.
198
+ */
199
+ VALUE
200
+ rb_ldap_mod_op (VALUE self)
201
+ {
202
+ RB_LDAPMOD_DATA *moddata;
203
+
204
+ GET_LDAPMOD_DATA (self, moddata);
205
+ return INT2NUM (moddata->mod->mod_op);
206
+ }
207
+
208
+ /*
209
+ * call-seq:
210
+ * mod.mod_type => String
211
+ *
212
+ * Return the name of the attribute associated with the LDAP::Mod object.
213
+ */
214
+ VALUE
215
+ rb_ldap_mod_type (VALUE self)
216
+ {
217
+ RB_LDAPMOD_DATA *moddata;
218
+
219
+ GET_LDAPMOD_DATA (self, moddata);
220
+ return rb_tainted_str_new2 (moddata->mod->mod_type);
221
+ }
222
+
223
+ /*
224
+ * call-seq:
225
+ * mod.mod_vals => Array of String
226
+ *
227
+ * Return the values associated with the Mod object.
228
+ */
229
+ VALUE
230
+ rb_ldap_mod_vals (VALUE self)
231
+ {
232
+ RB_LDAPMOD_DATA *moddata;
233
+ struct berval **bvals;
234
+ char **svals;
235
+ int i;
236
+ VALUE val;
237
+
238
+ GET_LDAPMOD_DATA (self, moddata);
239
+
240
+ if (moddata->mod->mod_op & LDAP_MOD_BVALUES)
241
+ {
242
+ bvals = moddata->mod->mod_vals.modv_bvals;
243
+ val = rb_ary_new ();
244
+ for (i = 0; bvals[i] != NULL; i++)
245
+ {
246
+ VALUE str;
247
+ str = rb_tainted_str_new (bvals[i]->bv_val, bvals[i]->bv_len);
248
+ rb_ary_push (val, str);
249
+ }
250
+ }
251
+ else
252
+ {
253
+ svals = moddata->mod->mod_vals.modv_strvals;
254
+ val = rb_ary_new ();
255
+ for (i = 0; svals[i] != NULL; i++)
256
+ {
257
+ VALUE str;
258
+ str = rb_tainted_str_new2 (svals[i]);
259
+ rb_ary_push (val, str);
260
+ }
261
+ }
262
+
263
+ return val;
264
+ }
265
+
266
+ /* call-seq:
267
+ * mod.inspect => String
268
+ *
269
+ * Produce a concise representation of the Mod object.
270
+ */
271
+ VALUE
272
+ rb_ldap_mod_inspect (VALUE self)
273
+ {
274
+ VALUE str;
275
+ VALUE hash = rb_hash_new ();
276
+ const char *c;
277
+
278
+ c = rb_obj_classname (self);
279
+ str = rb_str_new (0, strlen (c) + 10 + 16 + 1); /* 10:tags 16:addr 1:nul */
280
+ sprintf (RSTRING_PTR (str), "#<%s:0x%lx ", c, self);
281
+
282
+ #if RUBY_VERSION_CODE < 190
283
+ RSTRING(str)->len = strlen (RSTRING_PTR (str));
284
+ #else
285
+ rb_str_set_len(str, strlen (RSTRING_PTR (str)));
286
+ #endif
287
+
288
+ switch (FIX2INT (rb_ldap_mod_op (self)) & ~LDAP_MOD_BVALUES)
289
+ {
290
+ case LDAP_MOD_ADD:
291
+ rb_str_cat2 (str, "LDAP_MOD_ADD");
292
+ break;
293
+ case LDAP_MOD_DELETE:
294
+ rb_str_cat2 (str, "LDAP_MOD_DELETE");
295
+ break;
296
+ case LDAP_MOD_REPLACE:
297
+ rb_str_cat2 (str, "LDAP_MOD_REPLACE");
298
+ break;
299
+ #ifdef LDAP_MOD_INCREMENT
300
+ case LDAP_MOD_INCREMENT:
301
+ rb_str_cat2 (str, "LDAP_MOD_INCREMENT");
302
+ break;
303
+ #endif
304
+ #ifdef LDAP_MOD_OP
305
+ case LDAP_MOD_OP:
306
+ rb_str_cat2 (str, "LDAP_MOD_OP");
307
+ break;
308
+ #endif
309
+ default:
310
+ /* We shouldn't end up here. */
311
+ rb_str_cat2 (str, "unknown");
312
+ break;
313
+ }
314
+ if (FIX2INT (rb_ldap_mod_op (self)) & LDAP_MOD_BVALUES)
315
+ rb_str_cat2 (str, "|LDAP_MOD_BVALUES");
316
+ rb_str_cat2 (str, "\n");
317
+
318
+ rb_hash_aset (hash, rb_ldap_mod_type (self), rb_ldap_mod_vals (self));
319
+ rb_str_concat (str, rb_inspect (hash));
320
+ rb_str_cat2 (str, ">");
321
+
322
+ return str;
323
+ }
324
+
325
+ /* Document-class: LDAP::Mod
326
+ *
327
+ * Create and manipulate LDAP::Mod objects, which can then be passed to methods
328
+ * in the LDAP::Conn class, such as Conn#add, Conn#add_ext, Conn#modify and
329
+ * Conn#modify_ext.
330
+ */
331
+ void
332
+ Init_ldap_mod ()
333
+ {
334
+ rb_cLDAP_Mod = rb_define_class_under (rb_mLDAP, "Mod", rb_cObject);
335
+ #if RUBY_VERSION_CODE < 170
336
+ rb_define_singleton_method (rb_cLDAP_Mod, "new", rb_ldap_class_new, -1);
337
+ #endif
338
+ #if RUBY_VERSION_CODE >= 173
339
+ rb_define_alloc_func (rb_cLDAP_Mod, rb_ldap_mod_s_allocate);
340
+ #else
341
+ rb_define_singleton_method (rb_cLDAP_Mod, "allocate",
342
+ rb_ldap_mod_s_allocate, 0);
343
+ #endif
344
+ rb_ldap_mod_define_method ("initialize", rb_ldap_mod_initialize, -1);
345
+ rb_ldap_mod_define_method ("mod_op", rb_ldap_mod_op, 0);
346
+ rb_ldap_mod_define_method ("mod_type", rb_ldap_mod_type, 0);
347
+ rb_ldap_mod_define_method ("mod_vals", rb_ldap_mod_vals, 0);
348
+ rb_ldap_mod_define_method ("inspect", rb_ldap_mod_inspect, 0);
349
+
350
+ /*
351
+ rb_ldap_mod_define_method("mod_op=", rb_ldap_mod_set_op, 1);
352
+ rb_ldap_mod_define_method("mod_type=", rb_ldap_mod_set_type, 1);
353
+ rb_ldap_mod_define_method("mod_vals=", rb_ldap_mod_set_vals, 1);
354
+ */
355
+ }
@@ -0,0 +1,194 @@
1
+ /*
2
+ * rbldap.h
3
+ * $Id: rbldap.h,v 1.17 2006/08/09 11:23:04 ianmacd Exp $
4
+ */
5
+
6
+ #ifndef RB_LDAP_H
7
+ #define RB_LDAP_H 1
8
+
9
+ #ifdef USE_WLDAP32
10
+ # ifdef HAVE_WINLBER_H
11
+ # include "winlber.h"
12
+ # endif
13
+ # include "winldap.h"
14
+ #else
15
+ # include <lber.h>
16
+ # include <ldap.h>
17
+ #endif
18
+
19
+ #ifdef HAVE_LDAP_SSL_H
20
+ #include <ldap_ssl.h>
21
+ #endif
22
+
23
+ #ifndef LDAP_OPT_SUCCESS
24
+ # define LDAP_OPT_SUCCESS (0)
25
+ # define LDAP_OPT_ERROR (-1)
26
+ #endif
27
+
28
+ #define RB_LDAP_MAJOR_VERSION 0
29
+ #define RB_LDAP_MINOR_VERSION 9
30
+ #define RB_LDAP_PATCH_VERSION 9
31
+ #define RB_LDAP_VERSION "0.9.9"
32
+
33
+ #define LDAP_GET_OPT_MAX_BUFFER_SIZE (1024) /* >= sizeof(LDAPAPIInfo) */
34
+
35
+ #define RB_LDAP_SET_STR(var,val) {\
36
+ Check_Type(val, T_STRING); \
37
+ var = ALLOC_N(char, RSTRING_LEN(val) + 1); \
38
+ memcpy(var, RSTRING_PTR(val), RSTRING_LEN(val) + 1); \
39
+ }
40
+
41
+ #if defined(HAVE_LDAP_SEARCH_EXT_S)
42
+ # define HAVE_LDAPCONTROL
43
+ #endif
44
+
45
+ typedef struct rb_ldap_data
46
+ {
47
+ LDAP *ldap;
48
+ int bind;
49
+ int err;
50
+ } RB_LDAP_DATA;
51
+
52
+ #define RLDAP_DATA_PTR(obj) ((RB_LDAP_DATA*)DATA_PTR(obj))
53
+
54
+ typedef struct rb_ldapentry_data
55
+ {
56
+ LDAP *ldap;
57
+ LDAPMessage *msg;
58
+ } RB_LDAPENTRY_DATA;
59
+
60
+ typedef struct rb_ldapmod_data
61
+ {
62
+ LDAPMod *mod;
63
+ } RB_LDAPMOD_DATA;
64
+
65
+
66
+ #ifndef HAVE_LDAP_MEMFREE
67
+ # define ldap_memfree(ptr) free(ptr)
68
+ #endif
69
+
70
+ extern VALUE rb_mLDAP;
71
+
72
+ extern VALUE rb_sLDAP_APIInfo;
73
+ extern VALUE rb_cLDAP_Controls;
74
+
75
+ extern VALUE rb_cLDAP_Conn;
76
+ extern VALUE rb_cLDAP_SSLConn;
77
+ extern VALUE rb_cLDAP_Entry;
78
+ extern VALUE rb_cLDAP_Mod;
79
+ extern VALUE rb_eLDAP_Error;
80
+ extern VALUE rb_eLDAP_ResultError;
81
+ extern VALUE rb_eLDAP_InvalidDataError;
82
+ extern VALUE rb_eLDAP_InvalidEntryError;
83
+
84
+ #ifdef LDAP_OPT_API_INFO
85
+ VALUE rb_ldap_apiinfo_new (LDAPAPIInfo *);
86
+ LDAPAPIInfo *rb_ldap_get_apiinfo (VALUE);
87
+ #endif /* LDAP_OPT_API_INFO */
88
+
89
+ #ifdef HAVE_LDAPCONTROL
90
+ VALUE rb_ldap_control_new (LDAPControl *);
91
+ LDAPControl *rb_ldap_get_control (VALUE);
92
+ LDAPControl **rb_ldap_get_controls (VALUE);
93
+ void rb_ldap_free_controls (LDAPControl ** ctrls);
94
+ #endif
95
+
96
+ VALUE rb_ldap_class_new (int, VALUE[], VALUE);
97
+ VALUE rb_ldap_dummy_method (int, VALUE[], VALUE);
98
+ VALUE rb_ldap_err2string (VALUE, VALUE);
99
+ VALUE rb_ldap_dn2ufn (VALUE, VALUE);
100
+ VALUE rb_ldap_hash2mods (VALUE, VALUE, VALUE);
101
+ VALUE rb_ldap_entry2hash (VALUE, VALUE);
102
+
103
+ VALUE rb_ldap_conn_new (VALUE, LDAP *);
104
+ VALUE rb_ldap_conn_simple_bind_s (int, VALUE[], VALUE);
105
+ VALUE rb_ldap_conn_bind_s (int, VALUE[], VALUE);
106
+ VALUE rb_ldap_conn_start_tls_s (int, VALUE[], VALUE);
107
+ VALUE rb_ldap_conn_unbind (VALUE);
108
+ VALUE rb_ldap_conn_set_option (VALUE, VALUE, VALUE);
109
+ VALUE rb_ldap_conn_get_option (VALUE, VALUE);
110
+ VALUE rb_ldap_conn_perror (VALUE, VALUE);
111
+ VALUE rb_ldap_conn_result2error (VALUE, VALUE);
112
+ VALUE rb_ldap_conn_err2string (VALUE, VALUE);
113
+ VALUE rb_ldap_conn_search_s (int, VALUE[], VALUE);
114
+ VALUE rb_ldap_conn_search2_s (int, VALUE[], VALUE);
115
+ VALUE rb_ldap_conn_add_s (VALUE, VALUE, VALUE);
116
+ VALUE rb_ldap_conn_modify_s (VALUE, VALUE, VALUE);
117
+ VALUE rb_ldap_conn_modrdn_s (VALUE, VALUE, VALUE, VALUE);
118
+ VALUE rb_ldap_conn_delete_s (VALUE, VALUE);
119
+ VALUE rb_ldap_conn_err (VALUE);
120
+ VALUE rb_ldap_conn_set_sort (VALUE, VALUE, VALUE);
121
+ VALUE rb_ldap_conn_get_sort (VALUE);
122
+
123
+ VALUE rb_ldap_saslconn_bind (int, VALUE[], VALUE);
124
+
125
+ VALUE rb_ldap_entry_new (LDAP *, LDAPMessage *);
126
+ VALUE rb_ldap_entry_get_dn (VALUE self);
127
+ VALUE rb_ldap_entry_get_values (VALUE, VALUE);
128
+ VALUE rb_ldap_entry_get_attributes (VALUE);
129
+ VALUE rb_ldap_entry_to_hash (VALUE);
130
+
131
+ VALUE rb_ldap_mod_new (int, char *, char **);
132
+ VALUE rb_ldap_mod_new2 (int, char *, struct berval **);
133
+ VALUE rb_ldap_mod_op (VALUE);
134
+ VALUE rb_ldap_mod_type (VALUE);
135
+ VALUE rb_ldap_mod_vals (VALUE);
136
+
137
+ #define Check_Kind(obj,klass) {\
138
+ if(!rb_obj_is_kind_of(obj,klass))\
139
+ rb_raise(rb_eTypeError,"type mismatch");\
140
+ };
141
+
142
+ #define Check_LDAP_Result(err) { \
143
+ if( err != LDAP_SUCCESS && err != LDAP_SIZELIMIT_EXCEEDED ){ \
144
+ rb_raise(rb_eLDAP_ResultError, ldap_err2string(err)); \
145
+ } \
146
+ }
147
+
148
+ #define Check_LDAP_OPT_Result(err) { \
149
+ if( err != LDAP_OPT_SUCCESS ){ \
150
+ rb_raise(rb_eLDAP_ResultError, ldap_err2string(err)); \
151
+ } \
152
+ }
153
+
154
+ #define GET_LDAP_DATA(obj,ptr) {\
155
+ Data_Get_Struct(obj, struct rb_ldap_data, ptr); \
156
+ if( ! ptr->ldap ){ \
157
+ rb_raise(rb_eLDAP_InvalidDataError, "The LDAP handler has already unbound.");\
158
+ } \
159
+ }
160
+
161
+ #define Check_LDAPENTRY(obj) {\
162
+ RB_LDAPENTRY_DATA *ptr; \
163
+ Data_Get_Struct(obj, struct rb_ldapmsg_data, ptr); \
164
+ if( ! ptr->msg ){ \
165
+ rb_raise(rb_eLDAP_InvalidEntryError, "%s is not a valid entry", \
166
+ STR2CSTR(rb_inspect(obj))); \
167
+ }; \
168
+ }
169
+
170
+ #define GET_LDAPENTRY_DATA(obj,ptr) { \
171
+ Data_Get_Struct(obj, struct rb_ldapentry_data, ptr); \
172
+ if( ! ptr->msg ){ \
173
+ rb_raise(rb_eLDAP_InvalidEntryError, "%s is not a valid entry", \
174
+ STR2CSTR(rb_inspect(obj))); \
175
+ }; \
176
+ }
177
+
178
+ #define GET_LDAPMOD_DATA(obj,ptr) {\
179
+ Data_Get_Struct(obj, struct rb_ldapmod_data, ptr); \
180
+ if( ! ptr->mod ) \
181
+ rb_raise(rb_eLDAP_InvalidDataError, "The Mod data is not ready for use."); \
182
+ }
183
+
184
+ #define rb_ldap_define_class(cname,parent) \
185
+ rb_define_class_under(rb_mLDAP,cname,parent)
186
+
187
+ #define rb_ldap_conn_define_method(method,cfunc,argc) \
188
+ rb_define_method(rb_cLDAP_Conn,method,cfunc,argc)
189
+ #define rb_ldap_entry_define_method(method,cfunc,argc) \
190
+ rb_define_method(rb_cLDAP_Entry,method,cfunc,argc)
191
+ #define rb_ldap_mod_define_method(method,cfunc,argc) \
192
+ rb_define_method(rb_cLDAP_Mod,method,cfunc,argc)
193
+
194
+ #endif