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