* FLIM-ELS (flim-modules): Add `unique-id'.
[elisp/flim.git] / smtp.el
diff --git a/smtp.el b/smtp.el
index 6b6162a..9d0bd64 100644 (file)
--- a/smtp.el
+++ b/smtp.el
   (autoload 'starttls-open-stream "starttls")
   (autoload 'starttls-negotiate "starttls")
   (autoload 'sasl-cram-md5 "sasl")
-  (autoload 'sasl-plain "sasl"))
+  (autoload 'sasl-plain "sasl")
+  (autoload 'sasl-scram-md5-client-msg-1 "sasl")
+  (autoload 'sasl-scram-md5-client-msg-2 "sasl")
+  (autoload 'sasl-scram-md5-authenticate-server "sasl"))
                       
 (eval-when-compile (require 'cl))      ; push
 
@@ -100,6 +103,7 @@ don't define this value."
     (plain smtp-auth-plain)
     (login smtp-auth-login)
     (anonymous smtp-auth-anonymous)
+    (scram-md5 smtp-auth-scram-md5)
     ))
 
 (defcustom smtp-connection-type nil
@@ -132,29 +136,29 @@ don't define this value."
       (erase-buffer)
       (make-local-variable 'smtp-read-point)
       (setq smtp-read-point (point-min))
-
+      
       (unwind-protect
          (catch 'done
            (setq process 
                  (if smtp-connection-type
-                     (starttls-open-stream
-                      "SMTP" (current-buffer) server smtp-service)
+                     (as-binary-process
+                      (starttls-open-stream
+                       "SMTP" (current-buffer) server smtp-service))
                    (open-network-stream-as-binary
                     "SMTP" (current-buffer) server smtp-service)))
-           (or process (throw 'done nil))
-
+           
            (set-process-filter process 'smtp-process-filter)
-
+           
            (if (eq smtp-connection-type 'force)
                (starttls-negotiate process))
-
+           
            ;; Greeting
            (setq response (smtp-read-response process))
            (if (or (null (car response))
                    (not (integerp (car response)))
                    (>= (car response) 400))
                (throw 'done (car (cdr response))))
-
+       
            ;; EHLO
            (smtp-send-command process
                               (format "EHLO %s" (smtp-make-fqdn)))
@@ -182,7 +186,7 @@ don't define this value."
                        (push (intern (match-string 1 extension)) extensions))
                    (push (intern extension) extensions))
                  (setq extension-lines (cdr extension-lines)))))
-
+       
            ;; STARTTLS --- begin a TLS negotiation (RFC 2595)
            (when (and smtp-connection-type 
                       (null (eq smtp-connection-type 'force))
@@ -206,37 +210,37 @@ don't define this value."
                         (format "AUTH mechanism %s not available" auth)))))
 
            ;; ONEX --- One message transaction only (sendmail extension?)
-           (if (or (memq 'onex extensions)
-                   (memq 'xone extensions))
-               (progn
-                 (smtp-send-command process "ONEX")
-                 (setq response (smtp-read-response process))
-                 (if (or (null (car response))
-                         (not (integerp (car response)))
-                         (>= (car response) 400))
-                     (throw 'done (car (cdr response))))))
+;;;        (if (or (memq 'onex extensions)
+;;;                (memq 'xone extensions))
+;;;            (progn
+;;;              (smtp-send-command process "ONEX")
+;;;              (setq response (smtp-read-response process))
+;;;              (if (or (null (car response))
+;;;                      (not (integerp (car response)))
+;;;                      (>= (car response) 400))
+;;;                  (throw 'done (car (cdr response))))))
 
            ;; VERB --- Verbose (sendmail extension?)
-           (if (and smtp-debug-info
-                    (or (memq 'verb extensions)
-                        (memq 'xvrb extensions)))
-               (progn
-                 (smtp-send-command process "VERB")
-                 (setq response (smtp-read-response process))
-                 (if (or (null (car response))
-                         (not (integerp (car response)))
-                         (>= (car response) 400))
-                     (throw 'done (car (cdr response))))))
+;;;        (if (and smtp-debug-info
+;;;                 (or (memq 'verb extensions)
+;;;                     (memq 'xvrb extensions)))
+;;;            (progn
+;;;              (smtp-send-command process "VERB")
+;;;              (setq response (smtp-read-response process))
+;;;              (if (or (null (car response))
+;;;                      (not (integerp (car response)))
+;;;                      (>= (car response) 400))
+;;;                  (throw 'done (car (cdr response))))))
 
            ;; XUSR --- Initial (user) submission (sendmail extension?)
-           (if (memq 'xusr extensions)
-               (progn
-                 (smtp-send-command process "XUSR")
-                 (setq response (smtp-read-response process))
-                 (if (or (null (car response))
-                         (not (integerp (car response)))
-                         (>= (car response) 400))
-                     (throw 'done (car (cdr response))))))
+;;;        (if (memq 'xusr extensions)
+;;;            (progn
+;;;              (smtp-send-command process "XUSR")
+;;;              (setq response (smtp-read-response process))
+;;;              (if (or (null (car response))
+;;;                      (not (integerp (car response)))
+;;;                      (>= (car response) 400))
+;;;                  (throw 'done (car (cdr response))))))
 
            ;; MAIL FROM:<sender>
            (smtp-send-command
@@ -302,16 +306,16 @@ don't define this value."
                    (not (integerp (car response)))
                    (>= (car response) 400))
                (throw 'done (car (cdr response))))
-
+       
            t)
 
        (if (and process
                 (eq (process-status process) 'open))
-           (progn
-             ;; QUIT
-             (smtp-send-command process "QUIT")
-             (smtp-read-response process)
-             (delete-process process)))))))
+       (progn
+         ;; QUIT
+         (smtp-send-command process "QUIT")
+         (smtp-read-response process)
+         (delete-process process)))))))
 
 (defun smtp-process-filter (process output)
   (save-excursion
@@ -368,9 +372,11 @@ don't define this value."
     (setq smtp-read-point match-end)
     return-value))
 
-(defun smtp-send-command (process command)
+(defun smtp-send-command (process command &optional secure)
   (goto-char (point-max))
-  (insert command "\r\n")
+  (if secure
+      (insert "Here is insecure words.\r\n")
+    (insert command "\r\n"))
   (setq smtp-read-point (point))
   (process-send-string process command)
   (process-send-string process "\r\n"))
@@ -490,7 +496,7 @@ don't define this value."
                         (fillarray secure-word 0))
           secure-word (unwind-protect
                           (base64-encode-string secure-word)
-                        (fillarray secure-word 0))))
+                        (fillarray secure-word 0))) t)
     (fillarray secure-word 0)
     (setq response (smtp-read-response process))
     (if (or (null (car response))
@@ -511,7 +517,7 @@ don't define this value."
                         (fillarray secure-word 0))
           secure-word (unwind-protect
                           (concat "AUTH PLAIN " secure-word)
-                        (fillarray secure-word 0))))
+                        (fillarray secure-word 0))) t)
     (fillarray secure-word 0)
     (setq response (smtp-read-response process))
     (if (or (null (car response))
@@ -534,7 +540,7 @@ don't define this value."
      process
      (setq secure-word (unwind-protect
                           (base64-encode-string secure-word)
-                        (fillarray secure-word 0))))
+                        (fillarray secure-word 0))) t)
     (fillarray secure-word 0)
     (setq response (smtp-read-response process))
     (if (or (null (car response))
@@ -562,6 +568,89 @@ don't define this value."
            (>= (car response) 400))
        (throw 'done (car (cdr response))))))
  
+(defun smtp-auth-scram-md5 (process)
+  ;; now tesing
+  (let (server-msg-1 server-msg-2 client-msg-1 salted-pass
+                    response secure-word)
+    (smtp-send-command process "AUTH SCRAM-MD5")
+    (setq response (smtp-read-response process))
+    (if (or (null (car response))
+           (not (integerp (car response)))
+           (>= (car response) 400))
+       (throw 'done (car (cdr response))))
+    (unwind-protect
+       (smtp-send-command
+        process
+        (setq secure-word
+              (base64-encode-string
+               (setq client-msg-1
+                     (sasl-scram-md5-client-msg-1 
+                      smtp-authentication-user)))) t)
+      (fillarray secure-word 0))
+    (setq response (smtp-read-response process))
+    (if (or (null (car response))
+           (not (integerp (car response)))
+           (>= (car response) 400))
+       (progn
+         (fillarray client-msg-1 0)
+         (throw 'done (car (cdr response)))))
+    (setq secure-word
+         (unwind-protect
+             (substring (car (cdr response)) 4)
+           (fillarray (car (cdr response)) 0)))
+    (setq server-msg-1
+         (unwind-protect
+             (base64-decode-string secure-word)
+           (fillarray secure-word 0)))
+    (setq secure-word
+         (sasl-scram-md5-client-msg-2
+          server-msg-1 client-msg-1 
+          (setq salted-pass
+                (scram-md5-make-salted-pass
+                 smtp-authentication-passphrase
+                 (car
+                  (scram-md5-parse-server-msg-1 server-msg-1))))))
+    (setq secure-word
+         (unwind-protect
+             (base64-encode-string secure-word)
+           (fillarray secure-word 0)))
+    (unwind-protect
+       (smtp-send-command process secure-word t)
+      (fillarray secure-word 0))
+    (setq response (smtp-read-response process))
+    (if (or (null (car response))
+           (not (integerp (car response)))
+           (>= (car response) 400))
+       (progn 
+         (fillarray salted-pass 0)
+         (fillarray server-msg-1 0)
+         (fillarray client-msg-1 0)
+         (throw 'done (car (cdr response)))))
+    (setq server-msg-2
+         (unwind-protect
+             (base64-decode-string
+              (setq secure-word
+                    (substring (car (cdr response)) 4)))
+           (fillarray secure-word 0)))
+    (if (null
+        (unwind-protect
+            (sasl-scram-md5-authenticate-server
+             server-msg-1
+             server-msg-2
+             client-msg-1
+             salted-pass)
+          (fillarray salted-pass 0)
+          (fillarray server-msg-1 0)
+          (fillarray server-msg-2 0)
+          (fillarray client-msg-1 0)))
+       (throw 'done nil))
+    (smtp-send-command process "")
+    (setq response (smtp-read-response process))
+    (if (or (null (car response))
+           (not (integerp (car response)))
+           (>= (car response) 400))
+       (throw 'done (car (cdr response)))) ))
+    
 (provide 'smtp)
 
 ;;; smtp.el ends here