1 ;;; url-auth.el --- Uniform Resource Locator authorization modules
2 ;; Keywords: comm, data, processes, hypermedia
4 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5 ;;; Copyright (c) 1996 - 1999 Free Software Foundation, Inc.
7 ;;; This file is part of GNU Emacs.
9 ;;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;;; it under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 2, or (at your option)
12 ;;; any later version.
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.
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;;; Boston, MA 02111-1307, USA.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 (autoload 'url-warn "url")
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)))
37 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38 ;;; Basic authorization code
39 ;;; ------------------------
40 ;;; This implements the BASIC authorization type. See the online
42 ;;; http://www.w3.org/hypertext/WWW/AccessAuthorization/Basic.html
43 ;;; for the complete documentation on this type.
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.
50 Must be a symbol pointing to another variable that will actually store
51 the information. The value of this variable is an assoc list of assoc
52 lists. The first assoc list is keyed by the server name. The cdr of
53 this is an assoc list based on the 'directory' specified by the url we
56 (defun url-basic-auth (url &optional prompt overwrite realm args)
57 "Get the username/password for the specified URL.
58 If optional argument PROMPT is non-nil, ask for the username/password
59 to use for the url and its descendants. If optional third argument
60 OVERWRITE is non-nil, overwrite the old username/password pair if it
61 is found in the assoc list. If REALM is specified, use that as the realm
62 instead of the pathname inheritance method."
63 (let* ((href (if (stringp url)
64 (url-generic-parse-url url)
66 (server (url-host href))
67 (port (url-port href))
68 (path (url-filename href))
69 user pass byserv retval data)
70 (setq server (format "%s:%d" server port)
73 ((string-match "/$" path) path)
74 (t (url-basepath path)))
75 byserv (cdr-safe (assoc server
76 (symbol-value url-basic-auth-storage))))
78 ((and prompt (not byserv))
79 (setq user (read-string (url-auth-user-prompt url realm)
80 (user-real-login-name))
81 pass (read-passwd "Password: "))
82 (set url-basic-auth-storage
87 (format "%s:%s" user pass)))))
88 (symbol-value url-basic-auth-storage))))
90 (setq retval (cdr-safe (assoc path byserv)))
92 (string-match "/" path))
93 (while (and byserv (not retval))
94 (setq data (car (car byserv)))
95 (if (or (not (string-match "/" data)) ; Its a realm - take it!
97 (>= (length path) (length data))
98 (string= data (substring path 0 (length data)))))
99 (setq retval (cdr (car byserv))))
100 (setq byserv (cdr byserv))))
101 (if (or (and (not retval) prompt) overwrite)
103 (setq user (read-string (url-auth-user-prompt url realm)
104 (user-real-login-name))
105 pass (read-passwd "Password: ")
106 retval (base64-encode-string (format "%s:%s" user pass))
107 byserv (assoc server (symbol-value url-basic-auth-storage)))
109 (cons (cons path retval) (cdr byserv))))))
110 (t (setq retval nil)))
111 (if retval (setq retval (concat "Basic " retval)))
114 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
115 ;;; Digest authorization code
116 ;;; ------------------------
117 ;;; This implements the DIGEST authorization type. See the internet draft
118 ;;; ftp://ds.internic.net/internet-drafts/draft-ietf-http-digest-aa-01.txt
119 ;;; for the complete documentation on this type.
121 ;;; This is very secure
122 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
123 (defvar url-digest-auth-storage nil
124 "Where usernames and passwords are stored. Its value is an assoc list of
125 assoc lists. The first assoc list is keyed by the server name. The cdr of
126 this is an assoc list based on the 'directory' specified by the url we are
129 (defun url-digest-auth-create-key (username password realm method uri)
130 "Create a key for digest authentication method"
131 (let* ((info (if (stringp uri)
132 (url-generic-parse-url uri)
134 (a1 (md5 (concat username ":" realm ":" password)))
135 (a2 (md5 (concat method ":" (url-filename info)))))
138 (defun url-digest-auth (url &optional prompt overwrite realm args)
139 "Get the username/password for the specified URL.
140 If optional argument PROMPT is non-nil, ask for the username/password
141 to use for the url and its descendants. If optional third argument
142 OVERWRITE is non-nil, overwrite the old username/password pair if it
143 is found in the assoc list. If REALM is specified, use that as the realm
144 instead of hostname:portnum."
146 (let* ((href (if (stringp url)
147 (url-generic-parse-url url)
149 (server (url-host href))
150 (port (url-port href))
151 (path (url-filename href))
152 user pass byserv retval data)
155 ((string-match "/$" path) path)
156 (t (url-basepath path)))
157 server (format "%s:%d" server port)
158 byserv (cdr-safe (assoc server url-digest-auth-storage)))
160 ((and prompt (not byserv))
161 (setq user (read-string (url-auth-user-prompt url realm)
162 (user-real-login-name))
163 pass (read-passwd "Password: ")
164 url-digest-auth-storage
169 (url-digest-auth-create-key
171 (or url-request-method "GET")
173 url-digest-auth-storage)))
175 (setq retval (cdr-safe (assoc path byserv)))
176 (if (and (not retval) ; no exact match, check directories
177 (string-match "/" path)) ; not looking for a realm
178 (while (and byserv (not retval))
179 (setq data (car (car byserv)))
180 (if (or (not (string-match "/" data))
182 (>= (length path) (length data))
183 (string= data (substring path 0 (length data)))))
184 (setq retval (cdr (car byserv))))
185 (setq byserv (cdr byserv))))
186 (if (or (and (not retval) prompt) overwrite)
188 (setq user (read-string (url-auth-user-prompt url realm)
189 (user-real-login-name))
190 pass (read-passwd "Password: ")
193 (url-digest-auth-create-key
195 (or url-request-method "GET")
197 byserv (assoc server url-digest-auth-storage))
199 (cons (cons path retval) (cdr byserv))))))
200 (t (setq retval nil)))
202 (let ((nonce (or (cdr-safe (assoc "nonce" args)) "nonegiven"))
203 (opaque (or (cdr-safe (assoc "opaque" args)) "nonegiven")))
205 (concat "Digest username=\"%s\", realm=\"%s\","
206 "nonce=\"%s\", uri=\"%s\","
207 "response=\"%s\", opaque=\"%s\"")
208 (nth 0 retval) realm nonce (url-filename href)
209 (md5 (concat (nth 1 retval) ":" nonce ":"
210 (nth 2 retval))) opaque))))))
212 (defvar url-registered-auth-schemes nil
213 "A list of the registered authorization schemes and various and sundry
214 information associated with them.")
217 (defun url-get-authentication (url realm type prompt &optional args)
218 "Return an authorization string suitable for use in the WWW-Authenticate
219 header in an HTTP/1.0 request.
221 URL is the url you are requesting authorization to. This can be either a
222 string representing the URL, or the parsed representation returned by
223 `url-generic-parse-url'
224 REALM is the realm at a specific site we are looking for. This should be a
225 string specifying the exact realm, or nil or the symbol 'any' to
226 specify that the filename portion of the URL should be used as the
228 TYPE is the type of authentication to be returned. This is either a string
229 representing the type (basic, digest, etc), or nil or the symbol 'any'
230 to specify that any authentication is acceptable. If requesting 'any'
231 the strongest matching authentication will be returned. If this is
232 wrong, its no big deal, the error from the server will specify exactly
233 what type of auth to use
234 PROMPT is boolean - specifies whether to ask the user for a username/password
235 if one cannot be found in the cache"
237 (setq realm (cdr-safe (assoc "realm" args))))
239 (setq url (url-generic-parse-url url)))
240 (if (or (null type) (eq type 'any))
242 ;; Go through and get _all_ the authorization strings that could apply
243 ;; to this URL, store them along with the 'rating' we have in the list
244 ;; of schemes, then sort them so that the 'best' is at the front of the
245 ;; list, then get the car, then get the cdr.
246 ;; Zooom zooom zoooooom
253 (if (fboundp (car (cdr scheme)))
254 (cons (cdr (cdr scheme))
255 (funcall (car (cdr scheme)) url nil nil realm))
257 url-registered-auth-schemes)
262 ((and (cdr x) (null (cdr y))) t)
263 ((and (cdr x) (cdr y))
264 (>= (car x) (car y)))
266 (if (symbolp type) (setq type (symbol-name type)))
267 (let* ((scheme (car-safe
268 (cdr-safe (assoc (downcase type)
269 url-registered-auth-schemes)))))
270 (if (and scheme (fboundp scheme))
271 (funcall scheme url prompt
273 (funcall scheme url nil nil realm args))
277 (defun url-register-auth-scheme (type &optional function rating)
278 "Register an HTTP authentication method.
280 TYPE is a string or symbol specifying the name of the method. This
281 should be the same thing you expect to get returned in an Authenticate
282 header in HTTP/1.0 - it will be downcased.
283 FUNCTION is the function to call to get the authorization information. This
284 defaults to `url-?-auth', where ? is TYPE
285 RATING a rating between 1 and 10 of the strength of the authentication.
286 This is used when asking for the best authentication for a specific
287 URL. The item with the highest rating is returned."
289 ((stringp type) (downcase type))
290 ((symbolp type) (downcase (symbol-name type)))
291 (t (error "Bad call to `url-register-auth-scheme'"))))
292 (function (or function (intern (concat "url-" type "-auth"))))
295 ((stringp rating) (string-to-int rating))
297 (node (assoc type url-registered-auth-schemes)))
298 (if (not (fboundp function))
301 "Tried to register `%s' as an auth scheme"
302 ", but it is not a function!") function)))
305 (setcdr node (cons function rating))
306 (setq url-registered-auth-schemes
307 (cons (cons type (cons function rating))
308 url-registered-auth-schemes)))))
310 (defun url-auth-registered (scheme)
311 ;; Return non-nil iff SCHEME is registered as an auth type
312 (assoc scheme url-registered-auth-schemes))
316 ;;; arch-tag: 04058625-616d-44e4-9dbf-4b46b00b2a91