1 21 package org.jsmtpd.plugins.smtpExtension; 22 23 import java.io.BufferedWriter ; 24 import java.io.IOException ; 25 import java.io.OutputStreamWriter ; 26 import java.net.Socket ; 27 import java.net.URL ; 28 29 import javax.net.ssl.SSLSocket; 30 import javax.net.ssl.SSLSocketFactory; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 import org.jsmtpd.core.common.PluginInitException; 35 import org.jsmtpd.core.common.io.BareLFException; 36 import org.jsmtpd.core.common.io.InputSizeToBig; 37 import org.jsmtpd.core.common.smtpExtension.IProtocolHandler; 38 import org.jsmtpd.core.common.smtpExtension.ISmtpExtension; 39 import org.jsmtpd.core.common.smtpExtension.SmtpExtensionException; 40 41 44 public class TLSSwitcher implements ISmtpExtension { 45 46 private Log log = LogFactory.getLog(TLSSwitcher.class); 47 private SSLSocketFactory sfact; 48 49 private String keystoreName; 50 private transient String keystorePassword; 51 52 public boolean smtpTrigger(String command, IProtocolHandler protocol) throws SmtpExtensionException, IOException { 53 if ((command == null) || (command.length() < 8)) { 54 return false; 55 } 56 String tmp = command.substring(0, 8).toUpperCase(); 57 if (!"STARTTLS".equals(tmp)) 58 return false; 59 60 try { 61 log.debug("Trying to switch to TLS Mode"); 62 handleTLSRequest(protocol); 63 log.debug("Switched to TLS Mode"); 64 } catch (IOException e) { 65 log.error("IO Error while switching to TLS Mode"); 66 throw e; 67 } 68 return true; 69 } 70 71 private void handleTLSRequest(IProtocolHandler protocol) throws IOException { 72 try { 73 Socket old = protocol.getSock(); 74 SSLSocket securedSocket = (SSLSocket) sfact.createSocket(old, old.getInetAddress().getHostName(), old.getPort(), true); 75 securedSocket.setEnabledCipherSuites(securedSocket.getSupportedCipherSuites()); 76 securedSocket.setUseClientMode(false); 77 BufferedWriter wr = new BufferedWriter (new OutputStreamWriter (old.getOutputStream())); 78 wr.write("220 Go ahead\r\n"); 79 wr.flush(); 80 securedSocket.startHandshake(); 81 protocol.setSock(securedSocket); 82 protocol.setSecured(true); 83 } catch (IOException e) { 84 log.error("error while switching to secured mode : ", e); 85 throw e; 86 } 87 } 88 89 public String getWelcome() { 90 return "STARTTLS"; 91 } 92 93 public String getPluginName() { 94 return "TLS channel switcher for Jsmtpd"; 95 } 96 97 public void initPlugin() throws PluginInitException { 98 URL url = this.getClass().getClassLoader().getResource(keystoreName); 100 if (url != null) { 101 String ks = url.getFile(); 102 System.setProperty("javax.net.ssl.keyStore", ks); 103 System.setProperty("javax.net.ssl.keyStorePassword", keystorePassword); 104 } else { 106 log.info("keystore file not found, SSL not available"); 107 throw new PluginInitException(); 108 } 109 sfact = (SSLSocketFactory) SSLSocketFactory.getDefault(); 110 } 111 112 public void shutdownPlugin() { 113 } 115 116 117 public void setKeystoreName(String keystoreName) { 118 this.keystoreName = keystoreName; 119 } 120 121 public void setKeystorePassword(String keystorePassword) { 122 this.keystorePassword = keystorePassword; 123 } 124 125 public boolean smtpPreTrigger(String command, IProtocolHandler protocol) throws SmtpExtensionException, IOException , InputSizeToBig, IOException , BareLFException { 126 return false; 127 } 128 } | Popular Tags |