Clone of PhatAC @ https://github.com/floaterxk/PhatAC

AccountDatabase.cpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "StdAfx.h"
  2. #include "Database.h"
  3. #include "AccountDatabase.h"
  4. #include "CharacterDatabase.h"
  5. #include "World.h"
  6. #include "ClientCommands.h"
  7. CAccountDatabase::CAccountDatabase(CDatabase *DB)
  8. {
  9. m_DB = DB;
  10. m_hSTMT = DB->m_hSTMT;
  11. }
  12. #define ValidAccountChar(x) (((x >= '0') && (x <= '9')) || ((x >= 'A') && (x <= 'Z')) || ((x >= 'a') && (x <= 'z')))
  13. BOOL ValidAccountText(const char *account, const char *password)
  14. {
  15. if (!account || !password)
  16. return FALSE;
  17. int account_len = (int)strlen(account);
  18. int password_len = (int)strlen(password);
  19. if (account_len <= 0 || account_len >= 40)
  20. return FALSE;
  21. if (password_len <= 0 || password_len >= 20)
  22. return FALSE;
  23. for (int i = 0; i < account_len; i++)
  24. {
  25. if (!ValidAccountChar(account[i]))
  26. return FALSE;
  27. }
  28. for (int i = 0; i < password_len; i++)
  29. {
  30. if (!ValidAccountChar(password[i]))
  31. return FALSE;
  32. }
  33. return TRUE;
  34. }
  35. extern DWORD g_dwMagicNumber;
  36. BOOL CAccountDatabase::CheckAccount(const char *account, const char *password, int *accessLevel, std::string& actualAccount)
  37. {
  38. // This is all garbage... garbage.. and we'll be wiped as soon as possible.
  39. // Just trying to make things work for the time being.
  40. *accessLevel = BASIC_ACCESS;
  41. std::string temp;
  42. if (!ValidAccountText(account, password) || !_stricmp(account, "username"))
  43. {
  44. LOG(Database, Normal, "Invalid characters in account/password! Username: %s Password: %s\n", account, password);
  45. // return FALSE;
  46. temp = csprintf("anonymous%d", RandomLong(0, 999999999));
  47. account = temp.c_str();
  48. password = temp.c_str();
  49. }
  50. actualAccount = account;
  51. char szCorrectPassword[50];
  52. if (!_stricmp(account, "admin"))
  53. {
  54. _snprintf(szCorrectPassword, 50, "%06lu", g_dwMagicNumber);
  55. if (!strcmp(password, szCorrectPassword))
  56. {
  57. *accessLevel = ADMIN_ACCESS;
  58. return TRUE;
  59. }
  60. return FALSE;
  61. }
  62. char *accountlwr = _strlwr(_strdup(account));
  63. char *command = csprintf("SELECT Password FROM Accounts WHERE (Username = \'%s\');", accountlwr);
  64. free(accountlwr);
  65. SQLPrepare(m_hSTMT, (unsigned char *)command, SQL_NTS);
  66. SQLExecute(m_hSTMT);
  67. SQLBindCol(m_hSTMT, 1, SQL_C_CHAR, &szCorrectPassword, 50, NULL);
  68. RETCODE rc = SQLFetch(m_hSTMT);
  69. SQLCloseCursor(m_hSTMT);
  70. SQLFreeStmt(m_hSTMT, SQL_UNBIND);
  71. bool bMakeRandom = false;
  72. if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
  73. {
  74. if (!strcmp(szCorrectPassword, password))
  75. {
  76. //LOG(Temp, Normal, "Successful login from %s:%s\n", account, password);
  77. return TRUE;
  78. }
  79. else
  80. {
  81. LOG(Database, Normal, "Bad password on %s:%s (guess: %s)\n", account, szCorrectPassword, password);
  82. bMakeRandom = true;
  83. }
  84. }
  85. // Bad pasword or non-existent account, create a random one instead until we change this whole system.
  86. std::string newAccount = bMakeRandom ? csprintf("anonymous%d", RandomLong(0, 999999999)) : account;
  87. actualAccount = newAccount;
  88. accountlwr = _strlwr(_strdup(newAccount.c_str()));
  89. if (bMakeRandom)
  90. password = accountlwr;
  91. LOG(Database, Normal, "Creating new account %s:%s\n", accountlwr, password);
  92. command = csprintf("INSERT INTO Accounts (Username, Password) VALUES (\'%s\', \'%s\');", accountlwr, password);
  93. SQLPrepare(m_hSTMT, (unsigned char *)command, SQL_NTS);
  94. rc = SQLExecute(m_hSTMT);
  95. SQLFreeStmt(m_hSTMT, SQL_UNBIND);
  96. if (rc != SQL_ERROR)
  97. {
  98. char* szCharacterName = _strlwr(_strdup(newAccount.c_str()));
  99. char leadchar = szCharacterName[0];
  100. if (leadchar > 0x60 && leadchar < 0x7B)
  101. szCharacterName[0] = leadchar - 0x20;
  102. CCharacterDatabase* pCharDB;
  103. if ((pCharDB = m_DB->CharDB()) && g_pWorld)
  104. {
  105. _CHARDESC buffer;
  106. if (!pCharDB->GetCharacterDesc(szCharacterName, &buffer))
  107. {
  108. pCharDB->CreateCharacterDesc(accountlwr, g_pWorld->GenerateGUID(ePlayerGUID), szCharacterName);
  109. }
  110. }
  111. free(szCharacterName);
  112. free(accountlwr);
  113. return TRUE;
  114. }
  115. free(accountlwr);
  116. return FALSE;
  117. }