SQLAccountManager.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.tools.accountmanager;
  20. import java.security.MessageDigest;
  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.util.Base64;
  26. import java.util.Scanner;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.Server;
  29. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  30. /**
  31. * This class SQL Account Manager
  32. * @author netimperia
  33. */
  34. public class SQLAccountManager
  35. {
  36. private static String _uname = "";
  37. private static String _pass = "";
  38. private static String _level = "";
  39. private static String _mode = "";
  40. public static void main(String[] args)
  41. {
  42. Server.serverMode = Server.MODE_LOGINSERVER;
  43. Config.load();
  44. try (Scanner _scn = new Scanner(System.in))
  45. {
  46. while (true)
  47. {
  48. System.out.println("Please choose an option");
  49. System.out.println();
  50. System.out.println("1 - Create new account or update existing one (change pass and access level)");
  51. System.out.println("2 - Change access level");
  52. System.out.println("3 - Delete existing account");
  53. System.out.println("4 - List accounts and access levels");
  54. System.out.println("5 - Exit");
  55. while (!(_mode.equals("1") || _mode.equals("2") || _mode.equals("3") || _mode.equals("4") || _mode.equals("5")))
  56. {
  57. System.out.print("Your choice: ");
  58. _mode = _scn.next();
  59. }
  60. if (_mode.equals("1") || _mode.equals("2") || _mode.equals("3"))
  61. {
  62. while (_uname.trim().length() == 0)
  63. {
  64. System.out.print("Username: ");
  65. _uname = _scn.next().toLowerCase();
  66. }
  67. if (_mode.equals("1"))
  68. {
  69. while (_pass.trim().length() == 0)
  70. {
  71. System.out.print("Password: ");
  72. _pass = _scn.next();
  73. }
  74. }
  75. if (_mode.equals("1") || _mode.equals("2"))
  76. {
  77. while (_level.trim().length() == 0)
  78. {
  79. System.out.print("Access level: ");
  80. _level = _scn.next();
  81. }
  82. }
  83. }
  84. if (_mode.equals("1"))
  85. {
  86. // Add or Update
  87. addOrUpdateAccount(_uname.trim(), _pass.trim(), _level.trim());
  88. }
  89. else if (_mode.equals("2"))
  90. {
  91. // Change Level
  92. changeAccountLevel(_uname.trim(), _level.trim());
  93. }
  94. else if (_mode.equals("3"))
  95. {
  96. // Delete
  97. System.out.print("WARNING: This will not delete the gameserver data (characters, items, etc..)");
  98. System.out.print(" it will only delete the account login server data.");
  99. System.out.println();
  100. System.out.print("Do you really want to delete this account? Y/N: ");
  101. String yesno = _scn.next();
  102. if ((yesno != null) && yesno.equalsIgnoreCase("Y"))
  103. {
  104. deleteAccount(_uname.trim());
  105. }
  106. else
  107. {
  108. System.out.println("Deletion cancelled.");
  109. }
  110. }
  111. else if (_mode.equals("4"))
  112. {
  113. // List
  114. _mode = "";
  115. System.out.println();
  116. System.out.println("Please choose a listing mode");
  117. System.out.println();
  118. System.out.println("1 - Banned accounts only (accessLevel < 0)");
  119. System.out.println("2 - GM/privileged accounts (accessLevel > 0");
  120. System.out.println("3 - Regular accounts only (accessLevel = 0)");
  121. System.out.println("4 - List all");
  122. while (!(_mode.equals("1") || _mode.equals("2") || _mode.equals("3") || _mode.equals("4")))
  123. {
  124. System.out.print("Your choice: ");
  125. _mode = _scn.next();
  126. }
  127. System.out.println();
  128. printAccInfo(_mode);
  129. }
  130. else if (_mode.equals("5"))
  131. {
  132. System.exit(0);
  133. }
  134. _uname = "";
  135. _pass = "";
  136. _level = "";
  137. _mode = "";
  138. System.out.println();
  139. }
  140. }
  141. }
  142. private static void printAccInfo(String m)
  143. {
  144. int count = 0;
  145. String q = "SELECT login, accessLevel FROM accounts ";
  146. if (m.equals("1"))
  147. {
  148. q = q.concat("WHERE accessLevel < 0");
  149. }
  150. else if (m.equals("2"))
  151. {
  152. q = q.concat("WHERE accessLevel > 0");
  153. }
  154. else if (m.equals("3"))
  155. {
  156. q = q.concat("WHERE accessLevel = 0");
  157. }
  158. q = q.concat(" ORDER BY login ASC");
  159. try (Connection con = ConnectionFactory.getInstance().getConnection();
  160. PreparedStatement ps = con.prepareStatement(q);
  161. ResultSet rset = ps.executeQuery())
  162. {
  163. while (rset.next())
  164. {
  165. System.out.println(rset.getString("login") + " -> " + rset.getInt("accessLevel"));
  166. count++;
  167. }
  168. System.out.println("Displayed accounts: " + count);
  169. }
  170. catch (SQLException e)
  171. {
  172. System.out.println("There was error while displaying accounts:");
  173. System.out.println(e.getMessage());
  174. }
  175. }
  176. private static void addOrUpdateAccount(String account, String password, String level)
  177. {
  178. try (Connection con = ConnectionFactory.getInstance().getConnection();
  179. PreparedStatement ps = con.prepareStatement("REPLACE accounts(login, password, accessLevel) VALUES (?, ?, ?)"))
  180. {
  181. MessageDigest md = MessageDigest.getInstance("SHA");
  182. byte[] newPassword;
  183. newPassword = password.getBytes("UTF-8");
  184. newPassword = md.digest(newPassword);
  185. ps.setString(1, account);
  186. ps.setString(2, Base64.getEncoder().encodeToString(newPassword));
  187. ps.setString(3, level);
  188. if (ps.executeUpdate() > 0)
  189. {
  190. System.out.println("Account " + account + " has been created or updated");
  191. }
  192. else
  193. {
  194. System.out.println("Account " + account + " does not exist");
  195. }
  196. }
  197. catch (Exception e)
  198. {
  199. System.out.println("There was error while adding/updating account:");
  200. System.out.println(e.getMessage());
  201. }
  202. }
  203. private static void changeAccountLevel(String account, String level)
  204. {
  205. try (Connection con = ConnectionFactory.getInstance().getConnection();
  206. PreparedStatement ps = con.prepareStatement("UPDATE accounts SET accessLevel = ? WHERE login = ?"))
  207. {
  208. ps.setString(1, level);
  209. ps.setString(2, account);
  210. if (ps.executeUpdate() > 0)
  211. {
  212. System.out.println("Account " + account + " has been updated");
  213. }
  214. else
  215. {
  216. System.out.println("Account " + account + " does not exist");
  217. }
  218. }
  219. catch (SQLException e)
  220. {
  221. System.out.println("There was error while updating account:");
  222. System.out.println(e.getMessage());
  223. }
  224. }
  225. private static void deleteAccount(String account)
  226. {
  227. try (Connection con = ConnectionFactory.getInstance().getConnection();
  228. PreparedStatement ps = con.prepareStatement("DELETE FROM accounts WHERE login = ?"))
  229. {
  230. ps.setString(1, account);
  231. if (ps.executeUpdate() > 0)
  232. {
  233. System.out.println("Account " + account + " has been deleted");
  234. }
  235. else
  236. {
  237. System.out.println("Account " + account + " does not exist");
  238. }
  239. }
  240. catch (SQLException e)
  241. {
  242. System.out.println("There was error while deleting account:");
  243. System.out.println(e.getMessage());
  244. }
  245. }
  246. }