XEmacs 21.2.9
[chise/xemacs-chise.git.1] / lisp / ldap.el
1 ;;; ldap.el --- LDAP support for Emacs
2
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4
5 ;; Author: Oscar Figueiredo <Oscar.Figueiredo@di.epfl.ch>
6 ;; Maintainer: Oscar Figueiredo <Oscar.Figueiredo@di.epfl.ch>
7 ;; Created: Jan 1998
8 ;; Version: $Revision: 1.7.2.3 $
9 ;; Keywords: help comm
10
11 ;; This file is part of XEmacs
12
13 ;; XEmacs is free software; you can redistribute it and/or modify it
14 ;; under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; XEmacs is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with XEmacs; see the file COPYING.  If not, write to 
25 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29 ;;    This file provides mid-level and user-level functions to access directory
30 ;;    servers using the LDAP protocol (RFC 1777). 
31
32 ;;; Installation:
33 ;;    LDAP support must have been built into XEmacs.
34
35
36 ;;; Code:
37
38 (defgroup ldap nil
39   "Lightweight Directory Access Protocol"
40   :group 'comm)
41
42 (defcustom ldap-default-host nil
43   "*Default LDAP server."
44   :type '(choice (string :tag "Host name")
45                  (const :tag "Use library default" nil))
46   :group 'ldap)
47
48 (defcustom ldap-default-port nil
49   "*Default TCP port for LDAP connections.
50 Initialized from the LDAP library at build time. Default value is 389."
51   :type '(choice (const :tag "Use library default" nil)
52                  (integer :tag "Port number"))
53   :group 'ldap)
54
55 (defcustom ldap-default-base nil
56   "*Default base for LDAP searches.
57 This is a string using the syntax of RFC 1779.
58 For instance, \"o=ACME, c=US\" limits the search to the
59 Acme organization in the United States."
60   :type '(choice (const :tag "Use library default" nil)
61                  (string :tag "Search base"))
62   :group 'ldap)
63
64
65 (defcustom ldap-host-parameters-alist nil
66   "*Alist of host-specific options for LDAP transactions.
67 The format of each list element is:
68 \(HOST PROP1 VAL1 PROP2 VAL2 ...)
69 HOST is the name of an LDAP server. PROPn and VALn are property/value 
70 pairs describing parameters for the server.  Valid properties include: 
71   `binddn' is the distinguished name of the user to bind as 
72     (in RFC 1779 syntax).
73   `passwd' is the password to use for simple authentication.
74   `auth' is the authentication method to use. 
75     Possible values are: `simple', `krbv41' and `krbv42'.
76   `base' is the base for the search as described in RFC 1779.
77   `scope' is one of the three symbols `subtree', `base' or `onelevel'.
78   `deref' is one of the symbols `never', `always', `search' or `find'.
79   `timelimit' is the timeout limit for the connection in seconds.
80   `sizelimit' is the maximum number of matches to return."
81   :type '(repeat :menu-tag "Host parameters"
82                  :tag "Host parameters"
83                  (list :menu-tag "Host parameters"
84                        :tag "Host parameters"
85                        :value nil
86                        (string :tag "Host name")
87                        (checklist :inline t
88                                   :greedy t
89                                   (list
90                                    :tag "Binding DN"
91                                    :inline t
92                                    (const :tag "Binding DN" binddn)
93                                    string)
94                                   (list
95                                    :tag "Password"
96                                    :inline t
97                                    (const :tag "Password" passwd)
98                                    string)
99                                   (list
100                                    :tag "Authentication Method"
101                                    :inline t
102                                    (const :tag "Authentication Method" auth)
103                                    (choice
104                                     (const :menu-tag "None" :tag "None" nil)
105                                     (const :menu-tag "Simple" :tag "Simple" simple)
106                                     (const :menu-tag "Kerberos 4.1" :tag "Kerberos 4.1" krbv41)
107                                     (const :menu-tag "Kerberos 4.2" :tag "Kerberos 4.2" krbv42)))
108                                   (list
109                                    :tag "Search Base" 
110                                    :inline t
111                                    (const :tag "Search Base" base)
112                                    string)
113                                   (list
114                                    :tag "Search Scope" 
115                                    :inline t
116                                    (const :tag "Search Scope" scope)
117                                    (choice
118                                     (const :menu-tag "Default" :tag "Default" nil)
119                                     (const :menu-tag "Subtree" :tag "Subtree" subtree)
120                                     (const :menu-tag "Base" :tag "Base" base)
121                                     (const :menu-tag "One Level" :tag "One Level" onelevel)))
122                                   (list
123                                    :tag "Dereferencing"
124                                    :inline t
125                                    (const :tag "Dereferencing" deref)
126                                    (choice
127                                     (const :menu-tag "Default" :tag "Default" nil)
128                                     (const :menu-tag "Never" :tag "Never" never)
129                                     (const :menu-tag "Always" :tag "Always" always)
130                                     (const :menu-tag "When searching" :tag "When searching" search)
131                                     (const :menu-tag "When locating base" :tag "When locating base" find)))
132                                   (list
133                                    :tag "Time Limit"
134                                    :inline t
135                                    (const :tag "Time Limit" timelimit)
136                                    (integer :tag "(in seconds)"))
137                                   (list
138                                    :tag "Size Limit"
139                                    :inline t
140                                    (const :tag "Size Limit" sizelimit)
141                                    (integer :tag "(number of records)")))))
142 :group 'ldap)
143
144
145 (defun ldap-search (filter &optional host attributes attrsonly)
146   "Perform an LDAP search.
147 FILTER is the search filter in RFC1558 syntax, i.e. something that
148 looks like \"(cn=John Smith)\".
149 HOST is the LDAP host on which to perform the search.
150 ATTRIBUTES is a list of attributes to retrieve; nil means retrieve all.
151 If ATTRSONLY is non nil, the attributes will be retrieved without
152 the associated values.
153 Additional search parameters can be specified through 
154 `ldap-host-parameters-alist' which see."
155   (interactive "sFilter:")
156   (or host
157       (setq host ldap-default-host))
158   (or host
159       (error "No LDAP host specified"))
160   (let ((host-plist (cdr (assoc host ldap-host-parameters-alist)))
161         ldap)
162     (message "Opening LDAP connection to %s..." host)
163     (setq ldap (ldap-open host host-plist))
164     (message "Searching with LDAP on %s..." host)
165     (prog1 (ldap-search-internal ldap filter 
166                                  (plist-get host-plist 'base)
167                                  (plist-get host-plist 'scope)
168                                  attributes attrsonly)
169       (ldap-close ldap))))
170
171 (provide 'ldap)
172                 
173 ;;; ldap.el ends here