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