CDKeyAuth.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Renegade *
  23. * *
  24. * $Archive:: /Commando/Code/Commando/CDKeyAuth.cpp $*
  25. * *
  26. * Original Author:: Brian Hayes *
  27. * *
  28. * $Author:: Bhayes $*
  29. * *
  30. * $Modtime:: 3/15/02 2:56p $*
  31. * *
  32. * $Revision:: 3 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include <GameSpy\gcdkeyserver.h>
  38. #include <GameSpy\gcdkeyclient.h>
  39. #include <GameSpy\nonport.h>
  40. #include <GameSpy\gs_md5.h>
  41. #include <stdlib.h>
  42. #include "wwdebug.h"
  43. #include "CDKeyAuth.h"
  44. #include "gamespyauthmgr.h"
  45. #include "registry.h"
  46. #include "playermanager.h"
  47. #include "_globals.h"
  48. #include "ServerSettings.h"
  49. // static void c_auth_callback(int localid, int authenticated, char *errmsg, void *instance)
  50. // {
  51. // ((CCDKeyAuth *)instance)->auth_callback(localid, authenticated, errmsg);
  52. // }
  53. //generate a rand nchar challenge
  54. char * CCDKeyAuth::GenChallenge(int nchars)
  55. {
  56. static char s[33];
  57. if (nchars > 32) nchars = 32;
  58. s[nchars] = 0;
  59. while (nchars--)
  60. {
  61. s[nchars] = (char)('a' + rand() % 26);
  62. }
  63. return s;
  64. }
  65. /* Callback function to indicate whether a client has been authorized or not.
  66. If the client has been, then we send them a "welcome" string, representative of
  67. allowing them to "enter" the game. If they have not been authenticated, we dump
  68. them after sending an error message */
  69. void CCDKeyAuth::auth_callback(int localid, int authenticated, char *errmsg, void *instance)
  70. {
  71. // client_t *clients = (client_t *)instance;
  72. // char outbuf[512];
  73. WWDEBUG_SAY(("CDKeyAuth -- %d:%d:%s\n", localid, authenticated, errmsg));
  74. cPlayer * p_player = cPlayerManager::Find_Player(localid);
  75. if (p_player != NULL) {
  76. if (!authenticated) //doh.. bad!
  77. {
  78. // This client failed.
  79. //p_player->Set_GameSpy_Auth_State(GAMESPY_AUTH_STATE_REJECTING);
  80. //cGameSpyAuthMgr::Evict_Player(localid);
  81. cGameSpyAuthMgr::Initiate_Auth_Rejection(localid);
  82. // printf("Client %d was NOT authenticated (%s)\n",localid, errmsg);
  83. // sprintf(outbuf,"E:%s\n",errmsg);
  84. // send(clients[localid].sock, outbuf, strlen(outbuf),0);
  85. // shutdown(clients[localid].sock, 2);
  86. // closesocket(clients[localid].sock);
  87. // clients[localid].sock = INVALID_SOCKET;
  88. } else
  89. {
  90. // This client passed.
  91. p_player->Set_GameSpy_Auth_State(GAMESPY_AUTH_STATE_ACCEPTED);
  92. // printf("Client %d was authenticated (%s)\n",localid, errmsg);
  93. // sprintf(outbuf,"M:Welcome to the game, have fun! (%s)\n",errmsg);
  94. // send(clients[localid].sock, outbuf, strlen(outbuf),0);
  95. }
  96. }
  97. }
  98. void CCDKeyAuth::DisconnectUser(int localid) {
  99. gcd_disconnect_user(localid);
  100. }
  101. void CCDKeyAuth::AuthenticateUser(int localid, ULONG ip, char *challenge, char *authstring) {
  102. // Customize this with our playerdata struct
  103. // Take the response from our challenge that we sent to the client
  104. // and send it off to the Authserver along with the original challenge
  105. gcd_authenticate_user(localid, ip, challenge, authstring, CCDKeyAuth::auth_callback, NULL);
  106. }
  107. void CCDKeyAuth::AuthSerial(const char *challenge, StringClass &resp) {
  108. char response[RESPONSE_SIZE];
  109. StringClass sserial;
  110. WWASSERT(challenge);
  111. GetSerialNum(sserial);
  112. const char *serial = sserial;
  113. char *cdkey = new char [strlen(serial)+1];
  114. char *outb = cdkey;
  115. const char *linep = serial;
  116. char md5hash[33];
  117. while (*linep) {
  118. if (*linep >= '0' && *linep <= '9') {
  119. *outb++ = *linep;
  120. }
  121. linep++;
  122. }
  123. *outb = 0;
  124. // MD5 Hash Here.
  125. MD5Digest((BYTE *)cdkey, strlen(cdkey), md5hash);
  126. // hashserial, challenge, outbuf
  127. gcd_compute_response(md5hash, challenge, response);
  128. delete [] cdkey;
  129. resp = response;
  130. }
  131. void CCDKeyAuth::GetSerialNum(StringClass &serial) {
  132. RegistryClass main_reg(APPLICATION_SUB_KEY_NAME);
  133. StringClass stringval;
  134. StringClass serial_out;
  135. main_reg.Get_String("Serial", stringval);
  136. ServerSettingsClass::Encrypt_Serial(stringval, serial, false);
  137. }