]> code.delx.au - gnu-emacs-elpa/blob - packages/url-http-ntlm/url-http-ntlm.el
url-http-ntlm: Use url-http-ntlm namespace consistently
[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 ;; It supports one username and password per server.
26 ;;
27 ;; Installation:
28 ;;
29 ;; M-x package-install RET url-http-ntlm RET
30 ;;
31 ;; Acknowledgements:
32 ;;
33 ;; Taro Kawagishi <tarok@transpulse.org> wrote ntlm.el and md4.el,
34 ;; which are parts of FLIM (Faithful Library about Internet Message).
35 ;;
36 ;; http://stuff.mit.edu/afs/sipb/contrib/emacs/packages/flim-1.14.7/ntlm.el
37 ;; http://stuff.mit.edu/afs/sipb/contrib/emacs/packages/flim-1.14.7/md4.el
38
39 ;;; Code:
40 (require 'url-auth)
41 (require 'url-http)
42 (require 'mail-parse)
43 (require 'cl)
44 (require 'ntlm)
45
46 (defvar url-http-ntlm-auth-storage nil
47 "Authentication storage.
48 An alist that maps a server name to a pair of \(<username> <ntlm
49 hashes>\).
50
51 The hashes are built using `ntlm-get-password-hashes'.
52 The username can contain the domain name, in the form \"user@domain\".
53
54 Note that for any server, only one user and password is ever stored.")
55
56 (defun url-ntlm-auth (url &optional prompt overwrite realm args)
57 "Return an NTLM HTTP authorization header.
58 Get the contents of the Authorization header for a HTTP response
59 using NTLM authentication, to access URL. Because NTLM is a
60 two-step process, this function expects to be called twice, first
61 to generate the NTLM type 1 message (request), then to respond to
62 the server's type 2 message (challenge) with a suitable response.
63
64 PROMPT, OVERWRITE, and REALM are ignored.
65
66 ARGS is expected to contain the WWW-Authentication header from
67 the server's last response. These are used by
68 `url-http-get-stage' to determine what stage we are at."
69 (url-http-ntlm-ensure-keepalive)
70 (let ((stage (url-http-ntlm-get-stage args)))
71 (case stage
72 ;; NTLM Type 1 message: the request
73 (:request
74 (destructuring-bind (&optional server user hash)
75 (url-http-ntlm-authorisation url)
76 (when server
77 (url-http-ntlm-string
78 (ntlm-build-auth-request user server)))))
79 ;; NTLM Type 3 message: the response
80 (:response
81 (let ((challenge (url-http-ntlm-get-challenge)))
82 (destructuring-bind (server user hash)
83 (url-http-ntlm-authorisation url)
84 (url-http-ntlm-string
85 (ntlm-build-auth-response challenge
86 user
87 hash)))))
88 (:error
89 (url-http-ntlm-authorisation url :clear)))))
90
91 (defun url-http-ntlm-ensure-keepalive ()
92 "Report an error if `url-http-attempt-keepalives' is not set."
93 (assert url-http-attempt-keepalives
94 nil
95 (concat "NTLM authentication won't work unless"
96 " `url-http-attempt-keepalives' is set!")))
97
98 (defun url-http-ntlm-clean-headers ()
99 "Remove Authorization element from `url-http-extra-headers' alist."
100 (setq url-http-extra-headers
101 (url-http-ntlm-rmssoc "Authorization" url-http-extra-headers)))
102
103 (defvar url-http-ntlm-last-args nil
104 "Stores the last `url-http-ntlm-get-stage' arguments and return value.
105 This is used to detect multiple calls.")
106 (make-variable-buffer-local 'url-http-ntlm-last-args)
107
108 (defun url-http-ntlm-get-stage (args)
109 "Determine what stage of the NTLM handshake we are at.
110 PROMPT and ARGS come from `url-ntlm-auth''s caller,
111 `url-get-authentication'. Their meaning depends on the current
112 implementation - this function is well and truly coupled.
113
114 url-get-authentication' calls `url-ntlm-auth' once when checking
115 what authentication schemes are supported (PROMPT and ARGS are
116 nil), and then twice for every stage of the handshake: the first
117 time PROMPT is nil, the second, t; ARGS contains the server
118 response's \"WWW-Authenticate\" header, munged by
119 `url-parse-args'."
120 (let* ((response-rxp "^NTLM TlRMTVNTUAADAAA")
121 (challenge-rxp "^TLRMTVNTUAACAAA")
122 (auth-header (assoc "Authorization" url-http-extra-headers))
123 (case-fold-search t)
124 stage)
125 (if (eq args (car url-http-ntlm-last-args))
126 ;; multiple calls, return the same argument we returned last time
127 (cdr url-http-ntlm-last-args)
128 (let ((stage
129 (cond ((and auth-header (string-match response-rxp
130 (cdr auth-header)))
131 :error)
132 ((and (= (length args) 2)
133 (destructuring-bind (challenge ntlm) args
134 (and (string-equal "ntlm" (car ntlm))
135 (string-match challenge-rxp
136 (car challenge)))))
137 :response)
138 (t
139 :request))))
140 (url-http-ntlm-clean-headers)
141 (setq url-http-ntlm-last-args (cons args stage))
142 stage))))
143
144 (defun url-http-ntlm-authorisation (url &optional clear)
145 "Get or clear NTLM authentication details for URL.
146 If CLEAR is non-nil, clear any saved credentials for server.
147 Otherwise, return the credentials, prompting the user if
148 necessary.
149
150 If URL contains a username and a password, they are used and
151 stored credentials are not affected.
152
153 Note that for any server, only one user and password is ever
154 stored."
155 (let* ((href (if (stringp url)
156 (url-generic-parse-url url)
157 url))
158 (server (url-host href))
159 (user (url-user href))
160 (pass (url-password href))
161 (stored (assoc server url-http-ntlm-auth-storage))
162 (both (and user pass)))
163 (if clear
164 ;; clear
165 (unless both
166 (setq url-http-ntlm-auth-storage
167 (url-http-ntlm-rmssoc server url-http-ntlm-auth-storage))
168 nil)
169 ;; get
170 (if (or both
171 (and stored user (not (equal user (second stored))))
172 (not stored))
173 (let* ((user* (if both
174 user
175 (read-string (url-auth-user-prompt url realm)
176 (or user (user-real-login-name)))))
177 (pass* (if both
178 pass
179 (read-passwd "Password: ")))
180 (entry `(,server . (,user*
181 ,(ntlm-get-password-hashes pass*)))))
182 (unless both
183 (setq url-http-ntlm-auth-storage
184 (cons entry
185 (url-http-ntlm-rmssoc server
186 url-http-ntlm-auth-storage))))
187 entry)
188 stored))))
189
190 (defun url-http-ntlm-get-challenge ()
191 "Return the NTLM Type-2 message in the WWW-Authenticate header, if present."
192 (save-restriction
193 (mail-narrow-to-head)
194 (let ((www-authenticate (mail-fetch-field "www-authenticate")))
195 (when (string-match "NTLM\\s-+\\(\\S-+\\)"
196 www-authenticate)
197 (base64-decode-string (match-string 1 www-authenticate))))))
198
199 (defun url-http-ntlm-rmssoc (key alist)
200 "Remove all elements whose `car' match KEY from ALIST."
201 (remove* key alist :key 'car :test 'equal))
202
203 (defun url-http-ntlm-string (data)
204 "Return DATA encoded as an NTLM string."
205 (concat "NTLM " (base64-encode-string data :nobreak)))
206
207 (url-register-auth-scheme "ntlm" nil 8)
208
209 (provide 'url-http-ntlm)
210
211 ;;; url-http-ntlm.el ends here