]> code.delx.au - gnu-emacs-elpa/blob - packages/url-http-ntlm/url-http-ntlm.el
url-http-ntlm.el: Require ntlm 2.0.0
[gnu-emacs-elpa] / packages / url-http-ntlm / url-http-ntlm.el
1 ;;; url-http-ntlm.el --- NTLM authentication for the url library
2
3 ;; Copyright (C) 2008, 2015 Free Software Foundation, Inc.
4
5 ;; Author: Tom Schutzer-Weissmann <tom.weissmann@gmail.com>
6 ;; Maintainer: Thomas Fitzsimmons <fitzsim@fitzsim.org>
7 ;; Keywords: comm, data, processes, hypermedia
8 ;; Homepage: https://code.google.com/p/url-http-ntlm/
9 ;; Package-Requires: ((ntlm "2.0.0"))
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25 ;;
26 ;; This package provides a NTLM handler for the URL package.
27 ;;
28 ;; Installation:
29 ;;
30 ;; M-x package-install RET url-http-ntlm RET
31 ;;
32 ;; Acknowledgements:
33 ;;
34 ;; Taro Kawagishi <tarok@transpulse.org> wrote ntlm.el and md4.el,
35 ;; which are parts of FLIM (Faithful Library about Internet Message).
36 ;;
37 ;; http://stuff.mit.edu/afs/sipb/contrib/emacs/packages/flim-1.14.7/ntlm.el
38 ;; http://stuff.mit.edu/afs/sipb/contrib/emacs/packages/flim-1.14.7/md4.el
39
40 ;;; Code:
41 (require 'url-auth)
42 (require 'url-http)
43 (require 'mail-parse)
44 (require 'cl-lib)
45 (require 'ntlm)
46
47 ;; Remove authorization after redirect.
48 (when (and (boundp 'emacs-major-version)
49 (< emacs-major-version 25))
50 (require (intern (format "url-http-ntlm-parse-headers-%d.%d"
51 emacs-major-version
52 emacs-minor-version))))
53
54 \f
55 ;;; Private variables.
56 (defvar url-http-ntlm--auth-storage nil
57 "Authentication storage.
58 An alist that maps a server name to a pair of \(<username> <ntlm
59 hashes>\).
60
61 The hashes are built using `ntlm-get-password-hashes'.")
62
63 (defvar url-http-ntlm--last-args nil
64 "Stores the last `url-http-ntlm--get-stage' arguments and return value.
65 This is used to detect multiple calls.")
66 (make-variable-buffer-local 'url-http-ntlm--last-args)
67
68 (defvar url-http-ntlm--loop-timer-counter nil
69 "A hash table used to detect NTLM negotiation errors.
70 Keys are urls, entries are (START-TIME . COUNTER).")
71
72 (defvar url-http-ntlm--default-users nil
73 "An alist that maps each server to the default username for
74 that server.")
75
76 \f
77 ;;; Private functions.
78 (defun url-http-ntlm--detect-loop (url)
79 "Detect potential infinite loop when NTLM fails on URL."
80 (when (not url-http-ntlm--loop-timer-counter)
81 (setq url-http-ntlm--loop-timer-counter (make-hash-table :test 'equal)))
82 (let* ((url-string (url-recreate-url url))
83 (last-entry (gethash url-string url-http-ntlm--loop-timer-counter))
84 (start-time (car last-entry))
85 (counter (cdr last-entry)))
86 (if last-entry
87 (progn
88 (if (< (- (float-time) start-time) 10.0)
89 (if (< counter 20)
90 ;; Still within time window, so increment count.
91 (puthash url-string (cons start-time (1+ counter))
92 url-http-ntlm--loop-timer-counter)
93 ;; Error detected, so remove entry and clear.
94 (url-http-ntlm--authorization url-string :clear)
95 (remhash url-string url-http-ntlm--loop-timer-counter)
96 (error
97 (format (concat "Access rate to %s is too high,"
98 " indicating an NTLM failure;"
99 " to debug, re-run with url-debug set to 1")
100 url-string)))
101 ;; Timeout expired, so reset counter.
102 (puthash url-string (cons (float-time) 0)
103 url-http-ntlm--loop-timer-counter)))
104 ;; New access, so initialize counter to 0.
105 (puthash url-string (cons (float-time) 0)
106 url-http-ntlm--loop-timer-counter))))
107
108 (defun url-http-ntlm--ensure-user (url)
109 "Return URL with its user slot set.
110 If URL's user slot is nil, set it to the last user that made a
111 request to the host in URL's server slot."
112 (let ((new-url url))
113 (if (url-user new-url)
114 new-url
115 (setf (url-user new-url)
116 (cdr (assoc (url-host new-url) url-http-ntlm--default-users)))
117 new-url)))
118
119 (defun url-http-ntlm--ensure-keepalive ()
120 "Report an error if `url-http-attempt-keepalives' is not set."
121 (cl-assert url-http-attempt-keepalives
122 nil
123 (concat "NTLM authentication won't work unless"
124 " `url-http-attempt-keepalives' is set!")))
125
126 (defun url-http-ntlm--clean-headers ()
127 "Remove Authorization element from `url-http-extra-headers' alist."
128 (cl-declare (special url-http-extra-headers))
129 (setq url-http-extra-headers
130 (url-http-ntlm--rmssoc "Authorization" url-http-extra-headers)))
131
132 (defun url-http-ntlm--get-stage (args)
133 "Determine what stage of the NTLM handshake we are at.
134 PROMPT and ARGS come from `url-ntlm-auth''s caller,
135 `url-get-authentication'. Their meaning depends on the current
136 implementation - this function is well and truly coupled.
137
138 url-get-authentication' calls `url-ntlm-auth' once when checking
139 what authentication schemes are supported (PROMPT and ARGS are
140 nil), and then twice for every stage of the handshake: the first
141 time PROMPT is nil, the second, t; ARGS contains the server
142 response's \"WWW-Authenticate\" header, munged by
143 `url-parse-args'."
144 (cl-declare (special url-http-extra-headers))
145 (let* ((response-rxp "^NTLM TlRMTVNTUAADAAA")
146 (challenge-rxp "^TLRMTVNTUAACAAA")
147 (auth-header (assoc "Authorization" url-http-extra-headers))
148 (case-fold-search t)
149 stage)
150 (if (eq args (car url-http-ntlm--last-args))
151 ;; multiple calls, return the same argument we returned last time
152 (cdr url-http-ntlm--last-args)
153 (let ((stage
154 (cond ((and auth-header (string-match response-rxp
155 (cdr auth-header)))
156 :error)
157 ((and (= (length args) 2)
158 (cl-destructuring-bind (challenge ntlm) args
159 (and (string-equal "ntlm" (car ntlm))
160 (string-match challenge-rxp
161 (car challenge)))))
162 :response)
163 (t
164 :request))))
165 (url-http-ntlm--clean-headers)
166 (setq url-http-ntlm--last-args (cons args stage))
167 stage))))
168
169 (defun url-http-ntlm--authorization (url &optional clear realm)
170 "Get or clear NTLM authentication details for URL.
171 If CLEAR is non-nil, clear any saved credentials for server.
172 Otherwise, return the credentials, prompting the user if
173 necessary. REALM appears in the prompt.
174
175 If URL contains a username and a password, they are used and
176 stored credentials are not affected."
177 (let* ((href (if (stringp url)
178 (url-generic-parse-url url)
179 url))
180 (type (url-type href))
181 (user (url-user href))
182 (server (url-host href))
183 (port (url-portspec href))
184 (pass (url-password href))
185 (stored (assoc (list type user server port)
186 url-http-ntlm--auth-storage))
187 (both (and user pass)))
188 (if clear
189 ;; clear
190 (unless both
191 (setq url-http-ntlm--default-users
192 (url-http-ntlm--rmssoc server url-http-ntlm--default-users))
193 (setq url-http-ntlm--auth-storage
194 (url-http-ntlm--rmssoc '(type user* server port)
195 url-http-ntlm--auth-storage))
196 nil)
197 ;; get
198 (if (or both
199 (and stored user (not (equal user (cl-second (car stored)))))
200 (not stored))
201 (let* ((user* (or user
202 (url-do-auth-source-search server type :user)
203 (read-string (url-auth-user-prompt url realm)
204 (or user (user-real-login-name)))))
205 (pass* (if both
206 pass
207 (or (url-do-auth-source-search server type :secret)
208 (read-passwd (format "Password [for %s]: "
209 (url-recreate-url url))))))
210 (key (list type user* server port))
211 (entry `(,key . (,(ntlm-get-password-hashes pass*)))))
212 (unless both
213 (setq url-http-ntlm--default-users
214 (cons
215 `(,server . ,user*)
216 (url-http-ntlm--rmssoc server
217 url-http-ntlm--default-users)))
218 (setq url-http-ntlm--auth-storage
219 (cons entry
220 (url-http-ntlm--rmssoc
221 key
222 url-http-ntlm--auth-storage))))
223 entry)
224 stored))))
225
226 (defun url-http-ntlm--get-challenge ()
227 "Return the NTLM Type-2 message in the WWW-Authenticate header, if present."
228 (save-restriction
229 (mail-narrow-to-head)
230 (let ((www-authenticate (mail-fetch-field "www-authenticate")))
231 (when (string-match "NTLM\\s-+\\(\\S-+\\)"
232 www-authenticate)
233 (base64-decode-string (match-string 1 www-authenticate))))))
234
235 (defun url-http-ntlm--rmssoc (key alist)
236 "Remove all elements whose `car' match KEY from ALIST."
237 (cl-remove key alist :key 'car :test 'equal))
238
239 (defun url-http-ntlm--string (data)
240 "Return DATA encoded as an NTLM string."
241 (concat "NTLM " (base64-encode-string data :nobreak)))
242
243 \f
244 ;;; Public function called by `url-get-authentication'.
245 ;;;###autoload
246 (defun url-ntlm-auth (url &optional prompt overwrite realm args)
247 "Return an NTLM HTTP authorization header.
248 Get the contents of the Authorization header for a HTTP response
249 using NTLM authentication, to access URL. Because NTLM is a
250 two-step process, this function expects to be called twice, first
251 to generate the NTLM type 1 message (request), then to respond to
252 the server's type 2 message (challenge) with a suitable response.
253
254 PROMPT, OVERWRITE, and REALM are ignored.
255
256 ARGS is expected to contain the WWW-Authentication header from
257 the server's last response. These are used by
258 `url-http-get-stage' to determine what stage we are at."
259 (url-http-ntlm--ensure-keepalive)
260 (let* ((user-url (url-http-ntlm--ensure-user url))
261 (stage (url-http-ntlm--get-stage args)))
262 (cl-case stage
263 ;; NTLM Type 1 message: the request
264 (:request
265 (url-http-ntlm--detect-loop user-url)
266 (cl-destructuring-bind (&optional key hash)
267 (url-http-ntlm--authorization user-url nil realm)
268 (when (cl-third key)
269 (url-http-ntlm--string
270 (ntlm-build-auth-request (cl-second key) (cl-third key))))))
271 ;; NTLM Type 3 message: the response
272 (:response
273 (url-http-ntlm--detect-loop user-url)
274 (let ((challenge (url-http-ntlm--get-challenge)))
275 (cl-destructuring-bind (key hash)
276 (url-http-ntlm--authorization user-url nil realm)
277 (url-http-ntlm--string
278 (ntlm-build-auth-response challenge
279 (cl-second key)
280 hash)))))
281 (:error
282 (url-http-ntlm--authorization user-url :clear)))))
283
284 \f
285 ;;; Register `url-ntlm-auth' HTTP authentication method.
286 ;;;###autoload
287 (url-register-auth-scheme "ntlm" nil 8)
288
289 (provide 'url-http-ntlm)
290
291 ;;; url-http-ntlm.el ends here