SecondaryPasswordAuth.java 8.9 KB

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