sasl.el 8.2 KB

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