Mercurial > hg > xemacs-beta
annotate modules/ldap/eldap.c @ 4742:4cf435fcebbc
Make #'letf not error if handed a #'values form.
lisp/ChangeLog addition:
2009-11-14 Aidan Kehoe <kehoea@parhasard.net>
* cl-macs.el (letf):
Check whether arguments to #'values are bound, and make them
unbound after evaluating BODY; document the limitations of this
macro.
tests/ChangeLog addition:
2009-11-14 Aidan Kehoe <kehoea@parhasard.net>
* automated/lisp-tests.el:
Don't call Known-Bug-Expect-Failure now that the particular letf
bug it tickled is fixed.
| author | Aidan Kehoe <kehoea@parhasard.net> |
|---|---|
| date | Sat, 14 Nov 2009 11:43:09 +0000 |
| parents | 3a87551bfeb5 |
| children | c12b646d84ee e0db3c197671 |
| rev | line source |
|---|---|
| 428 | 1 /* LDAP client interface for XEmacs. |
| 2 Copyright (C) 1998 Free Software Foundation, Inc. | |
| 2367 | 3 Copyright (C) 2004 Ben Wing. |
| 4 | |
| 428 | 5 |
| 6 This file is part of XEmacs. | |
| 7 | |
| 8 XEmacs is free software; you can redistribute it and/or modify it | |
| 9 under the terms of the GNU General Public License as published by the | |
| 10 Free Software Foundation; either version 2, or (at your option) any | |
| 11 later version. | |
| 12 | |
| 13 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
| 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 16 for more details. | |
| 17 | |
| 18 You should have received a copy of the GNU General Public License | |
| 19 along with XEmacs; see the file COPYING. If not, write to | |
| 20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
| 21 Boston, MA 02111-1307, USA. */ | |
| 22 | |
| 23 /* Synched up with: Not in FSF. */ | |
| 24 | |
| 996 | 25 /* Author: Oscar Figueiredo with lots of support from Hrvoje Niksic */ |
| 428 | 26 |
| 27 /* This file provides lisp primitives for access to an LDAP library | |
| 28 conforming to the API defined in RFC 1823. | |
| 29 It has been tested with: | |
| 30 - UMich LDAP 3.3 (http://www.umich.edu/~dirsvcs/ldap/) | |
| 996 | 31 - OpenLDAP 1.2 (http://www.openldap.org/) |
| 32 - Netscape's LDAP SDK (http://developer.netscape.com/) */ | |
| 33 | |
| 428 | 34 |
| 996 | 35 #include <config.h> |
| 36 #include "lisp.h" | |
| 37 #include "opaque.h" | |
| 38 #include "sysdep.h" | |
| 39 #include "buffer.h" | |
| 40 #include "process.h" /* for report_process_error */ | |
| 1632 | 41 #ifdef HAVE_SHLIB |
| 42 # include "emodules.h" | |
| 43 #endif | |
| 428 | 44 |
| 996 | 45 #include <errno.h> |
| 428 | 46 |
| 47 #include "eldap.h" | |
| 996 | 48 |
| 49 static Fixnum ldap_default_port; | |
| 50 static Lisp_Object Vldap_default_base; | |
| 51 | |
| 52 static Lisp_Object Qeldap; | |
| 428 | 53 |
| 996 | 54 /* Needed by the lrecord definition */ |
| 55 Lisp_Object Qldapp; | |
| 428 | 56 |
| 996 | 57 /* ldap-open plist keywords */ |
| 58 static Lisp_Object Qport, Qauth, Qbinddn, Qpasswd, Qderef, Qtimelimit, Qsizelimit; | |
| 428 | 59 /* Search scope limits */ |
| 60 static Lisp_Object Qbase, Qonelevel, Qsubtree; | |
| 61 /* Authentication methods */ | |
| 996 | 62 static Lisp_Object Qkrbv41, Qkrbv42; |
| 428 | 63 /* Deref policy */ |
| 64 static Lisp_Object Qnever, Qalways, Qfind; | |
| 996 | 65 /* Modification types (Qdelete is defined in general.c) */ |
| 66 static Lisp_Object Qadd, Qreplace; | |
| 428 | 67 |
| 996 | 68 |
| 69 /************************************************************************/ | |
| 70 /* Utility Functions */ | |
| 71 /************************************************************************/ | |
| 72 | |
| 2268 | 73 static DECLARE_DOESNT_RETURN (signal_ldap_error (LDAP *, LDAPMessage *, int)); |
| 74 | |
| 75 static DOESNT_RETURN | |
| 2286 | 76 signal_ldap_error (LDAP *ld, |
| 77 #if defined HAVE_LDAP_PARSE_RESULT || defined HAVE_LDAP_RESULT2ERROR | |
| 78 LDAPMessage *res, | |
| 79 #else | |
| 80 LDAPMessage *UNUSED (res), | |
| 81 #endif | |
| 82 int ldap_err) | |
| 996 | 83 { |
| 84 if (ldap_err <= 0) | |
| 85 { | |
| 86 #if defined HAVE_LDAP_PARSE_RESULT | |
| 87 int err; | |
| 88 ldap_err = ldap_parse_result (ld, res, | |
| 89 &err, | |
| 90 NULL, NULL, NULL, NULL, 0); | |
| 91 if (ldap_err == LDAP_SUCCESS) | |
| 92 ldap_err = err; | |
| 93 #elif defined HAVE_LDAP_GET_LDERRNO | |
| 94 ldap_err = ldap_get_lderrno (ld, NULL, NULL); | |
| 95 #elif defined HAVE_LDAP_RESULT2ERROR | |
| 96 ldap_err = ldap_result2error (ld, res, 0); | |
| 97 #else | |
| 98 ldap_err = ld->ld_errno; | |
| 99 #endif | |
| 100 } | |
| 101 invalid_operation ("LDAP error", | |
| 2367 | 102 build_ext_string (ldap_err2string (ldap_err), Qnative)); |
| 996 | 103 } |
| 104 | |
| 105 | |
| 106 /************************************************************************/ | |
| 107 /* ldap lrecord basic functions */ | |
| 108 /************************************************************************/ | |
| 109 | |
| 110 static Lisp_Object | |
| 111 make_ldap (Lisp_LDAP *ldap) | |
| 112 { | |
| 113 return wrap_ldap (ldap); | |
| 114 } | |
| 115 | |
| 1220 | 116 static const struct memory_description ldap_description [] = { |
| 996 | 117 { XD_LISP_OBJECT, offsetof (struct Lisp_LDAP, host) }, |
| 118 { XD_END } | |
| 119 }; | |
| 120 | |
| 121 static Lisp_Object | |
| 122 mark_ldap (Lisp_Object obj) | |
| 123 { | |
| 124 return XLDAP (obj)->host; | |
| 125 } | |
| 126 | |
| 127 static void | |
| 2286 | 128 print_ldap (Lisp_Object obj, Lisp_Object printcharfun, int UNUSED (escapeflag)) |
| 996 | 129 { |
| 130 Lisp_LDAP *ldap = XLDAP (obj); | |
| 131 | |
| 132 if (print_readably) | |
| 133 printing_unreadable_object ("#<ldap %s>", XSTRING_DATA (ldap->host)); | |
| 134 | |
| 135 write_fmt_string_lisp (printcharfun, "#<ldap %S", 1, ldap->host); | |
| 136 if (!ldap->ld) | |
| 137 write_c_string (printcharfun,"(dead) "); | |
| 138 write_fmt_string (printcharfun, " 0x%lx>", (long)ldap); | |
| 139 } | |
| 140 | |
| 141 static Lisp_LDAP * | |
| 142 allocate_ldap (void) | |
| 143 { | |
| 3024 | 144 Lisp_LDAP *ldap = ALLOC_LCRECORD_TYPE (Lisp_LDAP, &lrecord_ldap); |
| 996 | 145 |
| 146 ldap->ld = NULL; | |
| 147 ldap->host = Qnil; | |
| 148 return ldap; | |
| 149 } | |
| 150 | |
| 151 static void | |
| 152 finalize_ldap (void *header, int for_disksave) | |
| 153 { | |
| 154 Lisp_LDAP *ldap = (Lisp_LDAP *) header; | |
| 155 | |
| 156 if (for_disksave) | |
| 157 invalid_operation ("Can't dump an emacs containing LDAP objects", | |
| 158 make_ldap (ldap)); | |
| 159 | |
| 160 if (ldap->ld) | |
| 161 ldap_unbind (ldap->ld); | |
| 162 ldap->ld = NULL; | |
| 163 } | |
| 164 | |
| 1220 | 165 DEFINE_LRECORD_IMPLEMENTATION ("ldap", ldap, 0, |
| 996 | 166 mark_ldap, print_ldap, finalize_ldap, |
| 167 NULL, NULL, ldap_description, Lisp_LDAP); | |
| 168 | |
| 169 | |
| 170 /************************************************************************/ | |
| 171 /* Basic ldap accessors */ | |
| 172 /************************************************************************/ | |
| 173 | |
| 174 /* ###autoload */ | |
| 175 DEFUN ("ldapp", Fldapp, 1, 1, 0, /* | |
| 176 Return t if OBJECT is a LDAP connection. | |
| 177 */ | |
| 178 (object)) | |
| 179 { | |
| 180 return LDAPP (object) ? Qt : Qnil; | |
| 181 } | |
| 182 | |
| 183 DEFUN ("ldap-host", Fldap_host, 1, 1, 0, /* | |
| 184 Return the server host of the connection LDAP, as a string. | |
| 185 */ | |
| 186 (ldap)) | |
| 187 { | |
| 188 CHECK_LDAP (ldap); | |
| 189 return (XLDAP (ldap))->host; | |
| 190 } | |
| 191 | |
| 192 DEFUN ("ldap-live-p", Fldap_live_p, 1, 1, 0, /* | |
| 193 Return t if LDAP is an active LDAP connection. | |
| 194 */ | |
| 195 (ldap)) | |
| 196 { | |
| 197 CHECK_LDAP (ldap); | |
| 198 return (XLDAP (ldap))->ld ? Qt : Qnil; | |
| 199 } | |
| 200 | |
| 201 /************************************************************************/ | |
| 202 /* Opening/Closing a LDAP connection */ | |
| 203 /************************************************************************/ | |
| 204 | |
| 205 | |
| 206 /* ###autoload */ | |
| 207 DEFUN ("ldap-open", Fldap_open, 1, 2, 0, /* | |
| 208 Open a LDAP connection to HOST. | |
| 209 PLIST is a plist containing additional parameters for the connection. | |
| 428 | 210 Valid keys in that list are: |
| 996 | 211 `port' the TCP port to use for the connection if different from |
| 212 `ldap-default-port'. | |
| 428 | 213 `auth' is the authentication method to use, possible values depend on |
| 214 the LDAP library XEmacs was compiled with: `simple', `krbv41' and `krbv42'. | |
| 215 `binddn' is the distinguished name of the user to bind as (in RFC 1779 syntax). | |
| 216 `passwd' is the password to use for simple authentication. | |
| 217 `deref' is one of the symbols `never', `always', `search' or `find'. | |
| 218 `timelimit' is the timeout limit for the connection in seconds. | |
| 219 `sizelimit' is the maximum number of matches to return. | |
| 220 */ | |
| 996 | 221 (host, plist)) |
| 428 | 222 { |
| 996 | 223 /* This function can GC */ |
| 224 Lisp_LDAP *ldap; | |
| 428 | 225 LDAP *ld; |
| 996 | 226 int ldap_port = 0; |
| 428 | 227 int ldap_auth = LDAP_AUTH_SIMPLE; |
| 2367 | 228 Extbyte *ldap_binddn = NULL; |
| 229 Extbyte *ldap_password = NULL; | |
| 428 | 230 int ldap_deref = LDAP_DEREF_NEVER; |
| 231 int ldap_timelimit = 0; | |
| 232 int ldap_sizelimit = 0; | |
| 996 | 233 int err; |
| 428 | 234 |
| 996 | 235 CHECK_STRING (host); |
| 428 | 236 |
| 996 | 237 { |
| 238 EXTERNAL_PROPERTY_LIST_LOOP_3 (keyword, value, plist) | |
| 239 { | |
| 240 /* TCP Port */ | |
| 241 if (EQ (keyword, Qport)) | |
| 242 { | |
| 243 CHECK_INT (value); | |
| 244 ldap_port = XINT (value); | |
| 245 } | |
| 246 /* Authentication method */ | |
| 247 if (EQ (keyword, Qauth)) | |
| 248 { | |
| 249 if (EQ (value, Qsimple)) | |
| 250 ldap_auth = LDAP_AUTH_SIMPLE; | |
| 428 | 251 #ifdef LDAP_AUTH_KRBV41 |
| 996 | 252 else if (EQ (value, Qkrbv41)) |
| 253 ldap_auth = LDAP_AUTH_KRBV41; | |
| 428 | 254 #endif |
| 255 #ifdef LDAP_AUTH_KRBV42 | |
| 996 | 256 else if (EQ (value, Qkrbv42)) |
| 257 ldap_auth = LDAP_AUTH_KRBV42; | |
| 428 | 258 #endif |
| 996 | 259 else |
| 260 invalid_constant ("Invalid authentication method", value); | |
| 261 } | |
| 262 /* Bind DN */ | |
| 263 else if (EQ (keyword, Qbinddn)) | |
| 264 { | |
| 265 CHECK_STRING (value); | |
| 266 LISP_STRING_TO_EXTERNAL (value, ldap_binddn, Qnative); | |
| 267 } | |
| 268 /* Password */ | |
| 269 else if (EQ (keyword, Qpasswd)) | |
| 270 { | |
| 271 CHECK_STRING (value); | |
| 2272 | 272 LISP_STRING_TO_EXTERNAL (value, ldap_password, Qnative); |
| 996 | 273 } |
| 274 /* Deref */ | |
| 275 else if (EQ (keyword, Qderef)) | |
| 276 { | |
| 277 if (EQ (value, Qnever)) | |
| 278 ldap_deref = LDAP_DEREF_NEVER; | |
| 279 else if (EQ (value, Qsearch)) | |
| 280 ldap_deref = LDAP_DEREF_SEARCHING; | |
| 281 else if (EQ (value, Qfind)) | |
| 282 ldap_deref = LDAP_DEREF_FINDING; | |
| 283 else if (EQ (value, Qalways)) | |
| 284 ldap_deref = LDAP_DEREF_ALWAYS; | |
| 285 else | |
| 286 invalid_constant ("Invalid deref value", value); | |
| 287 } | |
| 288 /* Timelimit */ | |
| 289 else if (EQ (keyword, Qtimelimit)) | |
| 290 { | |
| 291 CHECK_INT (value); | |
| 292 ldap_timelimit = XINT (value); | |
| 293 } | |
| 294 /* Sizelimit */ | |
| 295 else if (EQ (keyword, Qsizelimit)) | |
| 296 { | |
| 297 CHECK_INT (value); | |
| 298 ldap_sizelimit = XINT (value); | |
| 299 } | |
| 300 } | |
| 301 } | |
| 302 | |
| 303 if (ldap_port == 0) | |
| 304 { | |
| 305 ldap_port = ldap_default_port; | |
| 428 | 306 } |
| 307 | |
| 996 | 308 /* Connect to the server and bind */ |
| 309 slow_down_interrupts (); | |
| 2367 | 310 ld = ldap_open (NEW_LISP_STRING_TO_EXTERNAL (host, Qnative), ldap_port); |
| 996 | 311 speed_up_interrupts (); |
| 428 | 312 |
| 996 | 313 if (ld == NULL ) |
| 314 report_process_error ("Failed connecting to host", host); | |
| 428 | 315 |
| 996 | 316 #ifdef HAVE_LDAP_SET_OPTION |
| 317 if ((err = ldap_set_option (ld, LDAP_OPT_DEREF, | |
| 318 (void *)&ldap_deref)) != LDAP_SUCCESS) | |
| 319 signal_ldap_error (ld, NULL, err); | |
| 320 if ((err = ldap_set_option (ld, LDAP_OPT_TIMELIMIT, | |
| 321 (void *)&ldap_timelimit)) != LDAP_SUCCESS) | |
| 322 signal_ldap_error (ld, NULL, err); | |
| 323 if ((err = ldap_set_option (ld, LDAP_OPT_SIZELIMIT, | |
| 324 (void *)&ldap_sizelimit)) != LDAP_SUCCESS) | |
| 325 signal_ldap_error (ld, NULL, err); | |
| 326 if ((err = ldap_set_option (ld, LDAP_OPT_REFERRALS, | |
| 327 LDAP_OPT_ON)) != LDAP_SUCCESS) | |
| 328 signal_ldap_error (ld, NULL, err); | |
| 329 if ((err = ldap_set_option (ld, LDAP_OPT_RESTART, | |
| 330 LDAP_OPT_ON)) != LDAP_SUCCESS) | |
| 331 signal_ldap_error (ld, NULL, err); | |
| 332 #else /* not HAVE_LDAP_SET_OPTION */ | |
| 428 | 333 ld->ld_deref = ldap_deref; |
| 334 ld->ld_timelimit = ldap_timelimit; | |
| 335 ld->ld_sizelimit = ldap_sizelimit; | |
| 336 #ifdef LDAP_REFERRALS | |
| 337 ld->ld_options = LDAP_OPT_REFERRALS; | |
| 996 | 338 #else /* not LDAP_REFERRALS */ |
| 428 | 339 ld->ld_options = 0; |
| 996 | 340 #endif /* not LDAP_REFERRALS */ |
| 341 /* XEmacs uses interrupts (SIGIO,SIGALRM), LDAP calls need to ignore them */ | |
| 342 ld->ld_options |= LDAP_OPT_RESTART; | |
| 343 #endif /* not HAVE_LDAP_SET_OPTION */ | |
| 344 | |
| 2272 | 345 err = ldap_bind_s (ld, ldap_binddn, ldap_password, ldap_auth); |
| 996 | 346 if (err != LDAP_SUCCESS) |
| 347 { | |
| 348 signal_error (Qprocess_error, "Failed binding to the server", | |
| 2367 | 349 build_ext_string (ldap_err2string (err), Qnative)); |
| 996 | 350 } |
| 351 | |
| 352 ldap = allocate_ldap (); | |
| 353 ldap->ld = ld; | |
| 354 ldap->host = host; | |
| 355 | |
| 356 return make_ldap (ldap); | |
| 357 } | |
| 358 | |
| 359 | |
| 360 | |
| 361 DEFUN ("ldap-close", Fldap_close, 1, 1, 0, /* | |
| 362 Close an LDAP connection. | |
| 363 */ | |
| 364 (ldap)) | |
| 365 { | |
| 366 Lisp_LDAP *lldap; | |
| 367 CHECK_LIVE_LDAP (ldap); | |
| 368 lldap = XLDAP (ldap); | |
| 369 ldap_unbind (lldap->ld); | |
| 370 lldap->ld = NULL; | |
| 371 return Qnil; | |
| 372 } | |
| 373 | |
| 374 | |
| 375 | |
| 376 /************************************************************************/ | |
| 377 /* Working on a LDAP connection */ | |
| 378 /************************************************************************/ | |
| 379 struct ldap_unwind_struct | |
| 380 { | |
| 381 LDAPMessage *res; | |
| 382 struct berval **vals; | |
| 383 }; | |
| 384 | |
| 385 static Lisp_Object | |
| 386 ldap_search_unwind (Lisp_Object unwind_obj) | |
| 387 { | |
| 388 struct ldap_unwind_struct *unwind = | |
| 389 (struct ldap_unwind_struct *) get_opaque_ptr (unwind_obj); | |
| 390 if (unwind->res) | |
| 391 ldap_msgfree (unwind->res); | |
| 392 if (unwind->vals) | |
| 393 ldap_value_free_len (unwind->vals); | |
| 394 return Qnil; | |
| 395 } | |
| 396 | |
| 397 /* The following function is called `ldap-search-basic' instead of */ | |
| 398 /* plain `ldap-search' to maintain compatibility with the XEmacs 21.1 */ | |
| 399 /* API where `ldap-search' was the name of the high-level search */ | |
| 400 /* function */ | |
| 428 | 401 |
| 996 | 402 DEFUN ("ldap-search-basic", Fldap_search_basic, 2, 8, 0, /* |
| 403 Perform a search on an open LDAP connection. | |
| 404 LDAP is an LDAP connection object created with `ldap-open'. | |
| 405 FILTER is a filter string for the search as described in RFC 1558. | |
| 406 BASE is the distinguished name at which to start the search. | |
| 407 SCOPE is one of the symbols `base', `onelevel' or `subtree' indicating | |
| 408 the scope of the search. | |
| 409 ATTRS is a list of strings indicating which attributes to retrieve | |
| 410 for each matching entry. If nil return all available attributes. | |
| 411 If ATTRSONLY is non-nil then only the attributes are retrieved, not | |
| 412 the associated values. | |
| 413 If WITHDN is non-nil each entry in the result will be prepended with | |
| 414 its distinguished name DN. | |
| 415 If VERBOSE is non-nil progress messages will be echoed. | |
| 416 The function returns a list of matching entries. Each entry is itself | |
| 417 an alist of attribute/value pairs optionally preceded by the DN of the | |
| 418 entry according to the value of WITHDN. | |
| 419 */ | |
| 420 (ldap, filter, base, scope, attrs, attrsonly, withdn, verbose)) | |
| 421 { | |
| 422 /* This function can GC */ | |
| 423 | |
| 424 /* Vars for query */ | |
| 425 LDAP *ld; | |
| 426 LDAPMessage *e; | |
| 427 BerElement *ptr; | |
|
4710
3a87551bfeb5
Fixes for a number of minor warnings issued by gcc. See xemacs-patches message
Jerry James <james@xemacs.org>
parents:
3029
diff
changeset
|
428 Extbyte *a, *dn, *bs, *filt; |
| 996 | 429 int i, rc; |
| 430 int matches; | |
| 431 struct ldap_unwind_struct unwind; | |
| 432 | |
| 433 int ldap_scope = LDAP_SCOPE_SUBTREE; | |
| 2367 | 434 Extbyte **ldap_attributes = NULL; |
| 996 | 435 |
| 436 int speccount = specpdl_depth (); | |
| 437 | |
| 438 Lisp_Object list = Qnil; | |
| 439 Lisp_Object entry = Qnil; | |
| 440 Lisp_Object result = Qnil; | |
| 441 struct gcpro gcpro1, gcpro2, gcpro3; | |
| 442 | |
| 443 GCPRO3 (list, entry, result); | |
| 444 | |
| 445 unwind.res = NULL; | |
| 446 unwind.vals = NULL; | |
| 447 | |
| 448 /* Do all the parameter checking */ | |
| 449 CHECK_LIVE_LDAP (ldap); | |
| 450 ld = XLDAP (ldap)->ld; | |
| 451 | |
| 452 /* Filter */ | |
| 453 CHECK_STRING (filter); | |
| 454 | |
| 455 /* Search base */ | |
| 456 if (NILP (base)) | |
| 457 { | |
| 458 base = Vldap_default_base; | |
| 459 } | |
| 460 if (!NILP (base)) | |
| 461 { | |
| 462 CHECK_STRING (base); | |
| 463 } | |
| 464 | |
| 465 /* Search scope */ | |
| 466 if (!NILP (scope)) | |
| 467 { | |
| 468 if (EQ (scope, Qbase)) | |
| 469 ldap_scope = LDAP_SCOPE_BASE; | |
| 470 else if (EQ (scope, Qonelevel)) | |
| 471 ldap_scope = LDAP_SCOPE_ONELEVEL; | |
| 472 else if (EQ (scope, Qsubtree)) | |
| 473 ldap_scope = LDAP_SCOPE_SUBTREE; | |
| 474 else | |
| 475 invalid_constant ("Invalid scope", scope); | |
| 476 } | |
| 477 | |
| 478 /* Attributes to search */ | |
| 479 if (!NILP (attrs)) | |
| 480 { | |
| 481 CHECK_CONS (attrs); | |
| 482 ldap_attributes = alloca_array (char *, 1 + XINT (Flength (attrs))); | |
| 483 | |
| 484 i = 0; | |
| 2367 | 485 { |
| 486 EXTERNAL_LIST_LOOP_2 (current, attrs) | |
| 487 { | |
| 488 CHECK_STRING (current); | |
| 489 LISP_STRING_TO_EXTERNAL (current, ldap_attributes[i], Qnative); | |
| 490 ++i; | |
| 491 } | |
| 492 } | |
| 996 | 493 ldap_attributes[i] = NULL; |
| 494 } | |
| 495 | |
| 496 /* Attributes only ? */ | |
| 497 CHECK_SYMBOL (attrsonly); | |
| 428 | 498 |
| 499 /* Perform the search */ | |
|
4710
3a87551bfeb5
Fixes for a number of minor warnings issued by gcc. See xemacs-patches message
Jerry James <james@xemacs.org>
parents:
3029
diff
changeset
|
500 bs = NILP (base) ? "" : NEW_LISP_STRING_TO_EXTERNAL (base, Qnative); |
|
3a87551bfeb5
Fixes for a number of minor warnings issued by gcc. See xemacs-patches message
Jerry James <james@xemacs.org>
parents:
3029
diff
changeset
|
501 filt = NILP (filter) ? "" : NEW_LISP_STRING_TO_EXTERNAL (filter, Qnative); |
|
3a87551bfeb5
Fixes for a number of minor warnings issued by gcc. See xemacs-patches message
Jerry James <james@xemacs.org>
parents:
3029
diff
changeset
|
502 if (ldap_search (ld, bs, ldap_scope, filt, ldap_attributes, |
|
3a87551bfeb5
Fixes for a number of minor warnings issued by gcc. See xemacs-patches message
Jerry James <james@xemacs.org>
parents:
3029
diff
changeset
|
503 NILP (attrsonly) ? 0 : 1) |
| 996 | 504 == -1) |
| 428 | 505 { |
| 996 | 506 signal_ldap_error (ld, NULL, 0); |
| 428 | 507 } |
| 508 | |
| 996 | 509 /* Ensure we don't exit without cleaning up */ |
| 510 record_unwind_protect (ldap_search_unwind, | |
| 511 make_opaque_ptr (&unwind)); | |
| 512 | |
| 428 | 513 /* Build the results list */ |
| 514 matches = 0; | |
| 515 | |
| 996 | 516 rc = ldap_result (ld, LDAP_RES_ANY, 0, NULL, &unwind.res); |
| 517 | |
| 518 while (rc == LDAP_RES_SEARCH_ENTRY) | |
| 428 | 519 { |
| 996 | 520 QUIT; |
| 428 | 521 matches ++; |
| 996 | 522 e = ldap_first_entry (ld, unwind.res); |
| 523 /* #### This call to message() is pretty fascist, because it | |
| 524 destroys the current echo area contents, even when invoked | |
| 525 from Lisp. It should use echo_area_message() instead, and | |
| 526 restore the old echo area contents later. */ | |
| 527 if (! NILP (verbose)) | |
| 528 message ("Parsing ldap results... %d", matches); | |
| 428 | 529 entry = Qnil; |
| 996 | 530 /* Get the DN if required */ |
| 531 if (! NILP (withdn)) | |
| 532 { | |
| 533 dn = ldap_get_dn (ld, e); | |
| 534 if (dn == NULL) | |
| 535 signal_ldap_error (ld, e, 0); | |
| 536 entry = Fcons (build_ext_string (dn, Qnative), Qnil); | |
| 537 } | |
| 2367 | 538 for (a = ldap_first_attribute (ld, e, &ptr); |
| 428 | 539 a != NULL; |
| 2367 | 540 a = ldap_next_attribute (ld, e, ptr)) |
| 428 | 541 { |
| 996 | 542 list = Fcons (build_ext_string (a, Qnative), Qnil); |
| 543 unwind.vals = ldap_get_values_len (ld, e, a); | |
| 544 if (unwind.vals != NULL) | |
| 428 | 545 { |
| 996 | 546 for (i = 0; unwind.vals[i] != NULL; i++) |
| 428 | 547 { |
| 996 | 548 list = Fcons (make_ext_string ((Extbyte *) unwind.vals[i]->bv_val, |
| 549 unwind.vals[i]->bv_len, | |
| 550 Qnative), | |
| 428 | 551 list); |
| 552 } | |
| 553 } | |
| 554 entry = Fcons (Fnreverse (list), | |
| 555 entry); | |
| 996 | 556 ldap_value_free_len (unwind.vals); |
| 557 unwind.vals = NULL; | |
| 428 | 558 } |
| 559 result = Fcons (Fnreverse (entry), | |
| 560 result); | |
| 996 | 561 ldap_msgfree (unwind.res); |
| 562 unwind.res = NULL; | |
| 428 | 563 |
| 996 | 564 rc = ldap_result (ld, LDAP_RES_ANY, 0, NULL, &(unwind.res)); |
| 428 | 565 } |
| 566 | |
| 996 | 567 #if defined HAVE_LDAP_PARSE_RESULT |
| 568 { | |
| 569 int rc2 = ldap_parse_result (ld, unwind.res, | |
| 570 &rc, | |
| 571 NULL, NULL, NULL, NULL, 0); | |
| 572 if (rc2 != LDAP_SUCCESS) | |
| 573 rc = rc2; | |
| 574 } | |
| 428 | 575 #else |
| 996 | 576 if (rc == 0) |
| 577 signal_ldap_error (ld, NULL, LDAP_TIMELIMIT_EXCEEDED); | |
| 578 | |
| 579 if (rc == -1) | |
| 580 signal_ldap_error (ld, unwind.res, (unwind.res==NULL) ? ld->ld_errno : 0); | |
| 581 | |
| 582 #if defined HAVE_LDAP_RESULT2ERROR | |
| 583 rc = ldap_result2error (ld, unwind.res, 0); | |
| 584 #endif | |
| 428 | 585 #endif |
| 996 | 586 |
| 587 if (rc != LDAP_SUCCESS) | |
| 588 signal_ldap_error (ld, NULL, rc); | |
| 589 | |
| 590 ldap_msgfree (unwind.res); | |
| 591 unwind.res = (LDAPMessage *)NULL; | |
| 592 | |
| 593 /* #### See above for calling message(). */ | |
| 594 if (! NILP (verbose)) | |
| 595 message ("Parsing ldap results... done"); | |
| 596 | |
| 597 unbind_to (speccount); | |
| 598 UNGCPRO; | |
| 599 return Fnreverse (result); | |
| 600 } | |
| 601 | |
| 602 DEFUN ("ldap-add", Fldap_add, 3, 3, 0, /* | |
| 603 Add an entry to an LDAP directory. | |
| 604 LDAP is an LDAP connection object created with `ldap-open'. | |
| 605 DN is the distinguished name of the entry to add. | |
| 606 ENTRY is an entry specification, i.e., a list of cons cells | |
| 607 containing attribute/value string pairs. | |
| 608 */ | |
| 609 (ldap, dn, entry)) | |
| 610 { | |
| 611 LDAP *ld; | |
| 612 LDAPMod *ldap_mods, **ldap_mods_ptrs; | |
| 613 struct berval *bervals; | |
| 614 int rc; | |
| 615 int i, j; | |
| 616 Elemcount len; | |
| 617 | |
| 618 Lisp_Object values = Qnil; | |
| 2367 | 619 struct gcpro gcpro1; |
| 996 | 620 |
| 2367 | 621 GCPRO1 (values); |
| 996 | 622 |
| 623 /* Do all the parameter checking */ | |
| 624 CHECK_LIVE_LDAP (ldap); | |
| 625 ld = XLDAP (ldap)->ld; | |
| 626 | |
| 627 /* Check the DN */ | |
| 628 CHECK_STRING (dn); | |
| 629 | |
| 630 /* Check the entry */ | |
| 631 CHECK_CONS (entry); | |
| 632 if (NILP (entry)) | |
| 633 invalid_operation ("Cannot add void entry", entry); | |
| 428 | 634 |
| 996 | 635 /* Build the ldap_mods array */ |
| 636 len = (Elemcount) XINT (Flength (entry)); | |
| 637 ldap_mods = alloca_array (LDAPMod, len); | |
| 638 ldap_mods_ptrs = alloca_array (LDAPMod *, 1 + len); | |
| 639 i = 0; | |
| 2367 | 640 |
| 641 { | |
| 642 EXTERNAL_LIST_LOOP_2 (current, entry) | |
| 643 { | |
| 644 CHECK_CONS (current); | |
| 645 CHECK_STRING (XCAR (current)); | |
| 646 ldap_mods_ptrs[i] = &(ldap_mods[i]); | |
| 647 LISP_STRING_TO_EXTERNAL (XCAR (current), ldap_mods[i].mod_type, | |
| 648 Qnative); | |
| 649 ldap_mods[i].mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES; | |
| 650 values = XCDR (current); | |
| 651 if (CONSP (values)) | |
| 652 { | |
| 653 len = (Elemcount) XINT (Flength (values)); | |
| 654 bervals = alloca_array (struct berval, len); | |
| 655 ldap_mods[i].mod_vals.modv_bvals = | |
| 656 alloca_array (struct berval *, 1 + len); | |
| 657 j = 0; | |
| 658 { | |
| 659 EXTERNAL_LIST_LOOP_2 (cur2, values) | |
| 660 { | |
| 661 CHECK_STRING (cur2); | |
| 662 ldap_mods[i].mod_vals.modv_bvals[j] = &(bervals[j]); | |
| 663 TO_EXTERNAL_FORMAT (LISP_STRING, cur2, | |
| 664 ALLOCA, (bervals[j].bv_val, | |
| 665 bervals[j].bv_len), | |
| 666 Qnative); | |
| 667 j++; | |
| 668 } | |
| 669 } | |
| 670 ldap_mods[i].mod_vals.modv_bvals[j] = NULL; | |
| 671 } | |
| 672 else | |
| 673 { | |
| 674 CHECK_STRING (values); | |
| 675 bervals = alloca_array (struct berval, 1); | |
| 676 ldap_mods[i].mod_vals.modv_bvals = alloca_array (struct berval *, | |
| 677 2); | |
| 678 ldap_mods[i].mod_vals.modv_bvals[0] = &(bervals[0]); | |
| 679 TO_EXTERNAL_FORMAT (LISP_STRING, values, | |
| 680 ALLOCA, (bervals[0].bv_val, | |
| 681 bervals[0].bv_len), | |
| 682 Qnative); | |
| 683 ldap_mods[i].mod_vals.modv_bvals[1] = NULL; | |
| 684 } | |
| 685 i++; | |
| 686 } | |
| 687 } | |
| 996 | 688 ldap_mods_ptrs[i] = NULL; |
| 2367 | 689 rc = ldap_add_s (ld, NEW_LISP_STRING_TO_EXTERNAL (dn, Qnative), |
| 690 ldap_mods_ptrs); | |
| 996 | 691 if (rc != LDAP_SUCCESS) |
| 692 signal_ldap_error (ld, NULL, rc); | |
| 428 | 693 |
| 694 UNGCPRO; | |
| 996 | 695 return Qnil; |
| 696 } | |
| 697 | |
| 698 DEFUN ("ldap-modify", Fldap_modify, 3, 3, 0, /* | |
| 699 Add an entry to an LDAP directory. | |
| 700 LDAP is an LDAP connection object created with `ldap-open'. | |
| 701 DN is the distinguished name of the entry to modify. | |
| 702 MODS is a list of modifications to apply. | |
| 703 A modification is a list of the form (MOD-OP ATTR VALUE1 VALUE2 ...) | |
| 704 MOD-OP and ATTR are mandatory, VALUEs are optional depending on MOD-OP. | |
| 705 MOD-OP is the type of modification, one of the symbols `add', `delete' | |
| 706 or `replace'. ATTR is the LDAP attribute type to modify. | |
| 707 */ | |
| 708 (ldap, dn, mods)) | |
| 709 { | |
| 710 LDAP *ld; | |
| 711 LDAPMod *ldap_mods, **ldap_mods_ptrs; | |
| 712 struct berval *bervals; | |
| 713 int i, j, rc; | |
| 714 Lisp_Object mod_op; | |
| 715 Elemcount len; | |
| 716 | |
| 717 Lisp_Object values = Qnil; | |
| 3029 | 718 struct gcpro gcpro1; |
| 996 | 719 |
| 720 /* Do all the parameter checking */ | |
| 721 CHECK_LIVE_LDAP (ldap); | |
| 722 ld = XLDAP (ldap)->ld; | |
| 723 | |
| 724 /* Check the DN */ | |
| 725 CHECK_STRING (dn); | |
| 726 | |
| 727 /* Check the entry */ | |
| 728 CHECK_CONS (mods); | |
| 729 if (NILP (mods)) | |
| 730 return Qnil; | |
| 731 | |
| 732 /* Build the ldap_mods array */ | |
| 733 len = (Elemcount) XINT (Flength (mods)); | |
| 734 ldap_mods = alloca_array (LDAPMod, len); | |
| 735 ldap_mods_ptrs = alloca_array (LDAPMod *, 1 + len); | |
| 736 i = 0; | |
| 737 | |
| 2367 | 738 GCPRO1 (values); |
| 739 { | |
| 740 EXTERNAL_LIST_LOOP_2 (current, mods) | |
| 741 { | |
| 742 CHECK_CONS (current); | |
| 743 CHECK_SYMBOL (XCAR (current)); | |
| 744 mod_op = XCAR (current); | |
| 745 ldap_mods_ptrs[i] = &(ldap_mods[i]); | |
| 746 ldap_mods[i].mod_op = LDAP_MOD_BVALUES; | |
| 747 if (EQ (mod_op, Qadd)) | |
| 748 ldap_mods[i].mod_op |= LDAP_MOD_ADD; | |
| 749 else if (EQ (mod_op, Qdelete)) | |
| 750 ldap_mods[i].mod_op |= LDAP_MOD_DELETE; | |
| 751 else if (EQ (mod_op, Qreplace)) | |
| 752 ldap_mods[i].mod_op |= LDAP_MOD_REPLACE; | |
| 753 else | |
| 754 invalid_constant ("Invalid LDAP modification type", mod_op); | |
| 755 current = XCDR (current); | |
| 756 CHECK_STRING (XCAR (current)); | |
| 757 LISP_STRING_TO_EXTERNAL (XCAR (current), ldap_mods[i].mod_type, | |
| 758 Qnative); | |
| 759 values = XCDR (current); | |
| 760 len = (Elemcount) XINT (Flength (values)); | |
| 761 bervals = alloca_array (struct berval, len); | |
| 762 ldap_mods[i].mod_vals.modv_bvals = | |
| 763 alloca_array (struct berval *, 1 + len); | |
| 764 j = 0; | |
| 2387 | 765 { |
| 766 EXTERNAL_LIST_LOOP_2 (cur2, values) | |
| 767 { | |
| 768 CHECK_STRING (cur2); | |
| 769 ldap_mods[i].mod_vals.modv_bvals[j] = &(bervals[j]); | |
| 770 TO_EXTERNAL_FORMAT (LISP_STRING, cur2, | |
| 771 ALLOCA, (bervals[j].bv_val, | |
| 772 bervals[j].bv_len), | |
| 773 Qnative); | |
| 774 j++; | |
| 775 } | |
| 776 ldap_mods[i].mod_vals.modv_bvals[j] = NULL; | |
| 777 i++; | |
| 778 } | |
| 2367 | 779 } |
| 780 } | |
| 996 | 781 ldap_mods_ptrs[i] = NULL; |
| 2367 | 782 rc = ldap_modify_s (ld, NEW_LISP_STRING_TO_EXTERNAL (dn, Qnative), |
| 783 ldap_mods_ptrs); | |
| 996 | 784 if (rc != LDAP_SUCCESS) |
| 785 signal_ldap_error (ld, NULL, rc); | |
| 786 | |
| 787 UNGCPRO; | |
| 788 return Qnil; | |
| 789 } | |
| 790 | |
| 791 | |
| 792 DEFUN ("ldap-delete", Fldap_delete, 2, 2, 0, /* | |
| 793 Delete an entry to an LDAP directory. | |
| 794 LDAP is an LDAP connection object created with `ldap-open'. | |
| 795 DN is the distinguished name of the entry to delete. | |
| 796 */ | |
| 797 (ldap, dn)) | |
| 798 { | |
| 799 LDAP *ld; | |
| 800 int rc; | |
| 801 | |
| 802 /* Check parameters */ | |
| 803 CHECK_LIVE_LDAP (ldap); | |
| 804 ld = XLDAP (ldap)->ld; | |
| 805 CHECK_STRING (dn); | |
| 806 | |
| 2367 | 807 rc = ldap_delete_s (ld, NEW_LISP_STRING_TO_EXTERNAL (dn, Qnative)); |
| 996 | 808 if (rc != LDAP_SUCCESS) |
| 809 signal_ldap_error (ld, NULL, rc); | |
| 810 | |
| 811 return Qnil; | |
| 428 | 812 } |
| 813 | |
| 814 void | |
| 996 | 815 syms_of_eldap (void) |
| 428 | 816 { |
| 996 | 817 INIT_LRECORD_IMPLEMENTATION (ldap); |
| 428 | 818 |
| 996 | 819 DEFSYMBOL (Qeldap); |
| 820 DEFSYMBOL (Qldapp); | |
| 821 DEFSYMBOL (Qport); | |
| 822 DEFSYMBOL (Qauth); | |
| 823 DEFSYMBOL (Qbinddn); | |
| 824 DEFSYMBOL (Qpasswd); | |
| 825 DEFSYMBOL (Qderef); | |
| 826 DEFSYMBOL (Qtimelimit); | |
| 827 DEFSYMBOL (Qsizelimit); | |
| 828 DEFSYMBOL (Qbase); | |
| 829 DEFSYMBOL (Qonelevel); | |
| 830 DEFSYMBOL (Qsubtree); | |
| 831 DEFSYMBOL (Qkrbv41); | |
| 832 DEFSYMBOL (Qkrbv42); | |
| 833 DEFSYMBOL (Qnever); | |
| 834 DEFSYMBOL (Qalways); | |
| 835 DEFSYMBOL (Qfind); | |
| 836 DEFSYMBOL (Qadd); | |
| 837 DEFSYMBOL (Qreplace); | |
| 838 | |
| 839 DEFSUBR (Fldapp); | |
| 840 DEFSUBR (Fldap_host); | |
| 841 DEFSUBR (Fldap_live_p); | |
| 842 DEFSUBR (Fldap_open); | |
| 843 DEFSUBR (Fldap_close); | |
| 844 DEFSUBR (Fldap_search_basic); | |
| 845 DEFSUBR (Fldap_add); | |
| 846 DEFSUBR (Fldap_modify); | |
| 847 DEFSUBR (Fldap_delete); | |
| 428 | 848 } |
| 849 | |
| 850 void | |
| 996 | 851 vars_of_eldap (void) |
| 428 | 852 { |
| 996 | 853 |
| 854 Fprovide (Qeldap); | |
| 428 | 855 |
| 996 | 856 ldap_default_port = LDAP_PORT; |
| 857 Vldap_default_base = Qnil; | |
| 858 | |
| 859 DEFVAR_INT ("ldap-default-port", &ldap_default_port /* | |
| 860 Default TCP port for LDAP connections. | |
| 861 Initialized from the LDAP library. Default value is 389. | |
| 428 | 862 */ ); |
| 863 | |
| 864 DEFVAR_LISP ("ldap-default-base", &Vldap_default_base /* | |
| 865 Default base for LDAP searches. | |
| 866 This is a string using the syntax of RFC 1779. | |
| 867 For instance, "o=ACME, c=US" limits the search to the | |
| 868 Acme organization in the United States. | |
| 869 */ ); | |
| 870 | |
| 871 } | |
| 872 | |
| 996 | 873 #ifdef HAVE_SHLIB |
| 1706 | 874 EXTERN_C void unload_eldap (void); |
| 996 | 875 void |
| 876 unload_eldap (void) | |
| 877 { | |
| 878 /* Remove defined types */ | |
| 879 UNDEF_LRECORD_IMPLEMENTATION (ldap); | |
| 880 | |
| 881 /* Remove staticpro'ing of symbols */ | |
| 882 unstaticpro_nodump (&Qeldap); | |
| 883 unstaticpro_nodump (&Qldapp); | |
| 884 unstaticpro_nodump (&Qport); | |
| 885 unstaticpro_nodump (&Qauth); | |
| 886 unstaticpro_nodump (&Qbinddn); | |
| 887 unstaticpro_nodump (&Qpasswd); | |
| 888 unstaticpro_nodump (&Qderef); | |
| 889 unstaticpro_nodump (&Qtimelimit); | |
| 890 unstaticpro_nodump (&Qsizelimit); | |
| 891 unstaticpro_nodump (&Qbase); | |
| 892 unstaticpro_nodump (&Qonelevel); | |
| 893 unstaticpro_nodump (&Qsubtree); | |
| 894 unstaticpro_nodump (&Qkrbv41); | |
| 895 unstaticpro_nodump (&Qkrbv42); | |
| 896 unstaticpro_nodump (&Qnever); | |
| 897 unstaticpro_nodump (&Qalways); | |
| 898 unstaticpro_nodump (&Qfind); | |
| 899 unstaticpro_nodump (&Qadd); | |
| 900 unstaticpro_nodump (&Qreplace); | |
| 901 } | |
| 902 #endif /* HAVE_SHLIB */ |
