remove autoconfig
This commit is contained in:
parent
fd73ab353d
commit
bd8a86e455
@ -44,11 +44,6 @@
|
|||||||
<servlet-class>de.hsadmin.pillar.JsonPillarServlet</servlet-class>
|
<servlet-class>de.hsadmin.pillar.JsonPillarServlet</servlet-class>
|
||||||
</servlet>
|
</servlet>
|
||||||
|
|
||||||
<servlet>
|
|
||||||
<servlet-name>AutoconfigServlet</servlet-name>
|
|
||||||
<servlet-class>de.hsadmin.autoconfig.AutoconfigAutodiscoverServlet</servlet-class>
|
|
||||||
</servlet>
|
|
||||||
|
|
||||||
<servlet-mapping>
|
<servlet-mapping>
|
||||||
<servlet-name>Queue Status Servlet</servlet-name>
|
<servlet-name>Queue Status Servlet</servlet-name>
|
||||||
<url-pattern>/queueStatus</url-pattern>
|
<url-pattern>/queueStatus</url-pattern>
|
||||||
@ -59,16 +54,6 @@
|
|||||||
<url-pattern>/pillar</url-pattern>
|
<url-pattern>/pillar</url-pattern>
|
||||||
</servlet-mapping>
|
</servlet-mapping>
|
||||||
|
|
||||||
<servlet-mapping>
|
|
||||||
<servlet-name>AutoconfigServlet</servlet-name>
|
|
||||||
<url-pattern>/config-v1.1.xml</url-pattern>
|
|
||||||
</servlet-mapping>
|
|
||||||
|
|
||||||
<servlet-mapping>
|
|
||||||
<servlet-name>AutoconfigServlet</servlet-name>
|
|
||||||
<url-pattern>/autodiscover.xml</url-pattern>
|
|
||||||
</servlet-mapping>
|
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
<servlet-mapping>
|
<servlet-mapping>
|
||||||
<servlet-name>Queue PacTasks Servlet</servlet-name>
|
<servlet-name>Queue PacTasks Servlet</servlet-name>
|
||||||
|
@ -1,235 +0,0 @@
|
|||||||
package de.hsadmin.autoconfig;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
|
||||||
import javax.persistence.Query;
|
|
||||||
import javax.servlet.ServletException;
|
|
||||||
import javax.servlet.ServletInputStream;
|
|
||||||
import javax.servlet.http.HttpServlet;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
import javax.xml.transform.Transformer;
|
|
||||||
import javax.xml.transform.TransformerException;
|
|
||||||
import javax.xml.transform.TransformerFactory;
|
|
||||||
import javax.xml.transform.dom.DOMSource;
|
|
||||||
import javax.xml.transform.stream.StreamResult;
|
|
||||||
|
|
||||||
import org.apache.commons.httpclient.HttpStatus;
|
|
||||||
import org.w3c.dom.DOMException;
|
|
||||||
import org.w3c.dom.Document;
|
|
||||||
import org.w3c.dom.Element;
|
|
||||||
import org.w3c.dom.NodeList;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
|
|
||||||
import de.hsadmin.core.model.Transaction;
|
|
||||||
import de.hsadmin.core.util.HSAdminException;
|
|
||||||
import de.hsadmin.mods.email.EMailAddress;
|
|
||||||
import de.hsadmin.mods.user.UnixUser;
|
|
||||||
|
|
||||||
public class AutoconfigAutodiscoverServlet extends HttpServlet {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Thunderbird autoconfig
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
||||||
final String emailAddr = req.getParameter("emailaddress");
|
|
||||||
if (emailAddr == null || emailAddr.length() == 0) {
|
|
||||||
resp.sendError(HttpStatus.SC_UNAUTHORIZED);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final String[] addrParts = emailAddr.split("@");
|
|
||||||
if (addrParts.length != 2) {
|
|
||||||
resp.sendError(HttpStatus.SC_BAD_REQUEST);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
String uniqueMailbox = null;
|
|
||||||
try {
|
|
||||||
uniqueMailbox = getUniqueMailbox(emailAddr);
|
|
||||||
} catch (HSAdminException e) {
|
|
||||||
throw new ServletException(e);
|
|
||||||
}
|
|
||||||
final String pacName = uniqueMailbox.substring(0, 5);
|
|
||||||
final String pacDomain = pacName + ".hostsharing.net";
|
|
||||||
final String userName = uniqueMailbox;
|
|
||||||
try {
|
|
||||||
final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
|
||||||
final DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
|
||||||
final Document document = builder.newDocument();
|
|
||||||
final Element clientConfig = document.createElement("clientConfig");
|
|
||||||
clientConfig.setAttribute("version", "1.1");
|
|
||||||
document.appendChild(clientConfig);
|
|
||||||
final Element emailProvider = appendElementWithAttribute(document, clientConfig, "emailProvider", "id", "hostsharing.net");
|
|
||||||
appendElementWithText(document, emailProvider, "domain", "hostsharing.net");
|
|
||||||
final Element incomingServer = appendElementWithAttribute(document, emailProvider, "incomingServer", "type", "imap");
|
|
||||||
appendElementWithText(document, incomingServer, "hostname", pacDomain);
|
|
||||||
appendElementWithText(document, incomingServer, "port", "993");
|
|
||||||
appendElementWithText(document, incomingServer, "socketType", "SSL");
|
|
||||||
appendElementWithText(document, incomingServer, "username", userName);
|
|
||||||
appendElementWithText(document, incomingServer, "authentication", "password-cleartext");
|
|
||||||
final Element outgoingServer = appendElementWithAttribute(document, emailProvider, "outgoingServer", "type", "smtp");
|
|
||||||
appendElementWithText(document, outgoingServer, "hostname", pacDomain);
|
|
||||||
appendElementWithText(document, outgoingServer, "port", "465");
|
|
||||||
appendElementWithText(document, outgoingServer, "socketType", "SSL");
|
|
||||||
appendElementWithText(document, outgoingServer, "username", userName);
|
|
||||||
appendElementWithText(document, outgoingServer, "authentication", "password-cleartext");
|
|
||||||
appendElementWithText(document, outgoingServer, "addThisServer", "true");
|
|
||||||
appendElementWithText(document, outgoingServer, "useGlobalPreferredServer", "true");
|
|
||||||
serializeDocument(document, resp);
|
|
||||||
} catch (DOMException e) {
|
|
||||||
throw new ServletException(e);
|
|
||||||
} catch (ParserConfigurationException e) {
|
|
||||||
throw new ServletException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Microsoft autodiscover
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
||||||
final ServletInputStream xmlInputStream = req.getInputStream();
|
|
||||||
try {
|
|
||||||
String emailAddr = "";
|
|
||||||
final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
|
||||||
DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
|
||||||
final Document requestDocument = builder.parse(xmlInputStream);
|
|
||||||
final Element documentElement = requestDocument.getDocumentElement();
|
|
||||||
documentElement.normalize();
|
|
||||||
final NodeList emailAddressNodeList = documentElement.getElementsByTagName("EMailAddress");
|
|
||||||
if (emailAddressNodeList.getLength() == 1) {
|
|
||||||
emailAddr = emailAddressNodeList.item(0).getTextContent().trim();
|
|
||||||
}
|
|
||||||
final String[] addrParts = emailAddr.split("@");
|
|
||||||
if (addrParts.length != 2) {
|
|
||||||
resp.sendError(HttpStatus.SC_BAD_REQUEST);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
String uniqueMailbox = null;
|
|
||||||
try {
|
|
||||||
uniqueMailbox = getUniqueMailbox(emailAddr);
|
|
||||||
} catch (HSAdminException e) {
|
|
||||||
throw new ServletException(e);
|
|
||||||
}
|
|
||||||
final Document document = buildAutodiscoverDocument(builderFactory, uniqueMailbox);
|
|
||||||
serializeDocument(document, resp);
|
|
||||||
} catch (ParserConfigurationException e) {
|
|
||||||
throw new ServletException(e);
|
|
||||||
} catch (SAXException e) {
|
|
||||||
throw new ServletException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Document buildAutodiscoverDocument(final DocumentBuilderFactory builderFactory, String uniqueMailbox)
|
|
||||||
throws ParserConfigurationException {
|
|
||||||
DocumentBuilder builder;
|
|
||||||
final String pacName = uniqueMailbox.substring(0, 5);
|
|
||||||
final String pacDomain = pacName + ".hostsharing.net";
|
|
||||||
final String userName = uniqueMailbox;
|
|
||||||
builder = builderFactory.newDocumentBuilder();
|
|
||||||
final Document document = builder.newDocument();
|
|
||||||
final Element autodiscover = document.createElementNS("http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006", "Autodiscover");
|
|
||||||
document.appendChild(autodiscover);
|
|
||||||
final Element response = document.createElementNS("http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a", "Response");
|
|
||||||
autodiscover.appendChild(response);
|
|
||||||
final Element account = document.createElement("Account");
|
|
||||||
response.appendChild(account);
|
|
||||||
appendElementWithText(document, account, "AccountType", "email");
|
|
||||||
appendElementWithText(document, account, "Action", "settings");
|
|
||||||
final Element protocolIMAP = document.createElement("Protocol");
|
|
||||||
account.appendChild(protocolIMAP);
|
|
||||||
appendElementWithText(document, protocolIMAP, "Type", "IMAP");
|
|
||||||
appendElementWithText(document, protocolIMAP, "Server", pacDomain);
|
|
||||||
appendElementWithText(document, protocolIMAP, "Port", "993");
|
|
||||||
appendElementWithText(document, protocolIMAP, "DomainRequired", "off");
|
|
||||||
appendElementWithText(document, protocolIMAP, "SPA", "off");
|
|
||||||
appendElementWithText(document, protocolIMAP, "SSL", "on");
|
|
||||||
appendElementWithText(document, protocolIMAP, "AuthRequired", "on");
|
|
||||||
appendElementWithText(document, protocolIMAP, "LoginName", userName);
|
|
||||||
final Element protocolSMTP = document.createElement("Protocol");
|
|
||||||
account.appendChild(protocolSMTP);
|
|
||||||
appendElementWithText(document, protocolSMTP, "Type", "SMTP");
|
|
||||||
appendElementWithText(document, protocolSMTP, "Server", pacDomain);
|
|
||||||
appendElementWithText(document, protocolSMTP, "Port", "465");
|
|
||||||
appendElementWithText(document, protocolSMTP, "DomainRequired", "off");
|
|
||||||
appendElementWithText(document, protocolSMTP, "SPA", "off");
|
|
||||||
appendElementWithText(document, protocolSMTP, "SSL", "on");
|
|
||||||
appendElementWithText(document, protocolSMTP, "AuthRequired", "on");
|
|
||||||
appendElementWithText(document, protocolSMTP, "LoginName", userName);
|
|
||||||
return document;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Element appendElementWithAttribute(final Document document, final Element parent,
|
|
||||||
final String childElementName, final String attributeName, final String attributeValue) {
|
|
||||||
final Element child = document.createElement(childElementName);
|
|
||||||
child.setAttribute(attributeName, attributeValue);
|
|
||||||
parent.appendChild(child);
|
|
||||||
return child;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Element appendElementWithText(final Document document, final Element parent,
|
|
||||||
final String childElementName, final String text) {
|
|
||||||
final Element child = document.createElement(childElementName);
|
|
||||||
parent.appendChild(child);
|
|
||||||
child.appendChild(document.createTextNode(text));
|
|
||||||
return child;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void serializeDocument(final Document document, final HttpServletResponse resp)
|
|
||||||
throws ServletException {
|
|
||||||
resp.setContentType("text/xml");
|
|
||||||
try {
|
|
||||||
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
|
||||||
final Transformer transformer = transformerFactory.newTransformer();
|
|
||||||
transformer.transform(new DOMSource(document), new StreamResult(resp.getOutputStream()));
|
|
||||||
} catch (TransformerException e) {
|
|
||||||
throw new ServletException(e);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new ServletException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getUniqueMailbox(final String email) throws HSAdminException {
|
|
||||||
final String[] addrParts = email.split("@");
|
|
||||||
if (addrParts.length != 2) {
|
|
||||||
throw new HSAdminException("email address not valid");
|
|
||||||
}
|
|
||||||
final String localpart = addrParts[0];
|
|
||||||
final String domain = addrParts[1];
|
|
||||||
final Transaction transaction = new Transaction("autoconfig");
|
|
||||||
final EntityManager em = transaction.getEntityManager();
|
|
||||||
final Query emailQuery = em.createQuery("SELECT addr FROM EMailAddresses addr "
|
|
||||||
+ "WHERE addr.localpart = :localpart AND addr.domain.name = :domain AND ( addr.subdomain IS NULL OR addr.subdomain = :subdomain)");
|
|
||||||
emailQuery.setParameter("subdomain", "");
|
|
||||||
emailQuery.setParameter("domain", domain);
|
|
||||||
emailQuery.setParameter("localpart", localpart);
|
|
||||||
final Object emailResult = emailQuery.getSingleResult();
|
|
||||||
if (emailResult == null) {
|
|
||||||
transaction.close();
|
|
||||||
throw new HSAdminException("email address unknown");
|
|
||||||
}
|
|
||||||
final EMailAddress emailAddress = (EMailAddress) emailResult;
|
|
||||||
final String target = emailAddress.getTarget();
|
|
||||||
if (target == null || target.length() < 5 || (target.length() > 6 && target.charAt(5) != '-')) {
|
|
||||||
transaction.close();
|
|
||||||
throw new HSAdminException("no unique mailbox found");
|
|
||||||
}
|
|
||||||
final Query mboxQuery = em.createQuery("SELECT mbox FROM UnixUsers mbox WHERE mbox.name = :target");
|
|
||||||
mboxQuery.setParameter("target", target);
|
|
||||||
final Object mboxResult = mboxQuery.getSingleResult();
|
|
||||||
if (mboxResult == null || !(mboxResult instanceof UnixUser)) {
|
|
||||||
transaction.close();
|
|
||||||
throw new HSAdminException("no unique mailbox found");
|
|
||||||
}
|
|
||||||
final UnixUser mbox = (UnixUser) mboxResult;
|
|
||||||
final String uniqueUsername = mbox.getName();
|
|
||||||
transaction.close();
|
|
||||||
return uniqueUsername;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user