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