86997
|
1 ;;; sasl.el --- SASL client framework
|
|
2
|
87665
|
3 ;; Copyright (C) 2000, 2007, 2008 Free Software Foundation, Inc.
|
86997
|
4
|
|
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
|
|
6 ;; Keywords: SASL
|
|
7
|
|
8 ;; This file is part of GNU Emacs.
|
|
9
|
|
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
11 ;; it under the terms of the GNU General Public License as published by
|
|
12 ;; the Free Software Foundation; either version 3, or (at your option)
|
|
13 ;; any later version.
|
|
14
|
|
15 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 ;; GNU General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
|
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
23 ;; Boston, MA 02110-1301, USA.
|
|
24
|
|
25 ;;; Commentary:
|
|
26
|
|
27 ;; This module provides common interface functions to share several
|
|
28 ;; SASL mechanism drivers. The toplevel is designed to be mostly
|
|
29 ;; compatible with [Java-SASL].
|
|
30 ;;
|
|
31 ;; [SASL] J. Myers, "Simple Authentication and Security Layer (SASL)",
|
|
32 ;; RFC 2222, October 1997.
|
|
33 ;;
|
|
34 ;; [Java-SASL] R. Weltman & R. Lee, "The Java SASL Application Program
|
|
35 ;; Interface", draft-weltman-java-sasl-03.txt, March 2000.
|
|
36
|
|
37 ;;; Code:
|
|
38
|
|
39 (defvar sasl-mechanisms
|
|
40 '("CRAM-MD5" "DIGEST-MD5" "PLAIN" "LOGIN" "ANONYMOUS"
|
|
41 "NTLM" "SCRAM-MD5"))
|
|
42
|
|
43 (defvar sasl-mechanism-alist
|
|
44 '(("CRAM-MD5" sasl-cram)
|
|
45 ("DIGEST-MD5" sasl-digest)
|
|
46 ("PLAIN" sasl-plain)
|
|
47 ("LOGIN" sasl-login)
|
|
48 ("ANONYMOUS" sasl-anonymous)
|
|
49 ("NTLM" sasl-ntlm)
|
|
50 ("SCRAM-MD5" sasl-scram)))
|
|
51
|
|
52 (defvar sasl-unique-id-function #'sasl-unique-id-function)
|
|
53
|
|
54 (put 'sasl-error 'error-message "SASL error")
|
|
55 (put 'sasl-error 'error-conditions '(sasl-error error))
|
|
56
|
|
57 (defun sasl-error (datum)
|
|
58 (signal 'sasl-error (list datum)))
|
|
59
|
|
60 ;;; @ SASL client
|
|
61 ;;;
|
|
62
|
|
63 (defun sasl-make-client (mechanism name service server)
|
|
64 "Return a newly allocated SASL client.
|
|
65 NAME is name of the authorization. SERVICE is name of the service desired.
|
|
66 SERVER is the fully qualified host name of the server to authenticate to."
|
|
67 (vector mechanism name service server (make-symbol "sasl-client-properties")))
|
|
68
|
|
69 (defun sasl-client-mechanism (client)
|
|
70 "Return the authentication mechanism driver of CLIENT."
|
|
71 (aref client 0))
|
|
72
|
|
73 (defun sasl-client-name (client)
|
|
74 "Return the authorization name of CLIENT, a string."
|
|
75 (aref client 1))
|
|
76
|
|
77 (defun sasl-client-service (client)
|
|
78 "Return the service name of CLIENT, a string."
|
|
79 (aref client 2))
|
|
80
|
|
81 (defun sasl-client-server (client)
|
|
82 "Return the server name of CLIENT, a string."
|
|
83 (aref client 3))
|
|
84
|
|
85 (defun sasl-client-set-properties (client plist)
|
|
86 "Destructively set the properties of CLIENT.
|
|
87 The second argument PLIST is the new property list."
|
|
88 (setplist (aref client 4) plist))
|
|
89
|
|
90 (defun sasl-client-set-property (client property value)
|
|
91 "Add the given property/value to CLIENT."
|
|
92 (put (aref client 4) property value))
|
|
93
|
|
94 (defun sasl-client-property (client property)
|
|
95 "Return the value of the PROPERTY of CLIENT."
|
|
96 (get (aref client 4) property))
|
|
97
|
|
98 (defun sasl-client-properties (client)
|
|
99 "Return the properties of CLIENT."
|
|
100 (symbol-plist (aref client 4)))
|
|
101
|
|
102 ;;; @ SASL mechanism
|
|
103 ;;;
|
|
104
|
|
105 (defun sasl-make-mechanism (name steps)
|
|
106 "Make an authentication mechanism.
|
|
107 NAME is a IANA registered SASL mechanism name.
|
|
108 STEPS is list of continuation function."
|
|
109 (vector name
|
|
110 (mapcar
|
|
111 (lambda (step)
|
|
112 (let ((symbol (make-symbol (symbol-name step))))
|
|
113 (fset symbol (symbol-function step))
|
|
114 symbol))
|
|
115 steps)))
|
|
116
|
|
117 (defun sasl-mechanism-name (mechanism)
|
|
118 "Return name of MECHANISM, a string."
|
|
119 (aref mechanism 0))
|
|
120
|
|
121 (defun sasl-mechanism-steps (mechanism)
|
|
122 "Return the authentication steps of MECHANISM, a list of functions."
|
|
123 (aref mechanism 1))
|
|
124
|
|
125 (defun sasl-find-mechanism (mechanisms)
|
|
126 "Retrieve an apropriate mechanism object from MECHANISMS hints."
|
|
127 (let* ((sasl-mechanisms sasl-mechanisms)
|
|
128 (mechanism
|
|
129 (catch 'done
|
|
130 (while sasl-mechanisms
|
|
131 (if (member (car sasl-mechanisms) mechanisms)
|
|
132 (throw 'done (nth 1 (assoc (car sasl-mechanisms)
|
|
133 sasl-mechanism-alist))))
|
|
134 (setq sasl-mechanisms (cdr sasl-mechanisms))))))
|
|
135 (if mechanism
|
|
136 (require mechanism))
|
|
137 (get mechanism 'sasl-mechanism)))
|
|
138
|
|
139 ;;; @ SASL authentication step
|
|
140 ;;;
|
|
141
|
|
142 (defun sasl-step-data (step)
|
|
143 "Return the data which STEP holds, a string."
|
|
144 (aref step 1))
|
|
145
|
|
146 (defun sasl-step-set-data (step data)
|
|
147 "Store DATA string to STEP."
|
|
148 (aset step 1 data))
|
|
149
|
|
150 (defun sasl-next-step (client step)
|
|
151 "Evaluate the challenge and prepare an appropriate next response.
|
|
152 The data type of the value and optional 2nd argument STEP is nil or
|
|
153 opaque authentication step which holds the reference to the next action
|
|
154 and the current challenge. At the first time STEP should be set to nil."
|
|
155 (let* ((steps
|
|
156 (sasl-mechanism-steps
|
|
157 (sasl-client-mechanism client)))
|
|
158 (function
|
|
159 (if (vectorp step)
|
|
160 (nth 1 (memq (aref step 0) steps))
|
|
161 (car steps))))
|
|
162 (if function
|
|
163 (vector function (funcall function client step)))))
|
|
164
|
|
165 (defvar sasl-read-passphrase nil)
|
|
166 (defun sasl-read-passphrase (prompt)
|
|
167 (if (not sasl-read-passphrase)
|
|
168 (if (functionp 'read-passwd)
|
|
169 (setq sasl-read-passphrase 'read-passwd)
|
|
170 (if (load "passwd" t)
|
|
171 (setq sasl-read-passphrase 'read-passwd)
|
|
172 (autoload 'ange-ftp-read-passwd "ange-ftp")
|
|
173 (setq sasl-read-passphrase 'ange-ftp-read-passwd))))
|
|
174 (funcall sasl-read-passphrase prompt))
|
|
175
|
|
176 (defun sasl-unique-id ()
|
|
177 "Compute a data string which must be different each time.
|
|
178 It contain at least 64 bits of entropy."
|
|
179 (concat (funcall sasl-unique-id-function)(funcall sasl-unique-id-function)))
|
|
180
|
|
181 (defvar sasl-unique-id-char nil)
|
|
182
|
|
183 ;; stolen (and renamed) from message.el
|
|
184 (defun sasl-unique-id-function ()
|
|
185 ;; Don't use microseconds from (current-time), they may be unsupported.
|
|
186 ;; Instead we use this randomly inited counter.
|
|
187 (setq sasl-unique-id-char
|
|
188 (% (1+ (or sasl-unique-id-char (logand (random t) (1- (lsh 1 20)))))
|
|
189 ;; (current-time) returns 16-bit ints,
|
|
190 ;; and 2^16*25 just fits into 4 digits i base 36.
|
|
191 (* 25 25)))
|
|
192 (let ((tm (current-time)))
|
|
193 (concat
|
|
194 (sasl-unique-id-number-base36
|
|
195 (+ (car tm)
|
|
196 (lsh (% sasl-unique-id-char 25) 16)) 4)
|
|
197 (sasl-unique-id-number-base36
|
|
198 (+ (nth 1 tm)
|
|
199 (lsh (/ sasl-unique-id-char 25) 16)) 4))))
|
|
200
|
|
201 (defun sasl-unique-id-number-base36 (num len)
|
|
202 (if (if (< len 0)
|
|
203 (<= num 0)
|
|
204 (= len 0))
|
|
205 ""
|
|
206 (concat (sasl-unique-id-number-base36 (/ num 36) (1- len))
|
|
207 (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
|
|
208 (% num 36))))))
|
|
209
|
|
210 ;;; PLAIN (RFC2595 Section 6)
|
|
211 (defconst sasl-plain-steps
|
|
212 '(sasl-plain-response))
|
|
213
|
|
214 (defun sasl-plain-response (client step)
|
|
215 (let ((passphrase
|
|
216 (sasl-read-passphrase
|
|
217 (format "PLAIN passphrase for %s: " (sasl-client-name client))))
|
|
218 (authenticator-name
|
|
219 (sasl-client-property
|
|
220 client 'authenticator-name))
|
|
221 (name (sasl-client-name client)))
|
|
222 (unwind-protect
|
|
223 (if (and authenticator-name
|
|
224 (not (string= authenticator-name name)))
|
|
225 (concat authenticator-name "\0" name "\0" passphrase)
|
|
226 (concat "\0" name "\0" passphrase))
|
|
227 (fillarray passphrase 0))))
|
|
228
|
|
229 (put 'sasl-plain 'sasl-mechanism
|
|
230 (sasl-make-mechanism "PLAIN" sasl-plain-steps))
|
|
231
|
|
232 (provide 'sasl-plain)
|
|
233
|
|
234 ;;; LOGIN (No specification exists)
|
|
235 (defconst sasl-login-steps
|
|
236 '(ignore ;no initial response
|
|
237 sasl-login-response-1
|
|
238 sasl-login-response-2))
|
|
239
|
|
240 (defun sasl-login-response-1 (client step)
|
|
241 ;;; (unless (string-match "^Username:" (sasl-step-data step))
|
|
242 ;;; (sasl-error (format "Unexpected response: %s" (sasl-step-data step))))
|
|
243 (sasl-client-name client))
|
|
244
|
|
245 (defun sasl-login-response-2 (client step)
|
|
246 ;;; (unless (string-match "^Password:" (sasl-step-data step))
|
|
247 ;;; (sasl-error (format "Unexpected response: %s" (sasl-step-data step))))
|
|
248 (sasl-read-passphrase
|
|
249 (format "LOGIN passphrase for %s: " (sasl-client-name client))))
|
|
250
|
|
251 (put 'sasl-login 'sasl-mechanism
|
|
252 (sasl-make-mechanism "LOGIN" sasl-login-steps))
|
|
253
|
|
254 (provide 'sasl-login)
|
|
255
|
|
256 ;;; ANONYMOUS (RFC2245)
|
|
257 (defconst sasl-anonymous-steps
|
|
258 '(ignore ;no initial response
|
|
259 sasl-anonymous-response))
|
|
260
|
|
261 (defun sasl-anonymous-response (client step)
|
|
262 (or (sasl-client-property client 'trace)
|
|
263 (sasl-client-name client)))
|
|
264
|
|
265 (put 'sasl-anonymous 'sasl-mechanism
|
|
266 (sasl-make-mechanism "ANONYMOUS" sasl-anonymous-steps))
|
|
267
|
|
268 (provide 'sasl-anonymous)
|
|
269
|
|
270 (provide 'sasl)
|
|
271
|
|
272 ;;; arch-tag: 8b3326fa-4978-4fda-93e2-cb2c6255f887
|
|
273 ;;; sasl.el ends here
|