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