From 63201377f23e28d86eac8cbcc117b8947cfa3d59 Mon Sep 17 00:00:00 2001 From: Peter Hormanns Date: Thu, 26 Mar 2020 19:50:18 +0100 Subject: [PATCH] reset or change mailbox password if mailbox exists --- .../jalin/ldapadmin/hsadmin/MailboxDAO.java | 19 ++++++++++++++++++- .../ldapadmin/hsadmin/MailboxNotFound.java | 4 ++++ .../ldap/DirectoryServiceRunner.java | 2 +- .../jalin/ldapadmin/web/ProfileServlet.java | 18 ++++++++++++++++++ .../ldapadmin/web/ResetPasswordServlet.java | 11 +++++++++++ 5 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/jalin/ldapadmin/hsadmin/MailboxDAO.java b/src/main/java/de/jalin/ldapadmin/hsadmin/MailboxDAO.java index 11333f2..01c312a 100644 --- a/src/main/java/de/jalin/ldapadmin/hsadmin/MailboxDAO.java +++ b/src/main/java/de/jalin/ldapadmin/hsadmin/MailboxDAO.java @@ -57,7 +57,24 @@ public class MailboxDAO { final Map userAsMap = (Map) rpcResult[0]; return (String) userAsMap.get("name"); } - throw new MailboxNotFound(); + throw new MailboxNotFound("mailbox for " + localpart + " not found"); + } + + public void changePassword(String pac, String localpart, String newPassword) throws IOException, XmlRpcException, MailboxNotFound { + final String mailbox = getMailbox(pac, localpart); + final List xmlRpcParamsList = new ArrayList(); + xmlRpcParamsList.add(pac); + xmlRpcParamsList.add(ticketBox.getTicket()); + final HashMap setParamsMap = new HashMap(); + xmlRpcParamsList.add(setParamsMap); + setParamsMap.put("password", newPassword); + final HashMap whereParamsMap = new HashMap(); + xmlRpcParamsList.add(whereParamsMap); + whereParamsMap.put("name", mailbox); + final Object[] rpcResult = (Object[])rpcClient.execute("user.update", xmlRpcParamsList); + if (rpcResult.length != 1) { + throw new MailboxNotFound("mailbox for " + localpart + " not found"); + } } private void createMailbox(String pac, String localpart, String comment) throws IOException, XmlRpcException, MailboxNotFound { diff --git a/src/main/java/de/jalin/ldapadmin/hsadmin/MailboxNotFound.java b/src/main/java/de/jalin/ldapadmin/hsadmin/MailboxNotFound.java index 6a87205..189462c 100644 --- a/src/main/java/de/jalin/ldapadmin/hsadmin/MailboxNotFound.java +++ b/src/main/java/de/jalin/ldapadmin/hsadmin/MailboxNotFound.java @@ -4,4 +4,8 @@ public class MailboxNotFound extends Exception { private static final long serialVersionUID = 1L; + public MailboxNotFound(String message) { + super(message); + } + } diff --git a/src/main/java/de/jalin/ldapadmin/ldap/DirectoryServiceRunner.java b/src/main/java/de/jalin/ldapadmin/ldap/DirectoryServiceRunner.java index b689a72..8be14db 100644 --- a/src/main/java/de/jalin/ldapadmin/ldap/DirectoryServiceRunner.java +++ b/src/main/java/de/jalin/ldapadmin/ldap/DirectoryServiceRunner.java @@ -66,7 +66,7 @@ public class DirectoryServiceRunner { directoryService.setShutdownHookEnabled(true); directoryService.getChangeLog().setEnabled(false); directoryService.setAllowAnonymousAccess(false); - directoryService.setAccessControlEnabled(true); + directoryService.setAccessControlEnabled(false); directoryService.setPasswordHidden(false); return directoryService; } diff --git a/src/main/java/de/jalin/ldapadmin/web/ProfileServlet.java b/src/main/java/de/jalin/ldapadmin/web/ProfileServlet.java index 0bb1abc..ce45228 100644 --- a/src/main/java/de/jalin/ldapadmin/web/ProfileServlet.java +++ b/src/main/java/de/jalin/ldapadmin/web/ProfileServlet.java @@ -11,8 +11,12 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; +import org.apache.xmlrpc.XmlRpcException; + import de.jalin.ldapadmin.beans.User; import de.jalin.ldapadmin.beans.ValidationException; +import de.jalin.ldapadmin.hsadmin.MailboxDAO; +import de.jalin.ldapadmin.hsadmin.MailboxNotFound; import de.jalin.ldapadmin.ldap.GroupsDAO; import de.jalin.ldapadmin.ldap.LDAPSessionException; import de.jalin.ldapadmin.ldap.SimplePasswordException; @@ -22,7 +26,15 @@ import de.jalin.ldapadmin.ldap.UsersDAO; public class ProfileServlet extends AbstractLDAPServlet { private static final long serialVersionUID = 1L; + + private String pacAdmin; + @Override + public void init() throws ServletException { + super.init(); + pacAdmin = config.getPacAdmin(); + } + @Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { @@ -80,6 +92,12 @@ public class ProfileServlet extends AbstractLDAPServlet { throw new ValidationException("password2", messages.getString("ProfileServlet.passwords_donot_match")); } else { usr.setAndValidatePassword(password); + final MailboxDAO hsadminMailboxDAO = new MailboxDAO(getTicketProvider()); + try { + hsadminMailboxDAO.changePassword(pacAdmin, usr.getLogin(), password); + } catch (XmlRpcException | MailboxNotFound e) { + System.out.println(e.getLocalizedMessage()); + } } } } catch (SimplePasswordException e) { diff --git a/src/main/java/de/jalin/ldapadmin/web/ResetPasswordServlet.java b/src/main/java/de/jalin/ldapadmin/web/ResetPasswordServlet.java index 07d8668..80c5038 100644 --- a/src/main/java/de/jalin/ldapadmin/web/ResetPasswordServlet.java +++ b/src/main/java/de/jalin/ldapadmin/web/ResetPasswordServlet.java @@ -20,8 +20,11 @@ import javax.servlet.http.HttpSession; import org.apache.commons.net.smtp.SMTPClient; import org.apache.commons.net.smtp.SMTPReply; import org.apache.commons.net.smtp.SimpleSMTPHeader; +import org.apache.xmlrpc.XmlRpcException; import de.jalin.ldapadmin.beans.User; +import de.jalin.ldapadmin.hsadmin.MailboxDAO; +import de.jalin.ldapadmin.hsadmin.MailboxNotFound; import de.jalin.ldapadmin.ldap.LDAPSessionException; import de.jalin.ldapadmin.ldap.SimplePasswordException; import de.jalin.ldapadmin.ldap.UsersDAO; @@ -35,6 +38,7 @@ public class ResetPasswordServlet extends AbstractLDAPServlet { private String smtpHost; private String smtpPort; private String smtpFrom; + private String hsadminPacAdmin; private String tempDir; @Override @@ -43,6 +47,7 @@ public class ResetPasswordServlet extends AbstractLDAPServlet { smtpHost = config.getSmtpHost(); smtpPort = config.getSmtpPort(); smtpFrom = config.getSmtpFromAddress(); + hsadminPacAdmin = config.getPacAdmin(); tempDir = config.getTempDir(); } @@ -82,6 +87,7 @@ public class ResetPasswordServlet extends AbstractLDAPServlet { final UsersDAO usrDAO = new UsersDAO(ldapSession); final String loginParam = req.getParameter("login"); final User sessUsr = (User) httpSession.getAttribute("user"); + final MailboxDAO hsMailboxDAO = new MailboxDAO(getTicketProvider()); if (loginParam != null && sessUsr != null && loginParam.equals(sessUsr.getLogin())) { final String password1 = req.getParameter("password"); final String password2 = req.getParameter("password2"); @@ -94,6 +100,11 @@ public class ResetPasswordServlet extends AbstractLDAPServlet { try { sessUsr.setAndValidatePassword(password1); usrDAO.update(sessUsr); + try { + hsMailboxDAO.changePassword(hsadminPacAdmin, sessUsr.getLogin(), password1); + } catch (XmlRpcException | MailboxNotFound e) { + System.out.println(e.getLocalizedMessage()); + } httpSession.setAttribute("successmessage", messages.getString("ResetPasswordServlet.password_changed")); req.getRequestDispatcher("/new-password.jsp").forward(req, resp); return;