1 ;;; url-ldap.el --- LDAP Uniform Resource Locator retrieval code
2 ;; Copyright (c) 1998, 1999, 2004, 2005 Free Software Foundation, Inc.
4 ;; Keywords: comm, data, processes
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
31 (autoload 'tls-certificate-information
"tls")
33 ;; This has been implemented from RFC2255 'The LDAP URL Format' (Dec 1997)
35 ;; basic format is: ldap://host:port/dn?attributes?scope?filter?extensions
38 ;; ldap://ldap.itd.umich.edu/cn%3Dumbflabmanager%2C%20ou%3DUser%20Groups%2C%20ou%3DGroups%2C%20o%3DUniversity%20of%20Michigan%2C%20c%3DUS
39 ;; ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US
41 ;; For simple queries, I have verified compatibility with Netscape
42 ;; Communicator v4.5 under GNU/Linux.
44 ;; For anything _useful_ though, like specifying the attributes,
45 ;; scope, filter, or extensions, netscape claims the URL format is
46 ;; unrecognized. So I don't think it supports anything other than the
47 ;; defaults (scope=base,attributes=*,filter=(objectClass=*)
49 (defconst url-ldap-default-port
389 "Default LDAP port.")
50 (defalias 'url-ldap-expand-file-name
'url-default-expander
)
52 (defvar url-ldap-pretty-names
54 ("objectclass" .
"Object Class")
55 ("o" .
"Organization")
56 ("ou" .
"Organizational Unit")
59 ("givenname" .
"First Name")
63 ("postalcode" .
"ZIP Code")
64 ("telephonenumber" .
"Phone Number")
65 ("facsimiletelephonenumber" .
"Fax")
66 ("postaladdress" .
"Mailing Address")
67 ("description" .
"Notes"))
68 "*An assoc list mapping LDAP attribute names to pretty descriptions of them.")
70 (defvar url-ldap-attribute-formatters
71 '(("mail" .
(lambda (x) (format "<a href='mailto:%s'>%s</a>" x x
)))
72 ("owner" . url-ldap-dn-formatter
)
73 ("creatorsname" . url-ldap-dn-formatter
)
74 ("jpegphoto" . url-ldap-image-formatter
)
75 ("usercertificate" . url-ldap-certificate-formatter
)
76 ("modifiersname" . url-ldap-dn-formatter
)
77 ("namingcontexts" . url-ldap-dn-formatter
)
78 ("defaultnamingcontext" . url-ldap-dn-formatter
)
79 ("member" . url-ldap-dn-formatter
))
80 "*An assoc list mapping LDAP attribute names to pretty formatters for them.")
82 (defsubst url-ldap-attribute-pretty-name
(n)
83 (or (cdr-safe (assoc (downcase n
) url-ldap-pretty-names
)) n
))
85 (defsubst url-ldap-attribute-pretty-desc
(n v
)
86 (if (string-match "^\\([^;]+\\);" n
)
87 (setq n
(match-string 1 n
)))
88 (funcall (or (cdr-safe (assoc (downcase n
) url-ldap-attribute-formatters
)) 'identity
) v
))
90 (defun url-ldap-dn-formatter (dn)
92 (url-hexify-string dn
)
95 (defun url-ldap-certificate-formatter (data)
99 (let ((vals (if (fboundp 'ssl-certificate-information
)
100 (ssl-certificate-information data
)
101 (tls-certificate-information data
))))
103 "<b>Unable to parse certificate</b>"
104 (concat "<table border=0>\n"
107 (format "<tr><td>%s</td><td>%s</td></tr>\n" (car ava
) (cdr ava
)))
111 (defun url-ldap-image-formatter (data)
112 (format "<img alt='JPEG Photo' src='data:image/jpeg;base64,%s'>"
113 (url-hexify-string (base64-encode-string data
))))
116 (defun url-ldap (url)
117 "Perform an LDAP search specified by URL.
118 The return value is a buffer displaying the search results in HTML.
119 URL can be a URL string, or a URL vector of the type returned by
120 `url-generic-parse-url'."
122 (setq url
(url-generic-parse-url (url-unhex-string url
)))
123 (if (not (vectorp url
))
124 (error "Argument is not a valid URL")))
126 (set-buffer (generate-new-buffer " *url-ldap*"))
127 (setq url-current-object url
)
128 (insert "Content-type: text/html\r\n\r\n")
129 (if (not (fboundp 'ldap-search-internal
))
132 " <title>LDAP Not Supported</title>\n"
133 " <base href='" (url-recreate-url url
) "'>\n"
136 " <h1>LDAP Not Supported</h1>\n"
138 " This version of Emacs does not support LDAP.\n"
143 (data (url-filename url
))
144 (host (url-host url
))
145 (port (url-port url
))
153 ;; Get rid of leading /
154 (if (string-match "^/" data
)
155 (setq data
(substring data
1)))
157 (setq data
(mapcar (lambda (x) (if (/= (length x
) 0) x nil
)) (split-string data
"\\?"))
158 base-object
(nth 0 data
)
159 attributes
(nth 1 data
)
162 extensions
(nth 4 data
))
164 ;; fill in the defaults
165 (setq base-object
(url-unhex-string (or base-object
""))
166 scope
(intern (url-unhex-string (or scope
"base")))
167 filter
(url-unhex-string (or filter
"(objectClass=*)")))
169 (if (not (memq scope
'(base one sub
)))
170 (error "Malformed LDAP URL: Unknown scope: %S" scope
))
172 ;; Convert to the internal LDAP support scoping names.
173 (setq scope
(cdr (assq scope
'((base . base
) (one . onelevel
) (sub . subtree
)))))
176 (setq attributes
(mapcar 'url-unhex-string
(split-string attributes
","))))
178 ;; Parse out the exentions
180 (setq extensions
(mapcar (lambda (ext)
181 (if (string-match "\\([^=]*\\)=\\(.*\\)" ext
)
182 (cons (match-string 1 ext
) (match-string 2 ext
))
184 (split-string extensions
","))
185 extensions
(mapcar (lambda (ext)
186 (cons (url-unhex-string (car ext
))
187 (url-unhex-string (cdr ext
))))
190 (setq binddn
(cdr-safe (or (assoc "bindname" extensions
)
191 (assoc "!bindname" extensions
))))
193 ;; Now, let's actually do something with it.
194 (setq results
(cdr (ldap-search-internal
195 (list 'host
(concat host
":" (number-to-string port
))
197 'attributes attributes
204 " <title>LDAP Search Results</title>\n"
205 " <base href='" (url-recreate-url url
) "'>\n"
208 " <h1>" (int-to-string (length results
)) " matches</h1>\n")
212 " <table border=1>\n")
214 (if (= (length (cdr attr
)) 1)
215 ;; single match, easy
217 (url-ldap-attribute-pretty-name (car attr
))
219 (url-ldap-attribute-pretty-desc (car attr
) (car (cdr attr
)))
221 ;; Multiple matches, slightly uglier
223 (format " <td valign=top>")
224 (url-ldap-attribute-pretty-name (car attr
)) "</td><td>"
225 (mapconcat (lambda (x)
226 (url-ldap-attribute-pretty-desc (car attr
) x
))
232 (insert " </table>\n"))
242 ;; arch-tag: 6230e21c-41ae-4174-bd83-82c835676fc8
243 ;;; url-ldap.el ends here