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"
43 (defvar sasl-mechanism-alist
44 '(("CRAM-MD5" sasl-cram)
45 ("DIGEST-MD5" sasl-digest)
48 ("ANONYMOUS" sasl-anonymous)
50 ("SCRAM-MD5" sasl-scram)))
52 (defvar sasl-unique-id-function #'sasl-unique-id-function)
54 (put 'sasl-error 'error-message "SASL error")
55 (put 'sasl-error 'error-conditions '(sasl-error error))
57 (defun sasl-error (datum)
58 (signal 'sasl-error (list datum)))
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")))
69 (defun sasl-client-mechanism (client)
70 "Return the authentication mechanism driver of CLIENT."
73 (defun sasl-client-name (client)
74 "Return the authorization name of CLIENT, a string."
77 (defun sasl-client-service (client)
78 "Return the service name of CLIENT, a string."
81 (defun sasl-client-server (client)
82 "Return the server name of CLIENT, a string."
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))
90 (defun sasl-client-set-property (client property value)
91 "Add the given property/value to CLIENT."
92 (put (aref client 4) property value))
94 (defun sasl-client-property (client property)
95 "Return the value of the PROPERTY of CLIENT."
96 (get (aref client 4) property))
98 (defun sasl-client-properties (client)
99 "Return the properties of CLIENT."
100 (symbol-plist (aref client 4)))
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."
112 (let ((symbol (make-symbol (symbol-name step))))
113 (fset symbol (symbol-function step))
117 (defun sasl-mechanism-name (mechanism)
118 "Return name of MECHANISM, a string."
121 (defun sasl-mechanism-steps (mechanism)
122 "Return the authentication steps of MECHANISM, a list of functions."
125 (defun sasl-find-mechanism (mechanisms)
126 "Retrieve an apropriate mechanism object from MECHANISMS hints."
127 (let* ((sasl-mechanisms sasl-mechanisms)
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))))))
137 (get mechanism 'sasl-mechanism)))
139 ;;; @ SASL authentication step
142 (defun sasl-step-data (step)
143 "Return the data which STEP holds, a string."
146 (defun sasl-step-set-data (step data)
147 "Store DATA string to STEP."
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."
156 (sasl-mechanism-steps
157 (sasl-client-mechanism client)))
160 (nth 1 (memq (aref step 0) steps))
163 (vector function (funcall function client step)))))
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))
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)))
181 (defvar sasl-unique-id-char nil)
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.
192 (let ((tm (current-time)))
194 (sasl-unique-id-number-base36
196 (lsh (% sasl-unique-id-char 25) 16)) 4)
197 (sasl-unique-id-number-base36
199 (lsh (/ sasl-unique-id-char 25) 16)) 4))))
201 (defun sasl-unique-id-number-base36 (num len)
206 (concat (sasl-unique-id-number-base36 (/ num 36) (1- len))
207 (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
210 ;;; PLAIN (RFC2595 Section 6)
211 (defconst sasl-plain-steps
212 '(sasl-plain-response))
214 (defun sasl-plain-response (client step)
216 (sasl-read-passphrase
217 (format "PLAIN passphrase for %s: " (sasl-client-name client))))
219 (sasl-client-property
220 client 'authenticator-name))
221 (name (sasl-client-name client)))
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))))
229 (put 'sasl-plain 'sasl-mechanism
230 (sasl-make-mechanism "PLAIN" sasl-plain-steps))
232 (provide 'sasl-plain)
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))
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))
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))))
251 (put 'sasl-login 'sasl-mechanism
252 (sasl-make-mechanism "LOGIN" sasl-login-steps))
254 (provide 'sasl-login)
256 ;;; ANONYMOUS (RFC2245)
257 (defconst sasl-anonymous-steps
258 '(ignore ;no initial response
259 sasl-anonymous-response))
261 (defun sasl-anonymous-response (client step)
262 (or (sasl-client-property client 'trace)
263 (sasl-client-name client)))
265 (put 'sasl-anonymous 'sasl-mechanism
266 (sasl-make-mechanism "ANONYMOUS" sasl-anonymous-steps))
268 (provide 'sasl-anonymous)
272 ;;; sasl.el ends here