SecondaryPasswordAuth.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server 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 GNU
  14. * 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, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.security;
  20. import java.io.UnsupportedEncodingException;
  21. import java.security.MessageDigest;
  22. import java.security.NoSuchAlgorithmException;
  23. import java.sql.Connection;
  24. import java.sql.PreparedStatement;
  25. import java.sql.ResultSet;
  26. import java.util.Base64;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import com.l2jserver.L2DatabaseFactory;
  30. import com.l2jserver.gameserver.LoginServerThread;
  31. import com.l2jserver.gameserver.data.xml.impl.SecondaryAuthData;
  32. import com.l2jserver.gameserver.network.L2GameClient;
  33. import com.l2jserver.gameserver.network.serverpackets.Ex2ndPasswordAck;
  34. import com.l2jserver.gameserver.network.serverpackets.Ex2ndPasswordCheck;
  35. import com.l2jserver.gameserver.network.serverpackets.Ex2ndPasswordVerify;
  36. import com.l2jserver.gameserver.util.Util;
  37. /**
  38. * @author mrTJO
  39. */
  40. public class SecondaryPasswordAuth
  41. {
  42. private final Logger _log = Logger.getLogger(SecondaryPasswordAuth.class.getName());
  43. private final L2GameClient _activeClient;
  44. private String _password;
  45. private int _wrongAttempts;
  46. private boolean _authed;
  47. private static final String VAR_PWD = "secauth_pwd";
  48. private static final String VAR_WTE = "secauth_wte";
  49. private static final String SELECT_PASSWORD = "SELECT var, value FROM account_gsdata WHERE account_name=? AND var LIKE 'secauth_%'";
  50. private static final String INSERT_PASSWORD = "INSERT INTO account_gsdata VALUES (?, ?, ?)";
  51. private static final String UPDATE_PASSWORD = "UPDATE account_gsdata SET value=? WHERE account_name=? AND var=?";
  52. private static final String INSERT_ATTEMPT = "INSERT INTO account_gsdata VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE value=?";
  53. /**
  54. * @param activeClient
  55. */
  56. public SecondaryPasswordAuth(L2GameClient activeClient)
  57. {
  58. _activeClient = activeClient;
  59. _password = null;
  60. _wrongAttempts = 0;
  61. _authed = false;
  62. loadPassword();
  63. }
  64. private void loadPassword()
  65. {
  66. String var, value = null;
  67. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  68. PreparedStatement statement = con.prepareStatement(SELECT_PASSWORD))
  69. {
  70. statement.setString(1, _activeClient.getAccountName());
  71. try (ResultSet rs = statement.executeQuery())
  72. {
  73. while (rs.next())
  74. {
  75. var = rs.getString("var");
  76. value = rs.getString("value");
  77. if (var.equals(VAR_PWD))
  78. {
  79. _password = value;
  80. }
  81. else if (var.equals(VAR_WTE))
  82. {
  83. _wrongAttempts = Integer.parseInt(value);
  84. }
  85. }
  86. }
  87. }
  88. catch (Exception e)
  89. {
  90. _log.log(Level.SEVERE, "Error while reading password.", e);
  91. }
  92. }
  93. public boolean savePassword(String password)
  94. {
  95. if (passwordExist())
  96. {
  97. _log.warning("[SecondaryPasswordAuth]" + _activeClient.getAccountName() + " forced savePassword");
  98. _activeClient.closeNow();
  99. return false;
  100. }
  101. if (!validatePassword(password))
  102. {
  103. _activeClient.sendPacket(new Ex2ndPasswordAck(Ex2ndPasswordAck.WRONG_PATTERN));
  104. return false;
  105. }
  106. password = cryptPassword(password);
  107. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  108. PreparedStatement statement = con.prepareStatement(INSERT_PASSWORD))
  109. {
  110. statement.setString(1, _activeClient.getAccountName());
  111. statement.setString(2, VAR_PWD);
  112. statement.setString(3, password);
  113. statement.execute();
  114. }
  115. catch (Exception e)
  116. {
  117. _log.log(Level.SEVERE, "Error while writing password.", e);
  118. return false;
  119. }
  120. _password = password;
  121. return true;
  122. }
  123. public boolean insertWrongAttempt(int attempts)
  124. {
  125. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  126. PreparedStatement statement = con.prepareStatement(INSERT_ATTEMPT))
  127. {
  128. statement.setString(1, _activeClient.getAccountName());
  129. statement.setString(2, VAR_WTE);
  130. statement.setString(3, Integer.toString(attempts));
  131. statement.setString(4, Integer.toString(attempts));
  132. statement.execute();
  133. }
  134. catch (Exception e)
  135. {
  136. _log.log(Level.SEVERE, "Error while writing wrong attempts.", e);
  137. return false;
  138. }
  139. return true;
  140. }
  141. public boolean changePassword(String oldPassword, String newPassword)
  142. {
  143. if (!passwordExist())
  144. {
  145. _log.warning("[SecondaryPasswordAuth]" + _activeClient.getAccountName() + " forced changePassword");
  146. _activeClient.closeNow();
  147. return false;
  148. }
  149. if (!checkPassword(oldPassword, true))
  150. {
  151. return false;
  152. }
  153. if (!validatePassword(newPassword))
  154. {
  155. _activeClient.sendPacket(new Ex2ndPasswordAck(Ex2ndPasswordAck.WRONG_PATTERN));
  156. return false;
  157. }
  158. newPassword = cryptPassword(newPassword);
  159. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  160. PreparedStatement statement = con.prepareStatement(UPDATE_PASSWORD))
  161. {
  162. statement.setString(1, newPassword);
  163. statement.setString(2, _activeClient.getAccountName());
  164. statement.setString(3, VAR_PWD);
  165. statement.execute();
  166. }
  167. catch (Exception e)
  168. {
  169. _log.log(Level.SEVERE, "Error while reading password.", e);
  170. return false;
  171. }
  172. _password = newPassword;
  173. _authed = false;
  174. return true;
  175. }
  176. public boolean checkPassword(String password, boolean skipAuth)
  177. {
  178. password = cryptPassword(password);
  179. if (!password.equals(_password))
  180. {
  181. _wrongAttempts++;
  182. if (_wrongAttempts < SecondaryAuthData.getInstance().getMaxAttempts())
  183. {
  184. _activeClient.sendPacket(new Ex2ndPasswordVerify(Ex2ndPasswordVerify.PASSWORD_WRONG, _wrongAttempts));
  185. insertWrongAttempt(_wrongAttempts);
  186. }
  187. else
  188. {
  189. LoginServerThread.getInstance().sendTempBan(_activeClient.getAccountName(), _activeClient.getConnectionAddress().getHostAddress(), SecondaryAuthData.getInstance().getBanTime());
  190. LoginServerThread.getInstance().sendMail(_activeClient.getAccountName(), "SATempBan", _activeClient.getConnectionAddress().getHostAddress(), Integer.toString(SecondaryAuthData.getInstance().getMaxAttempts()), Long.toString(SecondaryAuthData.getInstance().getBanTime()), SecondaryAuthData.getInstance().getRecoveryLink());
  191. _log.warning(_activeClient.getAccountName() + " - (" + _activeClient.getConnectionAddress().getHostAddress() + ") has inputted the wrong password " + _wrongAttempts + " times in row.");
  192. insertWrongAttempt(0);
  193. _activeClient.close(new Ex2ndPasswordVerify(Ex2ndPasswordVerify.PASSWORD_BAN, SecondaryAuthData.getInstance().getMaxAttempts()));
  194. }
  195. return false;
  196. }
  197. if (!skipAuth)
  198. {
  199. _authed = true;
  200. _activeClient.sendPacket(new Ex2ndPasswordVerify(Ex2ndPasswordVerify.PASSWORD_OK, _wrongAttempts));
  201. }
  202. insertWrongAttempt(0);
  203. return true;
  204. }
  205. public boolean passwordExist()
  206. {
  207. return _password == null ? false : true;
  208. }
  209. public void openDialog()
  210. {
  211. if (passwordExist())
  212. {
  213. _activeClient.sendPacket(new Ex2ndPasswordCheck(Ex2ndPasswordCheck.PASSWORD_PROMPT));
  214. }
  215. else
  216. {
  217. _activeClient.sendPacket(new Ex2ndPasswordCheck(Ex2ndPasswordCheck.PASSWORD_NEW));
  218. }
  219. }
  220. public boolean isAuthed()
  221. {
  222. return _authed;
  223. }
  224. private String cryptPassword(String password)
  225. {
  226. try
  227. {
  228. MessageDigest md = MessageDigest.getInstance("SHA");
  229. byte[] raw = password.getBytes("UTF-8");
  230. byte[] hash = md.digest(raw);
  231. return Base64.getEncoder().encodeToString(hash);
  232. }
  233. catch (NoSuchAlgorithmException e)
  234. {
  235. _log.severe("[SecondaryPasswordAuth]Unsupported Algorythm");
  236. }
  237. catch (UnsupportedEncodingException e)
  238. {
  239. _log.severe("[SecondaryPasswordAuth]Unsupported Encoding");
  240. }
  241. return null;
  242. }
  243. private boolean validatePassword(String password)
  244. {
  245. if (!Util.isDigit(password))
  246. {
  247. return false;
  248. }
  249. if ((password.length() < 6) || (password.length() > 8))
  250. {
  251. return false;
  252. }
  253. return !SecondaryAuthData.getInstance().isForbiddenPassword(password);
  254. }
  255. }