Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / lisp / url / url-auth.el
CommitLineData
8c8b8430 1;;; url-auth.el --- Uniform Resource Locator authorization modules
00eef4de 2
73b0cd50 3;; Copyright (C) 1996-1999, 2004-2011 Free Software Foundation, Inc.
00eef4de 4
8c8b8430
SM
5;; Keywords: comm, data, processes, hypermedia
6
00eef4de
LH
7;; This file is part of GNU Emacs.
8
4936186e 9;; GNU Emacs is free software: you can redistribute it and/or modify
00eef4de 10;; it under the terms of the GNU General Public License as published by
4936186e
GM
11;; the Free Software Foundation, either version 3 of the License, or
12;; (at your option) any later version.
00eef4de
LH
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
4936186e 20;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
00eef4de
LH
21
22;;; Code:
8c8b8430
SM
23
24(require 'url-vars)
25(require 'url-parse)
26(autoload 'url-warn "url")
8705576e 27(autoload 'auth-source-user-or-password "auth-source")
97d1c236 28
8c8b8430
SM
29(defsubst url-auth-user-prompt (url realm)
30 "String to usefully prompt for a username."
31 (concat "Username [for "
32 (or realm (url-truncate-url-for-viewing
33 (url-recreate-url url)
34 (- (window-width) 10 20)))
35 "]: "))
36
37;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38;;; Basic authorization code
39;;; ------------------------
40;;; This implements the BASIC authorization type. See the online
41;;; documentation at
42;;; http://www.w3.org/hypertext/WWW/AccessAuthorization/Basic.html
43;;; for the complete documentation on this type.
44;;;
45;;; This is very insecure, but it works as a proof-of-concept
46;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
47(defvar url-basic-auth-storage 'url-http-real-basic-auth-storage
48 "Where usernames and passwords are stored.
49
50Must be a symbol pointing to another variable that will actually store
51the information. The value of this variable is an assoc list of assoc
52lists. The first assoc list is keyed by the server name. The cdr of
d1ce47b0 53this is an assoc list based on the 'directory' specified by the URL we
8c8b8430
SM
54are looking up.")
55
56(defun url-basic-auth (url &optional prompt overwrite realm args)
57 "Get the username/password for the specified URL.
58If optional argument PROMPT is non-nil, ask for the username/password
59to use for the url and its descendants. If optional third argument
60OVERWRITE is non-nil, overwrite the old username/password pair if it
61is found in the assoc list. If REALM is specified, use that as the realm
ff51e86d 62instead of the filename inheritance method."
8c8b8430
SM
63 (let* ((href (if (stringp url)
64 (url-generic-parse-url url)
65 url))
66 (server (url-host href))
97d1c236 67 (type (url-type href))
8c8b8430 68 (port (url-port href))
ff51e86d 69 (file (url-filename href))
2540a3ab
VJL
70 (user (url-user href))
71 (pass (url-password href))
72 byserv retval data)
8c8b8430 73 (setq server (format "%s:%d" server port)
ff51e86d 74 file (cond
8c8b8430 75 (realm realm)
ff51e86d
RS
76 ((string= "" file) "/")
77 ((string-match "/$" file) file)
78 (t (url-file-directory file)))
8c8b8430
SM
79 byserv (cdr-safe (assoc server
80 (symbol-value url-basic-auth-storage))))
81 (cond
82 ((and prompt (not byserv))
8705576e 83 (setq user (or
97d1c236
TZ
84 (auth-source-user-or-password "login" server type)
85 (read-string (url-auth-user-prompt url realm)
86 (or user (user-real-login-name))))
8705576e 87 pass (or
97d1c236
TZ
88 (auth-source-user-or-password "password" server type)
89 (read-passwd "Password: " nil (or pass ""))))
8c8b8430
SM
90 (set url-basic-auth-storage
91 (cons (list server
ff51e86d 92 (cons file
8c8b8430
SM
93 (setq retval
94 (base64-encode-string
ab8dad36
CY
95 (format "%s:%s" user
96 (encode-coding-string pass 'utf-8))))))
8c8b8430
SM
97 (symbol-value url-basic-auth-storage))))
98 (byserv
ff51e86d 99 (setq retval (cdr-safe (assoc file byserv)))
8c8b8430 100 (if (and (not retval)
ff51e86d 101 (string-match "/" file))
8c8b8430
SM
102 (while (and byserv (not retval))
103 (setq data (car (car byserv)))
d7c2974d 104 (if (or (not (string-match "/" data)) ; It's a realm - take it!
8c8b8430 105 (and
ff51e86d
RS
106 (>= (length file) (length data))
107 (string= data (substring file 0 (length data)))))
8c8b8430
SM
108 (setq retval (cdr (car byserv))))
109 (setq byserv (cdr byserv))))
110 (if (or (and (not retval) prompt) overwrite)
111 (progn
8705576e 112 (setq user (or
97d1c236
TZ
113 (auth-source-user-or-password "login" server type)
114 (read-string (url-auth-user-prompt url realm)
115 (user-real-login-name)))
8705576e 116 pass (or
97d1c236
TZ
117 (auth-source-user-or-password "password" server type)
118 (read-passwd "Password: "))
8c8b8430
SM
119 retval (base64-encode-string (format "%s:%s" user pass))
120 byserv (assoc server (symbol-value url-basic-auth-storage)))
121 (setcdr byserv
ff51e86d 122 (cons (cons file retval) (cdr byserv))))))
8c8b8430
SM
123 (t (setq retval nil)))
124 (if retval (setq retval (concat "Basic " retval)))
125 retval))
126
127;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
128;;; Digest authorization code
129;;; ------------------------
130;;; This implements the DIGEST authorization type. See the internet draft
131;;; ftp://ds.internic.net/internet-drafts/draft-ietf-http-digest-aa-01.txt
132;;; for the complete documentation on this type.
133;;;
134;;; This is very secure
135;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
136(defvar url-digest-auth-storage nil
d1ce47b0
JB
137 "Where usernames and passwords are stored.
138Its value is an assoc list of assoc lists. The first assoc list is
139keyed by the server name. The cdr of this is an assoc list based
140on the 'directory' specified by the url we are looking up.")
8c8b8430
SM
141
142(defun url-digest-auth-create-key (username password realm method uri)
143 "Create a key for digest authentication method"
144 (let* ((info (if (stringp uri)
145 (url-generic-parse-url uri)
146 uri))
147 (a1 (md5 (concat username ":" realm ":" password)))
148 (a2 (md5 (concat method ":" (url-filename info)))))
149 (list a1 a2)))
150
151(defun url-digest-auth (url &optional prompt overwrite realm args)
152 "Get the username/password for the specified URL.
153If optional argument PROMPT is non-nil, ask for the username/password
d1ce47b0 154to use for the URL and its descendants. If optional third argument
8c8b8430
SM
155OVERWRITE is non-nil, overwrite the old username/password pair if it
156is found in the assoc list. If REALM is specified, use that as the realm
157instead of hostname:portnum."
158 (if args
159 (let* ((href (if (stringp url)
160 (url-generic-parse-url url)
161 url))
162 (server (url-host href))
97d1c236 163 (type (url-type href))
8c8b8430 164 (port (url-port href))
ff51e86d 165 (file (url-filename href))
8c8b8430 166 user pass byserv retval data)
ff51e86d 167 (setq file (cond
8c8b8430 168 (realm realm)
ff51e86d
RS
169 ((string-match "/$" file) file)
170 (t (url-file-directory file)))
8c8b8430
SM
171 server (format "%s:%d" server port)
172 byserv (cdr-safe (assoc server url-digest-auth-storage)))
173 (cond
174 ((and prompt (not byserv))
97d1c236
TZ
175 (setq user (or
176 (auth-source-user-or-password "login" server type)
177 (read-string (url-auth-user-prompt url realm)
178 (user-real-login-name)))
179 pass (or
180 (auth-source-user-or-password "password" server type)
181 (read-passwd "Password: "))
8c8b8430
SM
182 url-digest-auth-storage
183 (cons (list server
ff51e86d 184 (cons file
8c8b8430
SM
185 (setq retval
186 (cons user
187 (url-digest-auth-create-key
188 user pass realm
189 (or url-request-method "GET")
190 url)))))
191 url-digest-auth-storage)))
192 (byserv
ff51e86d 193 (setq retval (cdr-safe (assoc file byserv)))
8c8b8430 194 (if (and (not retval) ; no exact match, check directories
ff51e86d 195 (string-match "/" file)) ; not looking for a realm
8c8b8430
SM
196 (while (and byserv (not retval))
197 (setq data (car (car byserv)))
198 (if (or (not (string-match "/" data))
199 (and
ff51e86d
RS
200 (>= (length file) (length data))
201 (string= data (substring file 0 (length data)))))
8c8b8430
SM
202 (setq retval (cdr (car byserv))))
203 (setq byserv (cdr byserv))))
e652840b
JW
204 (if overwrite
205 (if (and (not retval) prompt)
8705576e 206 (setq user (or
97d1c236
TZ
207 (auth-source-user-or-password "login" server type)
208 (read-string (url-auth-user-prompt url realm)
209 (user-real-login-name)))
8705576e 210 pass (or
97d1c236
TZ
211 (auth-source-user-or-password "password" server type)
212 (read-passwd "Password: "))
e652840b
JW
213 retval (setq retval
214 (cons user
215 (url-digest-auth-create-key
216 user pass realm
217 (or url-request-method "GET")
218 url)))
219 byserv (assoc server url-digest-auth-storage))
8c8b8430 220 (setcdr byserv
ff51e86d 221 (cons (cons file retval) (cdr byserv))))))
8c8b8430
SM
222 (t (setq retval nil)))
223 (if retval
e652840b
JW
224 (if (cdr-safe (assoc "opaque" args))
225 (let ((nonce (or (cdr-safe (assoc "nonce" args)) "nonegiven"))
226 (opaque (cdr-safe (assoc "opaque" args))))
227 (format
228 (concat "Digest username=\"%s\", realm=\"%s\","
229 "nonce=\"%s\", uri=\"%s\","
230 "response=\"%s\", opaque=\"%s\"")
231 (nth 0 retval) realm nonce (url-filename href)
232 (md5 (concat (nth 1 retval) ":" nonce ":"
233 (nth 2 retval))) opaque))
234 (let ((nonce (or (cdr-safe (assoc "nonce" args)) "nonegiven")))
235 (format
236 (concat "Digest username=\"%s\", realm=\"%s\","
237 "nonce=\"%s\", uri=\"%s\","
238 "response=\"%s\"")
239 (nth 0 retval) realm nonce (url-filename href)
240 (md5 (concat (nth 1 retval) ":" nonce ":"
241 (nth 2 retval))))))))))
8c8b8430
SM
242
243(defvar url-registered-auth-schemes nil
244 "A list of the registered authorization schemes and various and sundry
245information associated with them.")
246
247;;;###autoload
248(defun url-get-authentication (url realm type prompt &optional args)
249 "Return an authorization string suitable for use in the WWW-Authenticate
250header in an HTTP/1.0 request.
251
252URL is the url you are requesting authorization to. This can be either a
253 string representing the URL, or the parsed representation returned by
254 `url-generic-parse-url'
255REALM is the realm at a specific site we are looking for. This should be a
256 string specifying the exact realm, or nil or the symbol 'any' to
257 specify that the filename portion of the URL should be used as the
258 realm
259TYPE is the type of authentication to be returned. This is either a string
260 representing the type (basic, digest, etc), or nil or the symbol 'any'
261 to specify that any authentication is acceptable. If requesting 'any'
262 the strongest matching authentication will be returned. If this is
d7c2974d 263 wrong, it's no big deal, the error from the server will specify exactly
8c8b8430
SM
264 what type of auth to use
265PROMPT is boolean - specifies whether to ask the user for a username/password
266 if one cannot be found in the cache"
267 (if (not realm)
268 (setq realm (cdr-safe (assoc "realm" args))))
269 (if (stringp url)
270 (setq url (url-generic-parse-url url)))
271 (if (or (null type) (eq type 'any))
272 ;; Whooo doogies!
273 ;; Go through and get _all_ the authorization strings that could apply
274 ;; to this URL, store them along with the 'rating' we have in the list
275 ;; of schemes, then sort them so that the 'best' is at the front of the
276 ;; list, then get the car, then get the cdr.
277 ;; Zooom zooom zoooooom
278 (cdr-safe
279 (car-safe
280 (sort
281 (mapcar
282 (function
283 (lambda (scheme)
284 (if (fboundp (car (cdr scheme)))
285 (cons (cdr (cdr scheme))
286 (funcall (car (cdr scheme)) url nil nil realm))
287 (cons 0 nil))))
288 url-registered-auth-schemes)
289 (function
290 (lambda (x y)
291 (cond
292 ((null (cdr x)) nil)
293 ((and (cdr x) (null (cdr y))) t)
294 ((and (cdr x) (cdr y))
295 (>= (car x) (car y)))
296 (t nil)))))))
297 (if (symbolp type) (setq type (symbol-name type)))
298 (let* ((scheme (car-safe
299 (cdr-safe (assoc (downcase type)
300 url-registered-auth-schemes)))))
301 (if (and scheme (fboundp scheme))
302 (funcall scheme url prompt
303 (and prompt
304 (funcall scheme url nil nil realm args))
305 realm args)))))
306
307;;;###autoload
308(defun url-register-auth-scheme (type &optional function rating)
309 "Register an HTTP authentication method.
310
d1ce47b0
JB
311TYPE is a string or symbol specifying the name of the method.
312 This should be the same thing you expect to get returned in
313 an Authenticate header in HTTP/1.0 - it will be downcased.
314FUNCTION is the function to call to get the authorization information.
315 This defaults to `url-?-auth', where ? is TYPE.
8c8b8430
SM
316RATING a rating between 1 and 10 of the strength of the authentication.
317 This is used when asking for the best authentication for a specific
318 URL. The item with the highest rating is returned."
319 (let* ((type (cond
320 ((stringp type) (downcase type))
321 ((symbolp type) (downcase (symbol-name type)))
322 (t (error "Bad call to `url-register-auth-scheme'"))))
323 (function (or function (intern (concat "url-" type "-auth"))))
324 (rating (cond
325 ((null rating) 2)
216d3806 326 ((stringp rating) (string-to-number rating))
8c8b8430
SM
327 (t rating)))
328 (node (assoc type url-registered-auth-schemes)))
329 (if (not (fboundp function))
330 (url-warn 'security
caae2fd8
SM
331 (format (concat
332 "Tried to register `%s' as an auth scheme"
333 ", but it is not a function!") function)))
8c8b8430
SM
334
335 (if node
336 (setcdr node (cons function rating))
337 (setq url-registered-auth-schemes
338 (cons (cons type (cons function rating))
339 url-registered-auth-schemes)))))
340
341(defun url-auth-registered (scheme)
3ecd3a56 342 "Return non-nil if SCHEME is registered as an auth type."
8c8b8430
SM
343 (assoc scheme url-registered-auth-schemes))
344
345(provide 'url-auth)
e5566bd5 346
00eef4de 347;;; url-auth.el ends here