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.

1226 lines
37 KiB

  1. /*************************************************************************/
  2. /* Copyright (C) 1999 Microsoft Corporation */
  3. /* File: MSDVDAdm.cpp */
  4. /* Description: DImplementation of CMSDVDAdm */
  5. /* Author: Fang Wang */
  6. /*************************************************************************/
  7. #include "stdafx.h"
  8. #include "MSWebDVD.h"
  9. #include "MSDVDAdm.h"
  10. #include "iso3166.h"
  11. #include <stdio.h>
  12. #include <errors.h>
  13. #include <wincrypt.h>
  14. const TCHAR g_szRegistryKey[] = TEXT("Software\\Microsoft\\Multimedia\\DVD");
  15. const TCHAR g_szPassword[] = TEXT("DVDAdmin.password");
  16. const TCHAR g_szSalt[] = TEXT("DVDAdmin.ps"); // password salt
  17. const TCHAR g_szUserSalt[] = TEXT("DVDAdmin.us"); // username salt
  18. const TCHAR g_szUsername[] = TEXT("DVDAdmin.username");
  19. const TCHAR g_szPlayerLevel[] = TEXT("DVDAdmin.playerLevel");
  20. const TCHAR g_szPlayerCountry[] = TEXT("DVDAdmin.playerCountry");
  21. const TCHAR g_szDisableScrnSvr[] = TEXT("DVDAdmin.disableScreenSaver");
  22. const TCHAR g_szBookmarkOnClose[] = TEXT("DVDAdmin.bookmarkOnClose");
  23. const TCHAR g_szBookmarkOnStop[] = TEXT("DVDAdmin.bookmarkOnStop");
  24. const TCHAR g_szDefaultAudio[] = TEXT("DVDAdmin.defaultAudioLCID");
  25. const TCHAR g_szDefaultSP[] = TEXT("DVDAdmin.defaultSPLCID");
  26. const TCHAR g_szDefaultMenu[] = TEXT("DVDAdmin.defaultMenuLCID");
  27. /*************************************************************/
  28. /* Helper functions */
  29. /*************************************************************/
  30. /*************************************************************/
  31. /* Function: LoadStringFromRes */
  32. /* Description: load a string from resource */
  33. /*************************************************************/
  34. LPTSTR LoadStringFromRes(DWORD redId){
  35. TCHAR *string = new TCHAR[MAX_PATH];
  36. if(NULL == string){
  37. return(NULL);
  38. }
  39. ::ZeroMemory(string, sizeof(TCHAR) * MAX_PATH);
  40. ::LoadString(_Module.GetModuleInstance(), redId, string, MAX_PATH);
  41. return string;
  42. }/* end of if statement */
  43. /*************************************************************/
  44. /* Function: lstrlenWInternal */
  45. /*************************************************************/
  46. int WINAPI lstrlenWInternal(LPCWSTR lpString){
  47. int length = 0;
  48. while (*lpString++ != L'\0')
  49. length++;
  50. return length;
  51. }/* end of function lstrlenWInternal */
  52. /*************************************************************/
  53. /* Name: GetRegistryDword
  54. /* Description:
  55. /*************************************************************/
  56. BOOL GetRegistryDword(const TCHAR *pKey, DWORD* dwRet, DWORD dwDefault)
  57. {
  58. HKEY hKey;
  59. LONG lRet;
  60. *dwRet = dwDefault;
  61. lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, g_szRegistryKey, 0, KEY_QUERY_VALUE, &hKey);
  62. if (lRet == ERROR_SUCCESS) {
  63. DWORD dwType, dwLen;
  64. dwLen = sizeof(DWORD);
  65. if (ERROR_SUCCESS != RegQueryValueEx(hKey, pKey, NULL, &dwType, (LPBYTE)dwRet, &dwLen)){
  66. *dwRet = dwDefault;
  67. RegCloseKey(hKey);
  68. return FALSE;
  69. }
  70. RegCloseKey(hKey);
  71. }
  72. return (lRet == ERROR_SUCCESS);
  73. }
  74. /*************************************************************/
  75. /* Name: SetRegistryDword
  76. /* Description:
  77. /*************************************************************/
  78. BOOL SetRegistryDword(const TCHAR *pKey, DWORD dwRet)
  79. {
  80. HKEY hKey;
  81. LONG lRet;
  82. lRet = RegCreateKey(HKEY_LOCAL_MACHINE, g_szRegistryKey, &hKey);
  83. if (lRet == ERROR_SUCCESS) {
  84. lRet = RegSetValueEx(hKey, pKey, NULL, REG_DWORD, (LPBYTE)&dwRet, sizeof(dwRet));
  85. RegCloseKey(hKey);
  86. }
  87. return (lRet == ERROR_SUCCESS);
  88. }
  89. /*************************************************************/
  90. /* Name: GetRegistryString
  91. /* Description:
  92. /*************************************************************/
  93. BOOL GetRegistryString(const TCHAR *pKey, TCHAR* szRet, DWORD* dwLen, TCHAR* szDefault)
  94. {
  95. HKEY hKey;
  96. LONG lRet;
  97. DWORD dwTempLen = 0;
  98. lstrcpyn(szRet, szDefault, *dwLen);
  99. lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, g_szRegistryKey, 0, KEY_QUERY_VALUE, &hKey);
  100. if (lRet == ERROR_SUCCESS) {
  101. DWORD dwType;
  102. dwTempLen = (*dwLen) * sizeof(TCHAR);
  103. if (ERROR_SUCCESS != RegQueryValueEx(hKey, pKey, NULL, &dwType, (LPBYTE)szRet, &dwTempLen)) {
  104. lstrcpyn(szRet, szDefault, *dwLen);
  105. *dwLen = 0;
  106. }
  107. *dwLen = dwTempLen/sizeof(TCHAR);
  108. RegCloseKey(hKey);
  109. }
  110. return (lRet == ERROR_SUCCESS);
  111. }
  112. /*************************************************************/
  113. /* Name: SetRegistryString
  114. /* Description:
  115. /*************************************************************/
  116. BOOL SetRegistryString(const TCHAR *pKey, TCHAR *szString, DWORD dwLen)
  117. {
  118. HKEY hKey;
  119. LONG lRet;
  120. lRet = RegCreateKey(HKEY_LOCAL_MACHINE, g_szRegistryKey, &hKey);
  121. if (lRet == ERROR_SUCCESS) {
  122. lRet = RegSetValueEx(hKey, pKey, NULL, REG_SZ, (LPBYTE)szString, dwLen*sizeof(TCHAR));
  123. RegCloseKey(hKey);
  124. }
  125. return (lRet == ERROR_SUCCESS);
  126. }
  127. /*************************************************************/
  128. /* Name: GetRegistryByte
  129. /* Description:
  130. /*************************************************************/
  131. BOOL GetRegistryBytes(const TCHAR *pKey, BYTE* szRet, DWORD* dwLen)
  132. {
  133. HKEY hKey;
  134. LONG lRet;
  135. lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, g_szRegistryKey, 0, KEY_QUERY_VALUE, &hKey);
  136. if (lRet == ERROR_SUCCESS) {
  137. DWORD dwType;
  138. if (ERROR_SUCCESS != RegQueryValueEx(hKey, pKey, NULL, &dwType, (LPBYTE)szRet, dwLen)) {
  139. *dwLen = 0;
  140. }
  141. RegCloseKey(hKey);
  142. }
  143. return (lRet == ERROR_SUCCESS);
  144. }
  145. /*************************************************************/
  146. /* Name: SetRegistryBytes
  147. /* Description:
  148. /*************************************************************/
  149. BOOL SetRegistryBytes(const TCHAR *pKey, BYTE *szString, DWORD dwLen)
  150. {
  151. HKEY hKey;
  152. LONG lRet;
  153. lRet = RegCreateKey(HKEY_LOCAL_MACHINE, g_szRegistryKey, &hKey);
  154. BOOL bRet = TRUE;
  155. if (lRet == ERROR_SUCCESS) {
  156. if (szString == NULL) {
  157. lRet = RegDeleteValue(hKey, pKey);
  158. bRet = (lRet == ERROR_SUCCESS) || (lRet == ERROR_FILE_NOT_FOUND);
  159. }
  160. else {
  161. lRet = RegSetValueEx(hKey, pKey, NULL, REG_BINARY, (LPBYTE)szString, dwLen);
  162. bRet = (lRet == ERROR_SUCCESS);
  163. }
  164. RegCloseKey(hKey);
  165. }
  166. return (bRet);
  167. }
  168. // Start not so lame functions
  169. /*************************************************************/
  170. /* Name: GetRegistryDwordCU
  171. /* Description:
  172. /*************************************************************/
  173. BOOL GetRegistryDwordCU(const TCHAR *pKey, DWORD* dwRet, DWORD dwDefault)
  174. {
  175. HKEY hKey;
  176. LONG lRet;
  177. *dwRet = dwDefault;
  178. lRet = RegOpenKeyEx(HKEY_CURRENT_USER, g_szRegistryKey, 0, KEY_QUERY_VALUE, &hKey);
  179. if (lRet == ERROR_SUCCESS) {
  180. DWORD dwType, dwLen;
  181. dwLen = sizeof(DWORD);
  182. if (ERROR_SUCCESS != RegQueryValueEx(hKey, pKey, NULL, &dwType, (LPBYTE)dwRet, &dwLen)){
  183. *dwRet = dwDefault;
  184. RegCloseKey(hKey);
  185. return FALSE;
  186. }
  187. RegCloseKey(hKey);
  188. }
  189. return (lRet == ERROR_SUCCESS);
  190. }
  191. /*************************************************************/
  192. /* Name: SetRegistryDwordCU
  193. /* Description:
  194. /*************************************************************/
  195. BOOL SetRegistryDwordCU(const TCHAR *pKey, DWORD dwRet)
  196. {
  197. HKEY hKey;
  198. LONG lRet;
  199. lRet = RegCreateKey(HKEY_CURRENT_USER, g_szRegistryKey, &hKey);
  200. if (lRet == ERROR_SUCCESS) {
  201. lRet = RegSetValueEx(hKey, pKey, NULL, REG_DWORD, (LPBYTE)&dwRet, sizeof(dwRet));
  202. RegCloseKey(hKey);
  203. }
  204. return (lRet == ERROR_SUCCESS);
  205. }
  206. /*************************************************************/
  207. /* Name: GetRegistryStringCU
  208. /* Description:
  209. /*************************************************************/
  210. BOOL GetRegistryStringCU(const TCHAR *pKey, TCHAR* szRet, DWORD* dwLen, TCHAR* szDefault)
  211. {
  212. HKEY hKey;
  213. LONG lRet;
  214. DWORD dwTempLen = 0;
  215. lstrcpyn(szRet, szDefault, *dwLen);
  216. lRet = RegOpenKeyEx(HKEY_CURRENT_USER, g_szRegistryKey, 0, KEY_QUERY_VALUE, &hKey);
  217. if (lRet == ERROR_SUCCESS) {
  218. DWORD dwType;
  219. dwTempLen = (*dwLen) * sizeof(TCHAR);
  220. if (ERROR_SUCCESS != RegQueryValueEx(hKey, pKey, NULL, &dwType, (LPBYTE)szRet, &dwTempLen)) {
  221. lstrcpyn(szRet, szDefault, sizeof(szRet) / sizeof(szRet[0]));
  222. *dwLen = 0;
  223. }
  224. *dwLen = dwTempLen/sizeof(TCHAR);
  225. RegCloseKey(hKey);
  226. }
  227. return (lRet == ERROR_SUCCESS);
  228. }
  229. /*************************************************************/
  230. /* Name: SetRegistryStringCU
  231. /* Description:
  232. /*************************************************************/
  233. BOOL SetRegistryStringCU(const TCHAR *pKey, TCHAR *szString, DWORD dwLen)
  234. {
  235. HKEY hKey;
  236. LONG lRet;
  237. lRet = RegCreateKey(HKEY_CURRENT_USER, g_szRegistryKey, &hKey);
  238. if (lRet == ERROR_SUCCESS) {
  239. lRet = RegSetValueEx(hKey, pKey, NULL, REG_SZ, (LPBYTE)szString, dwLen*sizeof(TCHAR));
  240. RegCloseKey(hKey);
  241. }
  242. return (lRet == ERROR_SUCCESS);
  243. }
  244. /*************************************************************/
  245. /* Name: GetRegistryByteCU
  246. /* Description:
  247. /*************************************************************/
  248. BOOL GetRegistryBytesCU(const TCHAR *pKey, BYTE* szRet, DWORD* dwLen)
  249. {
  250. HKEY hKey;
  251. LONG lRet;
  252. lRet = RegOpenKeyEx(HKEY_CURRENT_USER, g_szRegistryKey, 0, KEY_QUERY_VALUE, &hKey);
  253. if (lRet == ERROR_SUCCESS) {
  254. DWORD dwType;
  255. if (ERROR_SUCCESS != RegQueryValueEx(hKey, pKey, NULL, &dwType, (LPBYTE)szRet, dwLen)) {
  256. *dwLen = 0;
  257. }
  258. RegCloseKey(hKey);
  259. }
  260. return (lRet == ERROR_SUCCESS);
  261. }
  262. /*************************************************************/
  263. /* Name: SetRegistryBytesCU
  264. /* Description:
  265. /*************************************************************/
  266. BOOL SetRegistryBytesCU(const TCHAR *pKey, BYTE *szString, DWORD dwLen)
  267. {
  268. HKEY hKey;
  269. LONG lRet;
  270. lRet = RegCreateKey(HKEY_CURRENT_USER, g_szRegistryKey, &hKey);
  271. BOOL bRet = TRUE;
  272. if (lRet == ERROR_SUCCESS) {
  273. if (szString == NULL) {
  274. lRet = RegDeleteValue(hKey, pKey);
  275. bRet = (lRet == ERROR_SUCCESS) || (lRet == ERROR_FILE_NOT_FOUND);
  276. }
  277. else {
  278. lRet = RegSetValueEx(hKey, pKey, NULL, REG_BINARY, (LPBYTE)szString, dwLen);
  279. bRet = (lRet == ERROR_SUCCESS);
  280. }
  281. RegCloseKey(hKey);
  282. }
  283. return (bRet);
  284. }
  285. // end not so lame functions
  286. /*************************************************************/
  287. /* Function: CMSDVDAdm */
  288. /*************************************************************/
  289. CMSDVDAdm::CMSDVDAdm(){
  290. DWORD temp;
  291. GetRegistryDword(g_szPlayerLevel, &temp, (DWORD)LEVEL_ADULT);
  292. m_lParentctrlLevel = temp;
  293. GetRegistryDword(g_szPlayerCountry, &temp, (DWORD)0);
  294. m_lParentctrlCountry = temp;
  295. GetRegistryDword(g_szDisableScrnSvr, &temp, (DWORD)VARIANT_TRUE);
  296. m_fDisableScreenSaver = (VARIANT_BOOL)temp;
  297. SaveScreenSaver();
  298. if (m_fDisableScreenSaver != VARIANT_FALSE)
  299. DisableScreenSaver();
  300. GetRegistryDword(g_szBookmarkOnStop, &temp, (DWORD)VARIANT_FALSE);
  301. m_fBookmarkOnStop = (VARIANT_BOOL)temp;
  302. GetRegistryDword(g_szBookmarkOnClose, &temp, (DWORD)VARIANT_TRUE);
  303. m_fBookmarkOnClose = (VARIANT_BOOL)temp;
  304. }/* end of function CMSDVDAdm */
  305. /*************************************************************/
  306. /* Function: ~CMSDVDAdm */
  307. /*************************************************************/
  308. CMSDVDAdm::~CMSDVDAdm(){
  309. RestoreScreenSaver();
  310. }/* end of function ~CMSDVDAdm */
  311. /*************************************************************/
  312. /* Name: EncryptPassword */
  313. /* Description: Hash the password */
  314. /* Params: */
  315. /* lpPassword: password to hash */
  316. /* lpAssaultedHash: hashed password, */
  317. /* allocated by this fucntion, released by caller */
  318. /* p_dwAssault: salt, save with hash; or salt passed in */
  319. /* genAssault: TRUE = generate salt; FALSE = salt passed in */
  320. /*************************************************************/
  321. HRESULT CMSDVDAdm::EncryptPassword(LPTSTR lpPassword, BYTE **lpAssaultedHash, DWORD *p_dwCryptLen, DWORD *p_dwAssault, BOOL genAssault){
  322. if(!lpPassword || !lpAssaultedHash || !p_dwAssault || !p_dwCryptLen){
  323. return E_POINTER;
  324. }
  325. if( lstrlen(lpPassword) > MAX_PASSWD){
  326. return E_INVALIDARG;
  327. }
  328. HCRYPTPROV hProv = NULL; // Handle to Crypto Context
  329. HCRYPTHASH hHash = NULL; // Handle to Hash Function
  330. DWORD dwAssault = 0; // As(Sa)u(lt) for hash
  331. DWORD dwAssaultedHash = 0; // Length of Assaulted hash
  332. // Init Crypto Context
  333. if(!CryptAcquireContext(&hProv, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)){
  334. return E_UNEXPECTED;
  335. }
  336. // Store the Salt in dwAssault, either generate it or copy the user passed value
  337. if(genAssault){
  338. if(!CryptGenRandom(hProv, sizeof(DWORD), reinterpret_cast<BYTE *>(&dwAssault))){
  339. if(hProv) CryptReleaseContext(hProv, 0);
  340. return E_UNEXPECTED;
  341. }
  342. *p_dwAssault = dwAssault;
  343. }
  344. else{
  345. dwAssault = *p_dwAssault;
  346. }
  347. // Create the handle to the Hash function
  348. if(!CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash)){
  349. if(hProv) CryptReleaseContext(hProv, 0);
  350. if(hHash) CryptDestroyHash(hHash);
  351. return E_UNEXPECTED;
  352. }
  353. // Hash the password
  354. if(!CryptHashData(hHash, reinterpret_cast<BYTE *>(lpPassword), lstrlen(lpPassword)*sizeof(lpPassword[0]), 0)){
  355. if(hProv) CryptReleaseContext(hProv, 0);
  356. if(hHash) CryptDestroyHash(hHash);
  357. return E_UNEXPECTED;
  358. }
  359. // Add the salt
  360. if(!CryptHashData(hHash, reinterpret_cast<BYTE *>(&dwAssault), sizeof(DWORD), 0)){
  361. if(hProv) CryptReleaseContext(hProv, 0);
  362. if(hHash) CryptDestroyHash(hHash);
  363. return E_UNEXPECTED;
  364. }
  365. // Get the size of the hashed data
  366. if(!CryptGetHashParam(hHash, HP_HASHVAL, 0, &dwAssaultedHash, 0)){
  367. if(hProv) CryptReleaseContext(hProv, 0);
  368. if(hHash) CryptDestroyHash(hHash);
  369. return E_UNEXPECTED;
  370. }
  371. // Allocate a string large enough to hold the hash data and a null
  372. *lpAssaultedHash = new BYTE[dwAssaultedHash];
  373. if(!lpAssaultedHash){
  374. if(hProv) CryptReleaseContext(hProv, 0);
  375. if(hHash) CryptDestroyHash(hHash);
  376. return E_UNEXPECTED;
  377. }
  378. // Zero the string
  379. ZeroMemory(*lpAssaultedHash, dwAssaultedHash);
  380. // Copy length of Encrypted bytes to return value
  381. *p_dwCryptLen = dwAssaultedHash;
  382. // Get the hash data and store it in a string
  383. if(!CryptGetHashParam(hHash, HP_HASHVAL, *lpAssaultedHash, &dwAssaultedHash, 0)){
  384. if(hProv) CryptReleaseContext(hProv, 0);
  385. if(hHash) CryptDestroyHash(hHash);
  386. if(lpAssaultedHash){
  387. delete[] *lpAssaultedHash;
  388. *lpAssaultedHash = NULL;
  389. }
  390. return E_UNEXPECTED;
  391. }
  392. // Clean up
  393. if(hProv) CryptReleaseContext(hProv, 0);
  394. if(hHash) CryptDestroyHash(hHash);
  395. return S_OK;
  396. }/* end of function EncryptPassword */
  397. /*************************************************************/
  398. /* Function: ConfirmPassword */
  399. /* Description: */
  400. /* There is no need for a user to confirm passwords unless */
  401. /* they are hacking the password. */
  402. /* ConfirmPassword always fails (and waits five seconds) */
  403. /*************************************************************/
  404. STDMETHODIMP CMSDVDAdm::ConfirmPassword(BSTR strUserName, BSTR strPassword, VARIANT_BOOL *pVal){
  405. Sleep(1000);
  406. return E_FAIL;
  407. }
  408. /*************************************************************/
  409. /* Function: _ConfirmPassword */
  410. /* Description: comfired a password with the one saved */
  411. /*************************************************************/
  412. STDMETHODIMP CMSDVDAdm::_ConfirmPassword(BSTR /*strUserName*/,
  413. BSTR strPassword, VARIANT_BOOL *fRight){
  414. HRESULT hr = S_OK;
  415. try {
  416. USES_CONVERSION;
  417. if(!strPassword || !fRight){
  418. throw E_POINTER;
  419. }
  420. UINT bStrLen = lstrlen(strPassword);
  421. if(bStrLen >= MAX_PASSWD){
  422. throw E_INVALIDARG;
  423. }
  424. LPTSTR szPassword = OLE2T(strPassword);
  425. BYTE szSavedPasswd[MAX_PASSWD];
  426. DWORD dwLen = MAX_PASSWD;
  427. BOOL bFound = GetRegistryBytes(g_szPassword, szSavedPasswd, &dwLen);
  428. // if no password has been set yet
  429. if (!bFound || dwLen == 0) {
  430. // so in this case accept only an empty string
  431. if(lstrlen(szPassword) <= 0){
  432. *fRight = VARIANT_TRUE;
  433. }
  434. else {
  435. *fRight = VARIANT_FALSE;
  436. }
  437. throw (hr);
  438. }
  439. DWORD dwAssault = 0;
  440. bFound = GetRegistryDword(g_szSalt, &dwAssault, 0);
  441. if(!bFound ){
  442. // Old style password since there is no salt
  443. // ignore current password until it is reset
  444. *fRight = VARIANT_TRUE;
  445. throw(hr);
  446. }
  447. // if password is 0 len and password is set don't even try to encrypt just return false
  448. if(lstrlen(szPassword) <= 0){
  449. *fRight = VARIANT_FALSE;
  450. throw(hr);
  451. }
  452. // Encrypt the password with the salt from the registry
  453. BYTE *pszEncrypted = NULL;
  454. DWORD dwCryptLen = 0;
  455. hr = EncryptPassword(szPassword, &pszEncrypted, &dwCryptLen, &dwAssault, FALSE);
  456. if(FAILED(hr)){
  457. throw (hr);
  458. }
  459. // Compare the Encrypted input password with the saved password
  460. if(memcmp(pszEncrypted, szSavedPasswd, (dwAssault <= dwLen?dwAssault:dwLen) ) == 0)
  461. *fRight = VARIANT_TRUE;
  462. else
  463. *fRight = VARIANT_FALSE;
  464. delete[] pszEncrypted;
  465. }
  466. catch(HRESULT hrTmp){
  467. hr = hrTmp;
  468. }
  469. catch(...){
  470. hr = E_UNEXPECTED;
  471. }
  472. if(FAILED(hr)){
  473. Sleep(1000);
  474. }
  475. return (HandleError(hr));
  476. }/* end of function ConfirmPassword */
  477. /*************************************************************/
  478. /* Function: ChangePassword */
  479. /* Description: password change requested */
  480. /*************************************************************/
  481. STDMETHODIMP CMSDVDAdm::ChangePassword(BSTR strUserName,
  482. BSTR strOldPassword, BSTR strNewPassword){
  483. HRESULT hr = S_OK;
  484. try {
  485. USES_CONVERSION;
  486. if(!strUserName || !strOldPassword || !strNewPassword){
  487. return E_POINTER;
  488. }
  489. // check the size of the string so we do not overwrite
  490. // or write a very big chunk into registry
  491. if(lstrlen(strNewPassword) >= MAX_PASSWD){
  492. throw(E_FAIL);
  493. }
  494. LPTSTR szNewPassword = OLE2T(strNewPassword);
  495. // Confirm old password first
  496. VARIANT_BOOL temp;
  497. _ConfirmPassword(strUserName, strOldPassword, &temp);
  498. if (temp == VARIANT_FALSE){
  499. throw E_ACCESSDENIED;
  500. }
  501. DWORD dwAssault = 0;
  502. DWORD dwCryptLen = 0;
  503. BYTE *pszEncrypted = NULL;
  504. hr = EncryptPassword(szNewPassword, &pszEncrypted, &dwCryptLen, &dwAssault, TRUE);
  505. if(FAILED(hr)){
  506. throw E_FAIL;
  507. }
  508. BOOL bSuccess = SetRegistryBytes(g_szPassword, pszEncrypted, dwCryptLen);
  509. if (!bSuccess){
  510. hr = E_FAIL;
  511. }
  512. delete[] pszEncrypted;
  513. // If storing the password hash failed, don't store the salt
  514. if(SUCCEEDED(hr)){
  515. bSuccess = SetRegistryDword(g_szSalt, dwAssault);
  516. if (!bSuccess){
  517. hr = E_FAIL;
  518. }
  519. }
  520. }
  521. catch(HRESULT hrTmp){
  522. hr = hrTmp;
  523. }
  524. catch(...){
  525. hr = E_UNEXPECTED;
  526. }
  527. return HandleError(hr);
  528. }/* end of function ChangePassword */
  529. /*************************************************************/
  530. /* Function: SaveParentalLevel */
  531. /*************************************************************/
  532. STDMETHODIMP CMSDVDAdm::SaveParentalLevel(long lParentalLevel,
  533. BSTR strUserName, BSTR strPassword){
  534. HRESULT hr = S_OK;
  535. try {
  536. if (lParentalLevel != LEVEL_DISABLED &&
  537. (lParentalLevel < LEVEL_G || lParentalLevel > LEVEL_ADULT)) {
  538. throw (E_INVALIDARG);
  539. } /* end of if statement */
  540. if (m_lParentctrlLevel != lParentalLevel) {
  541. // Confirm password first
  542. VARIANT_BOOL temp;
  543. _ConfirmPassword(strUserName, strPassword, &temp);
  544. if (temp == VARIANT_FALSE)
  545. throw (E_ACCESSDENIED);
  546. }
  547. BOOL bSuccess = SetRegistryDword(g_szPlayerLevel, (DWORD) lParentalLevel);
  548. if (!bSuccess){
  549. throw E_FAIL;
  550. }
  551. m_lParentctrlLevel = lParentalLevel;
  552. }
  553. catch(HRESULT hrTmp){
  554. hr = hrTmp;
  555. }
  556. catch(...){
  557. hr = E_UNEXPECTED;
  558. }
  559. return HandleError(hr);
  560. }/* end of function SaveParentalLevel */
  561. /*************************************************************/
  562. /* Name: SaveParentalCountry */
  563. /*************************************************************/
  564. STDMETHODIMP CMSDVDAdm::SaveParentalCountry(long lCountry,
  565. BSTR strUserName,BSTR strPassword){
  566. HRESULT hr = S_OK;
  567. try {
  568. if(lCountry < 0 && lCountry > 0xffff){
  569. throw(E_INVALIDARG);
  570. }/* end of if statement */
  571. BYTE bCountryCode[2];
  572. bCountryCode[0] = BYTE(lCountry>>8);
  573. bCountryCode[1] = BYTE(lCountry);
  574. // convert the input country code to upper case by applying ToUpper to each letter
  575. WORD wCountry = ISO3166::PackCode( (char *)bCountryCode );
  576. BOOL bFound = FALSE;
  577. for( unsigned i=0; i<ISO3166::GetNumCountries(); i++ )
  578. {
  579. if( ISO3166::PackCode(ISO3166::GetCountry(i).Code) == wCountry )
  580. {
  581. bFound = TRUE;
  582. }
  583. }
  584. // Not a valid country code
  585. if (!bFound) {
  586. throw(E_INVALIDARG);
  587. }/* end of if statement */
  588. if (m_lParentctrlCountry != lCountry) {
  589. // Confirm password first
  590. VARIANT_BOOL temp;
  591. _ConfirmPassword(strUserName, strPassword, &temp);
  592. if (temp == VARIANT_FALSE)
  593. throw(E_ACCESSDENIED);
  594. }
  595. BOOL bSuccess = SetRegistryDword(g_szPlayerCountry, (DWORD) lCountry);
  596. if (!bSuccess){
  597. throw E_FAIL;
  598. }
  599. m_lParentctrlCountry = lCountry;
  600. }
  601. catch(HRESULT hrTmp){
  602. hr = hrTmp;
  603. }
  604. catch(...){
  605. hr = E_UNEXPECTED;
  606. }
  607. return (HandleError(hr));
  608. }/* end of function SaveParentalCountry */
  609. /*************************************************************/
  610. /* Function: put_DisableScreenSaver */
  611. /*************************************************************/
  612. STDMETHODIMP CMSDVDAdm::put_DisableScreenSaver(VARIANT_BOOL fDisable){
  613. HRESULT hr = S_OK;
  614. try {
  615. if (fDisable == VARIANT_FALSE)
  616. RestoreScreenSaver();
  617. else
  618. DisableScreenSaver();
  619. SetRegistryDword(g_szDisableScrnSvr, (DWORD) fDisable);
  620. m_fDisableScreenSaver = fDisable;
  621. }
  622. catch(HRESULT hrTmp){
  623. hr = hrTmp;
  624. }
  625. catch(...){
  626. hr = E_UNEXPECTED;
  627. }
  628. return HandleError(hr);
  629. }/* end of function put_DisableScreenSaver */
  630. /*************************************************************/
  631. /* Function: get_DisableScreenSaver */
  632. /*************************************************************/
  633. STDMETHODIMP CMSDVDAdm::get_DisableScreenSaver(VARIANT_BOOL *fDisable){
  634. HRESULT hr = S_OK;
  635. try {
  636. if(NULL == fDisable){
  637. hr = E_POINTER;
  638. throw(hr);
  639. }/* end of if statement */
  640. *fDisable = m_fDisableScreenSaver;
  641. }
  642. catch(HRESULT hrTmp){
  643. hr = hrTmp;
  644. }
  645. catch(...){
  646. hr = E_UNEXPECTED;
  647. }
  648. return HandleError(hr);
  649. }/* end of function get_DisableScreenSaver */
  650. /*************************************************************/
  651. /* Function: SaveScreenSaver */
  652. /*************************************************************/
  653. HRESULT CMSDVDAdm::SaveScreenSaver(){
  654. SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &m_bScrnSvrOld, 0);
  655. SystemParametersInfo(SPI_GETLOWPOWERACTIVE, 0, &m_bPowerlowOld, 0);
  656. SystemParametersInfo(SPI_GETPOWEROFFACTIVE, 0, &m_bPowerOffOld, 0);
  657. return S_OK;
  658. }
  659. /*************************************************************/
  660. /* Function: DisableScreenSaver */
  661. /*************************************************************/
  662. HRESULT CMSDVDAdm::DisableScreenSaver(){
  663. SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
  664. SystemParametersInfo(SPI_SETLOWPOWERACTIVE, FALSE, NULL, 0);
  665. SystemParametersInfo(SPI_SETPOWEROFFACTIVE, FALSE, NULL, 0);
  666. return S_OK;
  667. }/* end of function DisableScreenSaver */
  668. /*************************************************************/
  669. /* Function: RestoreScreenSaver */
  670. /*************************************************************/
  671. STDMETHODIMP CMSDVDAdm::RestoreScreenSaver(){
  672. HRESULT hr = S_OK;
  673. try {
  674. SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, m_bScrnSvrOld, NULL, 0);
  675. SystemParametersInfo(SPI_SETLOWPOWERACTIVE, m_bPowerlowOld, NULL, 0);
  676. SystemParametersInfo(SPI_SETPOWEROFFACTIVE, m_bPowerOffOld, NULL, 0);
  677. }
  678. catch(HRESULT hrTmp){
  679. hr = hrTmp;
  680. }
  681. catch(...){
  682. hr = E_UNEXPECTED;
  683. }
  684. return HandleError(hr);
  685. }/* end of function RestoreScreenSaver */
  686. /*************************************************************/
  687. /* Function: GetParentalLevel */
  688. /*************************************************************/
  689. STDMETHODIMP CMSDVDAdm::GetParentalLevel(long *lLevel){
  690. HRESULT hr = S_OK;
  691. try {
  692. if(NULL == lLevel){
  693. hr = E_POINTER;
  694. throw(hr);
  695. }/* end of if statement */
  696. *lLevel = m_lParentctrlLevel;
  697. }
  698. catch(HRESULT hrTmp){
  699. hr = hrTmp;
  700. }
  701. catch(...){
  702. hr = E_UNEXPECTED;
  703. }
  704. return HandleError(hr);
  705. }/* end of function GetParentalLevel */
  706. /*************************************************************/
  707. /* Function: GetParentalCountry */
  708. /*************************************************************/
  709. STDMETHODIMP CMSDVDAdm::GetParentalCountry(long *lCountry){
  710. HRESULT hr = S_OK;
  711. try {
  712. if(NULL == lCountry){
  713. hr = E_POINTER;
  714. throw(hr);
  715. }/* end of if statement */
  716. *lCountry = m_lParentctrlCountry;
  717. }
  718. catch(HRESULT hrTmp){
  719. hr = hrTmp;
  720. }
  721. catch(...){
  722. hr = E_UNEXPECTED;
  723. }
  724. return HandleError(hr);
  725. }/* end of function GetParentalCountry */
  726. /*************************************************************/
  727. /* Name: get_DefaultAudioLCID
  728. /* Description: -1 means title default
  729. /*************************************************************/
  730. STDMETHODIMP CMSDVDAdm::get_DefaultAudioLCID(long *pVal){
  731. HRESULT hr = S_OK;
  732. try {
  733. if(NULL == pVal){
  734. hr = E_POINTER;
  735. throw(hr);
  736. }/* end of if statement */
  737. BOOL bSuccess = GetRegistryDwordCU(g_szDefaultAudio, (DWORD*) pVal, (DWORD)-1);
  738. }
  739. catch(HRESULT hrTmp){
  740. hr = hrTmp;
  741. }
  742. catch(...){
  743. hr = E_UNEXPECTED;
  744. }
  745. return HandleError(hr);
  746. } /* end of function get_DefaultAudioLCID */
  747. /*************************************************************/
  748. /* Name: put_DefaultAudioLCID
  749. /* Description: -1 means title default
  750. /*************************************************************/
  751. STDMETHODIMP CMSDVDAdm::put_DefaultAudioLCID(long newVal)
  752. {
  753. HRESULT hr = S_OK;
  754. try {
  755. if (!::IsValidLocale(newVal, LCID_SUPPORTED) && newVal != -1) {
  756. throw (E_INVALIDARG);
  757. } /* end of if statement */
  758. SetRegistryDwordCU(g_szDefaultAudio, (DWORD) newVal);
  759. }
  760. catch(HRESULT hrTmp){
  761. hr = hrTmp;
  762. }
  763. catch(...){
  764. hr = E_UNEXPECTED;
  765. }
  766. return HandleError(hr);
  767. } /* end of put_DefaultAudioLCID */
  768. /*************************************************************/
  769. /* Name: get_DefaultSubpictureLCID
  770. /* Description: -1 means title default
  771. /*************************************************************/
  772. STDMETHODIMP CMSDVDAdm::get_DefaultSubpictureLCID(long *pVal)
  773. {
  774. HRESULT hr = S_OK;
  775. try {
  776. if(NULL == pVal){
  777. hr = E_POINTER;
  778. throw(hr);
  779. }/* end of if statement */
  780. GetRegistryDwordCU(g_szDefaultSP, (DWORD*) pVal, (DWORD)-1);
  781. }
  782. catch(HRESULT hrTmp){
  783. hr = hrTmp;
  784. }
  785. catch(...){
  786. hr = E_UNEXPECTED;
  787. }
  788. return HandleError(hr);
  789. } /* end of get_DefaultSubpictureLCID */
  790. /*************************************************************/
  791. /* Name: put_DefaultSubpictureLCID
  792. /* Description: -1 means title default
  793. /*************************************************************/
  794. STDMETHODIMP CMSDVDAdm::put_DefaultSubpictureLCID(long newVal)
  795. {
  796. HRESULT hr = S_OK;
  797. try {
  798. if (!::IsValidLocale(newVal, LCID_SUPPORTED) && newVal != -1) {
  799. throw (E_INVALIDARG);
  800. } /* end of if statement */
  801. SetRegistryDwordCU(g_szDefaultSP, (DWORD) newVal);
  802. }
  803. catch(HRESULT hrTmp){
  804. hr = hrTmp;
  805. }
  806. catch(...){
  807. hr = E_UNEXPECTED;
  808. }
  809. return HandleError(hr);
  810. } /* end of put_DefaultSubpictureLCID */
  811. /*************************************************************/
  812. /* Name: get_DefaultMenuLCID
  813. /* Description: -1 means title default
  814. /*************************************************************/
  815. STDMETHODIMP CMSDVDAdm::get_DefaultMenuLCID(long *pVal)
  816. {
  817. HRESULT hr = S_OK;
  818. try {
  819. if(NULL == pVal){
  820. hr = E_POINTER;
  821. throw(hr);
  822. }/* end of if statement */
  823. GetRegistryDwordCU(g_szDefaultMenu, (DWORD*) pVal, (DWORD)-1);
  824. }
  825. catch(HRESULT hrTmp){
  826. hr = hrTmp;
  827. }
  828. catch(...){
  829. hr = E_UNEXPECTED;
  830. }
  831. return HandleError(hr);
  832. } /* end of get_DefaultMenuLCID */
  833. /*************************************************************/
  834. /* Name: put_DefaultMenuLCID
  835. /* Description: -1 means title default
  836. /*************************************************************/
  837. STDMETHODIMP CMSDVDAdm::put_DefaultMenuLCID(long newVal)
  838. {
  839. HRESULT hr = S_OK;
  840. try {
  841. if (!::IsValidLocale(newVal, LCID_SUPPORTED) && newVal != -1) {
  842. throw (E_INVALIDARG);
  843. } /* end of if statement */
  844. SetRegistryDwordCU(g_szDefaultMenu, (DWORD) newVal);
  845. }
  846. catch(HRESULT hrTmp){
  847. hr = hrTmp;
  848. }
  849. catch(...){
  850. hr = E_UNEXPECTED;
  851. }
  852. return HandleError(hr);
  853. } /* end of put_DefaultMenuLCID */
  854. /*************************************************************/
  855. /* Name: put_BookmarkOnStop
  856. /* Description:
  857. /*************************************************************/
  858. STDMETHODIMP CMSDVDAdm::put_BookmarkOnStop(VARIANT_BOOL fEnable){
  859. HRESULT hr = S_OK;
  860. try {
  861. m_fBookmarkOnStop = fEnable;
  862. SetRegistryDword(g_szBookmarkOnStop, (DWORD) fEnable);
  863. }
  864. catch(HRESULT hrTmp){
  865. hr = hrTmp;
  866. }
  867. catch(...){
  868. hr = E_UNEXPECTED;
  869. }
  870. return HandleError(hr);
  871. }
  872. /*************************************************************/
  873. /* Name: get_BookmarkOnStop
  874. /* Description:
  875. /*************************************************************/
  876. STDMETHODIMP CMSDVDAdm::get_BookmarkOnStop(VARIANT_BOOL *fEnable){
  877. HRESULT hr = S_OK;
  878. try {
  879. if(NULL == fEnable){
  880. hr = E_POINTER;
  881. throw(hr);
  882. }/* end of if statement */
  883. *fEnable = m_fBookmarkOnStop;
  884. }
  885. catch(HRESULT hrTmp){
  886. hr = hrTmp;
  887. }
  888. catch(...){
  889. hr = E_UNEXPECTED;
  890. }
  891. return HandleError(hr);
  892. }
  893. /*************************************************************/
  894. /* Name: put_BookmarkOnClose
  895. /* Description:
  896. /*************************************************************/
  897. STDMETHODIMP CMSDVDAdm::put_BookmarkOnClose(VARIANT_BOOL fEnable){
  898. HRESULT hr = S_OK;
  899. try {
  900. m_fBookmarkOnClose = fEnable;
  901. SetRegistryDword(g_szBookmarkOnClose, (DWORD) fEnable);
  902. }
  903. catch(HRESULT hrTmp){
  904. hr = hrTmp;
  905. }
  906. catch(...){
  907. hr = E_UNEXPECTED;
  908. }
  909. return HandleError(hr);
  910. }
  911. /*************************************************************/
  912. /* Name: get_BookmarkOnClose
  913. /* Description:
  914. /*************************************************************/
  915. STDMETHODIMP CMSDVDAdm::get_BookmarkOnClose(VARIANT_BOOL *fEnable){
  916. HRESULT hr = S_OK;
  917. try {
  918. if(NULL == fEnable){
  919. hr = E_POINTER;
  920. throw(hr);
  921. }/* end of if statement */
  922. *fEnable = m_fBookmarkOnClose;
  923. }
  924. catch(HRESULT hrTmp){
  925. hr = hrTmp;
  926. }
  927. catch(...){
  928. hr = E_UNEXPECTED;
  929. }
  930. return HandleError(hr);
  931. }
  932. /*************************************************************************/
  933. /* Function: InterfaceSupportsErrorInfo */
  934. /*************************************************************************/
  935. STDMETHODIMP CMSDVDAdm::InterfaceSupportsErrorInfo(REFIID riid){
  936. static const IID* arr[] = {
  937. &IID_IMSDVDAdm,
  938. };
  939. for (int i=0; i<sizeof(arr)/sizeof(arr[0]); i++){
  940. if (InlineIsEqualGUID(*arr[i], riid))
  941. return S_OK;
  942. }/* end of for loop */
  943. return S_FALSE;
  944. }/* end of function InterfaceSupportsErrorInfo */
  945. /*************************************************************************/
  946. /* Function: HandleError */
  947. /* Description: Gets Error Descriptio, so we can suppor IError Info. */
  948. /*************************************************************************/
  949. HRESULT CMSDVDAdm::HandleError(HRESULT hr){
  950. try {
  951. if(FAILED(hr)){
  952. // Ensure that the string is Null Terminated
  953. TCHAR strError[MAX_ERROR_TEXT_LEN+1];
  954. ZeroMemory(strError, MAX_ERROR_TEXT_LEN+1);
  955. if(AMGetErrorText(hr , strError , MAX_ERROR_TEXT_LEN)){
  956. USES_CONVERSION;
  957. Error(T2W(strError));
  958. }
  959. else {
  960. ATLTRACE(TEXT("Unhandled Error Code \n")); // please add it
  961. ATLASSERT(FALSE);
  962. }/* end of if statement */
  963. }/* end of if statement */
  964. }/* end of try statement */
  965. catch(HRESULT hrTmp){
  966. hr = hrTmp;
  967. }/* end of catch statement */
  968. catch(...){
  969. // keep the hr same
  970. }/* end of catch statement */
  971. return (hr);
  972. }/* end of function HandleError */
  973. /*************************************************************************/
  974. /* End of file: MSDVDAdm.cpp */
  975. /*************************************************************************/