Tcl Library Source Code

Artifact [c8743f12d9]
Login

Artifact c8743f12d994f03c4444a493f62f48de7f0d89266aa0df0dbf361cd9e3e9fdfb:

Attachment "smtp.tcl.patch" to ticket [a12bff914d] added by raymin 2017-10-27 06:23:32. (unpublished)
--- tcllib-1.18/modules/mime/smtp.tcl.orig	2016-02-04 05:02:32.000000000 +0000
+++ tcllib-1.18/modules/mime/smtp.tcl	2017-10-27 06:10:16.956612550 +0000
@@ -95,10 +95,13 @@
 #             -maxsecs     Maximum number of seconds to allow the SMTP server
 #                          to accept the message. If not specified, the default
 #                          is 120 seconds.
-#             -usetls      A boolean flag. If the server supports it and we
-#                          have the package, use TLS to secure the connection.
-#             -tlspolicy   A command to call if the TLS negotiation fails for
-#                          some reason. Return 'insecure' to continue with
+#             -usetls      A string specifying how to use TLS.  A value of
+#                          'none' means not use TLS.  A value of 'starttls'
+#                          means if the server supports it and we have the
+#                          package, use TLS to secure the connection.  A value
+#                          of 'tls' means use direct TLS connection.
+#             -tlspolicy   A command to call if the STARTTLS negotiation fails
+#                          for some reason. Return 'insecure' to continue with
 #                          normal SMTP or 'secure' to close the connection and
 #                          try another server.
 #             -username    These are needed if your SMTP server requires
@@ -127,7 +130,7 @@
     set servers [list localhost]
     set client "" ;# default is set after options processing
     set ports [list 25]
-    set tlsP 1
+    set tlsP "starttls"
     set tlspolicy {}
     set username {}
     set password {}
@@ -152,7 +155,16 @@
             -atleastone {set aloP   [boolean $value]}
             -debug      {set debugP [boolean $value]}
             -queue      {set queueP [boolean $value]}
-            -usetls     {set tlsP   [boolean $value]}
+	    -usetls {
+		if {$value in {none starttls tls}} {
+		    set tlsP $value
+		} else {        # backward compatible
+		    if {[catch {boolean $value} result]} {
+			error "unknown value for $option: $value"
+		    }
+		    set tlsP [expr {$result ? "starttls" : "none"}]
+		}
+	    }
             -tlspolicy  {set tlspolicy $value}
 	    -maxsecs    {set maxsecs [expr {$value < 0 ? 0 : $value}]}
             -header {
@@ -210,6 +222,10 @@
         }
     }
 
+    if {$tlsP eq "tls"} {
+	package require tls
+    }
+
     if {[lsearch -glob $lowerL resent-*] >= 0} {
         set prefixL resent-
         set prefixM Resent-
@@ -593,8 +609,8 @@
 #                          request.
 #             -ports       A list of ports on mail servers that could process
 #                          the request (one port per server-- defaults to 25).
-#             -usetls      A boolean to indicate we will use TLS if possible.
-#             -tlspolicy   Command called if TLS setup fails.
+#             -usetls      A string specifying how to use TLS.
+#             -tlspolicy   Command called if STARTTLS setup fails.
 #             -username    These provide the authentication information 
 #             -password    to be used if needed by the SMTP server.
 #
@@ -615,7 +631,7 @@
     array set state [list afterID "" options "" readable 0]
     array set options [list -debug 0 -client localhost -multiple 1 \
                             -maxsecs 120 -queue 0 -servers localhost \
-                            -ports 25 -usetls 1 -tlspolicy {} \
+			    -ports 25 -usetls starttls -tlspolicy {} \
                             -username {} -password {}]
     array set options $args
     set state(options) [array get options]
@@ -643,7 +659,7 @@
         if {[set code [catch {
             set state(sd) [socket -async $server $port]
             fconfigure $state(sd) -blocking off -translation binary
-            fileevent $state(sd) readable [list ::smtp::readable $token]
+	    fileevent $state(sd) writable [list ::smtp::connected $token]
         } result]]} {
             set ecode $errorCode
             set einfo $errorInfo
@@ -684,6 +700,35 @@
     return -code $code -errorinfo $einfo -errorcode $ecode $result
 }
 
+proc ::smtp::connected {token} {
+    variable $token
+    upvar 0 $token state
+
+    fileevent $state(sd) writable {}
+
+    set err [fconfigure $state(sd) -error]
+    if {$err ne ""} {
+	set state(readable) -2
+	set state(error) $err
+	return
+    }
+
+    array set options $state(options)
+    if {$options(-usetls) eq "tls"} {
+	if {[catch {
+	    ::tls::import $state(sd)
+	    ::tls::handshake $state(sd)
+	} result]} {
+	    set state(readable) -2
+	    set state(error) $result
+	    return
+	}
+	set state(tls) 1
+    }
+
+    fileevent $state(sd) readable [list ::smtp::readable $token]
+}
+
 # If we cannot load the tls package, ignore the error
 proc ::smtp::load_tls {} {
     set r [catch {package require tls}]
@@ -741,7 +786,7 @@
         
         # Support STARTTLS extension.
         # The state(tls) item is used to see if we have already tried this.
-        if {($options(-usetls)) && ![info exists state(tls)] \
+	if {($options(-usetls) eq "starttls") && ![info exists state(tls)] \
                 && (([lsearch $response(args) STARTTLS] >= 0)
                     || ([lsearch $response(args) TLS] >= 0))} {
             if {![load_tls]} {