Guest User

Passwordmanager

a guest
Apr 22nd, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.96 KB | None | 0 0
  1. /* This program is free software; you can redistribute it and/or modify
  2.  * it under the terms of the GNU General Public License as published by
  3.  * the Free Software Foundation; either version 2, or (at your option)
  4.  * any later version.
  5.  *
  6.  * This program is distributed in the hope that it will be useful,
  7.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9.  * GNU General Public License for more details.
  10.  *
  11.  * You should have received a copy of the GNU General Public License
  12.  * along with this program; if not, write to the Free Software
  13.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  14.  * 02111-1307, USA.
  15.  *
  16.  * http://www.gnu.org/copyleft/gpl.html
  17.  */
  18. package com.l2jfrozen.gameserver.model.actor.instance;
  19.  
  20. import java.security.MessageDigest;
  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.util.StringTokenizer;
  25.  
  26. import com.l2jfrozen.Config;
  27. import com.l2jfrozen.crypt.Base64;
  28. import com.l2jfrozen.gameserver.ai.CtrlIntention;
  29. import com.l2jfrozen.gameserver.network.serverpackets.ActionFailed;
  30. import com.l2jfrozen.gameserver.network.serverpackets.LeaveWorld;
  31. import com.l2jfrozen.gameserver.network.serverpackets.MyTargetSelected;
  32. import com.l2jfrozen.gameserver.network.serverpackets.NpcHtmlMessage;
  33. import com.l2jfrozen.gameserver.network.serverpackets.ValidateLocation;
  34. import com.l2jfrozen.gameserver.templates.L2NpcTemplate;
  35. import com.l2jfrozen.util.CloseUtil;
  36. import com.l2jfrozen.util.database.L2DatabaseFactory;
  37.  
  38. public class L2ChangePasswordInstance extends L2FolkInstance
  39. {
  40.  public L2ChangePasswordInstance(int objectId, L2NpcTemplate template)
  41.  {
  42.  super(objectId, template);
  43.  }
  44.  
  45.  @Override
  46.  public void onAction(L2PcInstance player)
  47.  {
  48.  if (!canTarget(player))
  49.  {
  50.  return;
  51.  }
  52.  
  53.  player.setLastFolkNPC(this);
  54.  
  55.  // Check if the L2PcInstance already target the L2NpcInstance
  56.  if (this != player.getTarget())
  57.  {
  58.  // Set the target of the L2PcInstance player
  59.  player.setTarget(this);
  60.  
  61.  // Send a Server->Client packet MyTargetSelected to the L2PcInstance player
  62.  MyTargetSelected my = new MyTargetSelected(getObjectId(), 0);
  63.  player.sendPacket(my);
  64.  my = null;
  65.  
  66.  // Send a Server->Client packet ValidateLocation to correct the L2NpcInstance position and heading on the client
  67.  player.sendPacket(new ValidateLocation(this));
  68.  }
  69.  else
  70.  {
  71.  // Calculate the distance between the L2PcInstance and the L2NpcInstance
  72.  if (!canInteract(player))
  73.  {
  74.  // Notify the L2PcInstance AI with AI_INTENTION_INTERACT
  75.  player.getAI().setIntention(CtrlIntention.AI_INTENTION_INTERACT, this);
  76.  }
  77.  else
  78.  {
  79.  showHtmlWindow(player);
  80.  }
  81.  }
  82.  
  83.  player.sendPacket(new ActionFailed());
  84.  }
  85.  
  86.  private void showHtmlWindow(L2PcInstance player)
  87.  {
  88.  String filename = "data/html/mods/change_password.htm";
  89.  NpcHtmlMessage html = new NpcHtmlMessage(1);
  90.  html.setFile(filename);
  91.  html.replace("%objectId%", String.valueOf(getObjectId()));
  92.  player.sendPacket(html);
  93.  filename = null;
  94.  html = null;
  95.  }
  96.  
  97.  @Override
  98.  public void onBypassFeedback(L2PcInstance player, String command)
  99.  {
  100.  if (command.startsWith("change_password"))
  101.  {
  102.  StringTokenizer st = new StringTokenizer(command);
  103.  st.nextToken();
  104.  String curPass = null;
  105.  String newPass = null;
  106.  String repPass = null;
  107.  try
  108.  {
  109.  if (st.hasMoreTokens())
  110.  {
  111.  curPass = st.nextToken();
  112.  newPass = st.nextToken();
  113.  repPass = st.nextToken();
  114.  }
  115.  else
  116.  {
  117.  player.sendMessage("Please fill in all the blanks before requesting for a password change.");
  118.  return;
  119.  }
  120.  changePassword(curPass, newPass, repPass, player);
  121.  }
  122.  catch (StringIndexOutOfBoundsException e)
  123.  {
  124.  if (Config.ENABLE_ALL_EXCEPTIONS)
  125.  {
  126.  e.printStackTrace();
  127.  }
  128.  }
  129.  }
  130.  }
  131.  
  132.  public static boolean changePassword(String currPass, String newPass, String repeatNewPass, L2PcInstance activeChar)
  133.  {
  134.  if (newPass.length() < 3)
  135.  {
  136.  activeChar.sendMessage("The new password is too short!");
  137.  return false;
  138.  }
  139.  if (newPass.length() > 16)
  140.  {
  141.  activeChar.sendMessage("The new password is too long!");
  142.  return false;
  143.  }
  144.  if (!newPass.equals(repeatNewPass))
  145.  {
  146.  activeChar.sendMessage("Repeated password doesn't match the new password.");
  147.  return false;
  148.  }
  149.  
  150.  Connection con = null;
  151.  String password = null;
  152.  try
  153.  {
  154.  MessageDigest md = MessageDigest.getInstance("SHA");
  155.  byte[] raw = currPass.getBytes("UTF-8");
  156.  raw = md.digest(raw);
  157.  String currPassEncoded = Base64.encodeBytes(raw);
  158.  
  159.  con = L2DatabaseFactory.getInstance().getConnection(false);
  160.  PreparedStatement statement = con.prepareStatement("SELECT password FROM accounts WHERE login=?");
  161.  statement.setString(1, activeChar.getAccountName());
  162.  ResultSet rset = statement.executeQuery();
  163.  while (rset.next())
  164.  {
  165.  password = rset.getString("password");
  166.  }
  167.  rset.close();
  168.  statement.close();
  169.  byte[] password2 = null;
  170.  if (currPassEncoded.equals(password))
  171.  {
  172.  password2 = newPass.getBytes("UTF-8");
  173.  password2 = md.digest(password2);
  174.  
  175.  PreparedStatement statement2 = con.prepareStatement("UPDATE accounts SET password=? WHERE login=?");
  176.  statement2.setString(1, Base64.encodeBytes(password2));
  177.  statement2.setString(2, activeChar.getAccountName());
  178.  statement2.executeUpdate();
  179.  statement2.close();
  180.  
  181.  activeChar.sendMessage("Your password has been changed successfully! For security reasons, You will be disconnected. Please login again!");
  182.  try
  183.  {
  184.  Thread.sleep(3000L);
  185.  }
  186.  catch (Exception e)
  187.  {
  188.  if (Config.ENABLE_ALL_EXCEPTIONS)
  189.  {
  190.  e.printStackTrace();
  191.  }
  192.  }
  193.  
  194.  activeChar.deleteMe();
  195.  activeChar.sendPacket(new LeaveWorld());
  196.  }
  197.  else
  198.  {
  199.  activeChar.sendMessage("The current password you've inserted is incorrect! Please try again!");
  200.  
  201.  return password2 != null;
  202.  }
  203.  }
  204.  catch (Exception e)
  205.  {
  206.  if (Config.ENABLE_ALL_EXCEPTIONS)
  207.  {
  208.  e.printStackTrace();
  209.  }
  210.  
  211.  _log.warning("could not update the password of account: " + activeChar.getAccountName());
  212.  }
  213.  finally
  214.  {
  215.  CloseUtil.close(con);
  216.  }
  217.  
  218.  return true;
  219.  }
  220. }
Add Comment
Please, Sign In to add comment