RequestAuthLogin.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.loginserver.clientpackets;
  16. import java.net.InetAddress;
  17. import java.security.GeneralSecurityException;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import javax.crypto.Cipher;
  21. import com.l2jserver.Config;
  22. import com.l2jserver.loginserver.GameServerTable.GameServerInfo;
  23. import com.l2jserver.loginserver.HackingException;
  24. import com.l2jserver.loginserver.L2LoginClient;
  25. import com.l2jserver.loginserver.L2LoginClient.LoginClientState;
  26. import com.l2jserver.loginserver.LoginController;
  27. import com.l2jserver.loginserver.LoginController.AuthLoginResult;
  28. import com.l2jserver.loginserver.serverpackets.AccountKicked;
  29. import com.l2jserver.loginserver.serverpackets.AccountKicked.AccountKickedReason;
  30. import com.l2jserver.loginserver.serverpackets.LoginFail.LoginFailReason;
  31. import com.l2jserver.loginserver.serverpackets.LoginOk;
  32. import com.l2jserver.loginserver.serverpackets.ServerList;
  33. /**
  34. * Format: x
  35. * 0 (a leading null)
  36. * x: the rsa encrypted block with the login an password
  37. */
  38. public class RequestAuthLogin extends L2LoginClientPacket
  39. {
  40. private static Logger _log = Logger.getLogger(RequestAuthLogin.class.getName());
  41. private byte[] _raw = new byte[128];
  42. private String _user;
  43. private String _password;
  44. private int _ncotp;
  45. /**
  46. * @return
  47. */
  48. public String getPassword()
  49. {
  50. return _password;
  51. }
  52. /**
  53. * @return
  54. */
  55. public String getUser()
  56. {
  57. return _user;
  58. }
  59. public int getOneTimePassword()
  60. {
  61. return _ncotp;
  62. }
  63. @Override
  64. public boolean readImpl()
  65. {
  66. if (super._buf.remaining() >= 128)
  67. {
  68. readB(_raw);
  69. return true;
  70. }
  71. else
  72. {
  73. return false;
  74. }
  75. }
  76. @Override
  77. public void run()
  78. {
  79. byte[] decrypted = null;
  80. L2LoginClient client = getClient();
  81. try
  82. {
  83. Cipher rsaCipher = Cipher.getInstance("RSA/ECB/nopadding");
  84. rsaCipher.init(Cipher.DECRYPT_MODE, client.getRSAPrivateKey());
  85. decrypted = rsaCipher.doFinal(_raw, 0x00, 0x80 );
  86. }
  87. catch (GeneralSecurityException e)
  88. {
  89. _log.log(Level.INFO, "" , e);
  90. return;
  91. }
  92. _user = new String(decrypted, 0x5E, 14 ).trim();
  93. _user = _user.toLowerCase();
  94. _password = new String(decrypted, 0x6C, 16).trim();
  95. _ncotp = decrypted[0x7c];
  96. _ncotp |= decrypted[0x7d] << 8;
  97. _ncotp |= decrypted[0x7e] << 16;
  98. _ncotp |= decrypted[0x7f] << 24;
  99. LoginController lc = LoginController.getInstance();
  100. try
  101. {
  102. AuthLoginResult result = lc.tryAuthLogin(_user, _password, client);
  103. switch (result)
  104. {
  105. case AUTH_SUCCESS:
  106. client.setAccount(_user);
  107. lc.getCharactersOnAccount(_user);
  108. client.setState(LoginClientState.AUTHED_LOGIN);
  109. client.setSessionKey(lc.assignSessionKeyToClient(_user, client));
  110. if (Config.SHOW_LICENCE)
  111. {
  112. client.sendPacket(new LoginOk(getClient().getSessionKey()));
  113. }
  114. else
  115. {
  116. getClient().sendPacket(new ServerList(getClient()));
  117. }
  118. break;
  119. case INVALID_PASSWORD:
  120. client.close(LoginFailReason.REASON_USER_OR_PASS_WRONG);
  121. break;
  122. case ACCOUNT_BANNED:
  123. client.close(new AccountKicked(AccountKickedReason.REASON_PERMANENTLY_BANNED));
  124. break;
  125. case ALREADY_ON_LS:
  126. L2LoginClient oldClient;
  127. if ((oldClient = lc.getAuthedClient(_user)) != null)
  128. {
  129. // kick the other client
  130. oldClient.close(LoginFailReason.REASON_ACCOUNT_IN_USE);
  131. lc.removeAuthedLoginClient(_user);
  132. }
  133. // kick also current client
  134. client.close(LoginFailReason.REASON_ACCOUNT_IN_USE);
  135. break;
  136. case ALREADY_ON_GS:
  137. GameServerInfo gsi;
  138. if ((gsi = lc.getAccountOnGameServer(_user)) != null)
  139. {
  140. client.close(LoginFailReason.REASON_ACCOUNT_IN_USE);
  141. // kick from there
  142. if (gsi.isAuthed())
  143. {
  144. gsi.getGameServerThread().kickPlayer(_user);
  145. }
  146. }
  147. break;
  148. }
  149. }
  150. catch (HackingException e)
  151. {
  152. InetAddress address = getClient().getConnection().getInetAddress();
  153. lc.addBanForAddress(address, Config.LOGIN_BLOCK_AFTER_BAN*1000);
  154. _log.info("Banned ("+address+") for "+Config.LOGIN_BLOCK_AFTER_BAN+" seconds, due to "+e.getConnects()+" incorrect login attempts.");
  155. }
  156. }
  157. }