1 ;;; sasl.el --- SASL client framework
3 ;; Copyright (C) 2000 Free Software Foundation, Inc.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
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].
31 ;; [SASL] J. Myers, "Simple Authentication and Security Layer (SASL)",
32 ;; RFC 2222, October 1997.
34 ;; [Java-SASL] R. Weltman & R. Lee, "The Java SASL Application Program
35 ;; Interface", draft-weltman-java-sasl-03.txt, March 2000.
41 (defvar sasl-mechanisms
42 '("CRAM-MD5" "DIGEST-MD5" "PLAIN" "LOGIN" "ANONYMOUS"))
44 (defvar sasl-mechanism-alist
45 '(("CRAM-MD5" sasl-cram)
46 ("DIGEST-MD5" sasl-digest)
49 ("ANONYMOUS" sasl-anonymous)))
51 (defvar sasl-unique-id-function #'sasl-unique-id-function)
53 (put 'sasl-error 'error-message "SASL error")
54 (put 'sasl-error 'error-conditions '(sasl-error error))
56 (defun sasl-error (datum)
57 (signal 'sasl-error (list datum)))
63 (luna-define-class sasl-client ()
70 (luna-define-internal-accessors 'sasl-client))
72 (defun sasl-make-client (mechanism name service server)
73 "Return a newly allocated SASL client.
74 NAME is name of the authorization. SERVICE is name of the service desired.
75 SERVER is the fully qualified host name of the server to authenticate to."
76 (luna-make-entity 'sasl-client
82 (defun sasl-client-mechanism (client)
83 "Return the authentication mechanism driver of CLIENT."
84 (sasl-client-mechanism-internal client))
86 (defun sasl-client-name (client)
87 "Return the authorization name of CLIENT, a string."
88 (sasl-client-name-internal client))
90 (defun sasl-client-service (client)
91 "Return the service name of CLIENT, a string."
92 (sasl-client-service-internal client))
94 (defun sasl-client-server (client)
95 "Return the server name of CLIENT, a string."
96 (sasl-client-server-internal client))
98 (defun sasl-client-set-properties (client plist)
99 "Destructively set the properties of CLIENT.
100 The second argument PLIST is the new property list."
101 (sasl-client-set-properties-internal client plist))
103 (defun sasl-client-set-property (client property value)
104 "Add the given property/value to CLIENT."
105 (sasl-client-set-properties-internal
106 client (plist-put (sasl-client-properties-internal client) property value)))
108 (defun sasl-client-property (client property)
109 "Return the value of the PROPERTY of CLIENT."
110 (plist-get (sasl-client-properties-internal client) property))
112 (defun sasl-client-properties (client)
113 "Return the properties of CLIENT."
114 (sasl-client-properties-internal client))
120 (luna-define-class sasl-mechanism ()
124 (luna-define-internal-accessors 'sasl-mechanism))
126 (luna-define-method initialize-instance :after ((mechanism sasl-mechanism)
128 (sasl-mechanism-set-steps-internal
132 (let ((symbol (make-symbol (symbol-name step))))
133 (fset symbol (symbol-function step))
135 (sasl-mechanism-steps-internal mechanism)))
138 (defun sasl-make-mechanism (name steps)
139 "Make an authentication mechanism.
140 NAME is a IANA registered SASL mechanism name.
141 STEPS is list of continuation function."
142 (luna-make-entity 'sasl-mechanism :name name :steps steps))
144 (defun sasl-mechanism-name (mechanism)
145 "Return name of MECHANISM, a string."
146 (sasl-mechanism-name-internal mechanism))
148 (defun sasl-mechanism-steps (mechanism)
149 "Return the authentication steps of MECHANISM, a list of functions."
150 (sasl-mechanism-steps-internal mechanism))
152 (defun sasl-find-mechanism (mechanisms)
153 "Retrieve an apropriate mechanism object from MECHANISMS hints."
154 (let* ((sasl-mechanisms sasl-mechanisms)
157 (while sasl-mechanisms
158 (if (member (car sasl-mechanisms) mechanisms)
159 (throw 'done (nth 1 (assoc (car sasl-mechanisms)
160 sasl-mechanism-alist))))
161 (setq sasl-mechanisms (cdr sasl-mechanisms))))))
164 (get mechanism 'sasl-mechanism)))
166 ;;; @ SASL authentication step
170 (luna-define-class sasl-step ()
175 (luna-define-internal-accessors 'sasl-step))
177 (defun sasl-step-data (step)
178 "Return the data which STEP holds, a string."
179 (sasl-step-data-internal step))
181 (defun sasl-step-set-data (step data)
182 "Store DATA string to STEP."
183 (sasl-step-set-data-internal step data))
185 (defun sasl-next-step (client step)
186 "Evaluate the challenge and prepare an appropriate next response.
187 The data type of the value and optional 2nd argument STEP is nil or
188 opaque authentication step which holds the reference to the next action
189 and the current challenge. At the first time STEP should be set to nil."
191 (sasl-mechanism-steps
192 (sasl-client-mechanism-internal client)))
196 (nth 1 (memq (sasl-step-continuation-internal step) steps)))))
198 (luna-make-entity 'sasl-step
199 :continuation function
200 :data (funcall function client step)))))
202 (defvar sasl-read-passphrase nil)
203 (defun sasl-read-passphrase (prompt)
204 (if (not sasl-read-passphrase)
205 (if (functionp 'read-passwd)
206 (setq sasl-read-passphrase 'read-passwd)
207 (if (load "passwd" t)
208 (setq sasl-read-passphrase 'read-passwd)
209 (autoload 'ange-ftp-read-passwd "ange-ftp")
210 (setq sasl-read-passphrase 'ange-ftp-read-passwd))))
211 (funcall sasl-read-passphrase prompt))
213 (defun sasl-unique-id ()
214 "Compute a data string which must be different each time.
215 It contain at least 64 bits of entropy."
216 (concat (funcall sasl-unique-id-function)(funcall sasl-unique-id-function)))
218 (defvar sasl-unique-id-char nil)
220 ;; stolen (and renamed) from message.el
221 (defun sasl-unique-id-function ()
222 ;; Don't use microseconds from (current-time), they may be unsupported.
223 ;; Instead we use this randomly inited counter.
224 (setq sasl-unique-id-char
225 (% (1+ (or sasl-unique-id-char (logand (random t) (1- (lsh 1 20)))))
226 ;; (current-time) returns 16-bit ints,
227 ;; and 2^16*25 just fits into 4 digits i base 36.
229 (let ((tm (current-time)))
231 (sasl-unique-id-number-base36
233 (lsh (% sasl-unique-id-char 25) 16)) 4)
234 (sasl-unique-id-number-base36
236 (lsh (/ sasl-unique-id-char 25) 16)) 4))))
238 (defun sasl-unique-id-number-base36 (num len)
243 (concat (sasl-unique-id-number-base36 (/ num 36) (1- len))
244 (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
247 ;;; PLAIN (RFC2595 Section 6)
248 (defconst sasl-plain-steps
249 '(sasl-plain-response))
251 (defun sasl-plain-response (client step)
253 (sasl-read-passphrase
254 (format "PLAIN passphrase for %s: " (sasl-client-name client))))
256 (sasl-client-property
257 client 'authenticator-name))
258 (name (sasl-client-name client)))
260 (if (and authenticator-name
261 (not (string= authenticator-name name)))
262 (concat authenticator-name "\0" name "\0" passphrase)
263 (concat "\0" name "\0" passphrase))
264 (fillarray passphrase 0))))
266 (put 'sasl-plain 'sasl-mechanism
267 (sasl-make-mechanism "PLAIN" sasl-plain-steps))
269 (provide 'sasl-plain)
271 ;;; LOGIN (No specification exists)
272 (defconst sasl-login-steps
273 '(ignore ;no initial response
274 sasl-login-response-1
275 sasl-login-response-2))
277 (defun sasl-login-response-1 (client step)
278 ;;; (unless (string-match "^Username:" (sasl-step-data step))
279 ;;; (sasl-error (format "Unexpected response: %s" (sasl-step-data step))))
280 (sasl-client-name client))
282 (defun sasl-login-response-2 (client step)
283 ;;; (unless (string-match "^Password:" (sasl-step-data step))
284 ;;; (sasl-error (format "Unexpected response: %s" (sasl-step-data step))))
285 (sasl-read-passphrase
286 (format "LOGIN passphrase for %s: " (sasl-client-name client))))
288 (put 'sasl-login 'sasl-mechanism
289 (sasl-make-mechanism "LOGIN" sasl-login-steps))
291 (provide 'sasl-login)
293 ;;; ANONYMOUS (RFC2245)
294 (defconst sasl-anonymous-steps
295 '(ignore ;no initial response
296 sasl-anonymous-response))
298 (defun sasl-anonymous-response (client step)
299 (or (sasl-client-property client 'trace)
300 (sasl-client-name client)))
302 (put 'sasl-anonymous 'sasl-mechanism
303 (sasl-make-mechanism "ANONYMOUS" sasl-anonymous-steps))
305 (provide 'sasl-anonymous)
309 ;;; sasl.el ends here