* smime.el: Require smime-ldap.
[gnus] / lisp / smime-ldap.el
1 ;;; smime-ldap.el --- client interface to LDAP for Emacs
2
3 ;; Copyright (C) 1998, 1999, 2000, 2005 Free Software Foundation, Inc.
4
5 ;; Author: Oscar Figueiredo <Oscar.Figueiredo@di.epfl.ch>
6 ;; Maintainer: Arne J\e,Ax\e(Brgensen <arne@arnested.dk>
7 ;; Created: February 2005
8 ;; Keywords: comm
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; This file has a slightly changed implementation of Emacs 21.3's
30 ;; ldap-search and ldap-search-internal from ldap.el. The changes are
31 ;; made to achieve compatibility with OpenLDAP v2 and to make it
32 ;; possible to retrieve LDAP attributes that are tagged ie ";binary".
33
34 ;; When Gnus drops support for Emacs 21.x this file can be removed and
35 ;; smime.el changed to
36
37 ;;   - (require 'smime-ldap)   =>   (require 'ldap)
38 ;;   - (smime-ldap-search ...) =>   (ldap-search ...)
39
40 ;; If we are running in Emacs 22 or newer it just uses the build-in
41 ;; version of ldap-search.
42
43 ;;; Code:
44
45 (load-library "net/ldap")
46
47 (defun smime-ldap-search (filter &optional host attributes attrsonly withdn)
48   "Perform an LDAP search.
49 FILTER is the search filter in RFC1558 syntax.
50 HOST is the LDAP host on which to perform the search.
51 ATTRIBUTES are the specific attributes to retrieve, nil means 
52 retrieve all.
53 ATTRSONLY, if non-nil, retrieves the attributes only, without 
54 the associated values.
55 If WITHDN is non-nil, each entry in the result will be prepended with
56 its distinguished name WITHDN.
57 Additional search parameters can be specified through 
58 `ldap-host-parameters-alist', which see."
59   (interactive "sFilter:")
60   (if (>= emacs-major-version 22)
61       (ldap-search filter host attributes attrsonly)
62     (or host
63         (setq host ldap-default-host)
64         (error "No LDAP host specified"))
65     (let ((host-plist (cdr (assoc host ldap-host-parameters-alist)))
66           result)
67       (setq result (smime-ldap-search-internal (append host-plist
68                                                        (list 'host host
69                                                              'filter filter
70                                                              'attributes attributes
71                                                              'attrsonly attrsonly
72                                                              'withdn withdn))))
73       (if ldap-ignore-attribute-codings
74           result
75         (mapcar (function
76                  (lambda (record)
77                    (mapcar 'ldap-decode-attribute record)))
78                 result)))))
79
80 (defun smime-ldap-search-internal (search-plist)
81   "Perform a search on a LDAP server.
82 SEARCH-PLIST is a property list describing the search request.
83 Valid keys in that list are:
84   `host' is a string naming one or more (blank-separated) LDAP servers to
85 to try to connect to.  Each host name may optionally be of the form HOST:PORT.
86   `filter' is a filter string for the search as described in RFC 1558.
87   `attributes' is a list of strings indicating which attributes to retrieve
88 for each matching entry. If nil, return all available attributes.
89   `attrsonly', if non-nil, indicates that only attributes are retrieved,
90 not their associated values.
91   `base' is the base for the search as described in RFC 1779.
92   `scope' is one of the three symbols `sub', `base' or `one'.
93   `binddn' is the distinguished name of the user to bind as (in RFC 1779 syntax).
94   `passwd' is the password to use for simple authentication.
95   `deref' is one of the symbols `never', `always', `search' or `find'.
96   `timelimit' is the timeout limit for the connection in seconds.
97   `sizelimit' is the maximum number of matches to return.
98   `withdn' if non-nil each entry in the result will be prepended with
99 its distinguished name DN.
100 The function returns a list of matching entries.  Each entry is itself
101 an alist of attribute/value pairs."
102   (let ((buf (get-buffer-create " *ldap-search*"))
103         (bufval (get-buffer-create " *ldap-value*"))
104         (host (or (plist-get search-plist 'host)
105                   ldap-default-host))
106         (filter (plist-get search-plist 'filter))
107         (attributes (plist-get search-plist 'attributes))
108         (attrsonly (plist-get search-plist 'attrsonly))
109         (base (or (plist-get search-plist 'base)
110                   ldap-default-base))
111         (scope (plist-get search-plist 'scope))
112         (binddn (plist-get search-plist 'binddn))
113         (passwd (plist-get search-plist 'passwd))
114         (deref (plist-get search-plist 'deref))
115         (timelimit (plist-get search-plist 'timelimit))
116         (sizelimit (plist-get search-plist 'sizelimit))
117         (withdn (plist-get search-plist 'withdn))
118         (numres 0)
119         arglist dn name value record result)
120     (if (or (null filter)
121             (equal "" filter))
122         (error "No search filter"))
123     (setq filter (cons filter attributes))
124     (save-excursion
125       (set-buffer buf)
126       (erase-buffer)
127       (if (and host
128                (not (equal "" host)))
129           (setq arglist (nconc arglist (list (format "-h%s" host)))))
130       (if (and attrsonly
131                (not (equal "" attrsonly)))
132           (setq arglist (nconc arglist (list "-A"))))
133       (if (and base
134                (not (equal "" base)))
135           (setq arglist (nconc arglist (list (format "-b%s" base)))))
136       (if (and scope
137                (not (equal "" scope)))
138           (setq arglist (nconc arglist (list (format "-s%s" scope)))))
139       (if (and binddn
140                (not (equal "" binddn)))
141           (setq arglist (nconc arglist (list (format "-D%s" binddn)))))
142       (if (and passwd
143                (not (equal "" passwd)))
144           (setq arglist (nconc arglist (list (format "-w%s" passwd)))))
145       (if (and deref
146                (not (equal "" deref)))
147           (setq arglist (nconc arglist (list (format "-a%s" deref)))))
148       (if (and timelimit
149                (not (equal "" timelimit)))
150           (setq arglist (nconc arglist (list (format "-l%s" timelimit)))))
151       (if (and sizelimit
152                (not (equal "" sizelimit)))
153           (setq arglist (nconc arglist (list (format "-z%s" sizelimit)))))
154       (eval `(call-process ldap-ldapsearch-prog
155                            nil
156                            buf
157                            nil
158                            ,@arglist
159                            "-tt"                ; Write values to temp files
160                            "-x"
161                            "-LL"
162                            ;                       ,@ldap-ldapsearch-args
163                            ,@filter))
164       (insert "\n")
165       (goto-char (point-min))
166
167       (while (re-search-forward "[\t\n\f]+ " nil t)
168         (replace-match "" nil nil))
169       (goto-char (point-min))
170
171       (if (looking-at "usage")
172           (error "Incorrect ldapsearch invocation")
173         (message "Parsing results... ")
174         (while (progn
175                  (skip-chars-forward " \t\n")
176                  (not (eobp)))
177           (setq dn (buffer-substring (point) (save-excursion
178                                                (end-of-line)
179                                                (point))))
180           (forward-line 1)
181           (while (looking-at "^\\(\\w*\\)\\(;\\w*\\)?[=:\t ]+\\(<[\t ]*file://\\)?\\(.*\\)$")
182             (setq name (match-string 1)
183                   value (match-string 4))
184             (save-excursion
185               (set-buffer bufval)
186               (erase-buffer)
187               (insert-file-contents-literally value)
188               (delete-file value)
189               (setq value (buffer-substring (point-min) (point-max))))
190             (setq record (cons (list name value)
191                                record))
192             (forward-line 1))
193           (setq result (cons (if withdn
194                                  (cons dn (nreverse record))
195                                (nreverse record)) result))
196           (setq record nil)
197           (skip-chars-forward " \t\n")
198           (message "Parsing results... %d" numres)
199           (1+ numres))
200         (message "Parsing results... done")
201         (nreverse result)))))
202
203 (provide 'smime-ldap)
204
205 ;;; smime-ldap.el ends here