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