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.
39 (defvar sasl-mechanisms
40 '("CRAM-MD5" "DIGEST-MD5" "PLAIN" "LOGIN" "ANONYMOUS"))
42 (defvar sasl-mechanism-alist
43 '(("CRAM-MD5" sasl-cram)
44 ("DIGEST-MD5" sasl-digest)
47 ("ANONYMOUS" sasl-anonymous)))
49 (defvar sasl-unique-id-function #'sasl-unique-id-function)
51 (put 'sasl-error 'error-message "SASL error")
52 (put 'sasl-error 'error-conditions '(sasl-error error))
54 (defun sasl-error (datum)
55 (signal 'sasl-error (list datum)))
60 (defun sasl-make-client (mechanism name service server)
61 "Return a newly allocated SASL client.
62 NAME is name of the authorization. SERVICE is name of the service desired.
63 SERVER is the fully qualified host name of the server to authenticate to."
64 (vector mechanism name service server (make-symbol "sasl-client-properties")))
66 (defun sasl-client-mechanism (client)
67 "Return the authentication mechanism driver of CLIENT."
70 (defun sasl-client-name (client)
71 "Return the authorization name of CLIENT, a string."
74 (defun sasl-client-service (client)
75 "Return the service name of CLIENT, a string."
78 (defun sasl-client-server (client)
79 "Return the server name of CLIENT, a string."
82 (defun sasl-client-set-properties (client plist)
83 "Destructively set the properties of CLIENT.
84 The second argument PLIST is the new property list."
85 (setplist (aref client 4) plist))
87 (defun sasl-client-set-property (client property value)
88 "Add the given property/value to CLIENT."
89 (put (aref client 4) property value))
91 (defun sasl-client-property (client property)
92 "Return the value of the PROPERTY of CLIENT."
93 (get (aref client 4) property))
95 (defun sasl-client-properties (client)
96 "Return the properties of CLIENT."
97 (symbol-plist (aref client 4)))
102 (defun sasl-make-mechanism (name steps)
103 "Make an authentication mechanism.
104 NAME is a IANA registered SASL mechanism name.
105 STEPS is list of continuation function."
109 (let ((symbol (make-symbol (symbol-name step))))
110 (fset symbol (symbol-function step))
114 (defun sasl-mechanism-name (mechanism)
115 "Return name of MECHANISM, a string."
118 (defun sasl-mechanism-steps (mechanism)
119 "Return the authentication steps of MECHANISM, a list of functions."
122 (defun sasl-find-mechanism (mechanisms)
123 "Retrieve an apropriate mechanism object from MECHANISMS hints."
124 (let* ((sasl-mechanisms sasl-mechanisms)
127 (while sasl-mechanisms
128 (if (member (car sasl-mechanisms) mechanisms)
129 (throw 'done (nth 1 (assoc (car sasl-mechanisms)
130 sasl-mechanism-alist))))
131 (setq sasl-mechanisms (cdr sasl-mechanisms))))))
134 (get mechanism 'sasl-mechanism)))
136 ;;; @ SASL authentication step
139 (defun sasl-step-data (step)
140 "Return the data which STEP holds, a string."
143 (defun sasl-step-set-data (step data)
144 "Store DATA string to STEP."
147 (defun sasl-next-step (client step)
148 "Evaluate the challenge and prepare an appropriate next response.
149 The data type of the value and optional 2nd argument STEP is nil or
150 opaque authentication step which holds the reference to the next action
151 and the current challenge. At the first time STEP should be set to nil."
153 (sasl-mechanism-steps
154 (sasl-client-mechanism client)))
157 (nth 1 (memq (aref step 0) steps))
160 (vector function (funcall function client step)))))
162 (defvar sasl-read-passphrase nil)
163 (defun sasl-read-passphrase (prompt)
164 (if (not sasl-read-passphrase)
165 (if (functionp 'read-passwd)
166 (setq sasl-read-passphrase 'read-passwd)
167 (if (load "passwd" t)
168 (setq sasl-read-passphrase 'read-passwd)
169 (autoload 'ange-ftp-read-passwd "ange-ftp")
170 (setq sasl-read-passphrase 'ange-ftp-read-passwd))))
171 (funcall sasl-read-passphrase prompt))
173 (defun sasl-unique-id ()
174 "Compute a data string which must be different each time.
175 It contain at least 64 bits of entropy."
176 (concat (funcall sasl-unique-id-function)(funcall sasl-unique-id-function)))
178 (defvar sasl-unique-id-char nil)
180 ;; stolen (and renamed) from message.el
181 (defun sasl-unique-id-function ()
182 ;; Don't use microseconds from (current-time), they may be unsupported.
183 ;; Instead we use this randomly inited counter.
184 (setq sasl-unique-id-char
185 (% (1+ (or sasl-unique-id-char (logand (random t) (1- (lsh 1 20)))))
186 ;; (current-time) returns 16-bit ints,
187 ;; and 2^16*25 just fits into 4 digits i base 36.
189 (let ((tm (current-time)))
191 (sasl-unique-id-number-base36
193 (lsh (% sasl-unique-id-char 25) 16)) 4)
194 (sasl-unique-id-number-base36
196 (lsh (/ sasl-unique-id-char 25) 16)) 4))))
198 (defun sasl-unique-id-number-base36 (num len)
203 (concat (sasl-unique-id-number-base36 (/ num 36) (1- len))
204 (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
207 ;;; PLAIN (RFC2595 Section 6)
208 (defconst sasl-plain-steps
209 '(sasl-plain-response))
211 (defun sasl-plain-response (client step)
213 (sasl-read-passphrase
214 (format "PLAIN passphrase for %s: " (sasl-client-name client))))
216 (sasl-client-property
217 client 'authenticator-name))
218 (name (sasl-client-name client)))
220 (if (and authenticator-name
221 (not (string= authenticator-name name)))
222 (concat authenticator-name "\0" name "\0" passphrase)
223 (concat "\0" name "\0" passphrase))
224 (fillarray passphrase 0))))
226 (put 'sasl-plain 'sasl-mechanism
227 (sasl-make-mechanism "PLAIN" sasl-plain-steps))
229 (provide 'sasl-plain)
231 ;;; LOGIN (No specification exists)
232 (defconst sasl-login-steps
233 '(ignore ;no initial response
234 sasl-login-response-1
235 sasl-login-response-2))
237 (defun sasl-login-response-1 (client step)
238 ;;; (unless (string-match "^Username:" (sasl-step-data step))
239 ;;; (sasl-error (format "Unexpected response: %s" (sasl-step-data step))))
240 (sasl-client-name client))
242 (defun sasl-login-response-2 (client step)
243 ;;; (unless (string-match "^Password:" (sasl-step-data step))
244 ;;; (sasl-error (format "Unexpected response: %s" (sasl-step-data step))))
245 (sasl-read-passphrase
246 (format "LOGIN passphrase for %s: " (sasl-client-name client))))
248 (put 'sasl-login 'sasl-mechanism
249 (sasl-make-mechanism "LOGIN" sasl-login-steps))
251 (provide 'sasl-login)
253 ;;; ANONYMOUS (RFC2245)
254 (defconst sasl-anonymous-steps
255 '(ignore ;no initial response
256 sasl-anonymous-response))
258 (defun sasl-anonymous-response (client step)
259 (or (sasl-client-property client 'trace)
260 (sasl-client-name client)))
262 (put 'sasl-anonymous 'sasl-mechanism
263 (sasl-make-mechanism "ANONYMOUS" sasl-anonymous-steps))
265 (provide 'sasl-anonymous)
269 ;;; sasl.el ends here