comparison src/eldap.c @ 418:e804706bfb8c r21-2-17

Import from CVS: tag r21-2-17
author cvs
date Mon, 13 Aug 2007 11:23:13 +0200
parents 697ef44129c6
children 41dbb7a9d5f2
comparison
equal deleted inserted replaced
417:43a18b32d56e 418:e804706bfb8c
24 24
25 /* This file provides lisp primitives for access to an LDAP library 25 /* This file provides lisp primitives for access to an LDAP library
26 conforming to the API defined in RFC 1823. 26 conforming to the API defined in RFC 1823.
27 It has been tested with: 27 It has been tested with:
28 - UMich LDAP 3.3 (http://www.umich.edu/~dirsvcs/ldap/) 28 - UMich LDAP 3.3 (http://www.umich.edu/~dirsvcs/ldap/)
29 - OpenLDAP 1.0.3 (http://www.openldap.org/) 29 - OpenLDAP 1.2 (http://www.openldap.org/)
30 - Netscape's LDAP SDK 1.0 (http://developer.netscape.com/) */ 30 - Netscape's LDAP SDK (http://developer.netscape.com/) */
31 31
32 32
33 #include <config.h> 33 #include <config.h>
34 #include "lisp.h" 34 #include "lisp.h"
35 #include "opaque.h" 35 #include "opaque.h"
37 #include "buffer.h" 37 #include "buffer.h"
38 38
39 #include <errno.h> 39 #include <errno.h>
40 40
41 #include "eldap.h" 41 #include "eldap.h"
42
43 #ifdef HAVE_NS_LDAP
44 # define HAVE_LDAP_SET_OPTION 1
45 # define HAVE_LDAP_GET_ERRNO 1
46 #else
47 # undef HAVE_LDAP_SET_OPTION
48 # undef HAVE_LDAP_GET_ERRNO
49 #endif
50 42
51 static int ldap_default_port; 43 static int ldap_default_port;
52 static Lisp_Object Vldap_default_base; 44 static Lisp_Object Vldap_default_base;
53 45
54 /* Needed by the lrecord definition */ 46 /* Needed by the lrecord definition */
67 /************************************************************************/ 59 /************************************************************************/
68 /* Utility Functions */ 60 /* Utility Functions */
69 /************************************************************************/ 61 /************************************************************************/
70 62
71 static void 63 static void
72 signal_ldap_error (LDAP *ld) 64 signal_ldap_error (LDAP *ld, LDAPMessage *res, int ldap_err)
73 { 65 {
74 #ifdef HAVE_LDAP_GET_ERRNO 66 if (ldap_err <= 0)
75 signal_simple_error 67 {
76 ("LDAP error", 68 #if defined HAVE_LDAP_PARSE_RESULT
77 build_string (ldap_err2string (ldap_get_lderrno (ld, NULL, NULL)))); 69 int err;
70 ldap_err = ldap_parse_result (ld, res,
71 &err,
72 NULL, NULL, NULL, NULL, 0);
73 if (ldap_err == LDAP_SUCCESS)
74 ldap_err = err;
75 #elif defined HAVE_LDAP_GET_LDERRNO
76 ldap_err = ldap_get_lderrno (ld, NULL, NULL);
77 #elif defined HAVE_LDAP_RESULT2ERROR
78 ldap_err = ldap_result2error (ld, res, 0);
78 #else 79 #else
80 ldap_err = ld->ld_errno;
81 #endif
82 }
79 signal_simple_error ("LDAP error", 83 signal_simple_error ("LDAP error",
80 build_string (ldap_err2string (ld->ld_errno))); 84 build_string (ldap_err2string (ldap_err)));
81 #endif
82 } 85 }
83 86
84 87
85 /************************************************************************/ 88 /************************************************************************/
86 /* ldap lrecord basic functions */ 89 /* ldap lrecord basic functions */
111 error ("printing unreadable object #<ldap %s>", 114 error ("printing unreadable object #<ldap %s>",
112 XSTRING_DATA (ldap->host)); 115 XSTRING_DATA (ldap->host));
113 116
114 write_c_string ("#<ldap ", printcharfun); 117 write_c_string ("#<ldap ", printcharfun);
115 print_internal (ldap->host, printcharfun, 1); 118 print_internal (ldap->host, printcharfun, 1);
116 if (!ldap->livep) 119 if (!ldap->ld)
117 write_c_string ("(dead) ",printcharfun); 120 write_c_string ("(dead) ",printcharfun);
118 sprintf (buf, " 0x%x>", (unsigned int)ldap); 121 sprintf (buf, " 0x%x>", (unsigned int)ldap);
119 write_c_string (buf, printcharfun); 122 write_c_string (buf, printcharfun);
120 } 123 }
121 124
125 struct Lisp_LDAP *ldap = 128 struct Lisp_LDAP *ldap =
126 alloc_lcrecord_type (struct Lisp_LDAP, &lrecord_ldap); 129 alloc_lcrecord_type (struct Lisp_LDAP, &lrecord_ldap);
127 130
128 ldap->ld = NULL; 131 ldap->ld = NULL;
129 ldap->host = Qnil; 132 ldap->host = Qnil;
130 ldap->livep = 0;
131 return ldap; 133 return ldap;
132 } 134 }
133 135
134 static void 136 static void
135 finalize_ldap (void *header, int for_disksave) 137 finalize_ldap (void *header, int for_disksave)
138 140
139 if (for_disksave) 141 if (for_disksave)
140 signal_simple_error ("Can't dump an emacs containing LDAP objects", 142 signal_simple_error ("Can't dump an emacs containing LDAP objects",
141 make_ldap (ldap)); 143 make_ldap (ldap));
142 144
143 if (ldap->livep) 145 if (ldap->ld)
144 ldap_unbind (ldap->ld); 146 ldap_unbind (ldap->ld);
147 ldap->ld = NULL;
145 } 148 }
146 149
147 DEFINE_LRECORD_IMPLEMENTATION ("ldap", ldap, 150 DEFINE_LRECORD_IMPLEMENTATION ("ldap", ldap,
148 mark_ldap, print_ldap, finalize_ldap, 151 mark_ldap, print_ldap, finalize_ldap,
149 NULL, NULL, struct Lisp_LDAP); 152 NULL, NULL, struct Lisp_LDAP);
176 Return t if LDAP is an active LDAP connection. 179 Return t if LDAP is an active LDAP connection.
177 */ 180 */
178 (ldap)) 181 (ldap))
179 { 182 {
180 CHECK_LDAP (ldap); 183 CHECK_LDAP (ldap);
181 return (XLDAP (ldap))->livep ? Qt : Qnil; 184 return (XLDAP (ldap))->ld ? Qt : Qnil;
182 } 185 }
183 186
184 /************************************************************************/ 187 /************************************************************************/
185 /* Opening/Closing a LDAP connection */ 188 /* Opening/Closing a LDAP connection */
186 /************************************************************************/ 189 /************************************************************************/
297 host, 300 host,
298 lisp_strerror (errno)); 301 lisp_strerror (errno));
299 302
300 303
301 #ifdef HAVE_LDAP_SET_OPTION 304 #ifdef HAVE_LDAP_SET_OPTION
302 if (ldap_set_option (ld, LDAP_OPT_DEREF, (void *)&ldap_deref) != LDAP_SUCCESS) 305 if ((err = ldap_set_option (ld, LDAP_OPT_DEREF,
303 signal_ldap_error (ld); 306 (void *)&ldap_deref)) != LDAP_SUCCESS)
304 if (ldap_set_option (ld, LDAP_OPT_TIMELIMIT, 307 signal_ldap_error (ld, NULL, err);
305 (void *)&ldap_timelimit) != LDAP_SUCCESS) 308 if ((err = ldap_set_option (ld, LDAP_OPT_TIMELIMIT,
306 signal_ldap_error (ld); 309 (void *)&ldap_timelimit)) != LDAP_SUCCESS)
307 if (ldap_set_option (ld, LDAP_OPT_SIZELIMIT, 310 signal_ldap_error (ld, NULL, err);
308 (void *)&ldap_sizelimit) != LDAP_SUCCESS) 311 if ((err = ldap_set_option (ld, LDAP_OPT_SIZELIMIT,
309 signal_ldap_error (ld); 312 (void *)&ldap_sizelimit)) != LDAP_SUCCESS)
310 if (ldap_set_option (ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON) != LDAP_SUCCESS) 313 signal_ldap_error (ld, NULL, err);
311 signal_ldap_error (ld); 314 if ((err = ldap_set_option (ld, LDAP_OPT_REFERRALS,
315 LDAP_OPT_ON)) != LDAP_SUCCESS)
316 signal_ldap_error (ld, NULL, err);
312 #else /* not HAVE_LDAP_SET_OPTION */ 317 #else /* not HAVE_LDAP_SET_OPTION */
313 ld->ld_deref = ldap_deref; 318 ld->ld_deref = ldap_deref;
314 ld->ld_timelimit = ldap_timelimit; 319 ld->ld_timelimit = ldap_timelimit;
315 ld->ld_sizelimit = ldap_sizelimit; 320 ld->ld_sizelimit = ldap_sizelimit;
316 #ifdef LDAP_REFERRALS 321 #ifdef LDAP_REFERRALS
329 build_string (ldap_err2string (err))); 334 build_string (ldap_err2string (err)));
330 335
331 ldap = allocate_ldap (); 336 ldap = allocate_ldap ();
332 ldap->ld = ld; 337 ldap->ld = ld;
333 ldap->host = host; 338 ldap->host = host;
334 ldap->livep = 1;
335 339
336 return make_ldap (ldap); 340 return make_ldap (ldap);
337 } 341 }
338 342
339 343
345 { 349 {
346 struct Lisp_LDAP *lldap; 350 struct Lisp_LDAP *lldap;
347 CHECK_LIVE_LDAP (ldap); 351 CHECK_LIVE_LDAP (ldap);
348 lldap = XLDAP (ldap); 352 lldap = XLDAP (ldap);
349 ldap_unbind (lldap->ld); 353 ldap_unbind (lldap->ld);
350 lldap->livep = 0; 354 lldap->ld = NULL;
351 return Qnil; 355 return Qnil;
352 } 356 }
353 357
354 358
355 359
399 /* Vars for query */ 403 /* Vars for query */
400 LDAP *ld; 404 LDAP *ld;
401 LDAPMessage *e; 405 LDAPMessage *e;
402 BerElement *ptr; 406 BerElement *ptr;
403 char *a, *dn; 407 char *a, *dn;
404 int i, rc; 408 int i, rc, rc2;
405 int matches; 409 int matches;
406 struct ldap_unwind_struct unwind; 410 struct ldap_unwind_struct unwind;
407 411
408 int ldap_scope = LDAP_SCOPE_SUBTREE; 412 int ldap_scope = LDAP_SCOPE_SUBTREE;
409 char **ldap_attributes = NULL; 413 char **ldap_attributes = NULL;
474 NILP (base) ? "" : (char *) XSTRING_DATA (base), 478 NILP (base) ? "" : (char *) XSTRING_DATA (base),
475 ldap_scope, 479 ldap_scope,
476 NILP (filter) ? "" : (char *) XSTRING_DATA (filter), 480 NILP (filter) ? "" : (char *) XSTRING_DATA (filter),
477 ldap_attributes, 481 ldap_attributes,
478 NILP (attrsonly) ? 0 : 1) 482 NILP (attrsonly) ? 0 : 1)
479 == -1) 483 == -1)
480 { 484 {
481 signal_ldap_error (ld); 485 signal_ldap_error (ld, NULL, 0);
482 } 486 }
483 487
484 /* Ensure we don't exit without cleaning up */ 488 /* Ensure we don't exit without cleaning up */
485 record_unwind_protect (ldap_search_unwind, 489 record_unwind_protect (ldap_search_unwind,
486 make_opaque_ptr (&unwind)); 490 make_opaque_ptr (&unwind));
506 /* Get the DN if required */ 510 /* Get the DN if required */
507 if (! NILP (withdn)) 511 if (! NILP (withdn))
508 { 512 {
509 dn = ldap_get_dn (ld, e); 513 dn = ldap_get_dn (ld, e);
510 if (dn == NULL) 514 if (dn == NULL)
511 { 515 signal_ldap_error (ld, e, 0);
512 signal_ldap_error (ld);
513 }
514 entry = Fcons (build_ext_string (dn, FORMAT_OS), Qnil); 516 entry = Fcons (build_ext_string (dn, FORMAT_OS), Qnil);
515 } 517 }
516 for (a= ldap_first_attribute (ld, e, &ptr); 518 for (a= ldap_first_attribute (ld, e, &ptr);
517 a != NULL; 519 a != NULL;
518 a = ldap_next_attribute (ld, e, ptr) ) 520 a = ldap_next_attribute (ld, e, ptr) )
543 rc = ldap_result (ld, LDAP_RES_ANY, 0, NULL, &(unwind.res)); 545 rc = ldap_result (ld, LDAP_RES_ANY, 0, NULL, &(unwind.res));
544 speed_up_interrupts (); 546 speed_up_interrupts ();
545 } 547 }
546 548
547 if (rc == -1) 549 if (rc == -1)
548 { 550 signal_ldap_error (ld, unwind.res, 0);
549 signal_ldap_error (ld); 551
550 } 552 if (rc == 0)
553 signal_ldap_error (ld, NULL, LDAP_TIMELIMIT_EXCEEDED);
554
555 #if defined HAVE_LDAP_PARSE_RESULT
556 rc2 = ldap_parse_result (ld, unwind.res,
557 &rc,
558 NULL, NULL, NULL, NULL, 0);
559 if (rc2 != LDAP_SUCCESS)
560 rc = rc2;
561 #elif defined HAVE_LDAP_RESULT2ERROR
551 rc = ldap_result2error (ld, unwind.res, 0); 562 rc = ldap_result2error (ld, unwind.res, 0);
552 if ((rc != LDAP_SUCCESS) && 563 #endif
553 (rc != LDAP_SIZELIMIT_EXCEEDED)) 564 if ((rc != LDAP_SUCCESS) && (rc != LDAP_SIZELIMIT_EXCEEDED))
554 { 565 signal_ldap_error (ld, NULL, rc);
555 signal_ldap_error (ld);
556 }
557 566
558 ldap_msgfree (unwind.res); 567 ldap_msgfree (unwind.res);
559 unwind.res = (LDAPMessage *)NULL; 568 unwind.res = (LDAPMessage *)NULL;
560 /* #### See above for calling message(). */ 569 /* #### See above for calling message(). */
561 message ("Parsing ldap results... done"); 570 message ("Parsing ldap results... done");