2006-12-12 18:57:24 -05:00
|
|
|
package davmail.smtp;
|
|
|
|
|
|
|
|
import davmail.AbstractConnection;
|
2009-04-23 10:54:06 -04:00
|
|
|
import davmail.BundleMessage;
|
2008-10-31 13:12:30 -04:00
|
|
|
import davmail.exchange.ExchangeSessionFactory;
|
2009-04-03 03:38:31 -04:00
|
|
|
import davmail.ui.tray.DavGatewayTray;
|
2006-12-12 18:57:24 -05:00
|
|
|
|
2008-12-09 05:10:38 -05:00
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
|
import javax.mail.internet.AddressException;
|
2006-12-12 18:57:24 -05:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.Socket;
|
2008-12-05 03:48:54 -05:00
|
|
|
import java.net.SocketException;
|
2006-12-12 18:57:24 -05:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.StringTokenizer;
|
2008-12-09 05:10:38 -05:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.ArrayList;
|
2006-12-12 18:57:24 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dav Gateway smtp connection implementation
|
|
|
|
*/
|
|
|
|
public class SmtpConnection extends AbstractConnection {
|
|
|
|
|
|
|
|
// Initialize the streams and start the thread
|
2006-12-14 10:14:18 -05:00
|
|
|
public SmtpConnection(Socket clientSocket) {
|
2009-04-23 10:54:06 -04:00
|
|
|
super(SmtpConnection.class.getName(), clientSocket, null);
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
|
2009-04-16 17:52:17 -04:00
|
|
|
@Override
|
2006-12-12 18:57:24 -05:00
|
|
|
public void run() {
|
|
|
|
String line;
|
|
|
|
StringTokenizer tokens;
|
2008-12-09 05:10:38 -05:00
|
|
|
List<String> recipients = new ArrayList<String>();
|
2006-12-12 18:57:24 -05:00
|
|
|
|
|
|
|
try {
|
2008-10-31 13:12:30 -04:00
|
|
|
ExchangeSessionFactory.checkConfig();
|
2006-12-12 18:57:24 -05:00
|
|
|
sendClient("220 DavMail SMTP ready at " + new Date());
|
|
|
|
for (; ;) {
|
|
|
|
line = readClient();
|
|
|
|
// unable to read line, connection closed ?
|
|
|
|
if (line == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens = new StringTokenizer(line);
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
String command = tokens.nextToken();
|
|
|
|
|
2009-01-29 17:18:53 -05:00
|
|
|
if (state == State.LOGIN) {
|
2008-09-18 17:34:03 -04:00
|
|
|
// AUTH LOGIN, read userName
|
2008-11-03 20:58:08 -05:00
|
|
|
userName = base64Decode(line);
|
2008-09-18 17:34:03 -04:00
|
|
|
sendClient("334 " + base64Encode("Password:"));
|
2009-01-29 17:18:53 -05:00
|
|
|
state = State.PASSWORD;
|
|
|
|
} else if (state == State.PASSWORD) {
|
2008-09-18 17:34:03 -04:00
|
|
|
// AUTH LOGIN, read password
|
2008-11-03 20:58:08 -05:00
|
|
|
password = base64Decode(line);
|
2008-09-18 17:34:03 -04:00
|
|
|
authenticate();
|
|
|
|
} else if ("QUIT".equalsIgnoreCase(command)) {
|
2006-12-12 18:57:24 -05:00
|
|
|
sendClient("221 Closing connection");
|
|
|
|
break;
|
|
|
|
} else if ("EHLO".equals(command)) {
|
2008-01-31 18:41:17 -05:00
|
|
|
sendClient("250-" + tokens.nextToken());
|
2006-12-12 18:57:24 -05:00
|
|
|
// inform server that AUTH is supported
|
|
|
|
// actually it is mandatory (only way to get credentials)
|
|
|
|
sendClient("250-AUTH LOGIN PLAIN");
|
|
|
|
sendClient("250 Hello");
|
|
|
|
} else if ("HELO".equals(command)) {
|
|
|
|
sendClient("250 Hello");
|
|
|
|
} else if ("AUTH".equals(command)) {
|
|
|
|
if (tokens.hasMoreElements()) {
|
|
|
|
String authType = tokens.nextToken();
|
|
|
|
if ("PLAIN".equals(authType) && tokens.hasMoreElements()) {
|
|
|
|
decodeCredentials(tokens.nextToken());
|
2008-09-18 17:34:03 -04:00
|
|
|
authenticate();
|
|
|
|
} else if ("LOGIN".equals(authType)) {
|
|
|
|
sendClient("334 " + base64Encode("Username:"));
|
2009-01-29 17:18:53 -05:00
|
|
|
state = State.LOGIN;
|
2006-12-12 18:57:24 -05:00
|
|
|
} else {
|
|
|
|
sendClient("451 Error : unknown authentication type");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendClient("451 Error : authentication type not specified");
|
|
|
|
}
|
|
|
|
} else if ("MAIL".equals(command)) {
|
2009-01-29 17:18:53 -05:00
|
|
|
if (state == State.AUTHENTICATED) {
|
|
|
|
state = State.STARTMAIL;
|
2008-12-09 05:10:38 -05:00
|
|
|
recipients.clear();
|
2006-12-12 18:57:24 -05:00
|
|
|
sendClient("250 Sender OK");
|
|
|
|
} else {
|
2009-01-29 17:18:53 -05:00
|
|
|
state = State.INITIAL;
|
2006-12-12 18:57:24 -05:00
|
|
|
sendClient("503 Bad sequence of commands");
|
|
|
|
}
|
|
|
|
} else if ("RCPT".equals(command)) {
|
2009-01-29 17:18:53 -05:00
|
|
|
if (state == State.STARTMAIL || state == State.RECIPIENT) {
|
2008-12-09 05:10:38 -05:00
|
|
|
if (line.startsWith("RCPT TO:")) {
|
2009-01-29 17:18:53 -05:00
|
|
|
state = State.RECIPIENT;
|
2008-12-09 05:10:38 -05:00
|
|
|
try {
|
|
|
|
InternetAddress internetAddress = new InternetAddress(line.substring("RCPT TO:".length()));
|
|
|
|
recipients.add(internetAddress.getAddress());
|
|
|
|
} catch (AddressException e) {
|
2009-01-29 17:18:53 -05:00
|
|
|
throw new IOException("Invalid recipient: " + line);
|
2008-12-09 05:10:38 -05:00
|
|
|
}
|
|
|
|
sendClient("250 Recipient OK");
|
|
|
|
} else {
|
|
|
|
sendClient("500 Unrecognized command");
|
|
|
|
}
|
|
|
|
|
2006-12-12 18:57:24 -05:00
|
|
|
} else {
|
2009-01-29 17:18:53 -05:00
|
|
|
state = State.AUTHENTICATED;
|
2006-12-12 18:57:24 -05:00
|
|
|
sendClient("503 Bad sequence of commands");
|
|
|
|
}
|
|
|
|
} else if ("DATA".equals(command)) {
|
2009-01-29 17:18:53 -05:00
|
|
|
if (state == State.RECIPIENT) {
|
|
|
|
state = State.MAILDATA;
|
2006-12-12 18:57:24 -05:00
|
|
|
sendClient("354 Start mail input; end with <CRLF>.<CRLF>");
|
|
|
|
|
|
|
|
try {
|
2008-12-09 05:10:38 -05:00
|
|
|
session.sendMessage(recipients, in);
|
2009-01-29 17:18:53 -05:00
|
|
|
state = State.AUTHENTICATED;
|
2006-12-12 18:57:24 -05:00
|
|
|
sendClient("250 Queued mail for delivery");
|
|
|
|
} catch (Exception e) {
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.error(e);
|
2009-01-29 17:18:53 -05:00
|
|
|
state = State.AUTHENTICATED;
|
2009-04-23 16:53:22 -04:00
|
|
|
sendClient("451 Error : " + e + ' ' + e.getMessage());
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2009-01-29 17:18:53 -05:00
|
|
|
state = State.AUTHENTICATED;
|
2006-12-12 18:57:24 -05:00
|
|
|
sendClient("503 Bad sequence of commands");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
sendClient("500 Unrecognized command");
|
|
|
|
}
|
|
|
|
|
|
|
|
os.flush();
|
|
|
|
}
|
2008-12-05 03:48:54 -05:00
|
|
|
|
|
|
|
} catch (SocketException e) {
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.debug(new BundleMessage("LOG_CONNECTION_CLOSED"));
|
2009-04-15 11:20:06 -04:00
|
|
|
} catch (Exception e) {
|
|
|
|
DavGatewayTray.error(e);
|
2007-04-25 19:29:46 -04:00
|
|
|
try {
|
2009-04-15 11:20:06 -04:00
|
|
|
sendClient("500 " + ((e.getMessage()==null)?e:e.getMessage()));
|
2007-04-25 19:29:46 -04:00
|
|
|
} catch (IOException e2) {
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.debug(new BundleMessage("LOG_EXCEPTION_SENDING_ERROR_TO_CLIENT"), e2);
|
2007-04-25 19:29:46 -04:00
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
} finally {
|
2007-05-15 05:37:55 -04:00
|
|
|
close();
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
DavGatewayTray.resetIcon();
|
|
|
|
}
|
|
|
|
|
2008-09-18 17:34:03 -04:00
|
|
|
/**
|
|
|
|
* Create authenticated session with Exchange server
|
2008-12-05 03:48:54 -05:00
|
|
|
*
|
2008-09-18 17:34:03 -04:00
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
|
|
|
protected void authenticate() throws IOException {
|
|
|
|
try {
|
2008-10-31 13:12:30 -04:00
|
|
|
session = ExchangeSessionFactory.getInstance(userName, password);
|
2008-09-18 17:34:03 -04:00
|
|
|
sendClient("235 OK Authenticated");
|
2009-01-29 17:18:53 -05:00
|
|
|
state = State.AUTHENTICATED;
|
2008-09-18 17:34:03 -04:00
|
|
|
} catch (Exception e) {
|
2008-11-24 07:35:58 -05:00
|
|
|
DavGatewayTray.error(e);
|
2008-09-18 17:34:03 -04:00
|
|
|
String message = e.getMessage();
|
|
|
|
if (message == null) {
|
|
|
|
message = e.toString();
|
|
|
|
}
|
|
|
|
message = message.replaceAll("\\n", " ");
|
|
|
|
sendClient("554 Authenticated failed " + message);
|
2009-01-29 17:18:53 -05:00
|
|
|
state = State.INITIAL;
|
2008-09-18 17:34:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-12-12 18:57:24 -05:00
|
|
|
/**
|
|
|
|
* Decode SMTP credentials
|
|
|
|
*
|
|
|
|
* @param encodedCredentials smtp encoded credentials
|
2009-04-16 18:20:30 -04:00
|
|
|
* @throws IOException if invalid credentials
|
2006-12-12 18:57:24 -05:00
|
|
|
*/
|
|
|
|
protected void decodeCredentials(String encodedCredentials) throws IOException {
|
2008-09-18 17:34:03 -04:00
|
|
|
String decodedCredentials = base64Decode(encodedCredentials);
|
2006-12-12 18:57:24 -05:00
|
|
|
int index = decodedCredentials.indexOf((char) 0, 1);
|
|
|
|
if (index > 0) {
|
|
|
|
userName = decodedCredentials.substring(1, index);
|
|
|
|
password = decodedCredentials.substring(index + 1);
|
|
|
|
} else {
|
|
|
|
throw new IOException("Invalid credentials");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|