Mercurial > emacs
annotate lisp/url/url-ldap.el @ 101406:6381c0548cdd
fix comments in last change
author | Adrian Robert <Adrian.B.Robert@gmail.com> |
---|---|
date | Fri, 23 Jan 2009 14:53:11 +0000 |
parents | a9dc0e7c3f2b |
children | 328150f0cf76 |
rev | line source |
---|---|
54695 | 1 ;;; url-ldap.el --- LDAP Uniform Resource Locator retrieval code |
64748
875dcc490074
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64084
diff
changeset
|
2 |
100908 | 3 ;; Copyright (C) 1998, 1999, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
4 |
54695 | 5 ;; Keywords: comm, data, processes |
6 | |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
7 ;; This file is part of GNU Emacs. |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
8 ;; |
94668
8259d0d8e107
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79720
diff
changeset
|
9 ;; GNU Emacs is free software: you can redistribute it and/or modify |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
10 ;; it under the terms of the GNU General Public License as published by |
94668
8259d0d8e107
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79720
diff
changeset
|
11 ;; the Free Software Foundation, either version 3 of the License, or |
8259d0d8e107
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79720
diff
changeset
|
12 ;; (at your option) any later version. |
8259d0d8e107
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79720
diff
changeset
|
13 |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
14 ;; GNU Emacs is distributed in the hope that it will be useful, |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
17 ;; GNU General Public License for more details. |
94668
8259d0d8e107
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79720
diff
changeset
|
18 |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
19 ;; You should have received a copy of the GNU General Public License |
94668
8259d0d8e107
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79720
diff
changeset
|
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
21 |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
22 ;;; Commentary: |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
23 |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
24 ;;; Code: |
54695 | 25 |
26 (require 'url-vars) | |
27 (require 'url-parse) | |
28 (require 'url-util) | |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
29 (require 'ldap) |
57449
202c408c174b
url-vars.el (url-gateway-method): Add new method `tls'.
Simon Josefsson <jas@extundo.com>
parents:
54799
diff
changeset
|
30 (autoload 'tls-certificate-information "tls") |
54695 | 31 |
32 ;; This has been implemented from RFC2255 'The LDAP URL Format' (Dec 1997) | |
33 ;; | |
34 ;; basic format is: ldap://host:port/dn?attributes?scope?filter?extensions | |
35 ;; | |
36 ;; Test URLs: | |
37 ;; ldap://ldap.itd.umich.edu/cn%3Dumbflabmanager%2C%20ou%3DUser%20Groups%2C%20ou%3DGroups%2C%20o%3DUniversity%20of%20Michigan%2C%20c%3DUS | |
38 ;; ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US | |
39 ;; | |
40 ;; For simple queries, I have verified compatibility with Netscape | |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
41 ;; Communicator v4.5 under GNU/Linux. |
54695 | 42 ;; |
43 ;; For anything _useful_ though, like specifying the attributes, | |
44 ;; scope, filter, or extensions, netscape claims the URL format is | |
45 ;; unrecognized. So I don't think it supports anything other than the | |
46 ;; defaults (scope=base,attributes=*,filter=(objectClass=*) | |
47 | |
48 (defconst url-ldap-default-port 389 "Default LDAP port.") | |
49 (defalias 'url-ldap-expand-file-name 'url-default-expander) | |
50 | |
51 (defvar url-ldap-pretty-names | |
52 '(("l" . "City") | |
53 ("objectclass" . "Object Class") | |
54 ("o" . "Organization") | |
55 ("ou" . "Organizational Unit") | |
56 ("cn" . "Name") | |
57 ("sn" . "Last Name") | |
58 ("givenname" . "First Name") | |
59 ("mail" . "Email") | |
60 ("title" . "Title") | |
61 ("c" . "Country") | |
62 ("postalcode" . "ZIP Code") | |
63 ("telephonenumber" . "Phone Number") | |
64 ("facsimiletelephonenumber" . "Fax") | |
65 ("postaladdress" . "Mailing Address") | |
66 ("description" . "Notes")) | |
67 "*An assoc list mapping LDAP attribute names to pretty descriptions of them.") | |
68 | |
69 (defvar url-ldap-attribute-formatters | |
70 '(("mail" . (lambda (x) (format "<a href='mailto:%s'>%s</a>" x x))) | |
71 ("owner" . url-ldap-dn-formatter) | |
72 ("creatorsname" . url-ldap-dn-formatter) | |
73 ("jpegphoto" . url-ldap-image-formatter) | |
74 ("usercertificate" . url-ldap-certificate-formatter) | |
75 ("modifiersname" . url-ldap-dn-formatter) | |
76 ("namingcontexts" . url-ldap-dn-formatter) | |
77 ("defaultnamingcontext" . url-ldap-dn-formatter) | |
78 ("member" . url-ldap-dn-formatter)) | |
79 "*An assoc list mapping LDAP attribute names to pretty formatters for them.") | |
80 | |
81 (defsubst url-ldap-attribute-pretty-name (n) | |
82 (or (cdr-safe (assoc (downcase n) url-ldap-pretty-names)) n)) | |
83 | |
84 (defsubst url-ldap-attribute-pretty-desc (n v) | |
85 (if (string-match "^\\([^;]+\\);" n) | |
86 (setq n (match-string 1 n))) | |
87 (funcall (or (cdr-safe (assoc (downcase n) url-ldap-attribute-formatters)) 'identity) v)) | |
88 | |
89 (defun url-ldap-dn-formatter (dn) | |
90 (concat "<a href='/" | |
91 (url-hexify-string dn) | |
92 "'>" dn "</a>")) | |
93 | |
94 (defun url-ldap-certificate-formatter (data) | |
95 (condition-case () | |
96 (require 'ssl) | |
97 (error nil)) | |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
98 (let ((vals (if (fboundp 'ssl-certificate-information) |
57449
202c408c174b
url-vars.el (url-gateway-method): Add new method `tls'.
Simon Josefsson <jas@extundo.com>
parents:
54799
diff
changeset
|
99 (ssl-certificate-information data) |
202c408c174b
url-vars.el (url-gateway-method): Add new method `tls'.
Simon Josefsson <jas@extundo.com>
parents:
54799
diff
changeset
|
100 (tls-certificate-information data)))) |
54695 | 101 (if (not vals) |
102 "<b>Unable to parse certificate</b>" | |
103 (concat "<table border=0>\n" | |
104 (mapconcat | |
105 (lambda (ava) | |
106 (format "<tr><td>%s</td><td>%s</td></tr>\n" (car ava) (cdr ava))) | |
107 vals "\n") | |
108 "</table>\n")))) | |
109 | |
110 (defun url-ldap-image-formatter (data) | |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
111 (format "<img alt='JPEG Photo' src='data:image/jpeg;base64,%s'>" |
54695 | 112 (url-hexify-string (base64-encode-string data)))) |
113 | |
114 ;;;###autoload | |
115 (defun url-ldap (url) | |
61429
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
116 "Perform an LDAP search specified by URL. |
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
117 The return value is a buffer displaying the search results in HTML. |
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
118 URL can be a URL string, or a URL vector of the type returned by |
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
119 `url-generic-parse-url'." |
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
120 (if (stringp url) |
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
121 (setq url (url-generic-parse-url (url-unhex-string url))) |
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
122 (if (not (vectorp url)) |
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
123 (error "Argument is not a valid URL"))) |
54695 | 124 (save-excursion |
125 (set-buffer (generate-new-buffer " *url-ldap*")) | |
126 (setq url-current-object url) | |
127 (insert "Content-type: text/html\r\n\r\n") | |
128 (if (not (fboundp 'ldap-search-internal)) | |
129 (insert "<html>\n" | |
130 " <head>\n" | |
131 " <title>LDAP Not Supported</title>\n" | |
132 " <base href='" (url-recreate-url url) "'>\n" | |
133 " </head>\n" | |
134 " <body>\n" | |
135 " <h1>LDAP Not Supported</h1>\n" | |
136 " <p>\n" | |
137 " This version of Emacs does not support LDAP.\n" | |
138 " </p>\n" | |
139 " </body>\n" | |
140 "</html>\n") | |
141 (let* ((binddn nil) | |
142 (data (url-filename url)) | |
143 (host (url-host url)) | |
144 (port (url-port url)) | |
145 (base-object nil) | |
146 (attributes nil) | |
147 (scope nil) | |
148 (filter nil) | |
149 (extensions nil) | |
61429
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
150 (results nil)) |
54695 | 151 |
152 ;; Get rid of leading / | |
153 (if (string-match "^/" data) | |
154 (setq data (substring data 1))) | |
155 | |
156 (setq data (mapcar (lambda (x) (if (/= (length x) 0) x nil)) (split-string data "\\?")) | |
157 base-object (nth 0 data) | |
158 attributes (nth 1 data) | |
159 scope (nth 2 data) | |
160 filter (nth 3 data) | |
161 extensions (nth 4 data)) | |
162 | |
163 ;; fill in the defaults | |
164 (setq base-object (url-unhex-string (or base-object "")) | |
165 scope (intern (url-unhex-string (or scope "base"))) | |
166 filter (url-unhex-string (or filter "(objectClass=*)"))) | |
167 | |
61429
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
168 (if (not (memq scope '(base one sub))) |
54695 | 169 (error "Malformed LDAP URL: Unknown scope: %S" scope)) |
170 | |
171 ;; Convert to the internal LDAP support scoping names. | |
172 (setq scope (cdr (assq scope '((base . base) (one . onelevel) (sub . subtree))))) | |
173 | |
174 (if attributes | |
175 (setq attributes (mapcar 'url-unhex-string (split-string attributes ",")))) | |
176 | |
177 ;; Parse out the exentions | |
178 (if extensions | |
179 (setq extensions (mapcar (lambda (ext) | |
180 (if (string-match "\\([^=]*\\)=\\(.*\\)" ext) | |
181 (cons (match-string 1 ext) (match-string 2 ext)) | |
182 (cons ext ext))) | |
183 (split-string extensions ",")) | |
184 extensions (mapcar (lambda (ext) | |
185 (cons (url-unhex-string (car ext)) | |
186 (url-unhex-string (cdr ext)))) | |
187 extensions))) | |
188 | |
189 (setq binddn (cdr-safe (or (assoc "bindname" extensions) | |
190 (assoc "!bindname" extensions)))) | |
64748
875dcc490074
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64084
diff
changeset
|
191 |
54695 | 192 ;; Now, let's actually do something with it. |
61429
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
193 (setq results (cdr (ldap-search-internal |
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
194 (list 'host (concat host ":" (number-to-string port)) |
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
195 'base base-object |
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
196 'attributes attributes |
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
197 'scope scope |
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
198 'filter filter |
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
199 'binddn binddn)))) |
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
200 |
54695 | 201 (insert "<html>\n" |
202 " <head>\n" | |
203 " <title>LDAP Search Results</title>\n" | |
204 " <base href='" (url-recreate-url url) "'>\n" | |
205 " </head>\n" | |
206 " <body>\n" | |
207 " <h1>" (int-to-string (length results)) " matches</h1>\n") | |
208 | |
209 (mapc (lambda (obj) | |
210 (insert " <hr>\n" | |
211 " <table border=1>\n") | |
212 (mapc (lambda (attr) | |
213 (if (= (length (cdr attr)) 1) | |
214 ;; single match, easy | |
215 (insert " <tr><td>" | |
216 (url-ldap-attribute-pretty-name (car attr)) | |
217 "</td><td>" | |
218 (url-ldap-attribute-pretty-desc (car attr) (car (cdr attr))) | |
219 "</td></tr>\n") | |
220 ;; Multiple matches, slightly uglier | |
221 (insert " <tr>\n" | |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
222 (format " <td valign=top>") |
54695 | 223 (url-ldap-attribute-pretty-name (car attr)) "</td><td>" |
224 (mapconcat (lambda (x) | |
225 (url-ldap-attribute-pretty-desc (car attr) x)) | |
226 (cdr attr) | |
227 "<br>\n") | |
228 "</td>" | |
229 " </tr>\n"))) | |
61429
49fd31d00693
(url-ldap): Add docstring. Fix call to `ldap-search-internal'.
Richard M. Stallman <rms@gnu.org>
parents:
57449
diff
changeset
|
230 obj) |
54695 | 231 (insert " </table>\n")) |
232 results) | |
233 | |
234 (insert " <hr>\n" | |
235 " </body>\n" | |
236 "</html>\n"))) | |
237 (current-buffer))) | |
238 | |
239 (provide 'url-ldap) | |
54699 | 240 |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
241 ;; arch-tag: 6230e21c-41ae-4174-bd83-82c835676fc8 |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
242 ;;; url-ldap.el ends here |