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