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