259
|
1 /* LDAP client interface for XEmacs.
|
265
|
2 Copyright (C) 1998 Free Software Foundation, Inc.
|
259
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: Not in FSF. */
|
|
22
|
|
23 /* Author: Oscar Figueiredo */
|
|
24
|
|
25 /* This file provides lisp primitives for access to an LDAP library
|
|
26 conforming to the API defined in RFC 1823.
|
|
27 It has been tested with:
|
|
28 - UMich LDAP 3.3 (http://www.umich.edu/~dirsvcs/ldap/)
|
|
29 - Netscape's LDAP SDK 1.0 (http://developer.netscape.com) */
|
|
30
|
|
31
|
|
32 #include <config.h>
|
|
33 #include "lisp.h"
|
|
34
|
265
|
35 #include <errno.h>
|
259
|
36 #include <lber.h>
|
|
37 #include <ldap.h>
|
|
38
|
|
39 #ifdef HAVE_NS_LDAP
|
|
40 #define HAVE_LDAP_SET_OPTION 1
|
|
41 #define HAVE_LDAP_GET_ERRNO 1
|
|
42 #else
|
|
43 #undef HAVE_LDAP_SET_OPTION
|
|
44 #undef HAVE_LDAP_GET_ERRNO
|
|
45 #endif
|
|
46
|
|
47 static Lisp_Object Vldap_default_base;
|
|
48 static Lisp_Object Vldap_default_host;
|
|
49
|
|
50 /* ldap-search-internal plist keywords */
|
|
51 static Lisp_Object Qhost, Qfilter, Qattributes, Qattrsonly, Qbase, Qscope,
|
|
52 Qauth, Qbinddn, Qpasswd, Qderef, Qtimelimit, Qsizelimit;
|
|
53 /* Search scope limits */
|
|
54 static Lisp_Object Qbase, Qonelevel, Qsubtree;
|
|
55 /* Authentication methods */
|
|
56 #ifdef LDAP_AUTH_KRBV41
|
|
57 static Lisp_Object Qkrbv41;
|
|
58 #endif
|
|
59 #ifdef LDAP_AUTH_KRBV42
|
|
60 static Lisp_Object Qkrbv42;
|
|
61 #endif
|
|
62 /* Deref policy */
|
261
|
63 static Lisp_Object Qnever, Qalways, Qfind;
|
259
|
64
|
|
65 DEFUN ("ldap-search-internal", Fldap_search_internal, 1, 1, 0, /*
|
|
66 Perform a search on a LDAP server.
|
|
67 SEARCH-PLIST is a property list describing the search request.
|
|
68 Valid keys in that list are:
|
|
69 `host' is a string naming one or more (blank separated) LDAP servers to
|
|
70 to try to connect to. Each host name may optionally be of the form host:port.
|
|
71 `filter' is a filter string for the search as described in RFC 1558
|
|
72 `attributes' is a list of strings indicating which attributes to retrieve
|
|
73 for each matching entry. If nil return all available attributes.
|
|
74 `attrsonly' if non-nil indicates that only the attributes are retrieved, not
|
|
75 the associated values.
|
|
76 `base' is the base for the search as described in RFC 1779.
|
|
77 `scope' is one of the three symbols `subtree', `base' or `onelevel'.
|
|
78 `auth' is the authentication method to use, possible values depend on
|
|
79 the LDAP library XEmacs was compiled with: `simple', `krbv41' and `krbv42'.
|
|
80 `binddn' is the distinguished name of the user to bind as (in RFC 1779 syntax).
|
|
81 `passwd' is the password to use for simple authentication.
|
|
82 `deref' is one of the symbols `never', `always', `search' or `find'.
|
|
83 `timelimit' is the timeout limit for the connection in seconds.
|
|
84 `sizelimit' is the maximum number of matches to return.
|
|
85 The function returns a list of matching entries. Each entry is itself
|
|
86 an alist of attribute/values.
|
|
87 */
|
|
88 (search_plist))
|
|
89 {
|
|
90 /* This function calls lisp */
|
|
91
|
|
92 /* Vars for query */
|
|
93 LDAP *ld;
|
|
94 LDAPMessage *res, *e;
|
|
95 BerElement *ptr;
|
|
96 char *a;
|
|
97 int i, rc, err;
|
|
98
|
|
99 char *ldap_host = NULL;
|
|
100 char *ldap_filter = NULL;
|
|
101 char **ldap_attributes = NULL;
|
|
102 int ldap_attrsonly = 0;
|
|
103 char *ldap_base = NULL;
|
|
104 int ldap_scope = LDAP_SCOPE_SUBTREE;
|
|
105 int ldap_auth = LDAP_AUTH_SIMPLE;
|
|
106 char *ldap_binddn = NULL;
|
|
107 char *ldap_passwd = NULL;
|
|
108 int ldap_deref = LDAP_DEREF_NEVER;
|
|
109 int ldap_timelimit = 0;
|
|
110 int ldap_sizelimit = 0;
|
|
111
|
|
112 char **vals = NULL;
|
|
113 int matches;
|
|
114
|
|
115 Lisp_Object list, entry, result, keyword, value;
|
|
116 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
|
|
117
|
|
118 list = entry = result = keyword = value = Qnil;
|
|
119 GCPRO5 (list, entry, result, keyword, value);
|
|
120
|
|
121
|
|
122 EXTERNAL_PROPERTY_LIST_LOOP(list, keyword, value, search_plist)
|
|
123 {
|
|
124 /* Host */
|
|
125 if (EQ (keyword, Qhost))
|
|
126 {
|
|
127 CHECK_STRING (value);
|
|
128 ldap_host = alloca (XSTRING_LENGTH (value) + 1);
|
|
129 strcpy (ldap_host, (char *)XSTRING_DATA (value));
|
|
130 }
|
|
131 /* Filter */
|
|
132 else if (EQ (keyword, Qfilter))
|
|
133 {
|
|
134 CHECK_STRING (value);
|
|
135 ldap_filter = alloca (XSTRING_LENGTH (value) + 1);
|
|
136 strcpy (ldap_filter, (char *)XSTRING_DATA (value));
|
|
137 }
|
|
138 /* Attributes */
|
|
139 else if (EQ (keyword, Qattributes))
|
|
140 {
|
|
141 if (! NILP (value))
|
|
142 {
|
|
143 Lisp_Object attr_left = value;
|
|
144 struct gcpro ngcpro1;
|
|
145
|
|
146 NGCPRO1 (attr_left);
|
|
147 CHECK_CONS (value);
|
|
148
|
|
149 ldap_attributes = alloca ((XINT (Flength (value)) + 1)*sizeof (char *));
|
|
150
|
|
151 for (i=0; !NILP (attr_left); i++) {
|
|
152 CHECK_STRING (XCAR (attr_left));
|
|
153 ldap_attributes[i] = alloca (XSTRING_LENGTH (XCAR (attr_left)) + 1);
|
|
154 strcpy(ldap_attributes[i],
|
|
155 (char *)(XSTRING_DATA( XCAR (attr_left))));
|
|
156 attr_left = XCDR (attr_left);
|
|
157 }
|
|
158 ldap_attributes[i] = NULL;
|
|
159 NUNGCPRO;
|
|
160 }
|
|
161 }
|
|
162 /* Attributes Only */
|
|
163 else if (EQ (keyword, Qattrsonly))
|
|
164 {
|
|
165 CHECK_SYMBOL (value);
|
|
166 ldap_attrsonly = NILP (value) ? 0 : 1;
|
|
167 }
|
|
168 /* Base */
|
|
169 else if (EQ (keyword, Qbase))
|
|
170 {
|
|
171 if (!NILP (value))
|
|
172 {
|
|
173 CHECK_STRING (value);
|
|
174 ldap_base = alloca (XSTRING_LENGTH (value) + 1);
|
|
175 strcpy (ldap_base, (char *)XSTRING_DATA (value));
|
|
176 }
|
|
177 }
|
|
178 /* Scope */
|
|
179 else if (EQ (keyword, Qscope))
|
|
180 {
|
|
181 CHECK_SYMBOL (value);
|
|
182
|
|
183 if (EQ (value, Qbase))
|
|
184 ldap_scope = LDAP_SCOPE_BASE;
|
|
185 else if (EQ (value, Qonelevel))
|
|
186 ldap_scope = LDAP_SCOPE_ONELEVEL;
|
|
187 else if (EQ (value, Qsubtree))
|
|
188 ldap_scope = LDAP_SCOPE_SUBTREE;
|
|
189 else
|
|
190 signal_simple_error ("Invalid scope", value);
|
|
191 }
|
|
192 /* Authentication method */
|
|
193 else if (EQ (keyword, Qauth))
|
|
194 {
|
|
195 CHECK_SYMBOL (value);
|
|
196
|
|
197 if (EQ (value, Qsimple))
|
|
198 ldap_auth = LDAP_AUTH_SIMPLE;
|
|
199 #ifdef LDAP_AUTH_KRBV41
|
|
200 else if (EQ (value, Qkrbv41))
|
|
201 ldap_auth = LDAP_AUTH_KRBV41;
|
|
202 #endif
|
|
203 #ifdef LDAP_AUTH_KRBV42
|
|
204 else if (EQ (value, Qkrbv42))
|
|
205 ldap_auth = LDAP_AUTH_KRBV42;
|
|
206 #endif
|
|
207 else
|
|
208 signal_simple_error ("Invalid authentication method", value);
|
|
209 }
|
|
210 /* Bind DN */
|
|
211 else if (EQ (keyword, Qbinddn))
|
|
212 {
|
|
213 if (!NILP (value))
|
|
214 {
|
|
215 CHECK_STRING (value);
|
|
216 ldap_binddn = alloca (XSTRING_LENGTH (value) + 1);
|
|
217 strcpy (ldap_binddn, (char *)XSTRING_DATA (value));
|
|
218 }
|
|
219 }
|
|
220 /* Password */
|
|
221 else if (EQ (keyword, Qpasswd))
|
|
222 {
|
|
223 if (!NILP (value))
|
|
224 {
|
|
225 CHECK_STRING (value);
|
|
226 ldap_passwd = alloca (XSTRING_LENGTH (value) + 1);
|
|
227 strcpy (ldap_passwd, (char *)XSTRING_DATA (value));
|
|
228 }
|
|
229 }
|
|
230 /* Deref */
|
|
231 else if (EQ (keyword, Qderef))
|
|
232 {
|
|
233 CHECK_SYMBOL (value);
|
|
234 if (EQ (value, Qnever))
|
|
235 ldap_deref = LDAP_DEREF_NEVER;
|
|
236 else if (EQ (value, Qsearch))
|
|
237 ldap_deref = LDAP_DEREF_SEARCHING;
|
|
238 else if (EQ (value, Qfind))
|
|
239 ldap_deref = LDAP_DEREF_FINDING;
|
|
240 else if (EQ (value, Qalways))
|
|
241 ldap_deref = LDAP_DEREF_ALWAYS;
|
|
242 else
|
|
243 signal_simple_error ("Invalid deref value", value);
|
|
244 }
|
|
245 /* Timelimit */
|
|
246 else if (EQ (keyword, Qtimelimit))
|
|
247 {
|
|
248 if (!NILP (value))
|
|
249 {
|
|
250 CHECK_INT (value);
|
|
251 ldap_timelimit = XINT (value);
|
|
252 }
|
|
253 }
|
|
254 /* Sizelimit */
|
|
255 else if (EQ (keyword, Qsizelimit))
|
|
256 {
|
|
257 if (!NILP (value))
|
|
258 {
|
|
259 CHECK_INT (value);
|
|
260 ldap_sizelimit = XINT (value);
|
|
261 }
|
|
262 }
|
|
263 }
|
|
264
|
|
265 /* Use ldap-default-base if no default base was given */
|
|
266 if (ldap_base == NULL && !NILP (Vldap_default_base))
|
|
267 {
|
|
268 CHECK_STRING (Vldap_default_base);
|
|
269 ldap_base = alloca (XSTRING_LENGTH (Vldap_default_base) + 1);
|
|
270 strcpy (ldap_base, (char *)XSTRING_DATA (Vldap_default_base));
|
|
271 }
|
|
272
|
|
273 /* Use ldap-default-host if no host was given */
|
|
274 if (ldap_host == NULL && !NILP (Vldap_default_host))
|
|
275 {
|
|
276 CHECK_STRING (Vldap_default_host);
|
|
277 ldap_host = alloca (XSTRING_LENGTH (Vldap_default_host) + 1);
|
|
278 strcpy (ldap_host, (char *)XSTRING_DATA (Vldap_default_host));
|
|
279 }
|
|
280
|
|
281 if (ldap_filter == NULL)
|
|
282 error ("Empty search filter");
|
|
283
|
|
284 /* Garbage collect before connecting (if using UMich lib).
|
|
285 This is ugly, I know, but without this, the UMich LDAP library 3.3
|
|
286 frequently reports "Can't contact LDAP server". I really need to
|
|
287 check what happens inside that lib. Anyway this should be harmless to
|
|
288 XEmacs and makes things work. */
|
|
289 #if defined (HAVE_UMICH_LDAP)
|
265
|
290 garbage_collect_1 ();
|
259
|
291 #endif
|
|
292
|
|
293 /* Connect to the server and bind */
|
|
294 message ("Connecting to %s...", ldap_host);
|
|
295 if ( (ld = ldap_open (ldap_host, LDAP_PORT)) == NULL )
|
265
|
296 signal_simple_error_2 ("Failed connecting to host",
|
|
297 build_string (ldap_host),
|
|
298 lisp_strerror (errno));
|
259
|
299
|
|
300 #if HAVE_LDAP_SET_OPTION
|
|
301 if (ldap_set_option (ld, LDAP_OPT_DEREF, (void *)&ldap_deref) != LDAP_SUCCESS)
|
|
302 error ("Failed to set deref option");
|
|
303 if (ldap_set_option (ld, LDAP_OPT_TIMELIMIT, (void *)&ldap_timelimit) != LDAP_SUCCESS)
|
|
304 error ("Failed to set timelimit option");
|
|
305 if (ldap_set_option (ld, LDAP_OPT_SIZELIMIT, (void *)&ldap_sizelimit) != LDAP_SUCCESS)
|
|
306 error ("Failed to set sizelimit option");
|
|
307 if (ldap_set_option (ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON) != LDAP_SUCCESS)
|
|
308 error ("Failed to set referral option");
|
|
309 #else /* HAVE_LDAP_SET_OPTION */
|
|
310 ld->ld_deref = ldap_deref;
|
|
311 ld->ld_timelimit = ldap_timelimit;
|
|
312 ld->ld_sizelimit = ldap_sizelimit;
|
|
313 #ifdef LDAP_REFERRALS
|
|
314 ld->ld_options = LDAP_OPT_REFERRALS;
|
|
315 #else /* LDAP_REFERRALS */
|
|
316 ld->ld_options = 0;
|
|
317 #endif /* LDAP_REFERRALS */
|
|
318 #endif /* HAVE_LDAP_SET_OPTION */
|
|
319
|
|
320 message ("Binding to %s...", ldap_host);
|
|
321 if ( (err = (ldap_bind_s (ld, ldap_binddn, ldap_passwd, ldap_auth ))) != LDAP_SUCCESS )
|
|
322 signal_simple_error ("Failed binding to the server",
|
|
323 build_string (ldap_err2string (err)));
|
|
324
|
|
325 /* Perform the search */
|
|
326 message ("Searching with LDAP on %s...", ldap_host);
|
|
327 if ( ldap_search (ld, ldap_base, ldap_scope, ldap_filter,
|
|
328 ldap_attributes, ldap_attrsonly) == -1)
|
|
329 {
|
|
330 ldap_unbind (ld);
|
|
331 #if HAVE_LDAP_GET_ERRNO
|
|
332 signal_simple_error ("Error during LDAP search",
|
|
333 build_string (ldap_err2string (ldap_get_lderrno (ld, NULL, NULL))));
|
|
334 #else
|
|
335 signal_simple_error ("Error during LDAP search",
|
|
336 build_string (ldap_err2string (ld->ld_errno)));
|
|
337 #endif
|
|
338 }
|
|
339
|
|
340 /* Build the results list */
|
|
341 matches = 0;
|
|
342
|
|
343 while ( (rc = ldap_result (ld, LDAP_RES_ANY, 0, NULL, &res))
|
|
344 == LDAP_RES_SEARCH_ENTRY )
|
|
345 {
|
|
346 matches ++;
|
|
347 e = ldap_first_entry (ld, res);
|
|
348 message ("Parsing results... %d", matches);
|
|
349 entry = Qnil;
|
|
350 for (a= ldap_first_attribute (ld, e, &ptr);
|
|
351 a != NULL;
|
|
352 a= ldap_next_attribute (ld, e, ptr) )
|
|
353 {
|
|
354 list = Fcons (build_string (a), Qnil);
|
|
355 vals = ldap_get_values (ld, e, a);
|
|
356 if (vals != NULL)
|
|
357 {
|
|
358 for (i=0; vals[i]!=NULL; i++)
|
|
359 {
|
|
360 list = Fcons (build_string (vals[i]),
|
|
361 list);
|
|
362 }
|
|
363 }
|
|
364 entry = Fcons (Fnreverse (list),
|
|
365 entry);
|
|
366 ldap_value_free (vals);
|
|
367 }
|
|
368 result = Fcons (Fnreverse (entry),
|
|
369 result);
|
|
370 ldap_msgfree (res);
|
|
371 }
|
|
372
|
|
373 if (rc == -1)
|
|
374 {
|
|
375 #if HAVE_LDAP_GET_ERRNO
|
|
376 signal_simple_error ("Error retrieving result",
|
|
377 build_string (ldap_err2string (ldap_get_lderrno (ld, NULL, NULL))));
|
|
378 #else
|
|
379 signal_simple_error ("Error retrieving result",
|
|
380 build_string (ldap_err2string (ld->ld_errno)));
|
|
381 #endif
|
|
382 }
|
|
383
|
|
384 if ((rc = ldap_result2error (ld, res, 0)) != LDAP_SUCCESS)
|
|
385 {
|
|
386 #if HAVE_LDAP_GET_ERRNO
|
|
387 signal_simple_error ("Error on result",
|
|
388 build_string (ldap_err2string (ldap_get_lderrno (ld, NULL, NULL))));
|
|
389 #else
|
|
390 signal_simple_error ("Error on result",
|
|
391 build_string (ldap_err2string (ld->ld_errno)));
|
|
392 #endif
|
|
393 }
|
|
394
|
|
395 ldap_msgfree (res);
|
|
396 ldap_unbind (ld);
|
|
397 message ("Done.");
|
|
398
|
|
399 result = Fnreverse (result);
|
|
400 clear_message ();
|
|
401
|
|
402 UNGCPRO;
|
|
403 return result;
|
|
404 }
|
|
405
|
|
406
|
|
407 void
|
|
408 syms_of_eldap (void)
|
|
409 {
|
|
410 DEFSUBR(Fldap_search_internal);
|
|
411
|
|
412 defsymbol (&Qhost, "host");
|
|
413 defsymbol (&Qfilter, "filter");
|
|
414 defsymbol (&Qattributes, "attributes");
|
|
415 defsymbol (&Qattrsonly, "attrsonly");
|
|
416 defsymbol (&Qbase, "base");
|
|
417 defsymbol (&Qscope, "scope");
|
|
418 defsymbol (&Qauth, "auth");
|
|
419 defsymbol (&Qbinddn, "binddn");
|
|
420 defsymbol (&Qpasswd, "passwd");
|
|
421 defsymbol (&Qderef, "deref");
|
|
422 defsymbol (&Qtimelimit, "timelimit");
|
|
423 defsymbol (&Qsizelimit, "sizelimit");
|
|
424 defsymbol (&Qonelevel, "onelevel");
|
|
425 defsymbol (&Qsubtree, "subtree");
|
|
426 #ifdef LDAP_AUTH_KRBV41
|
|
427 defsymbol (&Qkrbv41, "krbv41");
|
|
428 #endif
|
|
429 #ifdef LDAP_AUTH_KRBV42
|
|
430 defsymbol (&Qkrbv42, "krbv42");
|
|
431 #endif
|
|
432 defsymbol (&Qnever, "never");
|
|
433 defsymbol (&Qalways, "always");
|
|
434 defsymbol (&Qfind, "find");
|
|
435 }
|
|
436
|
|
437 void
|
|
438 vars_of_eldap (void)
|
|
439 {
|
|
440 Fprovide (intern ("ldap-internal"));
|
|
441
|
|
442 DEFVAR_LISP ("ldap-default-host", &Vldap_default_host /*
|
|
443 Default LDAP host.
|
|
444 */ );
|
|
445
|
|
446 DEFVAR_LISP ("ldap-default-base", &Vldap_default_base /*
|
|
447 Default base for LDAP searches.
|
|
448 This is a string using the syntax of RFC 1779.
|
|
449 For instance, "o=ACME, c=US" limits the search to the
|
|
450 Acme organization in the United States.
|
|
451 */ );
|
|
452
|
|
453 Vldap_default_host = Qnil;
|
|
454 Vldap_default_base = Qnil;
|
|
455 }
|