commits@javamail.java.net

[mercurial:83] Add mail.smtp.starttls.required property to require use of STARTTLS - GF

From: <shannon_at_kenai.com>
Date: Tue, 11 Nov 2008 01:05:12 +0000 (GMT)

Repository: mercurial
Revision: 83
Author: Bill Shannon <bill.shannon_at_sun.com>
Date: 2008-09-10 22:19:25 UTC

Log Message:
-----------
Add mail.smtp.starttls.required property to require use of STARTTLS -
GF 5861

Modified Paths:
--------------
    doc/release/CHANGES.txt
    mail/src/main/java/com/sun/mail/smtp/SMTPTransport.java
    mail/src/main/java/com/sun/mail/smtp/package.html

Diffs:
-----
diff -r c74f24a26260 -r ed3db1bebae7 doc/release/CHANGES.txt
--- a/doc/release/CHANGES.txt Mon Sep 08 17:57:21 2008 -0700
+++ b/doc/release/CHANGES.txt Wed Sep 10 15:19:25 2008 -0700
@@ -30,6 +30,7 @@
 GF 3929 Inconsistent synchronization in
com.sun.mail.iap.Protocol
 GF 4997 BASE64DecoderStream.skip (etc) skips the wrong number
of octets
 GF 5189 Can't specify SSLSocketFactory for STARTTLS in Javamail
1.4
+GF 5861 add mail.smtp.starttls.required property to require use
of STARTTLS
 <no id> ignore socket timeout while waiting in IMAP IDLE
 <no id> fix bug in MailDateFormat parsing in non-lenient mode
 <no id> add mail.mime.multipart.allowempty system property to
handle (illegal)
diff -r c74f24a26260 -r ed3db1bebae7
mail/src/main/java/com/sun/mail/smtp/SMTPTransport.java
--- a/mail/src/main/java/com/sun/mail/smtp/SMTPTransport.java Mon Sep
08 17:57:21 2008 -0700
+++ b/mail/src/main/java/com/sun/mail/smtp/SMTPTransport.java Wed Sep
10 15:19:25 2008 -0700
@@ -106,6 +106,7 @@
 
     private boolean reportSuccess; // throw an exception even on
success
     private boolean useStartTLS; // use STARTTLS command
+ private boolean requireStartTLS; // require STARTTLS command
     private boolean useRset; // use RSET instead of NOOP
 
     private PrintStream out; // debug output stream
@@ -159,6 +160,10 @@
        // mail.smtp.starttls.enable enables use of STARTTLS command
        useStartTLS = PropUtil.getBooleanSessionProperty(session,
                                "mail." + name + ".starttls.enable",
false);
+
+ // mail.smtp.starttls.required requires use of STARTTLS command
+ requireStartTLS = PropUtil.getBooleanSessionProperty(session,
+ "mail." + name + ".starttls.required",
false);
 
        // mail.smtp.userset causes us to use RSET instead of NOOP
        // for isConnected
@@ -309,6 +314,28 @@
     }
 
     /**
+ * Should we require the STARTTLS command to secure the
connection?
+ *
+ * @return true if the STARTTLS command will be required
+ *
+ * @since JavaMail 1.4.2
+ */
+ public synchronized boolean getRequireStartTLS() {
+ return requireStartTLS;
+ }
+
+ /**
+ * Set whether the STARTTLS command should be required.
+ *
+ * @param requireStartTLS should we require the STARTTLS command?
+ *
+ * @since JavaMail 1.4.2
+ */
+ public synchronized void setRequireStartTLS(boolean
requireStartTLS) {
+ this.requireStartTLS = requireStartTLS;
+ }
+
+ /**
      * Should we use the RSET command instead of the NOOP command
      * in the @{link #isConnected isConnected} method?
      *
@@ -429,14 +456,21 @@
        if (!succeed)
            helo(getLocalHost());
 
- if (useStartTLS && supportsExtension("STARTTLS")) {
- startTLS();
- /*
- * Have to issue another EHLO to update list of extensions
- * supported, especially authentication mechanisms.
- * Don't know if this could ever fail, but we ignore
failure.
- */
- ehlo(getLocalHost());
+ if (useStartTLS || requireStartTLS) {
+ if (supportsExtension("STARTTLS")) {
+ startTLS();
+ /*
+ * Have to issue another EHLO to update list of
extensions
+ * supported, especially authentication mechanisms.
+ * Don't know if this could ever fail, but we ignore
failure.
+ */
+ ehlo(getLocalHost());
+ } else if (requireStartTLS) {
+ if (debug)
+ out.println(
+ "DEBUG SMTP: STARTTLS required but not
supported");
+ return false;
+ }
        }
 
        if ((useAuth || (user != null && passwd != null)) &&
diff -r c74f24a26260 -r ed3db1bebae7
mail/src/main/java/com/sun/mail/smtp/package.html
--- a/mail/src/main/java/com/sun/mail/smtp/package.html Mon Sep 08
17:57:21 2008 -0700
+++ b/mail/src/main/java/com/sun/mail/smtp/package.html Wed Sep 10
15:19:25 2008 -0700
@@ -479,6 +479,17 @@
 </TR>
 
 <TR>
+<TD>mail.smtp.starttls.required</TD>
+<TD>boolean</TD>
+<TD>
+If true, requires the use of the <code>STARTTLS</code> command.
+If the server doesn't support the STARTTLS command, or the command
+fails, the connect method will fail.
+Defaults to false.
+</TD>
+</TR>
+
+<TR>
 <TD>mail.smtp.userset</TD>
 <TD>boolean</TD>
 <TD>