Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
5.4 KiB

  1. #include "msrating.h"
  2. #include "msluglob.h"
  3. #include "mslubase.h"
  4. #include "hint.h"
  5. #include "debug.h"
  6. #include <md5.h>
  7. extern PicsRatingSystemInfo *gPRSI;
  8. HRESULT VerifySupervisorPassword(LPCSTR pszPassword)
  9. {
  10. if ( ! ::fSupervisorKeyInit )
  11. {
  12. HKEY hkeyRating;
  13. LONG err;
  14. hkeyRating = CreateRegKeyNT(::szRATINGS);
  15. if (hkeyRating != NULL)
  16. {
  17. DWORD cbData = sizeof(::abSupervisorKey);
  18. DWORD dwType;
  19. // Attempt to look for "Key"
  20. err = ::RegQueryValueEx(hkeyRating, ::szRatingsSupervisorKeyName, NULL,
  21. &dwType, (LPBYTE)::abSupervisorKey, &cbData);
  22. ::RegCloseKey(hkeyRating);
  23. hkeyRating = NULL;
  24. if (err == ERROR_SUCCESS)
  25. {
  26. if (dwType != REG_BINARY || cbData != sizeof(::abSupervisorKey))
  27. {
  28. TraceMsg( TF_WARNING, "VerifySupervisorPassword() - Unexpected Error dwType=%d, cbData=%d!", dwType, cbData );
  29. return E_UNEXPECTED;
  30. }
  31. ::fSupervisorKeyInit = TRUE;
  32. }
  33. else
  34. {
  35. if (pszPassword == NULL)
  36. {
  37. TraceMsg( TF_WARNING, "VerifySupervisorPassword() - Supervisor Key '%s' not found!", ::szRatingsSupervisorKeyName );
  38. return E_FAIL;
  39. }
  40. }
  41. }
  42. else
  43. {
  44. TraceMsg( TF_ERROR, "VerifySupervisorPassword() - Failed to Create Ratings Registry Key!" );
  45. err = ERROR_FILE_NOT_FOUND;
  46. }
  47. if (err != ERROR_SUCCESS)
  48. {
  49. TraceMsg( TF_WARNING, "VerifySupervisorPassword() - Error=0x%x!", err );
  50. return HRESULT_FROM_WIN32(err);
  51. }
  52. }
  53. if (pszPassword == NULL)
  54. {
  55. TraceMsg( TF_ALWAYS, "VerifySupervisorPassword() - Comparing to NULL pszPassword returning S_FALSE." );
  56. return ResultFromScode(S_FALSE);
  57. }
  58. // We should probably not be comparing to a blank password.
  59. // ASSERT( pszPassword[0] != '\0' );
  60. if ( pszPassword[0] == '\0' )
  61. {
  62. TraceMsg( TF_ALWAYS, "VerifySupervisorPassword() - Comparing to blank pszPassword." );
  63. }
  64. MD5_CTX ctx;
  65. MD5Init(&ctx);
  66. MD5Update(&ctx, (const BYTE *)pszPassword, ::strlenf(pszPassword)+1);
  67. MD5Final(&ctx);
  68. return ResultFromScode(::memcmpf(::abSupervisorKey, ctx.digest, sizeof(::abSupervisorKey)) ? S_FALSE : S_OK);
  69. }
  70. HRESULT ChangeSupervisorPassword(LPCSTR pszOldPassword, LPCSTR pszNewPassword)
  71. {
  72. HRESULT hres;
  73. hres = ::VerifySupervisorPassword(pszOldPassword);
  74. if (hres == S_FALSE)
  75. {
  76. TraceMsg( TF_WARNING, "ChangeSupervisorPassword() - VerifySupervisorPassword() false!" );
  77. return E_ACCESSDENIED;
  78. }
  79. // If pszNewPassword is NULL or "" (blank password) we call RemoveSupervisorPassword().
  80. if ( ! pszNewPassword )
  81. {
  82. TraceMsg( TF_ALWAYS, "ChangeSupervisorPassword() - pszNewPassword is NULL - Removing Supervisor Password!" );
  83. return RemoveSupervisorPassword();
  84. }
  85. // Attempting to set a blank password should remove the Key from the Registry.
  86. if ( pszNewPassword[0] == '\0' )
  87. {
  88. TraceMsg( TF_ALWAYS, "ChangeSupervisorPassword() - pszNewPassword is an empty string - Removing Supervisor Password!" );
  89. return RemoveSupervisorPassword();
  90. }
  91. MD5_CTX ctx;
  92. MD5Init(&ctx);
  93. MD5Update(&ctx, (const BYTE *)pszNewPassword, ::strlenf(pszNewPassword)+1);
  94. MD5Final(&ctx);
  95. ::memcpyf(::abSupervisorKey, ctx.digest, sizeof(::abSupervisorKey));
  96. ::fSupervisorKeyInit = TRUE;
  97. hres = NOERROR;
  98. HKEY hkeyRating;
  99. hkeyRating = CreateRegKeyNT(::szRATINGS);
  100. if (hkeyRating != NULL)
  101. {
  102. BYTE abTemp[sizeof(::abSupervisorKey)];
  103. DWORD cbData = sizeof(::abSupervisorKey);
  104. DWORD dwType;
  105. if (::RegQueryValueEx(hkeyRating, ::szRatingsSupervisorKeyName, NULL,
  106. &dwType, abTemp, &cbData) != ERROR_SUCCESS)
  107. {
  108. hres = S_FALSE; /* tell caller we're creating the new key */
  109. }
  110. ::RegSetValueEx(hkeyRating, ::szRatingsSupervisorKeyName, NULL,
  111. REG_BINARY, (const BYTE *)::abSupervisorKey, sizeof(::abSupervisorKey));
  112. ::RegCloseKey(hkeyRating);
  113. }
  114. else
  115. {
  116. TraceMsg( TF_ERROR, "ChangeSupervisorPassword() - Failed to Create Ratings Registry Key!" );
  117. hres = E_FAIL;
  118. }
  119. return hres;
  120. }
  121. HRESULT RemoveSupervisorPassword(void)
  122. {
  123. HKEY hkeyRating;
  124. LONG err = E_FAIL;
  125. hkeyRating = CreateRegKeyNT(::szRATINGS);
  126. if (hkeyRating != NULL)
  127. {
  128. err = ::RegDeleteValue(hkeyRating, ::szRatingsSupervisorKeyName);
  129. if ( err == ERROR_SUCCESS )
  130. {
  131. CHint hint;
  132. hint.RemoveHint();
  133. TraceMsg( TF_ALWAYS, "RemoveSupervisorPassword() - Removed supervisor password and hint." );
  134. }
  135. ::RegCloseKey(hkeyRating);
  136. hkeyRating = NULL;
  137. }
  138. else
  139. {
  140. TraceMsg( TF_ERROR, "RemoveSupervisorPassword() - Failed to Create Ratings Registry Key!" );
  141. }
  142. if ( gPRSI )
  143. {
  144. gPRSI->fRatingInstalled = FALSE;
  145. }
  146. ::fSupervisorKeyInit = FALSE;
  147. return HRESULT_FROM_WIN32(err);
  148. }