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