57cfe789d5fedfc07cc48f5c0ad9350985376a65
[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 hostname.
44 A TCP port number can be appended to that name using a colon as 
45 a separator."
46   :type '(choice (string :tag "Host name")
47                  (const :tag "Use library default" nil))
48   :group 'ldap)
49
50 (defcustom ldap-default-port nil
51   "*Default TCP port for LDAP connections.
52 Initialized from the LDAP library at build time. Default value is 389."
53   :type '(choice (const :tag "Use library default" nil)
54                  (integer :tag "Port number"))
55   :group 'ldap)
56
57 (defcustom ldap-default-base nil
58   "*Default base for LDAP searches.
59 This is a string using the syntax of RFC 1779.
60 For instance, \"o=ACME, c=US\" limits the search to the
61 Acme organization in the United States."
62   :type '(choice (const :tag "Use library default" nil)
63                  (string :tag "Search base"))
64   :group 'ldap)
65
66
67 (defcustom ldap-host-parameters-alist nil
68   "*Alist of host-specific options for LDAP transactions.
69 The format of each list element is:
70 \(HOST PROP1 VAL1 PROP2 VAL2 ...)
71 HOST is the hostname of an LDAP server (with an optional TCP port number
72 appended to it  using a colon as a separator). 
73 PROPn and VALn are property/value pairs describing parameters for the server.
74 Valid properties include:
75   `binddn' is the distinguished name of the user to bind as 
76     (in RFC 1779 syntax).
77   `passwd' is the password to use for simple authentication.
78   `auth' is the authentication method to use. 
79     Possible values are: `simple', `krbv41' and `krbv42'.
80   `base' is the base for the search as described in RFC 1779.
81   `scope' is one of the three symbols `subtree', `base' or `onelevel'.
82   `deref' is one of the symbols `never', `always', `search' or `find'.
83   `timelimit' is the timeout limit for the connection in seconds.
84   `sizelimit' is the maximum number of matches to return."
85   :type '(repeat :menu-tag "Host parameters"
86                  :tag "Host parameters"
87                  (list :menu-tag "Host parameters"
88                        :tag "Host parameters"
89                        :value nil
90                        (string :tag "Host name")
91                        (checklist :inline t
92                                   :greedy t
93                                   (list
94                                    :tag "Search Base" 
95                                    :inline t
96                                    (const :tag "Search Base" base)
97                                    string)
98                                   (list
99                                    :tag "Binding DN"
100                                    :inline t
101                                    (const :tag "Binding DN" binddn)
102                                    string)
103                                   (list
104                                    :tag "Password"
105                                    :inline t
106                                    (const :tag "Password" passwd)
107                                    string)
108                                   (list
109                                    :tag "Authentication Method"
110                                    :inline t
111                                    (const :tag "Authentication Method" auth)
112                                    (choice
113                                     (const :menu-tag "None" :tag "None" nil)
114                                     (const :menu-tag "Simple" :tag "Simple" simple)
115                                     (const :menu-tag "Kerberos 4.1" :tag "Kerberos 4.1" krbv41)
116                                     (const :menu-tag "Kerberos 4.2" :tag "Kerberos 4.2" krbv42)))
117                                   (list
118                                    :tag "Search Scope" 
119                                    :inline t
120                                    (const :tag "Search Scope" scope)
121                                    (choice
122                                     (const :menu-tag "Default" :tag "Default" nil)
123                                     (const :menu-tag "Subtree" :tag "Subtree" subtree)
124                                     (const :menu-tag "Base" :tag "Base" base)
125                                     (const :menu-tag "One Level" :tag "One Level" onelevel)))
126                                   (list
127                                    :tag "Dereferencing"
128                                    :inline t
129                                    (const :tag "Dereferencing" deref)
130                                    (choice
131                                     (const :menu-tag "Default" :tag "Default" nil)
132                                     (const :menu-tag "Never" :tag "Never" never)
133                                     (const :menu-tag "Always" :tag "Always" always)
134                                     (const :menu-tag "When searching" :tag "When searching" search)
135                                     (const :menu-tag "When locating base" :tag "When locating base" find)))
136                                   (list
137                                    :tag "Time Limit"
138                                    :inline t
139                                    (const :tag "Time Limit" timelimit)
140                                    (integer :tag "(in seconds)"))
141                                   (list
142                                    :tag "Size Limit"
143                                    :inline t
144                                    (const :tag "Size Limit" sizelimit)
145                                    (integer :tag "(number of records)")))))
146 :group 'ldap)
147
148 (defun ldap-get-host-parameter (host parameter)
149   "Get the value of PARAMETER for HOST in `ldap-host-parameters-alist'."
150   (plist-get (cdr (assoc host ldap-host-parameters-alist))
151              parameter))
152         
153 (defun ldap-search (filter &optional host attributes attrsonly withdn)
154   "Perform an LDAP search.
155 FILTER is the search filter in RFC1558 syntax, i.e. something that
156 looks like \"(cn=John Smith)\".
157 HOST is the LDAP host on which to perform the search.
158 ATTRIBUTES is a list of attributes to retrieve; nil means retrieve all.
159 If ATTRSONLY is non nil, the attributes will be retrieved without
160 the associated values.
161 If WITHDN is non-nil each entry in the result will be prepennded with
162 its distinguished name DN.
163 Additional search parameters can be specified through 
164 `ldap-host-parameters-alist' which see.
165 The function returns a list of matching entries.  Each entry is itself
166 an alist of attribute/value pairs optionally preceded by the DN of the
167 entry according to the value of WITHDN."
168   (interactive "sFilter:")
169   (or host
170       (setq host ldap-default-host))
171   (or host
172       (error "No LDAP host specified"))
173   (let ((host-plist (cdr (assoc host ldap-host-parameters-alist)))
174         ldap)
175     (message "Opening LDAP connection to %s..." host)
176     (setq ldap (ldap-open host host-plist))
177     (message "Searching with LDAP on %s..." host)
178     (prog1 (ldap-search-internal ldap filter 
179                                  (plist-get host-plist 'base)
180                                  (plist-get host-plist 'scope)
181                                  attributes attrsonly withdn)
182       (ldap-close ldap))))
183
184 (provide 'ldap)
185                 
186 ;;; ldap.el ends here