Mercurial > emacs
annotate lisp/url/url-ldap.el @ 56394:245f03d9697f
*** empty log message ***
author | Luc Teirlinck <teirllm@auburn.edu> |
---|---|
date | Sun, 11 Jul 2004 01:35:37 +0000 |
parents | e74e244a3cff |
children | 202c408c174b eb7e8d483840 |
rev | line source |
---|---|
54695 | 1 ;;; url-ldap.el --- LDAP Uniform Resource Locator retrieval code |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
2 ;; Copyright (c) 1998 - 1999, 2004 Free Software Foundation, Inc. |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
3 |
54695 | 4 ;; Keywords: comm, data, processes |
5 | |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
6 ;; This file is part of GNU Emacs. |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
7 ;; |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
8 ;; GNU Emacs is free software; you can redistribute it and/or modify |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
9 ;; it under the terms of the GNU General Public License as published by |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
10 ;; the Free Software Foundation; either version 2, or (at your option) |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
11 ;; any later version. |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
12 ;; |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
13 ;; 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
|
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
16 ;; GNU General Public License for more details. |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
17 ;; |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
18 ;; You should have received a copy of the GNU General Public License |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
21 ;; Boston, MA 02111-1307, USA. |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
22 |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
23 ;;; Commentary: |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
24 |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
25 ;;; Code: |
54695 | 26 |
27 (require 'url-vars) | |
28 (require 'url-parse) | |
29 (require 'url-util) | |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
30 (require 'ldap) |
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) |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
99 (ssl-certificate-information data)))) |
54695 | 100 (if (not vals) |
101 "<b>Unable to parse certificate</b>" | |
102 (concat "<table border=0>\n" | |
103 (mapconcat | |
104 (lambda (ava) | |
105 (format "<tr><td>%s</td><td>%s</td></tr>\n" (car ava) (cdr ava))) | |
106 vals "\n") | |
107 "</table>\n")))) | |
108 | |
109 (defun url-ldap-image-formatter (data) | |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
110 (format "<img alt='JPEG Photo' src='data:image/jpeg;base64,%s'>" |
54695 | 111 (url-hexify-string (base64-encode-string data)))) |
112 | |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
113 ;; FIXME: This needs sorting out for the Emacs LDAP functions, specifically |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
114 ;; calls of ldap-open, ldap-close, ldap-search-internal |
54695 | 115 ;;;###autoload |
116 (defun url-ldap (url) | |
117 (save-excursion | |
118 (set-buffer (generate-new-buffer " *url-ldap*")) | |
119 (setq url-current-object url) | |
120 (insert "Content-type: text/html\r\n\r\n") | |
121 (if (not (fboundp 'ldap-search-internal)) | |
122 (insert "<html>\n" | |
123 " <head>\n" | |
124 " <title>LDAP Not Supported</title>\n" | |
125 " <base href='" (url-recreate-url url) "'>\n" | |
126 " </head>\n" | |
127 " <body>\n" | |
128 " <h1>LDAP Not Supported</h1>\n" | |
129 " <p>\n" | |
130 " This version of Emacs does not support LDAP.\n" | |
131 " </p>\n" | |
132 " </body>\n" | |
133 "</html>\n") | |
134 (let* ((binddn nil) | |
135 (data (url-filename url)) | |
136 (host (url-host url)) | |
137 (port (url-port url)) | |
138 (base-object nil) | |
139 (attributes nil) | |
140 (scope nil) | |
141 (filter nil) | |
142 (extensions nil) | |
143 (connection nil) | |
144 (results nil) | |
145 (extract-dn (and (fboundp 'function-max-args) | |
146 (= (function-max-args 'ldap-search-internal) 7)))) | |
147 | |
148 ;; Get rid of leading / | |
149 (if (string-match "^/" data) | |
150 (setq data (substring data 1))) | |
151 | |
152 (setq data (mapcar (lambda (x) (if (/= (length x) 0) x nil)) (split-string data "\\?")) | |
153 base-object (nth 0 data) | |
154 attributes (nth 1 data) | |
155 scope (nth 2 data) | |
156 filter (nth 3 data) | |
157 extensions (nth 4 data)) | |
158 | |
159 ;; fill in the defaults | |
160 (setq base-object (url-unhex-string (or base-object "")) | |
161 scope (intern (url-unhex-string (or scope "base"))) | |
162 filter (url-unhex-string (or filter "(objectClass=*)"))) | |
163 | |
164 (if (not (memq scope '(base one tree))) | |
165 (error "Malformed LDAP URL: Unknown scope: %S" scope)) | |
166 | |
167 ;; Convert to the internal LDAP support scoping names. | |
168 (setq scope (cdr (assq scope '((base . base) (one . onelevel) (sub . subtree))))) | |
169 | |
170 (if attributes | |
171 (setq attributes (mapcar 'url-unhex-string (split-string attributes ",")))) | |
172 | |
173 ;; Parse out the exentions | |
174 (if extensions | |
175 (setq extensions (mapcar (lambda (ext) | |
176 (if (string-match "\\([^=]*\\)=\\(.*\\)" ext) | |
177 (cons (match-string 1 ext) (match-string 2 ext)) | |
178 (cons ext ext))) | |
179 (split-string extensions ",")) | |
180 extensions (mapcar (lambda (ext) | |
181 (cons (url-unhex-string (car ext)) | |
182 (url-unhex-string (cdr ext)))) | |
183 extensions))) | |
184 | |
185 (setq binddn (cdr-safe (or (assoc "bindname" extensions) | |
186 (assoc "!bindname" extensions)))) | |
187 | |
188 ;; Now, let's actually do something with it. | |
189 (setq connection (ldap-open host (if binddn (list 'binddn binddn))) | |
190 results (if extract-dn | |
191 (ldap-search-internal connection filter base-object scope attributes nil t) | |
192 (ldap-search-internal connection filter base-object scope attributes nil))) | |
193 | |
194 (ldap-close connection) | |
195 (insert "<html>\n" | |
196 " <head>\n" | |
197 " <title>LDAP Search Results</title>\n" | |
198 " <base href='" (url-recreate-url url) "'>\n" | |
199 " </head>\n" | |
200 " <body>\n" | |
201 " <h1>" (int-to-string (length results)) " matches</h1>\n") | |
202 | |
203 (mapc (lambda (obj) | |
204 (insert " <hr>\n" | |
205 " <table border=1>\n") | |
206 (if extract-dn | |
207 (insert " <tr><th colspan=2>" (car obj) "</th></tr>\n")) | |
208 (mapc (lambda (attr) | |
209 (if (= (length (cdr attr)) 1) | |
210 ;; single match, easy | |
211 (insert " <tr><td>" | |
212 (url-ldap-attribute-pretty-name (car attr)) | |
213 "</td><td>" | |
214 (url-ldap-attribute-pretty-desc (car attr) (car (cdr attr))) | |
215 "</td></tr>\n") | |
216 ;; Multiple matches, slightly uglier | |
217 (insert " <tr>\n" | |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
218 (format " <td valign=top>") |
54695 | 219 (url-ldap-attribute-pretty-name (car attr)) "</td><td>" |
220 (mapconcat (lambda (x) | |
221 (url-ldap-attribute-pretty-desc (car attr) x)) | |
222 (cdr attr) | |
223 "<br>\n") | |
224 "</td>" | |
225 " </tr>\n"))) | |
226 (if extract-dn (cdr obj) obj)) | |
227 (insert " </table>\n")) | |
228 results) | |
229 | |
230 (insert " <hr>\n" | |
231 " </body>\n" | |
232 "</html>\n"))) | |
233 (current-buffer))) | |
234 | |
235 (provide 'url-ldap) | |
54699 | 236 |
54799
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
237 ;; arch-tag: 6230e21c-41ae-4174-bd83-82c835676fc8 |
e74e244a3cff
(ldap): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
238 ;;; url-ldap.el ends here |