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