1 ;;; smime-ldap.el --- client interface to LDAP for Emacs
3 ;; Copyright (C) 1998, 1999, 2000, 2005 Free Software Foundation, Inc.
5 ;; Author: Oscar Figueiredo <Oscar.Figueiredo@di.epfl.ch>
6 ;; Maintainer: Arne J
\e,Ax
\e(Brgensen <arne@arnested.dk>
7 ;; Created: February 2005
10 ;; This file is part of GNU Emacs.
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)
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.
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/>.
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".
32 ;; The file also adds a compatibility layer for Emacs and XEmacs.
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
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:")
52 (if (fboundp 'ldap-search-entries)
53 (ldap-search-entries filter host attributes attrsonly)
55 (cdr (ldap-search filter host attributes attrsonly))))
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)
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)
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))
96 arglist dn name value record result)
99 (error "No search filter"))
100 (setq filter (cons filter attributes))
101 (with-current-buffer buf
104 (not (equal "" host)))
105 (setq arglist (nconc arglist (list (format "-h%s" host)))))
107 (not (equal "" attrsonly)))
108 (setq arglist (nconc arglist (list "-A"))))
110 (not (equal "" base)))
111 (setq arglist (nconc arglist (list (format "-b%s" base)))))
113 (not (equal "" scope)))
114 (setq arglist (nconc arglist (list (format "-s%s" scope)))))
116 (not (equal "" binddn)))
117 (setq arglist (nconc arglist (list (format "-D%s" binddn)))))
119 (not (equal "" passwd)))
120 (setq arglist (nconc arglist (list (format "-w%s" passwd)))))
122 (not (equal "" deref)))
123 (setq arglist (nconc arglist (list (format "-a%s" deref)))))
125 (not (equal "" timelimit)))
126 (setq arglist (nconc arglist (list (format "-l%s" timelimit)))))
128 (not (equal "" sizelimit)))
129 (setq arglist (nconc arglist (list (format "-z%s" sizelimit)))))
130 (eval `(call-process ldap-ldapsearch-prog
135 "-tt" ; Write values to temp files
138 ; ,@ldap-ldapsearch-args
141 (goto-char (point-min))
143 (while (re-search-forward "[\t\n\f]+ " nil t)
144 (replace-match "" nil nil))
145 (goto-char (point-min))
147 (if (looking-at "usage")
148 (error "Incorrect ldapsearch invocation")
149 (message "Parsing results... ")
151 (skip-chars-forward " \t\n")
153 (setq dn (buffer-substring (point) (save-excursion
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
163 (insert-file-contents-literally value)
165 (setq value (buffer-substring (point-min) (point-max))))
166 (setq record (cons (list name value)
169 (setq result (cons (if withdn
170 (cons dn (nreverse record))
171 (nreverse record)) result))
173 (skip-chars-forward " \t\n")
174 (message "Parsing results... %d" numres)
176 (message "Parsing results... done")
177 (nreverse result)))))
179 (provide 'smime-ldap)
181 ;;; smime-ldap.el ends here