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