(gnus-blocked-images): Clarify privacy implications
[emacs.git] / lisp / url / url-auth.el
blob67e701ecb1661f65ceee1dc769ca03133eaea205
1 ;;; url-auth.el --- Uniform Resource Locator authorization modules -*- lexical-binding: t -*-
3 ;; Copyright (C) 1996-1999, 2004-2018 Free Software Foundation, Inc.
5 ;; Keywords: comm, data, processes, hypermedia
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 3 of the License, or
12 ;; (at your option) 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. If not, see <https://www.gnu.org/licenses/>.
22 ;;; Code:
24 (require 'url-vars)
25 (require 'url-parse)
26 (autoload 'url-warn "url")
27 (autoload 'auth-source-search "auth-source")
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 "]: "))
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.
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
54 are looking up.")
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 filename inheritance method."
63 (let* ((href (if (stringp url)
64 (url-generic-parse-url url)
65 url))
66 (server (url-host href))
67 (type (url-type href))
68 (port (url-port href))
69 (file (url-filename href))
70 (user (url-user href))
71 (pass (url-password href))
72 (enable-recursive-minibuffers t) ; for url-handler-mode (bug#10298)
73 byserv retval data)
74 (setq server (format "%s:%d" server port)
75 file (cond
76 (realm realm)
77 ((string= "" file) "/")
78 ((string-match "/$" file) file)
79 (t (url-file-directory file)))
80 byserv (cdr-safe (assoc server
81 (symbol-value url-basic-auth-storage))))
82 (cond
83 ((and user pass)
84 ;; Explicit http://user:pass@foo/ URL. Just return the credentials.
85 (setq retval (base64-encode-string (format "%s:%s" user pass))))
86 ((and prompt (not byserv))
87 (setq user (or
88 (url-do-auth-source-search server type :user)
89 (read-string (url-auth-user-prompt url realm)
90 (or user (user-real-login-name))))
91 pass (or
92 (url-do-auth-source-search server type :secret)
93 (read-passwd "Password: " nil (or pass ""))))
94 (set url-basic-auth-storage
95 (cons (list server
96 (cons file
97 (setq retval
98 (base64-encode-string
99 (format "%s:%s" user
100 (encode-coding-string pass 'utf-8))))))
101 (symbol-value url-basic-auth-storage))))
102 (byserv
103 (setq retval (cdr-safe (assoc file byserv)))
104 (if (and (not retval)
105 (string-match "/" file))
106 (while (and byserv (not retval))
107 (setq data (car (car byserv)))
108 (if (or (not (string-match "/" data)) ; It's a realm - take it!
109 (and
110 (>= (length file) (length data))
111 (string= data (substring file 0 (length data)))))
112 (setq retval (cdr (car byserv))))
113 (setq byserv (cdr byserv))))
114 (if (or (and (not retval) prompt) overwrite)
115 (progn
116 (setq user (or
117 (url-do-auth-source-search server type :user)
118 (read-string (url-auth-user-prompt url realm)
119 (user-real-login-name)))
120 pass (or
121 (url-do-auth-source-search server type :secret)
122 (read-passwd "Password: "))
123 retval (base64-encode-string (format "%s:%s" user pass))
124 byserv (assoc server (symbol-value url-basic-auth-storage)))
125 (setcdr byserv
126 (cons (cons file retval) (cdr byserv))))))
127 (t (setq retval nil)))
128 (if retval (setq retval (concat "Basic " retval)))
129 retval))
131 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
132 ;;; Digest authorization code
133 ;;; ------------------------
134 ;;; This implements the DIGEST authorization type. See RFC 2617
135 ;;; https://www.ietf.org/rfc/rfc2617.txt
136 ;;; for the complete documentation on this type.
138 ;;; This is very secure
139 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
140 (defvar url-digest-auth-storage nil
141 "Where usernames and passwords are stored.
142 Its value is an assoc list of assoc lists. The first assoc list is
143 keyed by the server name. The cdr of this is an assoc list based
144 on the \"directory\" specified by the url we are looking up.")
146 (defsubst url-digest-auth-colonjoin (&rest args)
147 "Concatenate ARGS as strings with colon as a separator."
148 (mapconcat 'identity args ":"))
150 (defsubst url-digest-auth-kd (data secret)
151 "Apply digest algorithm to DATA using SECRET and return the result."
152 (md5 (url-digest-auth-colonjoin secret data)))
154 (defsubst url-digest-auth-make-ha1 (user realm password)
155 "Compute checksum out of strings USER, REALM, and PASSWORD."
156 (md5 (url-digest-auth-colonjoin user realm password)))
158 (defsubst url-digest-auth-make-ha2 (method digest-uri)
159 "Compute checksum out of strings METHOD and DIGEST-URI."
160 (md5 (url-digest-auth-colonjoin method digest-uri)))
162 (defsubst url-digest-auth-make-request-digest (ha1 ha2 nonce)
163 "Construct the request-digest from hash strings HA1, HA2, and NONCE.
164 This is the value that server receives as a proof that user knows
165 a password."
166 (url-digest-auth-kd (url-digest-auth-colonjoin nonce ha2) ha1))
168 (defsubst url-digest-auth-make-request-digest-qop (qop ha1 ha2 nonce nc cnonce)
169 "Construct the request-digest with qop.
170 QOP describes the \"quality of protection\" and algorithm to use.
171 All of the strings QOP, HA1, HA2, NONCE, NC, and CNONCE are
172 combined into a single hash value that proves to a server the
173 user knows a password. It's worth noting that HA2 already
174 depends on value of QOP."
175 (url-digest-auth-kd (url-digest-auth-colonjoin
176 nonce nc cnonce qop ha2) ha1))
178 (defsubst url-digest-auth-directory-id (url realm)
179 "Make an identifier for selecting a key in key cache.
180 The identifier is made either from URL or REALM. It represents a
181 protection space within a server so that one server can have
182 multiple authorizations."
183 (or realm (or (url-file-directory (url-filename url)) "/")))
185 (defsubst url-digest-auth-server-id (url)
186 "Make an identifier for selecting a server in key cache.
187 The identifier is made from URL's host and port. Together with
188 `url-digest-auth-directory-id' these identify a single key in the
189 key cache `url-digest-auth-storage'."
190 (format "%s:%d" (url-host url) (url-port url)))
192 (defun url-digest-auth-make-cnonce ()
193 "Compute a new unique client nonce value."
194 (base64-encode-string
195 (apply 'format "%016x%04x%04x%05x%05x" (random) (current-time)) t))
197 (defun url-digest-auth-nonce-count (_nonce)
198 "The number requests sent to server with the given NONCE.
199 This count includes the request we're preparing here.
201 Currently, this is not implemented and will always return 1.
203 Value returned is in string format with leading zeroes, such as
204 \"00000001\"."
205 (format "%08x" 1))
207 (defun url-digest-auth-name-value-string (pairs)
208 "Concatenate name-value pairs in association list PAIRS.
210 Output is formatted as \"name1=\\\"value1\\\", name2=\\\"value2\\\", ...\""
211 (mapconcat (lambda (pair)
212 (format "%s=\"%s\""
213 (symbol-name (car pair))
214 (cdr pair)))
215 pairs ", "))
217 (defun url-digest-auth-source-creds (url)
218 "Find credentials for URL object from the Emacs auth-source.
219 Return value is a plist that has `:user' and `:secret' properties
220 if credentials were found. Otherwise nil."
221 (let ((server (url-digest-auth-server-id url))
222 (type (url-type url)))
223 (list :user (url-do-auth-source-search server type :user)
224 :secret (url-do-auth-source-search server type :secret))))
226 (defun url-digest-prompt-creds (url realm &optional creds)
227 "Prompt credentials for URL and REALM, defaulting to CREDS.
228 CREDS is a plist that may have properties `:user' and `:secret'."
229 ;; Set explicitly in case creds were nil. This makes the second
230 ;; plist-put modify the same plist.
231 (setq creds
232 (plist-put creds :user
233 (read-string (url-auth-user-prompt url realm)
234 (or (plist-get creds :user)
235 (user-real-login-name)))))
236 (plist-put creds :secret
237 (read-passwd "Password: " nil (plist-get creds :secret))))
239 (defun url-digest-auth-directory-id-assoc (dirkey keylist)
240 "Find the best match for DIRKEY in key alist KEYLIST.
242 The string DIRKEY should be obtained using
243 `url-digest-auth-directory-id'. The key list to search through
244 is the alist KEYLIST where car of each element may match DIRKEY.
245 If DIRKEY represents a realm, the list is searched only for an
246 exact match. For directory names, an ancestor is sufficient for
247 a match."
249 ;; Check exact match first.
250 (assoc dirkey keylist)
251 ;; No exact match found. Continue to look for partial match if
252 ;; dirkey is not a realm.
253 (and (string-match "/" dirkey)
254 (let (match)
255 (while (and (null match) keylist)
256 (if (or
257 ;; Any realm candidate matches. Why?
258 (not (string-match "/" (caar keylist)))
259 ;; Parent directory matches.
260 (string-prefix-p (caar keylist) dirkey))
261 (setq match (car keylist))
262 (setq keylist (cdr keylist))))
263 match))))
265 (defun url-digest-cached-key (url realm)
266 "Find best match for URL and REALM from `url-digest-auth-storage'.
267 The return value is a list consisting of a realm (or a directory)
268 a user name, and hashed authentication tokens HA1 and HA2.
269 Modifying the contents of the returned list will modify the cache
270 variable `url-digest-auth-storage' itself."
271 (url-digest-auth-directory-id-assoc
272 (url-digest-auth-directory-id url realm)
273 (cdr (assoc (url-digest-auth-server-id url) url-digest-auth-storage))))
275 (defun url-digest-cache-key (key url)
276 "Add key to `url-digest-auth-storage'.
277 KEY has the same format as returned by `url-digest-cached-key'.
278 The key is added to cache hierarchy under server id, deduced from
279 URL."
280 (let ((serverid (url-digest-auth-server-id url)))
281 (push (list serverid key) url-digest-auth-storage)))
283 (defun url-digest-auth-create-key (username password realm method uri)
284 "Create a key for digest authentication method.
285 The USERNAME and PASSWORD are the credentials for REALM and are
286 used in making a hashed value named HA1. The HTTP METHOD and URI
287 makes a second hashed value HA2. These hashes are used in making
288 the authentication key that can be stored without saving the
289 password in plain text. The return value is a list (HA1 HA2).
291 For backward compatibility, URI is allowed to be a URL cl-struct
292 object."
293 (and username password realm
294 (list (url-digest-auth-make-ha1 username realm password)
295 (url-digest-auth-make-ha2 method (cond ((stringp uri) uri)
296 (t (url-filename uri)))))))
298 (defun url-digest-auth-build-response (key url realm attrs)
299 "Compute authorization string for the given challenge using KEY.
301 The string looks like 'Digest username=\"John\", realm=\"The
302 Realm\", ...'
304 Part of the challenge is already solved in a pre-computed KEY
305 which is list of a realm (or a directory), user name, and hash
306 tokens HA1 and HA2.
308 Some fields are filled as is from the given URL, REALM, and
309 using the contents of alist ATTRS.
311 ATTRS is expected to contain at least the server's \"nonce\"
312 value. It also might contain the optional \"opaque\" value.
313 Newer implementations conforming to RFC 2617 should also contain
314 qop (Quality Of Protection) and related attributes.
316 Restrictions on Quality of Protection scheme: The qop value
317 \"auth-int\" or algorithm any other than \"MD5\" are not
318 implemented."
320 (when key
321 (let ((user (nth 1 key))
322 (ha1 (nth 2 key))
323 (ha2 (nth 3 key))
324 (digest-uri (url-filename url))
325 (qop (cdr-safe (assoc "qop" attrs)))
326 (nonce (cdr-safe (assoc "nonce" attrs)))
327 (opaque (cdr-safe (assoc "opaque" attrs))))
329 (concat
330 "Digest "
331 (url-digest-auth-name-value-string
332 (append (list (cons 'username user)
333 (cons 'realm realm)
334 (cons 'nonce nonce)
335 (cons 'uri digest-uri))
337 (cond
338 ((null qop)
339 (list (cons 'response (url-digest-auth-make-request-digest
340 ha1 ha2 nonce))))
341 ((string= qop "auth")
342 (let ((nc (url-digest-auth-nonce-count nonce))
343 (cnonce (url-digest-auth-make-cnonce)))
344 (list (cons 'qop qop)
345 (cons 'nc nc)
346 (cons 'cnonce cnonce)
347 (cons 'response
348 (url-digest-auth-make-request-digest-qop
349 qop ha1 ha2 nonce nc cnonce)))))
350 (t (message "Quality of protection \"%s\" is not implemented." qop)
351 nil))
354 (if opaque (list (cons 'opaque opaque)))))))))
356 (defun url-digest-find-creds (url prompt &optional realm)
357 "Find or ask credentials for URL.
359 Primary method for finding credentials is from Emacs auth-source.
360 If password isn't found, and PROMPT is non-nil, query credentials
361 via minibuffer. Optional REALM may be used when prompting as a
362 hint to the user.
364 Return value is nil in case either user name or password wasn't
365 found. Otherwise, it's a plist containing `:user' and `:secret'.
366 Additional `:source' property denotes the origin of the
367 credentials and its value can be either symbol `authsource' or
368 `interactive'."
369 (let ((creds (url-digest-auth-source-creds url)))
371 ;; If credentials weren't found and prompting is allowed, prompt
372 ;; the user.
373 (if (and prompt
374 (or (null creds)
375 (null (plist-get creds :secret))))
376 (progn
377 (setq creds (url-digest-prompt-creds url realm creds))
378 (plist-put creds :source 'interactive))
379 (plist-put creds :source 'authsource))
381 (and (plist-get creds :user)
382 (plist-get creds :secret)
383 creds)))
385 (defun url-digest-find-new-key (url realm prompt)
386 "Find credentials and create a new authorization key for given URL and REALM.
388 Return value is the new key, or nil if credentials weren't found.
389 \"New\" in this context means a key that's not yet found in cache
390 variable `url-digest-auth-storage'. You may use `url-digest-cache-key'
391 to put it there.
393 This function uses `url-digest-find-creds' to find the
394 credentials. It first looks in auth-source. If not found, and
395 PROMPT is non-nil, user is asked for credentials interactively
396 via minibuffer."
397 (let (creds)
398 (unwind-protect
399 (if (setq creds (url-digest-find-creds url prompt realm))
400 (cons (url-digest-auth-directory-id url realm)
401 (cons (plist-get creds :user)
402 (url-digest-auth-create-key
403 (plist-get creds :user)
404 (plist-get creds :secret)
405 realm
406 (or url-request-method "GET")
407 (url-filename url)))))
408 (if (and creds
409 ;; Don't clear secret for `authsource' since it will
410 ;; corrupt any future fetches for it.
411 (not (eq (plist-get creds :source) 'authsource)))
412 (clear-string (plist-get creds :secret))))))
414 (defun url-digest-auth (url &optional prompt overwrite realm attrs)
415 "Get the HTTP Digest response string for the specified URL.
417 If optional argument PROMPT is non-nil, ask for the username and
418 password to use for the URL and its descendants but only if one
419 cannot be found from cache. Look also in Emacs auth-source.
421 If optional third argument OVERWRITE is non-nil, overwrite the
422 old credentials, if they're found in cache, with new ones from
423 user prompt or from Emacs auth-source.
425 If REALM is specified, use that instead of the URL descendant
426 method to match cached credentials.
428 Alist ATTRS contains additional attributes for the authentication
429 challenge such as nonce and opaque."
430 (if attrs
431 (let* ((href (if (stringp url) (url-generic-parse-url url) url))
432 (enable-recursive-minibuffers t)
433 (key (url-digest-cached-key href realm)))
435 (if (or (null key) overwrite)
436 (let ((newkey (url-digest-find-new-key href realm (cond
437 (key nil)
438 (t prompt)))))
439 (if (and newkey key overwrite)
440 (setcdr key (cdr newkey))
441 (if (and newkey (null key))
442 (url-digest-cache-key (setq key newkey) href)))))
444 (if key
445 (url-digest-auth-build-response key href realm attrs)))))
447 (defvar url-registered-auth-schemes nil
448 "A list of the registered authorization schemes and various and sundry
449 information associated with them.")
451 (defun url-do-auth-source-search (server type parameter)
452 (let* ((auth-info (auth-source-search :max 1 :host server :port type))
453 (auth-info (nth 0 auth-info))
454 (token (plist-get auth-info parameter))
455 (token (if (functionp token) (funcall token) token)))
456 token))
458 ;;;###autoload
459 (defun url-get-authentication (url realm type prompt &optional args)
460 "Return an authorization string suitable for use in the WWW-Authenticate
461 header in an HTTP/1.0 request.
463 URL is the url you are requesting authorization to. This can be either a
464 string representing the URL, or the parsed representation returned by
465 `url-generic-parse-url'
466 REALM is the realm at a specific site we are looking for. This should be a
467 string specifying the exact realm, or nil or the symbol `any' to
468 specify that the filename portion of the URL should be used as the
469 realm
470 TYPE is the type of authentication to be returned. This is either a string
471 representing the type (basic, digest, etc), or nil or the symbol `any'
472 to specify that any authentication is acceptable. If requesting `any'
473 the strongest matching authentication will be returned. If this is
474 wrong, it's no big deal, the error from the server will specify exactly
475 what type of auth to use
476 PROMPT is boolean - specifies whether to ask the user for a username/password
477 if one cannot be found in the cache"
478 (if (not realm)
479 (setq realm (cdr-safe (assoc "realm" args))))
480 (if (stringp url)
481 (setq url (url-generic-parse-url url)))
482 (if (or (null type) (eq type 'any))
483 ;; Whooo doogies!
484 ;; Go through and get _all_ the authorization strings that could apply
485 ;; to this URL, store them along with the 'rating' we have in the list
486 ;; of schemes, then sort them so that the 'best' is at the front of the
487 ;; list, then get the car, then get the cdr.
488 ;; Zooom zooom zoooooom
489 (cdr-safe
490 (car-safe
491 (sort
492 (mapcar
493 (function
494 (lambda (scheme)
495 (if (fboundp (car (cdr scheme)))
496 (cons (cdr (cdr scheme))
497 (funcall (car (cdr scheme)) url nil nil realm))
498 (cons 0 nil))))
499 url-registered-auth-schemes)
500 (function
501 (lambda (x y)
502 (cond
503 ((null (cdr x)) nil)
504 ((and (cdr x) (null (cdr y))) t)
505 ((and (cdr x) (cdr y))
506 (>= (car x) (car y)))
507 (t nil)))))))
508 (if (symbolp type) (setq type (symbol-name type)))
509 (let* ((scheme (car-safe
510 (cdr-safe (assoc (downcase type)
511 url-registered-auth-schemes)))))
512 (if (and scheme (fboundp scheme))
513 (funcall scheme url prompt
514 (and prompt
515 (funcall scheme url nil nil realm args))
516 realm args)))))
518 ;;;###autoload
519 (defun url-register-auth-scheme (type &optional function rating)
520 "Register an HTTP authentication method.
522 TYPE is a string or symbol specifying the name of the method.
523 This should be the same thing you expect to get returned in
524 an Authenticate header in HTTP/1.0 - it will be downcased.
525 FUNCTION is the function to call to get the authorization information.
526 This defaults to `url-?-auth', where ? is TYPE.
527 RATING a rating between 1 and 10 of the strength of the authentication.
528 This is used when asking for the best authentication for a specific
529 URL. The item with the highest rating is returned."
530 (let* ((type (cond
531 ((stringp type) (downcase type))
532 ((symbolp type) (downcase (symbol-name type)))
533 (t (error "Bad call to `url-register-auth-scheme'"))))
534 (function (or function (intern (concat "url-" type "-auth"))))
535 (rating (cond
536 ((null rating) 2)
537 ((stringp rating) (string-to-number rating))
538 (t rating)))
539 (node (assoc type url-registered-auth-schemes)))
540 (if (not (fboundp function))
541 (url-warn
542 'security
543 (format-message
544 "Tried to register `%s' as an auth scheme, but it is not a function!"
545 function)))
546 (if node
547 (setcdr node (cons function rating))
548 (setq url-registered-auth-schemes
549 (cons (cons type (cons function rating))
550 url-registered-auth-schemes)))))
552 (defun url-auth-registered (scheme)
553 "Return non-nil if SCHEME is registered as an auth type."
554 (assoc scheme url-registered-auth-schemes))
556 (provide 'url-auth)
558 ;;; url-auth.el ends here