(message-reply): Take an optional switch-buffer parameter so that Gnus window confs...
[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 3, 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., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, 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 ;; The file also adds a compatibility layer for Emacs and XEmacs.
35
36 ;;; Code:
37
38 (require 'ldap)
39
40 (defun smime-ldap-search (filter &optional host attributes attrsonly withdn)
41   "Perform an LDAP search.
42 FILTER is the search filter in RFC1558 syntax.
43 HOST is the LDAP host on which to perform the search.
44 ATTRIBUTES are the specific attributes to retrieve, nil means
45 retrieve all.
46 ATTRSONLY, if non-nil, retrieves the attributes only, without
47 the associated values.
48 If WITHDN is non-nil, each entry in the result will be prepended with
49 its distinguished name WITHDN.
50 Additional search parameters can be specified through
51 `ldap-host-parameters-alist', which see."
52   (interactive "sFilter:")
53   ;; for XEmacs
54   (if (fboundp 'ldap-search-entries)
55       (ldap-search-entries filter host attributes attrsonly)
56     ;; for Emacs
57     (cdr (ldap-search filter host attributes attrsonly))))
58
59 (defun smime-ldap-search-internal (search-plist)
60   "Perform a search on a LDAP server.
61 SEARCH-PLIST is a property list describing the search request.
62 Valid keys in that list are:
63 `host' is a string naming one or more (blank-separated) LDAP servers to
64 to try to connect to.  Each host name may optionally be of the form HOST:PORT.
65 `filter' is a filter string for the search as described in RFC 1558.
66 `attributes' is a list of strings indicating which attributes to retrieve
67 for each matching entry. If nil, return all available attributes.
68 `attrsonly', if non-nil, indicates that only attributes are retrieved,
69 not their associated values.
70 `base' is the base for the search as described in RFC 1779.
71 `scope' is one of the three symbols `sub', `base' or `one'.
72 `binddn' is the distinguished name of the user to bind as (in RFC 1779 syntax).
73 `passwd' is the password to use for simple authentication.
74 `deref' is one of the symbols `never', `always', `search' or `find'.
75 `timelimit' is the timeout limit for the connection in seconds.
76 `sizelimit' is the maximum number of matches to return.
77 `withdn' if non-nil each entry in the result will be prepended with
78 its distinguished name DN.
79 The function returns a list of matching entries.  Each entry is itself
80 an alist of attribute/value pairs."
81   (let ((buf (get-buffer-create " *ldap-search*"))
82         (bufval (get-buffer-create " *ldap-value*"))
83         (host (or (plist-get search-plist 'host)
84                   ldap-default-host))
85         (filter (plist-get search-plist 'filter))
86         (attributes (plist-get search-plist 'attributes))
87         (attrsonly (plist-get search-plist 'attrsonly))
88         (base (or (plist-get search-plist 'base)
89                   ldap-default-base))
90         (scope (plist-get search-plist 'scope))
91         (binddn (plist-get search-plist 'binddn))
92         (passwd (plist-get search-plist 'passwd))
93         (deref (plist-get search-plist 'deref))
94         (timelimit (plist-get search-plist 'timelimit))
95         (sizelimit (plist-get search-plist 'sizelimit))
96         (withdn (plist-get search-plist 'withdn))
97         (numres 0)
98         arglist dn name value record result)
99     (if (or (null filter)
100             (equal "" filter))
101         (error "No search filter"))
102     (setq filter (cons filter attributes))
103     (with-current-buffer buf
104       (erase-buffer)
105       (if (and host
106                (not (equal "" host)))
107           (setq arglist (nconc arglist (list (format "-h%s" host)))))
108       (if (and attrsonly
109                (not (equal "" attrsonly)))
110           (setq arglist (nconc arglist (list "-A"))))
111       (if (and base
112                (not (equal "" base)))
113           (setq arglist (nconc arglist (list (format "-b%s" base)))))
114       (if (and scope
115                (not (equal "" scope)))
116           (setq arglist (nconc arglist (list (format "-s%s" scope)))))
117       (if (and binddn
118                (not (equal "" binddn)))
119           (setq arglist (nconc arglist (list (format "-D%s" binddn)))))
120       (if (and passwd
121                (not (equal "" passwd)))
122           (setq arglist (nconc arglist (list (format "-w%s" passwd)))))
123       (if (and deref
124                (not (equal "" deref)))
125           (setq arglist (nconc arglist (list (format "-a%s" deref)))))
126       (if (and timelimit
127                (not (equal "" timelimit)))
128           (setq arglist (nconc arglist (list (format "-l%s" timelimit)))))
129       (if (and sizelimit
130                (not (equal "" sizelimit)))
131           (setq arglist (nconc arglist (list (format "-z%s" sizelimit)))))
132       (eval `(call-process ldap-ldapsearch-prog
133                            nil
134                            buf
135                            nil
136                            ,@arglist
137                            "-tt"                ; Write values to temp files
138                            "-x"
139                            "-LL"
140                            ;                       ,@ldap-ldapsearch-args
141                            ,@filter))
142       (insert "\n")
143       (goto-char (point-min))
144
145       (while (re-search-forward "[\t\n\f]+ " nil t)
146         (replace-match "" nil nil))
147       (goto-char (point-min))
148
149       (if (looking-at "usage")
150           (error "Incorrect ldapsearch invocation")
151         (message "Parsing results... ")
152         (while (progn
153                  (skip-chars-forward " \t\n")
154                  (not (eobp)))
155           (setq dn (buffer-substring (point) (save-excursion
156                                                (end-of-line)
157                                                (point))))
158           (forward-line 1)
159           (while (looking-at (concat "^\\(\\w*\\)\\(;\\w*\\)?[=:\t ]+"
160                                      "\\(<[\t ]*file://\\)?\\(.*\\)$"))
161             (setq name (match-string 1)
162                   value (match-string 4))
163             (with-current-buffer bufval
164               (erase-buffer)
165               (insert-file-contents-literally value)
166               (delete-file value)
167               (setq value (buffer-substring (point-min) (point-max))))
168             (setq record (cons (list name value)
169                                record))
170             (forward-line 1))
171           (setq result (cons (if withdn
172                                  (cons dn (nreverse record))
173                                (nreverse record)) result))
174           (setq record nil)
175           (skip-chars-forward " \t\n")
176           (message "Parsing results... %d" numres)
177           (1+ numres))
178         (message "Parsing results... done")
179         (nreverse result)))))
180
181 (provide 'smime-ldap)
182
183 ;;; smime-ldap.el ends here