superpw.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 3k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. #include "msrating.h"
  2. #include "msluglob.h"
  3. #include <md5.h>
  4. extern HKEY CreateRegKeyNT(LPCSTR pszKey);
  5. extern BOOL RunningOnNT(void);
  6. HRESULT VerifySupervisorPassword(LPCSTR pszPassword)
  7. {
  8. if (!::fSupervisorKeyInit) {
  9. HKEY hkeyRating;
  10. LONG err;
  11. hkeyRating = CreateRegKeyNT(::szRATINGS);
  12. if (hkeyRating !=  NULL) {
  13. DWORD cbData = sizeof(::abSupervisorKey);
  14. DWORD dwType;
  15. err = ::RegQueryValueEx(hkeyRating, ::szRatingsSupervisorKeyName, NULL,
  16. &dwType, (LPBYTE)::abSupervisorKey, &cbData);
  17.             if (err != ERROR_SUCCESS) {
  18.                 if (pszPassword == NULL) {
  19.                     ::RegCloseKey(hkeyRating);
  20.                     return E_FAIL;
  21.                 }                    
  22.      err = ::RegQueryValueEx(hkeyRating, ::szUsersSupervisorKeyName, NULL,
  23.      &dwType, (LPBYTE)::abSupervisorKey, &cbData);
  24.             }
  25. ::RegCloseKey(hkeyRating);
  26. if (err == ERROR_SUCCESS) {
  27. if (dwType != REG_BINARY || cbData != sizeof(::abSupervisorKey)) {
  28. return E_UNEXPECTED;
  29. }
  30. ::fSupervisorKeyInit = TRUE;
  31. }
  32. }
  33. else
  34. err = ERROR_FILE_NOT_FOUND;
  35. if (err != ERROR_SUCCESS) {
  36. return HRESULT_FROM_WIN32(err);
  37. }
  38. }
  39. if (pszPassword == NULL)
  40. return ResultFromScode(S_FALSE);
  41. MD5_CTX ctx;
  42. MD5Init(&ctx);
  43. MD5Update(&ctx, (const BYTE *)pszPassword, ::strlenf(pszPassword)+1);
  44. MD5Final(&ctx);
  45. return ResultFromScode(::memcmpf(::abSupervisorKey, ctx.digest, sizeof(::abSupervisorKey)) ? S_FALSE : S_OK);
  46. }
  47. HRESULT ChangeSupervisorPassword(LPCSTR pszOldPassword, LPCSTR pszNewPassword)
  48. {
  49. HRESULT hres;
  50. hres = ::VerifySupervisorPassword(pszOldPassword);
  51. if (hres == S_FALSE) {
  52. return E_ACCESSDENIED;
  53. }
  54. MD5_CTX ctx;
  55. MD5Init(&ctx);
  56. MD5Update(&ctx, (const BYTE *)pszNewPassword, ::strlenf(pszNewPassword)+1);
  57. MD5Final(&ctx);
  58. ::memcpyf(::abSupervisorKey, ctx.digest, sizeof(::abSupervisorKey));
  59. ::fSupervisorKeyInit = TRUE;
  60.     hres = NOERROR;
  61. HKEY hkeyRating;
  62. hkeyRating = CreateRegKeyNT(::szRATINGS);
  63. if (hkeyRating != NULL) {
  64.         BYTE abTemp[sizeof(::abSupervisorKey)];
  65. DWORD cbData = sizeof(::abSupervisorKey);
  66. DWORD dwType;
  67. if (::RegQueryValueEx(hkeyRating, ::szRatingsSupervisorKeyName, NULL,
  68.   &dwType, abTemp, &cbData) != ERROR_SUCCESS)
  69.             hres = S_FALSE; /* tell caller we're creating the new key */
  70. ::RegSetValueEx(hkeyRating, ::szRatingsSupervisorKeyName, NULL,
  71. REG_BINARY, (const BYTE *)::abSupervisorKey, sizeof(::abSupervisorKey));
  72. ::RegCloseKey(hkeyRating);
  73. }
  74. return hres;
  75. }
  76. HRESULT RemoveSupervisorPassword(void)
  77. {
  78. HKEY hkeyRating;
  79. LONG err;
  80. hkeyRating = CreateRegKeyNT(::szRATINGS);
  81. if (hkeyRating !=  NULL) {
  82. err = ::RegDeleteValue(hkeyRating, ::szRatingsSupervisorKeyName);
  83. ::RegCloseKey(hkeyRating);
  84. }
  85. ::fSupervisorKeyInit = FALSE;
  86. return HRESULT_FROM_WIN32(err);
  87. }