ruby-ldap 0.9.9

Sign up to get free protection for your applications and to get access to all the features.
data/entry.c ADDED
@@ -0,0 +1,215 @@
1
+ /*
2
+ * entry.c
3
+ * $Id: entry.c,v 1.13 2005/03/15 10:15:32 ianmacd Exp $
4
+ */
5
+
6
+ #include "ruby.h"
7
+ #include "rbldap.h"
8
+
9
+ VALUE rb_cLDAP_Entry;
10
+
11
+
12
+ void
13
+ rb_ldap_entry_free (RB_LDAPENTRY_DATA * edata)
14
+ {
15
+ /* edata->msg is valid in a block given by each search operation */
16
+ /* ldap_msgfree should be called after ldap_search */
17
+ }
18
+
19
+ VALUE
20
+ rb_ldap_entry_new (LDAP * ldap, LDAPMessage * msg)
21
+ {
22
+ VALUE val;
23
+ RB_LDAPENTRY_DATA *edata;
24
+ val = Data_Make_Struct (rb_cLDAP_Entry, RB_LDAPENTRY_DATA,
25
+ 0, 0 /* rb_ldap_entry_free */ , edata);
26
+ edata->ldap = ldap;
27
+ edata->msg = msg;
28
+ return val;
29
+ }
30
+
31
+ /*
32
+ * call-seq:
33
+ * entry.get_dn => String
34
+ * entry.dn => String
35
+ */
36
+ VALUE
37
+ rb_ldap_entry_get_dn (VALUE self)
38
+ {
39
+ RB_LDAPENTRY_DATA *edata;
40
+ char *cdn;
41
+ VALUE dn;
42
+
43
+ GET_LDAPENTRY_DATA (self, edata);
44
+
45
+ cdn = ldap_get_dn (edata->ldap, edata->msg);
46
+ if (cdn)
47
+ {
48
+ dn = rb_tainted_str_new2 (cdn);
49
+ ldap_memfree (cdn);
50
+ }
51
+ else
52
+ {
53
+ dn = Qnil;
54
+ }
55
+
56
+ return dn;
57
+ }
58
+
59
+ /*
60
+ * call-seq:
61
+ * entry.get_values(attr) => Array of String
62
+ * entry.vals(attr) => Array of String
63
+ * entry[attr] => Array of String
64
+ *
65
+ * Return an array of all the values belonging to the attribute, +attr+, of
66
+ * the entry.
67
+ */
68
+ VALUE
69
+ rb_ldap_entry_get_values (VALUE self, VALUE attr)
70
+ {
71
+ RB_LDAPENTRY_DATA *edata;
72
+ char *c_attr;
73
+ struct berval **c_vals;
74
+ int i;
75
+ int count;
76
+ VALUE vals;
77
+
78
+ GET_LDAPENTRY_DATA (self, edata);
79
+ c_attr = StringValueCStr (attr);
80
+
81
+ c_vals = ldap_get_values_len (edata->ldap, edata->msg, c_attr);
82
+ if (c_vals)
83
+ {
84
+ vals = rb_ary_new ();
85
+ count = ldap_count_values_len (c_vals);
86
+ for (i = 0; i < count; i++)
87
+ {
88
+ VALUE str;
89
+ str = rb_tainted_str_new (c_vals[i]->bv_val, c_vals[i]->bv_len);
90
+ rb_ary_push (vals, str);
91
+ }
92
+ ldap_value_free_len (c_vals);
93
+ }
94
+ else
95
+ {
96
+ vals = Qnil;
97
+ }
98
+
99
+ return vals;
100
+ }
101
+
102
+ /*
103
+ * call-seq:
104
+ * entry.get_attributes => Array of String
105
+ * entry.attrs => Array of String
106
+ *
107
+ * Return an array of all the attributes belonging to the entry.
108
+ */
109
+ VALUE
110
+ rb_ldap_entry_get_attributes (VALUE self)
111
+ {
112
+ RB_LDAPENTRY_DATA *edata;
113
+ VALUE vals;
114
+ char *attr;
115
+ BerElement *ber;
116
+
117
+ GET_LDAPENTRY_DATA (self, edata);
118
+
119
+ vals = rb_ary_new ();
120
+ for (attr = ldap_first_attribute (edata->ldap, edata->msg, &ber);
121
+ attr != NULL;
122
+ attr = ldap_next_attribute (edata->ldap, edata->msg, ber))
123
+ {
124
+ rb_ary_push (vals, rb_tainted_str_new2 (attr));
125
+ }
126
+
127
+ /* this code may cause segv
128
+ #if !defined(USE_OPENLDAP1)
129
+ if( ber != NULL ){
130
+ ber_free(ber, 0);
131
+ }
132
+ #endif
133
+ */
134
+
135
+ return vals;
136
+ }
137
+
138
+ /*
139
+ * call-seq:
140
+ * entry.to_hash => Hash
141
+ *
142
+ * Convert the entry to a hash.
143
+ */
144
+ VALUE
145
+ rb_ldap_entry_to_hash (VALUE self)
146
+ {
147
+ VALUE attrs = rb_ldap_entry_get_attributes (self);
148
+ VALUE hash = rb_hash_new ();
149
+ VALUE attr, vals;
150
+ int i;
151
+
152
+ Check_Type (attrs, T_ARRAY);
153
+ rb_hash_aset (hash, rb_tainted_str_new2 ("dn"),
154
+ rb_ary_new3 (1, rb_ldap_entry_get_dn (self)));
155
+ for (i = 0; i < RARRAY_LEN (attrs); i++)
156
+ {
157
+ attr = rb_ary_entry (attrs, i);
158
+ vals = rb_ldap_entry_get_values (self, attr);
159
+ rb_hash_aset (hash, attr, vals);
160
+ }
161
+
162
+ return hash;
163
+ }
164
+
165
+ /*
166
+ * call-seq:
167
+ * entry.inspect => String
168
+ *
169
+ * Produce a concise representation of the entry.
170
+ */
171
+ VALUE
172
+ rb_ldap_entry_inspect (VALUE self)
173
+ {
174
+ VALUE str;
175
+ const char *c;
176
+
177
+ c = rb_obj_classname (self);
178
+ str = rb_str_new (0, strlen (c) + 10 + 16 + 1); /* 10:tags 16:addr 1:nul */
179
+ sprintf (RSTRING_PTR (str), "#<%s:0x%lx\n", c, self);
180
+
181
+ #if RUBY_VERSION_CODE < 190
182
+ RSTRING(str)->len = strlen (RSTRING_PTR (str));
183
+ #else
184
+ rb_str_set_len(str, strlen (RSTRING_PTR (str)));
185
+ #endif
186
+
187
+ rb_str_concat (str, rb_inspect (rb_ldap_entry_to_hash (self)));
188
+ rb_str_cat2 (str, ">");
189
+
190
+ return str;
191
+ }
192
+
193
+ /* Document-class: LDAP::Entry
194
+ *
195
+ * These methods can be used to probe the entries returned by LDAP searches.
196
+ */
197
+ void
198
+ Init_ldap_entry ()
199
+ {
200
+ rb_cLDAP_Entry = rb_define_class_under (rb_mLDAP, "Entry", rb_cObject);
201
+ rb_define_const (rb_mLDAP, "Message", rb_cLDAP_Entry); /* for compatibility */
202
+ rb_undef_method (CLASS_OF (rb_cLDAP_Entry), "new");
203
+ rb_undef_alloc_func (rb_cLDAP_Entry);
204
+ rb_ldap_entry_define_method ("get_dn", rb_ldap_entry_get_dn, 0);
205
+ rb_ldap_entry_define_method ("get_values", rb_ldap_entry_get_values, 1);
206
+ rb_ldap_entry_define_method ("get_attributes",
207
+ rb_ldap_entry_get_attributes, 0);
208
+ rb_alias (rb_cLDAP_Entry, rb_intern ("dn"), rb_intern ("get_dn"));
209
+ rb_alias (rb_cLDAP_Entry, rb_intern ("vals"), rb_intern ("get_values"));
210
+ rb_alias (rb_cLDAP_Entry, rb_intern ("[]"), rb_intern ("get_values"));
211
+ rb_alias (rb_cLDAP_Entry, rb_intern ("attrs"),
212
+ rb_intern ("get_attributes"));
213
+ rb_ldap_entry_define_method ("to_hash", rb_ldap_entry_to_hash, 0);
214
+ rb_ldap_entry_define_method ("inspect", rb_ldap_entry_inspect, 0);
215
+ }
@@ -0,0 +1,268 @@
1
+ #!/usr/bin/env ruby-1.4
2
+ #
3
+ # extconf.rb for ldap extension
4
+ # $Id: extconf.rb,v 1.7 2006/04/18 23:49:56 ianmacd Exp $
5
+ #
6
+
7
+ require 'mkmf'
8
+
9
+ $INTERACTIVE = false
10
+
11
+ if( ARGV.include?("--help") )
12
+ print <<EOF
13
+ --with-ldap-dir specify the LDAP directory.
14
+ --with-ldap-include specify the directory containing ldap.h and lber.h.
15
+ --with-ldap-lib specify the directory containing the LDAP libraries.
16
+ --with-netscape build with Netscape SDK.
17
+ --with-openldap1 build with OpenLDAP 1.x.
18
+ --with-openldap2 build with OpenLDAP 2.x.
19
+ --with-wldap32 Active Directory Client API.
20
+
21
+ The following are library configuration options:
22
+ --with-libcrypto=crypto, --without-libcrypto
23
+ --with-libssl=ssl, --without-libssl
24
+ --with-libnsl=nsl, --without-libnsl
25
+ --with-libldap=ldap, --without-libldap
26
+ --with-liblber=lber, --without-liblber
27
+ --with-libldap_r=ldap_r, --without-libldap_r
28
+ --with-libpthread=pthread, --without-libpthread
29
+ --with-libresolv=resolv, --without-libresolv
30
+
31
+ --help show this help.
32
+ EOF
33
+ exit(0)
34
+ end
35
+
36
+ def find_files(dir = nil)
37
+ if( dir )
38
+ search_dirs = [dir]
39
+ else
40
+ search_dirs =
41
+ ["/usr/local", "/usr", "/opt"] +
42
+ Dir.glob("/usr/local/./*ldap*").collect{|d| d.gsub(/\/\.\//, "/")} +
43
+ Dir.glob("/usr/./*ldap*").collect{|d| d.gsub(/\/\.\//, "/")}
44
+ end
45
+ for d in search_dirs
46
+ h = File.join(d,"include","ldap.h")
47
+ l = File.join(d,"lib","libldap*")
48
+ if( File.exist?(h) )
49
+ l = Dir.glob(l)[0]
50
+ if( l )
51
+ if( $INTERACTIVE )
52
+ print("--with-ldap-dir=#{d} [y/n]")
53
+ ans = $stdin.gets
54
+ ans.chop!
55
+ if( ans == "y" )
56
+ result = [d, File.basename(l).split(".")[0][3..-1], File.basename(h)]
57
+ return result
58
+ break
59
+ end
60
+ else
61
+ print("--with-ldap-dir=#{d}\n")
62
+ result = [d, File.basename(l).split(".")[0][3..-1], File.basename(h)]
63
+ return result
64
+ break
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ def ldap_with_config(arg, default = nil)
72
+ cfg1 = with_config(arg, nil)
73
+ cfg2 = arg_config("--without-" + arg, nil)
74
+ if( cfg1 )
75
+ return cfg1
76
+ else
77
+ if( cfg2 )
78
+ return nil
79
+ else
80
+ return default
81
+ end
82
+ end
83
+ end
84
+
85
+ $use_netscape = ldap_with_config("netscape")
86
+ $use_openldap1 = ldap_with_config("openldap1")
87
+ $use_openldap2 = ldap_with_config("openldap2")
88
+ $use_wldap32 = ldap_with_config("wldap32")
89
+
90
+ dir_config('ldap')
91
+ $ldap_dir = ldap_with_config("ldap-dir") || ldap_with_config("ldap")
92
+
93
+ $ldap_dir, $libldap, $ldap_h = find_files($ldap_dir)
94
+
95
+ if( !($use_netscape || $use_openldap1 || $use_openldap2 || $use_wldap32) )
96
+ case $libldap
97
+ when /^ldapssl[0-9]+$/
98
+ print("--with-netscape\n")
99
+ $use_netscape = "4"
100
+ when /^ssldap50+$/, /^ldap50+$/
101
+ print("--with-netscape=5")
102
+ $use_netscape = "5"
103
+ else
104
+ print("--with-openldap2\n")
105
+ $use_openldap2 = true
106
+ end
107
+ end
108
+ if( $use_netscape == true )
109
+ $use_netscape = "5"
110
+ end
111
+
112
+ if( $use_netscape )
113
+ case $use_netscape
114
+ when /^4/
115
+ $defs << "-DUSE_NETSCAPE_SDK"
116
+ #$libnsl = ldap_with_config("libnsl", "nsl")
117
+ #$libpthread = ldap_with_config("libpthread", "pthread")
118
+ $libresolv = ldap_with_config("libresolv", "resolv")
119
+ $libldap = ldap_with_config("libldap", $libldap)
120
+ $libns = ldap_with_config("libns", "nspr3,plc3,plds3").split(",")
121
+ when /^5/
122
+ $defs << "-DUSE_NETSCAPE_SDK"
123
+ #$libnsl = ldap_with_config("libnsl", "nsl")
124
+ #$libpthread = ldap_with_config("libpthread", "pthread")
125
+ $libresolv = ldap_with_config("libresolv", "resolv")
126
+ $libldap = ldap_with_config("libldap", $libldap)
127
+ $libns = ldap_with_config("libns", "nspr4,plc4,plds4").split(",")
128
+ $liblber = ldap_with_config("liblber", "lber50")
129
+ $libssl = ldap_with_config("libssl", "ssl3")
130
+ end
131
+ end
132
+
133
+ if( $use_openldap1 )
134
+ $defs << "-DUSE_OPENLDAP1"
135
+ $defs << "-DUSE_OPENLDAP"
136
+ $libresolv = ldap_with_config("libresolv", "resolv")
137
+ $libldap = ldap_with_config("libldap", "ldap")
138
+ $liblber = ldap_with_config("liblber", "lber")
139
+ end
140
+
141
+ if( $use_openldap2 )
142
+ $defs << "-DUSE_OPENLDAP2"
143
+ $defs << "-DUSE_OPENLDAP"
144
+ # OpenLDAP 2.3 finally deprecates a bunch of non-_ext functions. We need
145
+ # this to enable them.
146
+ $defs << "-DLDAP_DEPRECATED"
147
+ $libresolv = ldap_with_config("libresolv", "resolv")
148
+ $libcrypto = ldap_with_config("libcrypto", "crypto")
149
+ $libssl = ldap_with_config("libssl", "ssl")
150
+ $libpthread = ldap_with_config("libpthread", "pthread")
151
+ $libnsl = ldap_with_config("libnsl", "nsl")
152
+ $liblber = ldap_with_config("liblber", "lber")
153
+ $libldap_r = ldap_with_config("libldap_r", "ldap_r")
154
+ $libldap = ldap_with_config("libldap", "ldap")
155
+ end
156
+
157
+ if( $use_wldap32 )
158
+ srcdir = File.dirname($0)
159
+ if( !File.exist?("win") )
160
+ Dir.mkdir("win")
161
+ end
162
+ `lib /def:#{srcdir}/win/wldap32.def /out:#{srcdir}/win/wldap32.lib`
163
+ $defs << "-DUSE_WLDAP32"
164
+ dir_config("wldap32", "#{srcdir}/win", "./win")
165
+ $libldap = ldap_with_config("libldap", "wldap32")
166
+ end
167
+
168
+ if( $libpthread )
169
+ $defs << "-D_REENTRANT"
170
+ end
171
+
172
+ if( $use_wldap32 )
173
+ have_header("winldap.h")
174
+ have_header("winlber.h")
175
+ have_header("sys/time.h")
176
+ else
177
+ ldap_h = have_header("ldap.h")
178
+ lber_h = have_header("lber.h")
179
+ ldap_ssl_h = have_header("ldap_ssl.h")
180
+ if( !(ldap_h && lber_h) )
181
+ print("can't find ldap.h and lber.h\n")
182
+ print("use the option '--with-ldap-dir'!\n")
183
+ exit(0)
184
+ end
185
+
186
+ have_header("openssl/ssl.h") || have_header("ssl.h")
187
+ have_header("openssl/crypto.h") || have_header("crypto.h")
188
+ end
189
+
190
+ for l in [$libcrypto, $libssl, $libnsl, $libpthread, $libresolv,
191
+ $libns, $liblber, $libldap_r, $libldap].flatten
192
+ if( l )
193
+ have_library(l)
194
+ end
195
+ end
196
+
197
+ have_func("ldap_init")
198
+ have_func("ldap_set_option")
199
+ have_func("ldap_get_option")
200
+ have_func("ldap_start_tls_s") if $use_openldap2
201
+ have_func("ldap_memfree")
202
+ have_func("ldap_perror") if !arg_config("--disable-ldap-perror")
203
+ have_func("ldap_sort_entries")
204
+ #have_func("ldap_sort_values")
205
+ have_func("ldapssl_init") # NS SDK
206
+ have_func("ldap_sslinit") # WLDAP32
207
+ have_func("ldap_sasl_bind_s")
208
+ have_func("ldap_compare_s")
209
+ have_func("ldap_add_ext_s")
210
+ have_func("ldap_compare_ext_s")
211
+ have_func("ldap_delete_ext_s")
212
+ have_func("ldap_modify_ext_s")
213
+ have_func("ldap_search_ext_s")
214
+ have_func("ldap_unbind_ext_s")
215
+ have_func("ldap_sasl_interactive_bind_s")
216
+
217
+ $defs << "-DRUBY_VERSION_CODE=#{RUBY_VERSION.gsub(/\D/, '')}"
218
+
219
+ create_makefile("ldap")
220
+
221
+
222
+ $slapd = ldap_with_config("slapd") || File.join($ldap_dir,"libexec","slapd")
223
+ $schema_dir = ldap_with_config("schema-dir")
224
+ if( !$schema_dir )
225
+ $schema_dir = File.join($ldap_dir,"etc","openldap","schema")
226
+ if( !File.exist?($schema_dir) )
227
+ $schema_dir = File.join($ldap_dir,"etc","openldap")
228
+ end
229
+ end
230
+
231
+ $run_test = "/bin/sh $(srcdir)/test/test.sh #{CONFIG['RUBY_INSTALL_NAME']}"
232
+ if( $use_openldap1 )
233
+ $run_test += " openldap1"
234
+ else( $use_openldap2 )
235
+ if( $libssl && $libcrypto )
236
+ $run_test = "/bin/sh $(srcdir)/test/test.sh #{CONFIG['RUBY_INSTALL_NAME']} newcert; " + $run_test
237
+ $run_test += " openldap2-ssl"
238
+ else
239
+ $run_test += " openldap2"
240
+ end
241
+ end
242
+ $run_test += " #{$slapd} #{$schema_dir}"
243
+
244
+
245
+ File.open("Makefile","a"){|f|
246
+ f.print <<EOF
247
+
248
+ test::
249
+ \t#{$run_test}
250
+
251
+ testclean: test-clean
252
+
253
+ test-clean::
254
+ \t(cd $(srcdir); /bin/sh test/test.sh clean)
255
+
256
+ reconfig::
257
+ \t$(RUBY_INSTALL_NAME) $(srcdir)/extconf.rb #{ARGV.join(" ")}
258
+
259
+ doc:
260
+ #\t$(RUBY_INSTALL_NAME) -rrdoc/ri/ri_paths -e 'puts RI::Paths::PATH[0]'
261
+ \trdoc --ri-site *.c lib/ldap
262
+
263
+ unit:
264
+ \t(cd test; $(RUBY_INSTALL_NAME) tc_ldif.rb)
265
+
266
+ .PHONY: doc
267
+ EOF
268
+ }