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