update.
[elisp/flim.git] / sasl.el
1 ;;; sasl.el --- SASL client framework
2
3 ;; Copyright (C) 2000 Free Software Foundation, Inc.
4
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: SASL
7
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
9
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.
14
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.
19
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.
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 (require 'luna)
40
41 (defvar sasl-mechanisms
42   '("CRAM-MD5" "DIGEST-MD5" "PLAIN" "LOGIN" "ANONYMOUS"))
43
44 (defvar sasl-mechanism-alist
45   '(("CRAM-MD5" sasl-cram)
46     ("DIGEST-MD5" sasl-digest)
47     ("PLAIN" sasl-plain)
48     ("LOGIN" sasl-login)
49     ("ANONYMOUS" sasl-anonymous)))
50
51 (defvar sasl-unique-id-function #'sasl-unique-id-function)
52
53 (put 'sasl-error 'error-message "SASL error")
54 (put 'sasl-error 'error-conditions '(sasl-error error))
55
56 (defun sasl-error (datum)
57   (signal 'sasl-error (list datum)))
58
59 ;;; @ SASL client
60 ;;;
61
62 (eval-and-compile
63   (luna-define-class sasl-client ()
64                      (mechanism
65                       name
66                       service
67                       server
68                       properties))
69
70   (luna-define-internal-accessors 'sasl-client))
71
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
77                     :mechanism mechanism
78                     :name name
79                     :service service
80                     :server server))
81
82 (defun sasl-client-mechanism (client)
83   "Return the authentication mechanism driver of CLIENT."
84   (sasl-client-mechanism-internal client))
85
86 (defun sasl-client-name (client)
87   "Return the authorization name of CLIENT, a string."
88   (sasl-client-name-internal client))
89
90 (defun sasl-client-service (client)
91   "Return the service name of CLIENT, a string."
92   (sasl-client-service-internal client))
93
94 (defun sasl-client-server (client)
95   "Return the server name of CLIENT, a string."
96   (sasl-client-server-internal client))
97
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))
102
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)))
107
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))
111
112 (defun sasl-client-properties (client)
113   "Return the properties of CLIENT."
114   (sasl-client-properties-internal client))
115
116 ;;; @ SASL mechanism
117 ;;;
118
119 (eval-and-compile
120   (luna-define-class sasl-mechanism ()
121                      (name
122                       steps))
123
124   (luna-define-internal-accessors 'sasl-mechanism))
125
126 (luna-define-method initialize-instance :after ((mechanism sasl-mechanism)
127                                                 &rest init-args)
128   (sasl-mechanism-set-steps-internal
129    mechanism
130    (mapcar
131     (lambda (step)
132       (let ((symbol (make-symbol (symbol-name step))))
133         (fset symbol (symbol-function step))
134         symbol))
135     (sasl-mechanism-steps-internal mechanism)))
136   mechanism)
137
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))
143
144 (defun sasl-mechanism-name (mechanism)
145   "Return name of MECHANISM, a string."
146   (sasl-mechanism-name-internal mechanism))
147
148 (defun sasl-mechanism-steps (mechanism)
149   "Return the authentication steps of MECHANISM, a list of functions."
150   (sasl-mechanism-steps-internal mechanism))
151
152 (defun sasl-find-mechanism (mechanisms)
153   "Retrieve an apropriate mechanism object from MECHANISMS hints."
154   (let* ((sasl-mechanisms sasl-mechanisms)
155          (mechanism
156           (catch 'done
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))))))
162     (if mechanism
163         (require mechanism))
164     (get mechanism 'sasl-mechanism)))
165
166 ;;; @ SASL authentication step
167 ;;;
168
169 (eval-and-compile
170   (luna-define-class sasl-step ()
171                      (continuation
172                       data))
173                       
174
175   (luna-define-internal-accessors 'sasl-step))
176
177 (defun sasl-step-data (step)
178   "Return the data which STEP holds, a string."
179   (sasl-step-data-internal step))
180
181 (defun sasl-step-set-data (step data)
182   "Store DATA string to STEP."
183   (sasl-step-set-data-internal step data))
184
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."
190   (let* ((steps
191           (sasl-mechanism-steps
192            (sasl-client-mechanism-internal client)))
193          (function
194           (if (null step)
195               (car steps)
196             (nth 1 (memq (sasl-step-continuation-internal step) steps)))))
197     (if function
198         (luna-make-entity 'sasl-step
199                           :continuation function
200                           :data (funcall function client step)))))
201
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))
212
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)))
217
218 (defvar sasl-unique-id-char nil)
219
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.
228            (* 25 25)))
229   (let ((tm (current-time)))
230     (concat
231      (sasl-unique-id-number-base36
232       (+ (car   tm)
233          (lsh (% sasl-unique-id-char 25) 16)) 4)
234      (sasl-unique-id-number-base36
235       (+ (nth 1 tm)
236          (lsh (/ sasl-unique-id-char 25) 16)) 4))))
237
238 (defun sasl-unique-id-number-base36 (num len)
239   (if (if (< len 0)
240           (<= num 0)
241         (= len 0))
242       ""
243     (concat (sasl-unique-id-number-base36 (/ num 36) (1- len))
244             (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
245                                   (% num 36))))))
246
247 ;;; PLAIN (RFC2595 Section 6)
248 (defconst sasl-plain-steps
249   '(sasl-plain-response))
250
251 (defun sasl-plain-response (client step)
252   (let ((passphrase
253          (sasl-read-passphrase
254           (format "PLAIN passphrase for %s: " (sasl-client-name client))))
255         (authenticator-name
256          (sasl-client-property
257           client 'authenticator-name))
258         (name (sasl-client-name client)))
259     (unwind-protect
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))))
265
266 (put 'sasl-plain 'sasl-mechanism
267      (sasl-make-mechanism "PLAIN" sasl-plain-steps))
268
269 (provide 'sasl-plain)
270
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))
276
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))
281
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))))
287
288 (put 'sasl-login 'sasl-mechanism
289      (sasl-make-mechanism "LOGIN" sasl-login-steps))
290
291 (provide 'sasl-login)
292
293 ;;; ANONYMOUS (RFC2245)
294 (defconst sasl-anonymous-steps
295   '(ignore                              ;no initial response
296     sasl-anonymous-response))
297
298 (defun sasl-anonymous-response (client step)
299   (or (sasl-client-property client 'trace)
300       (sasl-client-name client)))
301
302 (put 'sasl-anonymous 'sasl-mechanism
303      (sasl-make-mechanism "ANONYMOUS" sasl-anonymous-steps))
304
305 (provide 'sasl-anonymous)
306
307 (provide 'sasl)
308
309 ;;; sasl.el ends here