SecondaryPasswordAuth.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. Connection con = null;
  64. try
  65. {
  66. con = L2DatabaseFactory.getInstance().getConnection();
  67. PreparedStatement statement = con.prepareStatement(SELECT_PASSWORD);
  68. statement.setString(1, _activeClient.getAccountName());
  69. ResultSet rs = statement.executeQuery();
  70. while (rs.next())
  71. {
  72. var = rs.getString("var");
  73. value = rs.getString("value");
  74. if (var.equals(VAR_PWD))
  75. {
  76. _password = value;
  77. }
  78. else if (var.equals(VAR_WTE))
  79. {
  80. _wrongAttempts = Integer.parseInt(value);
  81. }
  82. }
  83. statement.close();
  84. }
  85. catch (Exception e)
  86. {
  87. _log.log(Level.SEVERE, "Error while reading password.", e);
  88. }
  89. finally
  90. {
  91. L2DatabaseFactory.close(con);
  92. }
  93. }
  94. public boolean savePassword(String password)
  95. {
  96. if (passwordExist())
  97. {
  98. _log.warning("[SecondaryPasswordAuth]"+_activeClient.getAccountName()+" forced savePassword");
  99. _activeClient.closeNow();
  100. return false;
  101. }
  102. if (!validatePassword(password))
  103. {
  104. _activeClient.sendPacket(new Ex2ndPasswordAck(Ex2ndPasswordAck.WRONG_PATTERN));
  105. return false;
  106. }
  107. password = cryptPassword(password);
  108. Connection con = null;
  109. try
  110. {
  111. con = L2DatabaseFactory.getInstance().getConnection();
  112. PreparedStatement statement = con.prepareStatement(INSERT_PASSWORD);
  113. statement.setString(1, _activeClient.getAccountName());
  114. statement.setString(2, VAR_PWD);
  115. statement.setString(3, password);
  116. statement.execute();
  117. statement.close();
  118. }
  119. catch (Exception e)
  120. {
  121. _log.log(Level.SEVERE, "Error while writing password.", e);
  122. return false;
  123. }
  124. finally
  125. {
  126. L2DatabaseFactory.close(con);
  127. }
  128. _password = password;
  129. return true;
  130. }
  131. public boolean insertWrongAttempt(int attempts)
  132. {
  133. Connection con = null;
  134. try
  135. {
  136. con = L2DatabaseFactory.getInstance().getConnection();
  137. PreparedStatement statement = con.prepareStatement(INSERT_ATTEMPT);
  138. statement.setString(1, _activeClient.getAccountName());
  139. statement.setString(2, VAR_WTE);
  140. statement.setString(3, Integer.toString(attempts));
  141. statement.setString(4, Integer.toString(attempts));
  142. statement.execute();
  143. statement.close();
  144. }
  145. catch (Exception e)
  146. {
  147. _log.log(Level.SEVERE, "Error while writing wrong attempts.", e);
  148. return false;
  149. }
  150. finally
  151. {
  152. L2DatabaseFactory.close(con);
  153. }
  154. return true;
  155. }
  156. public boolean changePassword(String oldPassword, String newPassword)
  157. {
  158. if (!passwordExist())
  159. {
  160. _log.warning("[SecondaryPasswordAuth]"+_activeClient.getAccountName()+" forced changePassword");
  161. _activeClient.closeNow();
  162. return false;
  163. }
  164. if (!checkPassword(oldPassword, true))
  165. return false;
  166. if (!validatePassword(newPassword))
  167. {
  168. _activeClient.sendPacket(new Ex2ndPasswordAck(Ex2ndPasswordAck.WRONG_PATTERN));
  169. return false;
  170. }
  171. newPassword = cryptPassword(newPassword);
  172. Connection con = null;
  173. try
  174. {
  175. con = L2DatabaseFactory.getInstance().getConnection();
  176. PreparedStatement statement = con.prepareStatement(UPDATE_PASSWORD);
  177. statement.setString(1, newPassword);
  178. statement.setString(2, _activeClient.getAccountName());
  179. statement.setString(3, VAR_PWD);
  180. statement.execute();
  181. statement.close();
  182. }
  183. catch (Exception e)
  184. {
  185. _log.log(Level.SEVERE, "Error while reading password.", e);
  186. return false;
  187. }
  188. finally
  189. {
  190. L2DatabaseFactory.close(con);
  191. }
  192. _password = newPassword;
  193. _authed = false;
  194. return true;
  195. }
  196. public boolean checkPassword(String password, boolean skipAuth)
  197. {
  198. password = cryptPassword(password);
  199. if (!password.equals(_password))
  200. {
  201. _wrongAttempts++;
  202. if (_wrongAttempts < Config.SECOND_AUTH_MAX_ATTEMPTS)
  203. {
  204. _activeClient.sendPacket(new Ex2ndPasswordVerify(Ex2ndPasswordVerify.PASSWORD_WRONG, _wrongAttempts));
  205. insertWrongAttempt(_wrongAttempts);
  206. }
  207. else
  208. {
  209. LoginServerThread.getInstance().sendTempBan(_activeClient.getAccountName(), _activeClient.getConnectionAddress().getHostAddress(), Config.SECOND_AUTH_BAN_TIME);
  210. LoginServerThread.getInstance().sendMail(_activeClient.getAccountName(), "SATempBan",
  211. _activeClient.getConnectionAddress().getHostAddress(), Integer.toString(Config.SECOND_AUTH_MAX_ATTEMPTS),
  212. Long.toString(Config.SECOND_AUTH_BAN_TIME), Config.SECOND_AUTH_REC_LINK);
  213. _log.warning(_activeClient.getAccountName()+" - ("+
  214. _activeClient.getConnectionAddress().getHostAddress()+") has inputted the wrong password "+
  215. _wrongAttempts+" times in row.");
  216. insertWrongAttempt(0);
  217. _activeClient.close(new Ex2ndPasswordVerify(Ex2ndPasswordVerify.PASSWORD_BAN, Config.SECOND_AUTH_MAX_ATTEMPTS));
  218. }
  219. return false;
  220. }
  221. if (!skipAuth)
  222. {
  223. _authed = true;
  224. _activeClient.sendPacket(new Ex2ndPasswordVerify(Ex2ndPasswordVerify.PASSWORD_OK, _wrongAttempts));
  225. }
  226. insertWrongAttempt(0);
  227. return true;
  228. }
  229. public boolean passwordExist()
  230. {
  231. return _password == null ? false : true;
  232. }
  233. public void openDialog()
  234. {
  235. if (passwordExist())
  236. _activeClient.sendPacket(new Ex2ndPasswordCheck(Ex2ndPasswordCheck.PASSWORD_PROMPT));
  237. else
  238. _activeClient.sendPacket(new Ex2ndPasswordCheck(Ex2ndPasswordCheck.PASSWORD_NEW));
  239. }
  240. public boolean isAuthed()
  241. {
  242. return _authed;
  243. }
  244. private String cryptPassword(String password)
  245. {
  246. try
  247. {
  248. MessageDigest md = MessageDigest.getInstance("SHA");
  249. byte[] raw = password.getBytes("UTF-8");
  250. byte[] hash = md.digest(raw);
  251. return Base64.encodeBytes(hash);
  252. }
  253. catch (NoSuchAlgorithmException e)
  254. {
  255. _log.severe("[SecondaryPasswordAuth]Unsupported Algorythm");
  256. }
  257. catch (UnsupportedEncodingException e)
  258. {
  259. _log.severe("[SecondaryPasswordAuth]Unsupported Encoding");
  260. }
  261. return null;
  262. }
  263. private boolean validatePassword(String password)
  264. {
  265. if (!Util.isDigit(password))
  266. return false;
  267. if (password.length() < 6 || password.length() > 8)
  268. return false;
  269. for (int i = 0; i < password.length()-1; i++)
  270. {
  271. char curCh = password.charAt(i);
  272. char nxtCh = password.charAt(i+1);
  273. if (curCh+1 == nxtCh)
  274. return false;
  275. else if (curCh-1 == nxtCh)
  276. return false;
  277. else if (curCh == nxtCh)
  278. return false;
  279. }
  280. for (int i = 0; i < password.length()-2; i++)
  281. {
  282. String toChk = password.substring(i+1);
  283. StringBuffer chkEr = new StringBuffer(password.substring(i, i+2));
  284. if (toChk.contains(chkEr))
  285. return false;
  286. else if (toChk.contains(chkEr.reverse()))
  287. return false;
  288. }
  289. _wrongAttempts = 0;
  290. return true;
  291. }
  292. }