url-auth.el 13 KB

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