1 /* LDAP client interface for XEmacs.
2 Copyright (C) 1998 Free Software Foundation, Inc.
4 This file is part of XEmacs.
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
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
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. */
21 /* Synched up with: Not in FSF. */
23 /* Author: Oscar Figueiredo with lots of support from Hrvoje Niksic */
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 - OpenLDAP 1.2 (http://www.openldap.org/)
30 - Netscape's LDAP SDK (http://developer.netscape.com/) */
43 static int ldap_default_port;
44 static Lisp_Object Vldap_default_base;
46 /* Needed by the lrecord definition */
49 /* ldap-open plist keywords */
50 static Lisp_Object Qport, Qauth, Qbinddn, Qpasswd, Qderef, Qtimelimit, Qsizelimit;
51 /* Search scope limits */
52 static Lisp_Object Qbase, Qonelevel, Qsubtree;
53 /* Authentication methods */
54 static Lisp_Object Qkrbv41, Qkrbv42;
56 static Lisp_Object Qnever, Qalways, Qfind;
58 /************************************************************************/
59 /* Utility Functions */
60 /************************************************************************/
63 signal_ldap_error (LDAP *ld, LDAPMessage *res, int ldap_err)
67 #if defined HAVE_LDAP_PARSE_RESULT
69 ldap_err = ldap_parse_result (ld, res,
71 NULL, NULL, NULL, NULL, 0);
72 if (ldap_err == LDAP_SUCCESS)
74 #elif defined HAVE_LDAP_GET_LDERRNO
75 ldap_err = ldap_get_lderrno (ld, NULL, NULL);
76 #elif defined HAVE_LDAP_RESULT2ERROR
77 ldap_err = ldap_result2error (ld, res, 0);
79 ldap_err = ld->ld_errno;
82 signal_simple_error ("LDAP error",
83 build_string (ldap_err2string (ldap_err)));
87 /************************************************************************/
88 /* ldap lrecord basic functions */
89 /************************************************************************/
92 make_ldap (struct Lisp_LDAP *ldap)
94 Lisp_Object lisp_ldap;
95 XSETLDAP (lisp_ldap, ldap);
100 mark_ldap (Lisp_Object obj)
102 return XLDAP (obj)->host;
106 print_ldap (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
110 struct Lisp_LDAP *ldap = XLDAP (obj);
113 error ("printing unreadable object #<ldap %s>",
114 XSTRING_DATA (ldap->host));
116 write_c_string ("#<ldap ", printcharfun);
117 print_internal (ldap->host, printcharfun, 1);
119 write_c_string ("(dead) ",printcharfun);
120 sprintf (buf, " 0x%x>", (unsigned int)ldap);
121 write_c_string (buf, printcharfun);
124 static struct Lisp_LDAP *
127 struct Lisp_LDAP *ldap =
128 alloc_lcrecord_type (struct Lisp_LDAP, &lrecord_ldap);
136 finalize_ldap (void *header, int for_disksave)
138 struct Lisp_LDAP *ldap = (struct Lisp_LDAP *) header;
141 signal_simple_error ("Can't dump an emacs containing LDAP objects",
145 ldap_unbind (ldap->ld);
149 DEFINE_LRECORD_IMPLEMENTATION ("ldap", ldap,
150 mark_ldap, print_ldap, finalize_ldap,
151 NULL, NULL, 0, struct Lisp_LDAP);
156 /************************************************************************/
157 /* Basic ldap accessors */
158 /************************************************************************/
160 DEFUN ("ldapp", Fldapp, 1, 1, 0, /*
161 Return t if OBJECT is a LDAP connection.
165 return LDAPP (object) ? Qt : Qnil;
168 DEFUN ("ldap-host", Fldap_host, 1, 1, 0, /*
169 Return the server host of the connection LDAP, as a string.
174 return (XLDAP (ldap))->host;
177 DEFUN ("ldap-live-p", Fldap_status, 1, 1, 0, /*
178 Return t if LDAP is an active LDAP connection.
183 return (XLDAP (ldap))->ld ? Qt : Qnil;
186 /************************************************************************/
187 /* Opening/Closing a LDAP connection */
188 /************************************************************************/
191 DEFUN ("ldap-open", Fldap_open, 1, 2, 0, /*
192 Open a LDAP connection to HOST.
193 PLIST is a plist containing additional parameters for the connection.
194 Valid keys in that list are:
195 `port' the TCP port to use for the connection if different from
197 `auth' is the authentication method to use, possible values depend on
198 the LDAP library XEmacs was compiled with: `simple', `krbv41' and `krbv42'.
199 `binddn' is the distinguished name of the user to bind as (in RFC 1779 syntax).
200 `passwd' is the password to use for simple authentication.
201 `deref' is one of the symbols `never', `always', `search' or `find'.
202 `timelimit' is the timeout limit for the connection in seconds.
203 `sizelimit' is the maximum number of matches to return.
207 /* This function can GC */
208 struct Lisp_LDAP *ldap;
211 int ldap_auth = LDAP_AUTH_SIMPLE;
212 char *ldap_binddn = NULL;
213 char *ldap_passwd = NULL;
214 int ldap_deref = LDAP_DEREF_NEVER;
215 int ldap_timelimit = 0;
216 int ldap_sizelimit = 0;
219 Lisp_Object list, keyword, value;
223 EXTERNAL_PROPERTY_LIST_LOOP (list, keyword, value, plist)
226 if (EQ (keyword, Qport))
229 ldap_port = XINT (value);
231 /* Authentication method */
232 if (EQ (keyword, Qauth))
234 if (EQ (value, Qsimple))
235 ldap_auth = LDAP_AUTH_SIMPLE;
236 #ifdef LDAP_AUTH_KRBV41
237 else if (EQ (value, Qkrbv41))
238 ldap_auth = LDAP_AUTH_KRBV41;
240 #ifdef LDAP_AUTH_KRBV42
241 else if (EQ (value, Qkrbv42))
242 ldap_auth = LDAP_AUTH_KRBV42;
245 signal_simple_error ("Invalid authentication method", value);
248 else if (EQ (keyword, Qbinddn))
250 CHECK_STRING (value);
251 GET_C_STRING_OS_DATA_ALLOCA (value, ldap_binddn);
254 else if (EQ (keyword, Qpasswd))
256 CHECK_STRING (value);
257 GET_C_STRING_OS_DATA_ALLOCA (value, ldap_passwd);
260 else if (EQ (keyword, Qderef))
262 if (EQ (value, Qnever))
263 ldap_deref = LDAP_DEREF_NEVER;
264 else if (EQ (value, Qsearch))
265 ldap_deref = LDAP_DEREF_SEARCHING;
266 else if (EQ (value, Qfind))
267 ldap_deref = LDAP_DEREF_FINDING;
268 else if (EQ (value, Qalways))
269 ldap_deref = LDAP_DEREF_ALWAYS;
271 signal_simple_error ("Invalid deref value", value);
274 else if (EQ (keyword, Qtimelimit))
277 ldap_timelimit = XINT (value);
280 else if (EQ (keyword, Qsizelimit))
283 ldap_sizelimit = XINT (value);
289 ldap_port = ldap_default_port;
292 /* Connect to the server and bind */
293 slow_down_interrupts ();
294 ld = ldap_open ((char *)XSTRING_DATA (host), ldap_port);
295 speed_up_interrupts ();
298 signal_simple_error_2 ("Failed connecting to host",
300 lisp_strerror (errno));
303 #ifdef HAVE_LDAP_SET_OPTION
304 if ((err = ldap_set_option (ld, LDAP_OPT_DEREF,
305 (void *)&ldap_deref)) != LDAP_SUCCESS)
306 signal_ldap_error (ld, NULL, err);
307 if ((err = ldap_set_option (ld, LDAP_OPT_TIMELIMIT,
308 (void *)&ldap_timelimit)) != LDAP_SUCCESS)
309 signal_ldap_error (ld, NULL, err);
310 if ((err = ldap_set_option (ld, LDAP_OPT_SIZELIMIT,
311 (void *)&ldap_sizelimit)) != LDAP_SUCCESS)
312 signal_ldap_error (ld, NULL, err);
313 if ((err = ldap_set_option (ld, LDAP_OPT_REFERRALS,
314 LDAP_OPT_ON)) != LDAP_SUCCESS)
315 signal_ldap_error (ld, NULL, err);
316 #else /* not HAVE_LDAP_SET_OPTION */
317 ld->ld_deref = ldap_deref;
318 ld->ld_timelimit = ldap_timelimit;
319 ld->ld_sizelimit = ldap_sizelimit;
320 #ifdef LDAP_REFERRALS
321 ld->ld_options = LDAP_OPT_REFERRALS;
322 #else /* not LDAP_REFERRALS */
324 #endif /* not LDAP_REFERRALS */
325 #endif /* not HAVE_LDAP_SET_OPTION */
327 /* ldap_bind_s calls select and may be wedged by SIGIO. */
328 slow_down_interrupts ();
329 err = ldap_bind_s (ld, ldap_binddn, ldap_passwd, ldap_auth);
330 speed_up_interrupts ();
331 if (err != LDAP_SUCCESS)
332 signal_simple_error ("Failed binding to the server",
333 build_string (ldap_err2string (err)));
335 ldap = allocate_ldap ();
339 return make_ldap (ldap);
344 DEFUN ("ldap-close", Fldap_close, 1, 1, 0, /*
345 Close an LDAP connection.
349 struct Lisp_LDAP *lldap;
350 CHECK_LIVE_LDAP (ldap);
351 lldap = XLDAP (ldap);
352 ldap_unbind (lldap->ld);
359 /************************************************************************/
360 /* Working on a LDAP connection */
361 /************************************************************************/
362 struct ldap_unwind_struct
365 struct berval **vals;
370 ldap_search_unwind (Lisp_Object unwind_obj)
372 struct ldap_unwind_struct *unwind =
373 (struct ldap_unwind_struct *) get_opaque_ptr (unwind_obj);
375 ldap_msgfree (unwind->res);
377 ldap_value_free_len (unwind->vals);
381 DEFUN ("ldap-search-internal", Fldap_search_internal, 2, 7, 0, /*
382 Perform a search on an open LDAP connection.
383 LDAP is an LDAP connection object created with `ldap-open'.
384 FILTER is a filter string for the search as described in RFC 1558.
385 BASE is the distinguished name at which to start the search.
386 SCOPE is one of the symbols `base', `onelevel' or `subtree' indicating
387 the scope of the search.
388 ATTRS is a list of strings indicating which attributes to retrieve
389 for each matching entry. If nil return all available attributes.
390 If ATTRSONLY is non-nil then only the attributes are retrieved, not
391 the associated values.
392 If WITHDN is non-nil each entry in the result will be prepennded with
393 its distinguished name DN.
394 The function returns a list of matching entries. Each entry is itself
395 an alist of attribute/value pairs optionally preceded by the DN of the
396 entry according to the value of WITHDN.
398 (ldap, filter, base, scope, attrs, attrsonly, withdn))
400 /* This function can GC */
409 struct ldap_unwind_struct unwind;
411 int ldap_scope = LDAP_SCOPE_SUBTREE;
412 char **ldap_attributes = NULL;
414 int speccount = specpdl_depth ();
416 Lisp_Object list, entry, result;
417 struct gcpro gcpro1, gcpro2, gcpro3;
419 list = entry = result = Qnil;
420 GCPRO3 (list, entry, result);
425 /* Do all the parameter checking */
426 CHECK_LIVE_LDAP (ldap);
427 ld = XLDAP (ldap)->ld;
430 CHECK_STRING (filter);
435 base = Vldap_default_base;
445 if (EQ (scope, Qbase))
446 ldap_scope = LDAP_SCOPE_BASE;
447 else if (EQ (scope, Qonelevel))
448 ldap_scope = LDAP_SCOPE_ONELEVEL;
449 else if (EQ (scope, Qsubtree))
450 ldap_scope = LDAP_SCOPE_SUBTREE;
452 signal_simple_error ("Invalid scope", scope);
455 /* Attributes to search */
459 ldap_attributes = alloca_array (char *, 1 + XINT (Flength (attrs)));
462 EXTERNAL_LIST_LOOP (attrs, attrs)
464 Lisp_Object current = XCAR (attrs);
465 CHECK_STRING (current);
466 GET_C_STRING_OS_DATA_ALLOCA (current, ldap_attributes[i]);
469 ldap_attributes[i] = NULL;
472 /* Attributes only ? */
473 CHECK_SYMBOL (attrsonly);
475 /* Perform the search */
477 NILP (base) ? "" : (char *) XSTRING_DATA (base),
479 NILP (filter) ? "" : (char *) XSTRING_DATA (filter),
481 NILP (attrsonly) ? 0 : 1)
484 signal_ldap_error (ld, NULL, 0);
487 /* Ensure we don't exit without cleaning up */
488 record_unwind_protect (ldap_search_unwind,
489 make_opaque_ptr (&unwind));
491 /* Build the results list */
494 /* ldap_result calls select() and can get wedged by EINTR signals */
495 slow_down_interrupts ();
496 rc = ldap_result (ld, LDAP_RES_ANY, 0, NULL, &unwind.res);
497 speed_up_interrupts ();
498 while (rc == LDAP_RES_SEARCH_ENTRY)
502 e = ldap_first_entry (ld, unwind.res);
503 /* #### This call to message() is pretty fascist, because it
504 destroys the current echo area contents, even when invoked
505 from Lisp. It should use echo_area_message() instead, and
506 restore the old echo area contents later. */
507 message ("Parsing ldap results... %d", matches);
509 /* Get the DN if required */
512 dn = ldap_get_dn (ld, e);
514 signal_ldap_error (ld, e, 0);
515 entry = Fcons (build_ext_string (dn, FORMAT_OS), Qnil);
517 for (a= ldap_first_attribute (ld, e, &ptr);
519 a = ldap_next_attribute (ld, e, ptr) )
521 list = Fcons (build_ext_string (a, FORMAT_OS), Qnil);
522 unwind.vals = ldap_get_values_len (ld, e, a);
523 if (unwind.vals != NULL)
525 for (i = 0; unwind.vals[i] != NULL; i++)
527 list = Fcons (make_ext_string (unwind.vals[i]->bv_val,
528 unwind.vals[i]->bv_len,
533 entry = Fcons (Fnreverse (list),
535 ldap_value_free_len (unwind.vals);
538 result = Fcons (Fnreverse (entry),
540 ldap_msgfree (unwind.res);
543 slow_down_interrupts ();
544 rc = ldap_result (ld, LDAP_RES_ANY, 0, NULL, &(unwind.res));
545 speed_up_interrupts ();
549 signal_ldap_error (ld, unwind.res, 0);
552 signal_ldap_error (ld, NULL, LDAP_TIMELIMIT_EXCEEDED);
554 #if defined HAVE_LDAP_PARSE_RESULT
555 rc2 = ldap_parse_result (ld, unwind.res,
557 NULL, NULL, NULL, NULL, 0);
558 if (rc2 != LDAP_SUCCESS)
560 #elif defined HAVE_LDAP_RESULT2ERROR
561 rc = ldap_result2error (ld, unwind.res, 0);
563 if ((rc != LDAP_SUCCESS) && (rc != LDAP_SIZELIMIT_EXCEEDED))
564 signal_ldap_error (ld, NULL, rc);
566 ldap_msgfree (unwind.res);
567 unwind.res = (LDAPMessage *)NULL;
568 /* #### See above for calling message(). */
569 message ("Parsing ldap results... done");
571 unbind_to (speccount, Qnil);
573 return Fnreverse (result);
580 defsymbol (&Qldapp, "ldapp");
581 defsymbol (&Qport, "port");
582 defsymbol (&Qauth, "auth");
583 defsymbol (&Qbinddn, "binddn");
584 defsymbol (&Qpasswd, "passwd");
585 defsymbol (&Qderef, "deref");
586 defsymbol (&Qtimelimit, "timelimit");
587 defsymbol (&Qsizelimit, "sizelimit");
588 defsymbol (&Qbase, "base");
589 defsymbol (&Qonelevel, "onelevel");
590 defsymbol (&Qsubtree, "subtree");
591 defsymbol (&Qkrbv41, "krbv41");
592 defsymbol (&Qkrbv42, "krbv42");
593 defsymbol (&Qnever, "never");
594 defsymbol (&Qalways, "always");
595 defsymbol (&Qfind, "find");
598 DEFSUBR (Fldap_host);
599 DEFSUBR (Fldap_status);
600 DEFSUBR (Fldap_open);
601 DEFSUBR (Fldap_close);
602 DEFSUBR (Fldap_search_internal);
609 ldap_default_port = LDAP_PORT;
610 Vldap_default_base = Qnil;
612 DEFVAR_INT ("ldap-default-port", &ldap_default_port /*
613 Default TCP port for LDAP connections.
614 Initialized from the LDAP library. Default value is 389.
617 DEFVAR_LISP ("ldap-default-base", &Vldap_default_base /*
618 Default base for LDAP searches.
619 This is a string using the syntax of RFC 1779.
620 For instance, "o=ACME, c=US" limits the search to the
621 Acme organization in the United States.