SecondaryPasswordAuth.java 8.8 KB

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