KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > plugins > smtpExtension > TLSSwitcher


1 /*
2  *
3  * Jsmtpd, Java SMTP daemon
4  * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */

21 package org.jsmtpd.plugins.smtpExtension;
22
23 import java.io.BufferedWriter JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.net.Socket JavaDoc;
27 import java.net.URL JavaDoc;
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 /**
42  * @author Jean-Francois POUX
43  */

44 public class TLSSwitcher implements ISmtpExtension {
45
46     private Log log = LogFactory.getLog(TLSSwitcher.class);
47     private SSLSocketFactory sfact;
48
49     private String JavaDoc keystoreName;
50     private transient String JavaDoc keystorePassword;
51
52     public boolean smtpTrigger(String JavaDoc command, IProtocolHandler protocol) throws SmtpExtensionException, IOException JavaDoc {
53         if ((command == null) || (command.length() < 8)) {
54             return false;
55         }
56         String JavaDoc 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 JavaDoc 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 JavaDoc {
72         try {
73             Socket JavaDoc 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 JavaDoc wr = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(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 JavaDoc e) {
84             log.error("error while switching to secured mode : ", e);
85             throw e;
86         }
87     }
88
89     public String JavaDoc getWelcome() {
90         return "STARTTLS";
91     }
92
93     public String JavaDoc getPluginName() {
94         return "TLS channel switcher for Jsmtpd";
95     }
96
97     public void initPlugin() throws PluginInitException {
98         // place the keystore on system var
99
URL JavaDoc url = this.getClass().getClassLoader().getResource(keystoreName);
100         if (url != null) {
101             String JavaDoc ks = url.getFile();
102             System.setProperty("javax.net.ssl.keyStore", ks);
103             System.setProperty("javax.net.ssl.keyStorePassword", keystorePassword);
104             //Todo: try to open keystore
105
} 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         // nothing
114
}
115
116
117     public void setKeystoreName(String JavaDoc keystoreName) {
118         this.keystoreName = keystoreName;
119     }
120
121     public void setKeystorePassword(String JavaDoc keystorePassword) {
122         this.keystorePassword = keystorePassword;
123     }
124
125     public boolean smtpPreTrigger(String JavaDoc command, IProtocolHandler protocol) throws SmtpExtensionException, IOException JavaDoc, InputSizeToBig, IOException JavaDoc, BareLFException {
126         return false;
127     }
128 }
Popular Tags