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