]> code.delx.au - gnu-emacs-elpa/blob - packages/url-http-ntlm/url-http-ntlm.el
url-http-ntlm: Remove limit of one username and password per server
[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 (setq url-http-extra-headers
127 (url-http-ntlm--rmssoc "Authorization" url-http-extra-headers)))
128
129 (defun url-http-ntlm--get-stage (args)
130 "Determine what stage of the NTLM handshake we are at.
131 PROMPT and ARGS come from `url-ntlm-auth''s caller,
132 `url-get-authentication'. Their meaning depends on the current
133 implementation - this function is well and truly coupled.
134
135 url-get-authentication' calls `url-ntlm-auth' once when checking
136 what authentication schemes are supported (PROMPT and ARGS are
137 nil), and then twice for every stage of the handshake: the first
138 time PROMPT is nil, the second, t; ARGS contains the server
139 response's \"WWW-Authenticate\" header, munged by
140 `url-parse-args'."
141 (let* ((response-rxp "^NTLM TlRMTVNTUAADAAA")
142 (challenge-rxp "^TLRMTVNTUAACAAA")
143 (auth-header (assoc "Authorization" url-http-extra-headers))
144 (case-fold-search t)
145 stage)
146 (if (eq args (car url-http-ntlm--last-args))
147 ;; multiple calls, return the same argument we returned last time
148 (cdr url-http-ntlm--last-args)
149 (let ((stage
150 (cond ((and auth-header (string-match response-rxp
151 (cdr auth-header)))
152 :error)
153 ((and (= (length args) 2)
154 (cl-destructuring-bind (challenge ntlm) args
155 (and (string-equal "ntlm" (car ntlm))
156 (string-match challenge-rxp
157 (car challenge)))))
158 :response)
159 (t
160 :request))))
161 (url-http-ntlm--clean-headers)
162 (setq url-http-ntlm--last-args (cons args stage))
163 stage))))
164
165 (defun url-http-ntlm--authorisation (url &optional clear realm)
166 "Get or clear NTLM authentication details for URL.
167 If CLEAR is non-nil, clear any saved credentials for server.
168 Otherwise, return the credentials, prompting the user if
169 necessary. REALM appears in the prompt.
170
171 If URL contains a username and a password, they are used and
172 stored credentials are not affected."
173 (let* ((href (if (stringp url)
174 (url-generic-parse-url url)
175 url))
176 (type (url-type href))
177 (user (url-user href))
178 (server (url-host href))
179 (port (url-portspec href))
180 (pass (url-password href))
181 (stored (assoc (list type user server port)
182 url-http-ntlm--auth-storage))
183 (both (and user pass)))
184 (if clear
185 ;; clear
186 (unless both
187 (setq url-http-ntlm--default-users
188 (url-http-ntlm--rmssoc server url-http-ntlm--default-users))
189 (setq url-http-ntlm--auth-storage
190 (url-http-ntlm--rmssoc '(type user* server port)
191 url-http-ntlm--auth-storage))
192 nil)
193 ;; get
194 (if (or both
195 (and stored user (not (equal user (cl-second (car stored)))))
196 (not stored))
197 (let* ((user* (or user
198 (read-string (url-auth-user-prompt url realm)
199 (or user (user-real-login-name)))))
200 (pass* (if both
201 pass
202 (read-passwd (format "Password [for %s]: "
203 (url-recreate-url url)))))
204 (key (list type user* server port))
205 (entry `(,key . (,(ntlm-get-password-hashes pass*)))))
206 (unless both
207 (setq url-http-ntlm--default-users
208 (cons
209 `(,server . ,user*)
210 (url-http-ntlm--rmssoc server
211 url-http-ntlm--default-users)))
212 (setq url-http-ntlm--auth-storage
213 (cons entry
214 (url-http-ntlm--rmssoc
215 key
216 url-http-ntlm--auth-storage))))
217 entry)
218 stored))))
219
220 (defun url-http-ntlm--get-challenge ()
221 "Return the NTLM Type-2 message in the WWW-Authenticate header, if present."
222 (save-restriction
223 (mail-narrow-to-head)
224 (let ((www-authenticate (mail-fetch-field "www-authenticate")))
225 (when (string-match "NTLM\\s-+\\(\\S-+\\)"
226 www-authenticate)
227 (base64-decode-string (match-string 1 www-authenticate))))))
228
229 (defun url-http-ntlm--rmssoc (key alist)
230 "Remove all elements whose `car' match KEY from ALIST."
231 (cl-remove key alist :key 'car :test 'equal))
232
233 (defun url-http-ntlm--string (data)
234 "Return DATA encoded as an NTLM string."
235 (concat "NTLM " (base64-encode-string data :nobreak)))
236
237 \f
238 ;;; Public function called by `url-get-authentication'.
239 (defun url-ntlm-auth (url &optional prompt overwrite realm args)
240 "Return an NTLM HTTP authorization header.
241 Get the contents of the Authorization header for a HTTP response
242 using NTLM authentication, to access URL. Because NTLM is a
243 two-step process, this function expects to be called twice, first
244 to generate the NTLM type 1 message (request), then to respond to
245 the server's type 2 message (challenge) with a suitable response.
246
247 PROMPT, OVERWRITE, and REALM are ignored.
248
249 ARGS is expected to contain the WWW-Authentication header from
250 the server's last response. These are used by
251 `url-http-get-stage' to determine what stage we are at."
252 (url-http-ntlm--ensure-keepalive)
253 (let* ((user-url (url-http-ntlm--ensure-user url))
254 (stage (url-http-ntlm--get-stage args)))
255 (cl-case stage
256 ;; NTLM Type 1 message: the request
257 (:request
258 (url-http-ntlm--detect-loop user-url)
259 (cl-destructuring-bind (&optional key hash)
260 (url-http-ntlm--authorisation user-url nil realm)
261 (when (cl-third key)
262 (url-http-ntlm--string
263 (ntlm-build-auth-request (cl-second key) (cl-third key))))))
264 ;; NTLM Type 3 message: the response
265 (:response
266 (url-http-ntlm--detect-loop user-url)
267 (let ((challenge (url-http-ntlm--get-challenge)))
268 (cl-destructuring-bind (key hash)
269 (url-http-ntlm--authorisation user-url nil realm)
270 (url-http-ntlm--string
271 (ntlm-build-auth-response challenge
272 (cl-second key)
273 hash)))))
274 (:error
275 (url-http-ntlm--authorisation user-url :clear)))))
276
277 \f
278 ;;; Register `url-ntlm-auth' HTTP authentication method.
279 (url-register-auth-scheme "ntlm" nil 8)
280
281 (provide 'url-http-ntlm)
282
283 ;;; url-http-ntlm.el ends here