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