SecondaryPasswordAuth.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. {
  166. return false;
  167. }
  168. if (!validatePassword(newPassword))
  169. {
  170. _activeClient.sendPacket(new Ex2ndPasswordAck(Ex2ndPasswordAck.WRONG_PATTERN));
  171. return false;
  172. }
  173. newPassword = cryptPassword(newPassword);
  174. Connection con = null;
  175. try
  176. {
  177. con = L2DatabaseFactory.getInstance().getConnection();
  178. PreparedStatement statement = con.prepareStatement(UPDATE_PASSWORD);
  179. statement.setString(1, newPassword);
  180. statement.setString(2, _activeClient.getAccountName());
  181. statement.setString(3, VAR_PWD);
  182. statement.execute();
  183. statement.close();
  184. }
  185. catch (Exception e)
  186. {
  187. _log.log(Level.SEVERE, "Error while reading password.", e);
  188. return false;
  189. }
  190. finally
  191. {
  192. L2DatabaseFactory.close(con);
  193. }
  194. _password = newPassword;
  195. _authed = false;
  196. return true;
  197. }
  198. public boolean checkPassword(String password, boolean skipAuth)
  199. {
  200. password = cryptPassword(password);
  201. if (!password.equals(_password))
  202. {
  203. _wrongAttempts++;
  204. if (_wrongAttempts < Config.SECOND_AUTH_MAX_ATTEMPTS)
  205. {
  206. _activeClient.sendPacket(new Ex2ndPasswordVerify(Ex2ndPasswordVerify.PASSWORD_WRONG, _wrongAttempts));
  207. insertWrongAttempt(_wrongAttempts);
  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", _activeClient.getConnectionAddress().getHostAddress(), Integer.toString(Config.SECOND_AUTH_MAX_ATTEMPTS), Long.toString(Config.SECOND_AUTH_BAN_TIME), Config.SECOND_AUTH_REC_LINK);
  213. _log.warning(_activeClient.getAccountName() + " - (" + _activeClient.getConnectionAddress().getHostAddress() + ") has inputted the wrong password " + _wrongAttempts + " times in row.");
  214. insertWrongAttempt(0);
  215. _activeClient.close(new Ex2ndPasswordVerify(Ex2ndPasswordVerify.PASSWORD_BAN, Config.SECOND_AUTH_MAX_ATTEMPTS));
  216. }
  217. return false;
  218. }
  219. if (!skipAuth)
  220. {
  221. _authed = true;
  222. _activeClient.sendPacket(new Ex2ndPasswordVerify(Ex2ndPasswordVerify.PASSWORD_OK, _wrongAttempts));
  223. }
  224. insertWrongAttempt(0);
  225. return true;
  226. }
  227. public boolean passwordExist()
  228. {
  229. return _password == null ? false : true;
  230. }
  231. public void openDialog()
  232. {
  233. if (passwordExist())
  234. {
  235. _activeClient.sendPacket(new Ex2ndPasswordCheck(Ex2ndPasswordCheck.PASSWORD_PROMPT));
  236. }
  237. else
  238. {
  239. _activeClient.sendPacket(new Ex2ndPasswordCheck(Ex2ndPasswordCheck.PASSWORD_NEW));
  240. }
  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. {
  269. return false;
  270. }
  271. if ((password.length() < 6) || (password.length() > 8))
  272. {
  273. return false;
  274. }
  275. for (int i = 0; i < (password.length() - 1); i++)
  276. {
  277. char curCh = password.charAt(i);
  278. char nxtCh = password.charAt(i + 1);
  279. if ((curCh + 1) == nxtCh)
  280. {
  281. return false;
  282. }
  283. else if ((curCh - 1) == nxtCh)
  284. {
  285. return false;
  286. }
  287. else if (curCh == nxtCh)
  288. {
  289. return false;
  290. }
  291. }
  292. for (int i = 0; i < (password.length() - 2); i++)
  293. {
  294. String toChk = password.substring(i + 1);
  295. StringBuffer chkEr = new StringBuffer(password.substring(i, i + 2));
  296. if (toChk.contains(chkEr))
  297. {
  298. return false;
  299. }
  300. else if (toChk.contains(chkEr.reverse()))
  301. {
  302. return false;
  303. }
  304. }
  305. _wrongAttempts = 0;
  306. return true;
  307. }
  308. }