Source code of Windows XP (NT5)
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.

4861 lines
141 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1998.
  5. //
  6. // File: Reg.cpp
  7. //
  8. // Contents: Registration routines
  9. //
  10. // Classes:
  11. //
  12. // Notes:
  13. //
  14. // History: 05-Nov-97 rogerg Created.
  15. // 11-18-97 susia Added Autosync and user reg key functions
  16. //
  17. //--------------------------------------------------------------------------
  18. #include "precomp.h"
  19. #ifdef _SENS
  20. #include <eventsys.h> // include event system
  21. #include <sens.h>
  22. #include <sensevts.h>
  23. #endif // _SENS
  24. // temporariy define new mstask flag in case hasn't
  25. // propogated to sdk\inc
  26. #ifndef TASK_FLAG_RUN_ONLY_IF_LOGGED_ON
  27. #define TASK_FLAG_RUN_ONLY_IF_LOGGED_ON (0x2000)
  28. #endif // TASK_FLAG_RUN_ONLY_IF_LOGGED_ON
  29. extern HINSTANCE g_hmodThisDll; // Handle to this DLL itself.
  30. extern OSVERSIONINFOA g_OSVersionInfo;
  31. extern CRITICAL_SECTION g_DllCriticalSection; // Global Critical Section for this DLL
  32. // only return success on NT 5.0
  33. BOOL GetUserDefaultSecurityAttribs(SECURITY_ATTRIBUTES *psa
  34. ,PSECURITY_DESCRIPTOR psd,
  35. PACL *ppOutAcl)
  36. {
  37. BOOL bRetVal;
  38. int cbAcl;
  39. PACL pAcl = NULL;
  40. PSID pInteractiveUserSid = NULL;
  41. PSID pLocalSystemSid = NULL;
  42. PSID pAdminsSid = NULL;
  43. SID_IDENTIFIER_AUTHORITY LocalSystemAuthority = SECURITY_NT_AUTHORITY;
  44. *ppOutAcl = NULL;
  45. bRetVal = FALSE;
  46. // in the structure.
  47. bRetVal = InitializeSecurityDescriptor(
  48. psd, // Pointer to SD
  49. SECURITY_DESCRIPTOR_REVISION // SD revision
  50. );
  51. if (!bRetVal)
  52. {
  53. AssertSz(0,"Unable to Init SecurityDescriptor");
  54. goto errRtn;
  55. }
  56. // setup acls.
  57. bRetVal = AllocateAndInitializeSid(
  58. &LocalSystemAuthority, // Pointer to identifier authority
  59. 1, // Count of subauthority
  60. SECURITY_INTERACTIVE_RID, // Subauthority 0
  61. 0, // Subauthority 1
  62. 0, // Subauthority 2
  63. 0, // Subauthority 3
  64. 0, // Subauthority 4
  65. 0, // Subauthority 5
  66. 0, // Subauthority 6
  67. 0, // Subauthority 7
  68. &pInteractiveUserSid // pointer to pointer to SID
  69. );
  70. if (!bRetVal)
  71. {
  72. AssertSz(0,"Alocate sid failed");
  73. goto errRtn;
  74. }
  75. bRetVal = AllocateAndInitializeSid(
  76. &LocalSystemAuthority, // Pointer to identifier authority
  77. 2,
  78. SECURITY_BUILTIN_DOMAIN_RID,
  79. DOMAIN_ALIAS_RID_ADMINS,
  80. 0,
  81. 0, // Subauthority 3
  82. 0, // Subauthority 4
  83. 0, // Subauthority 5
  84. 0, // Subauthority 6
  85. 0, // Subauthority 7
  86. &pAdminsSid // pointer to pointer to SID
  87. );
  88. if (!bRetVal)
  89. {
  90. AssertSz(0,"Alocate sid failed");
  91. goto errRtn;
  92. }
  93. bRetVal = AllocateAndInitializeSid(
  94. &LocalSystemAuthority,// Pointer to identifier authority
  95. 1, // Count of subauthority
  96. SECURITY_LOCAL_SYSTEM_RID, // Subauthority 0
  97. 0, // Subauthority 1
  98. 0, // Subauthority 2
  99. 0, // Subauthority 3
  100. 0, // Subauthority 4
  101. 0, // Subauthority 5
  102. 0, // Subauthority 6
  103. 0, // Subauthority 7
  104. &pLocalSystemSid // pointer to pointer to SID
  105. );
  106. if (!bRetVal)
  107. {
  108. AssertSz(0,"Alocate sid failed");
  109. goto errRtn;
  110. }
  111. cbAcl = sizeof (ACL)
  112. + 3 * sizeof (ACCESS_ALLOWED_ACE)
  113. + GetLengthSid(pInteractiveUserSid)
  114. + GetLengthSid(pLocalSystemSid)
  115. + GetLengthSid(pAdminsSid);
  116. pAcl = (PACL) new char[cbAcl];
  117. if (NULL == pAcl)
  118. {
  119. bRetVal = FALSE;
  120. AssertSz(0,"unable to alloc ACL");
  121. goto errRtn;
  122. }
  123. bRetVal = InitializeAcl(
  124. pAcl, // Pointer to the ACL
  125. cbAcl, // Size of ACL
  126. ACL_REVISION // Revision level of ACL
  127. );
  128. if (!bRetVal)
  129. {
  130. AssertSz(0,"InitAcl failed");
  131. goto errRtn;
  132. }
  133. bRetVal = AddAccessAllowedAce(
  134. pAcl, // Pointer to the ACL
  135. ACL_REVISION, // ACL revision level
  136. SPECIFIC_RIGHTS_ALL | GENERIC_READ | DELETE , // Access Mask
  137. pInteractiveUserSid // Pointer to SID
  138. );
  139. if (!bRetVal)
  140. {
  141. AssertSz(0,"AddAccessAllowed Failed");
  142. goto errRtn;
  143. }
  144. bRetVal = AddAccessAllowedAce(
  145. pAcl, // Pointer to the ACL
  146. ACL_REVISION, // ACL revision level
  147. GENERIC_ALL, // Access Mask
  148. pAdminsSid // Pointer to SID
  149. );
  150. if (!bRetVal)
  151. {
  152. AssertSz(0,"AddAccessAllowed Failed");
  153. goto errRtn;
  154. }
  155. bRetVal = AddAccessAllowedAce(
  156. pAcl, // Pointer to the ACL
  157. ACL_REVISION, // ACL revision level
  158. GENERIC_ALL, // Access Mask
  159. pLocalSystemSid // Pointer to SID
  160. );
  161. if (!bRetVal)
  162. {
  163. AssertSz(0,"AddAccessAllowed Failed");
  164. goto errRtn;
  165. }
  166. bRetVal = SetSecurityDescriptorDacl(psd,TRUE,pAcl,FALSE);
  167. if (!bRetVal)
  168. {
  169. AssertSz(0,"SetSecurityDescirptorDacl Failed");
  170. goto errRtn;
  171. }
  172. psa->nLength = sizeof(SECURITY_ATTRIBUTES);
  173. psa->lpSecurityDescriptor = psd;
  174. psa->bInheritHandle = FALSE;
  175. errRtn:
  176. if (pInteractiveUserSid)
  177. {
  178. FreeSid(pInteractiveUserSid);
  179. }
  180. if (pLocalSystemSid)
  181. {
  182. FreeSid(pLocalSystemSid);
  183. }
  184. if (pAdminsSid)
  185. {
  186. FreeSid(pAdminsSid);
  187. }
  188. //
  189. // On failure, we clean the ACL up. On success, the caller cleans
  190. // it up after using it.
  191. //
  192. if (FALSE == bRetVal)
  193. {
  194. if (pAcl)
  195. {
  196. delete pAcl;
  197. }
  198. }
  199. else
  200. {
  201. Assert(pAcl);
  202. *ppOutAcl = pAcl;
  203. }
  204. return bRetVal;
  205. }
  206. const WCHAR SZ_USERSIDKEY[] = TEXT("SID");
  207. // calls regOpen or create based on fCreate param
  208. LONG RegGetKeyHelper(HKEY hkey,LPCWSTR pszKey,REGSAM samDesired,BOOL fCreate,
  209. HKEY *phkResult,DWORD *pdwDisposition)
  210. {
  211. LONG lRet = -1;
  212. Assert(pdwDisposition);
  213. *pdwDisposition = 0;
  214. if (fCreate)
  215. {
  216. lRet = RegCreateKeyEx(hkey,pszKey,0,NULL,REG_OPTION_NON_VOLATILE,
  217. samDesired,NULL,phkResult,pdwDisposition);
  218. }
  219. else
  220. {
  221. lRet = RegOpenKeyEx(hkey,pszKey,0,samDesired,phkResult);
  222. if (ERROR_SUCCESS == lRet)
  223. {
  224. *pdwDisposition = REG_OPENED_EXISTING_KEY;
  225. }
  226. }
  227. return lRet;
  228. }
  229. // called to create a new UserKey or subkey
  230. LONG RegCreateUserSubKey(
  231. HKEY hKey,
  232. LPCWSTR lpSubKey,
  233. REGSAM samDesired,
  234. PHKEY phkResult)
  235. {
  236. LONG lRet;
  237. DWORD dwDisposition;
  238. lRet = RegCreateKeyEx(hKey,lpSubKey,0,NULL,REG_OPTION_NON_VOLATILE,
  239. samDesired,NULL,phkResult,&dwDisposition);
  240. if (VER_PLATFORM_WIN32_NT == g_OSVersionInfo.dwPlatformId)
  241. {
  242. // !! if subkey contains \\ don't traverse back through the list
  243. if ( (ERROR_SUCCESS == lRet) && (REG_CREATED_NEW_KEY == dwDisposition))
  244. {
  245. HKEY hKeySecurity;
  246. SECURITY_ATTRIBUTES sa;
  247. SECURITY_DESCRIPTOR sd;
  248. PACL pOutAcl;
  249. if (ERROR_SUCCESS == RegOpenKeyEx(hKey,
  250. lpSubKey,
  251. REG_OPTION_OPEN_LINK, WRITE_DAC,&hKeySecurity) )
  252. {
  253. if (GetUserDefaultSecurityAttribs(&sa,&sd,&pOutAcl))
  254. {
  255. RegSetKeySecurity(hKeySecurity,
  256. (SECURITY_INFORMATION) DACL_SECURITY_INFORMATION,
  257. &sd);
  258. delete pOutAcl;
  259. }
  260. RegCloseKey(hKeySecurity);
  261. }
  262. }
  263. }
  264. return lRet;
  265. }
  266. STDAPI_(HKEY) RegOpenUserKey(HKEY hkeyParent,REGSAM samDesired,BOOL fCreate,BOOL fCleanReg)
  267. {
  268. TCHAR pszDomainAndUser[MAX_DOMANDANDMACHINENAMESIZE];
  269. HKEY hKeyUser;
  270. BOOL fSetUserSid = FALSE;
  271. WCHAR szUserSID[MAX_PATH + 1];
  272. DWORD dwType;
  273. DWORD dwDisposition;
  274. LONG ret;
  275. GetDefaultDomainAndUserName(pszDomainAndUser,TEXT("_"),MAX_DOMANDANDMACHINENAMESIZE);
  276. // If suppose to clean the settings for the user/ delete the key
  277. if (fCleanReg)
  278. {
  279. RegDeleteKeyNT(hkeyParent,pszDomainAndUser);
  280. }
  281. if (ERROR_SUCCESS != (ret = RegGetKeyHelper(hkeyParent,pszDomainAndUser,samDesired,fCreate,
  282. &hKeyUser,&dwDisposition)))
  283. {
  284. hKeyUser = NULL;
  285. }
  286. // On NT 5.0 Verify sid matches and if doesn't blow away any settings.
  287. // then create again.
  288. if (hKeyUser && (VER_PLATFORM_WIN32_NT == g_OSVersionInfo.dwPlatformId))
  289. {
  290. WCHAR szRegSID[MAX_PATH + 1];
  291. DWORD dwDataSize;
  292. dwDataSize = ARRAY_SIZE(szRegSID);
  293. if (ERROR_SUCCESS !=RegQueryValueEx(hKeyUser,
  294. SZ_USERSIDKEY,NULL, &dwType,
  295. (LPBYTE) szRegSID, &dwDataSize))
  296. {
  297. fSetUserSid = TRUE;
  298. // if have to set the sid need to make sure openned
  299. // with set Value and if didn't close key and
  300. // let create re-open it with the desired access.
  301. if (!(samDesired & KEY_SET_VALUE))
  302. {
  303. RegCloseKey(hKeyUser);
  304. hKeyUser = NULL;
  305. }
  306. }
  307. else
  308. {
  309. dwDataSize = ARRAY_SIZE(szUserSID);
  310. if (GetUserTextualSid(szUserSID, &dwDataSize))
  311. {
  312. if (lstrcmp(szRegSID, szUserSID))
  313. {
  314. // if don't have access privledges
  315. // to delete the User this will fail.
  316. // may want a call into SENS to delete the
  317. // User Key on Failure.
  318. RegCloseKey(hKeyUser);
  319. hKeyUser = NULL; // set to NULL so check below fails.
  320. RegDeleteKeyNT(hkeyParent,pszDomainAndUser);
  321. }
  322. }
  323. }
  324. }
  325. if (NULL == hKeyUser)
  326. {
  327. if (ERROR_SUCCESS != (ret = RegGetKeyHelper(hkeyParent,pszDomainAndUser,
  328. samDesired,fCreate,
  329. &hKeyUser,&dwDisposition)))
  330. {
  331. hKeyUser = NULL;
  332. }
  333. else
  334. {
  335. if (REG_CREATED_NEW_KEY == dwDisposition)
  336. {
  337. fSetUserSid = TRUE;
  338. }
  339. }
  340. }
  341. // on creation setup the security
  342. if (VER_PLATFORM_WIN32_NT == g_OSVersionInfo.dwPlatformId)
  343. {
  344. if ( (ERROR_SUCCESS == ret) && (REG_CREATED_NEW_KEY == dwDisposition))
  345. {
  346. HKEY hKeySecurity;
  347. SECURITY_ATTRIBUTES sa;
  348. SECURITY_DESCRIPTOR sd;
  349. PACL pOutAcl;
  350. // !! should have own call for sync type key security
  351. if (ERROR_SUCCESS == RegOpenKeyEx(hkeyParent,pszDomainAndUser,
  352. REG_OPTION_OPEN_LINK, WRITE_DAC,&hKeySecurity) )
  353. {
  354. if (GetUserDefaultSecurityAttribs(&sa,&sd,&pOutAcl))
  355. {
  356. RegSetKeySecurity(hKeySecurity,
  357. (SECURITY_INFORMATION) DACL_SECURITY_INFORMATION,
  358. &sd);
  359. delete pOutAcl;
  360. }
  361. RegCloseKey(hKeySecurity);
  362. }
  363. }
  364. }
  365. // setup the User sid.
  366. // depends on key being openned with KEY_SET_VALUE
  367. if (hKeyUser && fSetUserSid && (VER_PLATFORM_WIN32_NT == g_OSVersionInfo.dwPlatformId)
  368. && (samDesired & KEY_SET_VALUE))
  369. {
  370. WCHAR szUserSID[MAX_PATH + 1];
  371. DWORD dwDataSize;
  372. dwDataSize = ARRAY_SIZE(szUserSID);
  373. if (GetUserTextualSid(szUserSID, &dwDataSize))
  374. {
  375. DWORD dwType = REG_SZ;
  376. RegSetValueEx (hKeyUser,SZ_USERSIDKEY,NULL,
  377. dwType,
  378. (LPBYTE) szUserSID,
  379. (lstrlen(szUserSID) + 1)*sizeof(WCHAR));
  380. }
  381. }
  382. return hKeyUser;
  383. }
  384. STDAPI_(HKEY) RegGetSyncTypeKey(DWORD dwSyncType,REGSAM samDesired,BOOL fCreate)
  385. {
  386. HKEY hKeySyncType;
  387. LPCWSTR pszKey;
  388. LONG ret;
  389. DWORD dwDisposition;
  390. // get appropriate key to open based on sync type
  391. switch(dwSyncType)
  392. {
  393. case SYNCTYPE_MANUAL:
  394. pszKey = MANUALSYNC_REGKEY;
  395. break;
  396. case SYNCTYPE_AUTOSYNC:
  397. pszKey = AUTOSYNC_REGKEY;
  398. break;
  399. case SYNCTYPE_IDLE:
  400. pszKey = IDLESYNC_REGKEY;
  401. break;
  402. case SYNCTYPE_SCHEDULED:
  403. pszKey = SCHEDSYNC_REGKEY;
  404. break;
  405. case SYNCTYPE_PROGRESS:
  406. pszKey = PROGRESS_REGKEY;
  407. break;
  408. default:
  409. AssertSz(0,"Unknown SyncType");
  410. pszKey = NULL;
  411. break;
  412. }
  413. if (NULL == pszKey)
  414. {
  415. return NULL;
  416. }
  417. // first try to open the existing key
  418. if (ERROR_SUCCESS != (ret = RegGetKeyHelper(HKEY_LOCAL_MACHINE,pszKey,samDesired,fCreate,
  419. &hKeySyncType,&dwDisposition)))
  420. {
  421. // if can't open key try to create
  422. if (ERROR_ACCESS_DENIED == ret )
  423. {
  424. // if access denied, call sens to reset
  425. // the security on the topelevel keys.
  426. SyncMgrExecCmd_ResetRegSecurity();
  427. ret = RegGetKeyHelper(HKEY_LOCAL_MACHINE,pszKey,samDesired,fCreate,
  428. &hKeySyncType,&dwDisposition);
  429. }
  430. if (ERROR_SUCCESS != ret)
  431. {
  432. hKeySyncType = NULL;
  433. }
  434. }
  435. if (VER_PLATFORM_WIN32_NT == g_OSVersionInfo.dwPlatformId)
  436. {
  437. if ( (ERROR_SUCCESS == ret) && (REG_CREATED_NEW_KEY == dwDisposition))
  438. {
  439. HKEY hKeySecurity;
  440. SECURITY_ATTRIBUTES sa;
  441. SECURITY_DESCRIPTOR sd;
  442. PACL pOutAcl;
  443. // !! should have own call for sync type key security
  444. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,pszKey,
  445. REG_OPTION_OPEN_LINK, WRITE_DAC,&hKeySecurity) )
  446. {
  447. if (GetUserDefaultSecurityAttribs(&sa,&sd,&pOutAcl))
  448. {
  449. RegSetKeySecurity(hKeySecurity,
  450. (SECURITY_INFORMATION) DACL_SECURITY_INFORMATION,
  451. &sd);
  452. delete pOutAcl;
  453. }
  454. RegCloseKey(hKeySecurity);
  455. }
  456. }
  457. }
  458. return hKeySyncType;
  459. }
  460. STDAPI_(HKEY) RegGetCurrentUserKey(DWORD dwSyncType,REGSAM samDesired,BOOL fCreate)
  461. {
  462. HKEY hKeySyncType;
  463. HKEY hKeyUser = NULL;
  464. hKeySyncType = RegGetSyncTypeKey(dwSyncType,samDesired ,fCreate);
  465. if (hKeySyncType)
  466. {
  467. hKeyUser = RegOpenUserKey(hKeySyncType,samDesired,fCreate,FALSE /* fClean */);
  468. RegCloseKey(hKeySyncType);
  469. }
  470. return hKeyUser;
  471. }
  472. // tries to open and set security if necessary on the handler keys.
  473. STDAPI_(HKEY) RegGetHandlerTopLevelKey(REGSAM samDesired)
  474. {
  475. HKEY hKeyTopLevel;
  476. LONG ret;
  477. DWORD dwDisposition;
  478. // if open failed then try a create.
  479. if (ERROR_SUCCESS != (ret = RegCreateKeyEx (HKEY_LOCAL_MACHINE,HANDLERS_REGKEY,0, NULL,
  480. REG_OPTION_NON_VOLATILE,samDesired,NULL,
  481. &hKeyTopLevel,
  482. &dwDisposition)))
  483. {
  484. // if got an access denige on the handlers key
  485. // call sens to reset.
  486. if (ERROR_ACCESS_DENIED == ret )
  487. {
  488. // if access denied, call sens to reset
  489. // the security on the topelevel keys.
  490. // and try again
  491. SyncMgrExecCmd_ResetRegSecurity();
  492. ret = RegCreateKeyEx (HKEY_LOCAL_MACHINE,HANDLERS_REGKEY,0, NULL,
  493. REG_OPTION_NON_VOLATILE,samDesired,NULL,
  494. &hKeyTopLevel,
  495. &dwDisposition);
  496. }
  497. if (ERROR_SUCCESS != ret)
  498. {
  499. hKeyTopLevel = NULL;
  500. }
  501. }
  502. if (VER_PLATFORM_WIN32_NT == g_OSVersionInfo.dwPlatformId)
  503. {
  504. if ( (ERROR_SUCCESS == ret) && (REG_CREATED_NEW_KEY == dwDisposition))
  505. {
  506. HKEY hKeySecurity;
  507. SECURITY_ATTRIBUTES sa;
  508. SECURITY_DESCRIPTOR sd;
  509. PACL pOutAcl;
  510. // !! should have own call for toplevel handler key security
  511. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  512. HANDLERS_REGKEY,
  513. REG_OPTION_OPEN_LINK, WRITE_DAC,&hKeySecurity) )
  514. {
  515. if (GetUserDefaultSecurityAttribs(&sa,&sd,&pOutAcl))
  516. {
  517. RegSetKeySecurity(hKeySecurity,
  518. (SECURITY_INFORMATION) DACL_SECURITY_INFORMATION,
  519. &sd);
  520. delete pOutAcl;
  521. }
  522. RegCloseKey(hKeySecurity);
  523. }
  524. }
  525. }
  526. return hKeyTopLevel;
  527. }
  528. STDAPI_(HKEY) RegGetHandlerKey(HKEY hkeyParent,LPCWSTR pszHandlerClsid,REGSAM samDesired,
  529. BOOL fCreate)
  530. {
  531. HKEY hKeyHandler = NULL;
  532. LRESULT lRet;
  533. DWORD dwDisposition;
  534. if (ERROR_SUCCESS != (lRet = RegGetKeyHelper(hkeyParent,pszHandlerClsid,samDesired,
  535. fCreate,&hKeyHandler,&dwDisposition)))
  536. {
  537. hKeyHandler = NULL;
  538. }
  539. if (NULL == hKeyHandler)
  540. {
  541. // if got an access denied call sens to unlock
  542. if (ERROR_ACCESS_DENIED == lRet )
  543. {
  544. // if access denied, call sens to reset
  545. // the security on the topelevel keys.
  546. // and try again
  547. SyncMgrExecCmd_ResetRegSecurity();
  548. lRet = RegGetKeyHelper(hkeyParent,pszHandlerClsid,samDesired,
  549. fCreate,&hKeyHandler,&dwDisposition);
  550. }
  551. if (ERROR_SUCCESS != lRet)
  552. {
  553. hKeyHandler = NULL;
  554. }
  555. }
  556. if (VER_PLATFORM_WIN32_NT == g_OSVersionInfo.dwPlatformId)
  557. {
  558. if ( (ERROR_SUCCESS == lRet) && (REG_CREATED_NEW_KEY == dwDisposition))
  559. {
  560. HKEY hKeySecurity;
  561. SECURITY_ATTRIBUTES sa;
  562. SECURITY_DESCRIPTOR sd;
  563. PACL pOutAcl;
  564. // !! should have own call for handler key security
  565. if (ERROR_SUCCESS == RegOpenKeyEx(hkeyParent,
  566. pszHandlerClsid,
  567. REG_OPTION_OPEN_LINK, WRITE_DAC,&hKeySecurity) )
  568. {
  569. if (GetUserDefaultSecurityAttribs(&sa,&sd,&pOutAcl))
  570. {
  571. RegSetKeySecurity(hKeySecurity,
  572. (SECURITY_INFORMATION) DACL_SECURITY_INFORMATION,
  573. &sd);
  574. delete pOutAcl;
  575. }
  576. RegCloseKey(hKeySecurity);
  577. }
  578. }
  579. }
  580. return hKeyHandler;
  581. }
  582. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  583. Function: DWORD RegDeleteKeyNT(HKEY hStartKey , LPTSTR pKeyName )
  584. Summary: Recursively delete a key on NT
  585. Returns: Returns TRUE if successful, FALSE otherwise
  586. ------------------------------------------------------------------------F-F*/
  587. STDAPI_(DWORD) RegDeleteKeyNT(HKEY hStartKey , LPCWSTR pKeyName )
  588. {
  589. DWORD dwRtn, dwSubKeyLength;
  590. LPTSTR pSubKey = NULL;
  591. TCHAR szSubKey[MAX_KEY_LENGTH]; // (256) this should be dynamic.
  592. HKEY hkey;
  593. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  594. CMutexRegistry.Enter();
  595. // do not allow NULL or empty key name
  596. if ( pKeyName && lstrlen(pKeyName))
  597. {
  598. if( (dwRtn=RegOpenKeyEx(hStartKey,pKeyName,
  599. 0, KEY_ENUMERATE_SUB_KEYS | DELETE, &hkey )) == ERROR_SUCCESS)
  600. {
  601. while (dwRtn == ERROR_SUCCESS )
  602. {
  603. dwSubKeyLength = MAX_KEY_LENGTH;
  604. dwRtn=RegEnumKeyEx(
  605. hkey,
  606. 0, // always index zero
  607. szSubKey,
  608. &dwSubKeyLength,
  609. NULL,
  610. NULL,
  611. NULL,
  612. NULL
  613. );
  614. if(dwRtn == ERROR_NO_MORE_ITEMS)
  615. {
  616. dwRtn = RegDeleteKey(hStartKey, pKeyName);
  617. break;
  618. }
  619. else if(dwRtn == ERROR_SUCCESS)
  620. dwRtn=RegDeleteKeyNT(hkey, szSubKey);
  621. }
  622. RegCloseKey(hkey);
  623. // Do not save return code because error
  624. // has already occurred
  625. }
  626. }
  627. else
  628. dwRtn = ERROR_BADKEY;
  629. CMutexRegistry.Leave();
  630. return dwRtn;
  631. }
  632. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  633. Function: RegGetProgressDetailsState()
  634. Summary: Gets the expanded or collapsed user preference for the progress dialog
  635. and the pushpin preference
  636. Returns: Returns TRUE if succeeded, FALSE otherwise
  637. ------------------------------------------------------------------------F-F*/
  638. STDAPI_(BOOL) RegGetProgressDetailsState(REFCLSID clsidDlg,BOOL *pfPushPin, BOOL *pfExpanded)
  639. {
  640. SCODE sc = S_FALSE;
  641. HKEY hkeyUserProgress,hkeyclsidDlg;
  642. DWORD dwType = REG_DWORD ;
  643. DWORD dwDataSize = sizeof(DWORD);
  644. WCHAR wszCLSID[GUID_SIZE + 1];
  645. if (0 == StringFromGUID2(clsidDlg, wszCLSID, GUID_SIZE))
  646. {
  647. AssertSz(0,"Unable to make Guid a String");
  648. return FALSE;
  649. }
  650. //Prgress dialog defaults to collapsed, pushpin out
  651. *pfExpanded = FALSE;
  652. *pfPushPin = FALSE;
  653. hkeyUserProgress = RegGetCurrentUserKey(SYNCTYPE_PROGRESS,KEY_READ,FALSE);
  654. if (hkeyUserProgress)
  655. {
  656. if (ERROR_SUCCESS == RegOpenKeyEx(hkeyUserProgress,wszCLSID,0,KEY_READ,
  657. &hkeyclsidDlg))
  658. {
  659. RegQueryValueEx(hkeyclsidDlg,TEXT("Expanded"),NULL, &dwType,
  660. (LPBYTE) pfExpanded,
  661. &dwDataSize);
  662. dwType = REG_DWORD ;
  663. dwDataSize = sizeof(DWORD);
  664. RegQueryValueEx(hkeyclsidDlg,TEXT("PushPin"),NULL, &dwType,
  665. (LPBYTE) pfPushPin,
  666. &dwDataSize);
  667. RegCloseKey(hkeyclsidDlg);
  668. sc = S_OK;
  669. }
  670. RegCloseKey(hkeyUserProgress);
  671. }
  672. if (sc == S_OK)
  673. return TRUE;
  674. else
  675. return FALSE;
  676. }
  677. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  678. Function: RegSetProgressDetailsState(BOOL fExpanded)
  679. Summary: Sets the expanded or collapsed user preference for the progress dialog
  680. Returns: Returns TRUE if succeeded, FALSE otherwise
  681. ------------------------------------------------------------------------F-F*/
  682. STDAPI_(BOOL) RegSetProgressDetailsState(REFCLSID clsidDlg,BOOL fPushPin, BOOL fExpanded)
  683. {
  684. BOOL fResult = FALSE;
  685. HKEY hkeyUserProgress,hkeyclsidDlg;
  686. WCHAR wszCLSID[GUID_SIZE + 1];
  687. if (0 == StringFromGUID2(clsidDlg, wszCLSID, GUID_SIZE))
  688. {
  689. AssertSz(0,"Unable to make Guid a String");
  690. return FALSE;
  691. }
  692. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  693. CMutexRegistry.Enter();
  694. hkeyUserProgress = RegGetCurrentUserKey(SYNCTYPE_PROGRESS,KEY_WRITE | KEY_READ,TRUE);
  695. if (hkeyUserProgress)
  696. {
  697. if (ERROR_SUCCESS == RegCreateUserSubKey(hkeyUserProgress,wszCLSID,
  698. KEY_WRITE | KEY_READ,
  699. &hkeyclsidDlg))
  700. {
  701. fResult = TRUE;
  702. if (ERROR_SUCCESS != RegSetValueEx(hkeyclsidDlg,TEXT("Expanded"),NULL, REG_DWORD,
  703. (LPBYTE) &(fExpanded),
  704. sizeof(DWORD)))
  705. {
  706. fResult = FALSE;
  707. }
  708. if (ERROR_SUCCESS == RegSetValueEx(hkeyclsidDlg,TEXT("PushPin"),NULL, REG_DWORD,
  709. (LPBYTE) &(fPushPin),
  710. sizeof(DWORD)))
  711. {
  712. fResult = FALSE;
  713. }
  714. RegCloseKey(hkeyclsidDlg);
  715. }
  716. RegCloseKey(hkeyUserProgress);
  717. }
  718. CMutexRegistry.Leave();
  719. return fResult;
  720. }
  721. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  722. Function: RegGetSyncItemSettings( DWORD dwSyncType,
  723. CLSID clsidHandler,
  724. SYNCMGRITEMID ItemId,
  725. const TCHAR *pszConnectionName,
  726. DWORD *pdwCheckState,
  727. DWORD dwDefaultCheckState,
  728. TCHAR *pszSchedName)
  729. Summary: Gets the settings per handler, itemID, and connection name.
  730. If no selections on this connection, the default is ?
  731. Returns: Returns TRUE if successful, FALSE otherwise
  732. ------------------------------------------------------------------------F-F*/
  733. STDAPI_(BOOL) RegGetSyncItemSettings( DWORD dwSyncType,
  734. CLSID clsidHandler,
  735. SYNCMGRITEMID ItemId,
  736. const TCHAR *pszConnectionName,
  737. DWORD *pdwCheckState,
  738. DWORD dwDefaultCheckState,
  739. TCHAR *pszSchedName)
  740. {
  741. HKEY hKeyUser;
  742. *pdwCheckState = dwDefaultCheckState;
  743. // special case schedule
  744. if (SYNCTYPE_SCHEDULED == dwSyncType)
  745. {
  746. // items are always turned off by default and if no schedule name
  747. // don't bother.
  748. *pdwCheckState = FALSE;
  749. if (!pszSchedName)
  750. {
  751. return FALSE;
  752. }
  753. }
  754. // open the user key for the type
  755. hKeyUser = RegGetCurrentUserKey(dwSyncType,KEY_READ,FALSE);
  756. if (NULL == hKeyUser)
  757. {
  758. return FALSE;
  759. }
  760. // for a schedule we need to go ahead and open up the schedule name
  761. // key
  762. HKEY hKeySchedule = NULL;
  763. // Review if want GetCurrentUserKey to Handle this.
  764. if (SYNCTYPE_SCHEDULED == dwSyncType)
  765. {
  766. if (ERROR_SUCCESS != RegOpenKeyEx((hKeyUser),
  767. pszSchedName,0,KEY_READ,
  768. &hKeySchedule))
  769. {
  770. hKeySchedule = NULL;
  771. RegCloseKey(hKeyUser);
  772. return FALSE;
  773. }
  774. }
  775. BOOL fResult;
  776. fResult = RegLookupSettings(hKeySchedule ? hKeySchedule : hKeyUser,
  777. clsidHandler,
  778. ItemId,
  779. pszConnectionName,
  780. pdwCheckState);
  781. if (hKeySchedule)
  782. {
  783. RegCloseKey(hKeySchedule);
  784. }
  785. if (hKeyUser)
  786. {
  787. RegCloseKey(hKeyUser);
  788. }
  789. return fResult;
  790. }
  791. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  792. Function: RegSetSyncItemSettings( DWORD dwSyncType,
  793. CLSID clsidHandler,
  794. SYNCMGRITEMID ItemId,
  795. const TCHAR *pszConnectionName,
  796. DWORD dwCheckState,
  797. TCHAR *pszSchedName)
  798. Summary: Sets the settings per handler, itemID, and connection name.
  799. Returns: Returns TRUE if successful, FALSE otherwise
  800. ------------------------------------------------------------------------F-F*/
  801. STDAPI_(BOOL) RegSetSyncItemSettings( DWORD dwSyncType,
  802. CLSID clsidHandler,
  803. SYNCMGRITEMID ItemId,
  804. const TCHAR *pszConnectionName,
  805. DWORD dwCheckState,
  806. TCHAR *pszSchedName)
  807. {
  808. HKEY hKeyUser;
  809. if (SYNCTYPE_SCHEDULED == dwSyncType)
  810. {
  811. if (NULL == pszSchedName)
  812. {
  813. return FALSE;
  814. }
  815. }
  816. // open the user key for the type
  817. hKeyUser = RegGetCurrentUserKey(dwSyncType,KEY_WRITE | KEY_READ,TRUE);
  818. if (NULL == hKeyUser)
  819. {
  820. return FALSE;
  821. }
  822. // for a schedule we need to go ahead and open up the schedule name
  823. // key
  824. HKEY hKeySchedule = NULL;
  825. if (SYNCTYPE_SCHEDULED == dwSyncType)
  826. {
  827. if (ERROR_SUCCESS != RegCreateUserSubKey(hKeyUser,
  828. pszSchedName,KEY_WRITE | KEY_READ,
  829. &hKeySchedule))
  830. {
  831. hKeySchedule = NULL;
  832. RegCloseKey(hKeyUser);
  833. return FALSE;
  834. }
  835. }
  836. BOOL fResult;
  837. fResult = RegWriteOutSettings(hKeySchedule ? hKeySchedule : hKeyUser,
  838. clsidHandler,
  839. ItemId,
  840. pszConnectionName,
  841. dwCheckState);
  842. if (hKeySchedule)
  843. {
  844. RegCloseKey(hKeySchedule);
  845. }
  846. if (hKeyUser)
  847. {
  848. RegCloseKey(hKeyUser);
  849. }
  850. return fResult;
  851. }
  852. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  853. Function: RegQueryLoadHandlerOnEvent( )
  854. Summary: Determines if there is any reason to load this handler
  855. for the specified event and Connection
  856. Returns: Returns TRUE if successful, FALSE otherwise
  857. ------------------------------------------------------------------------F-F*/
  858. STDAPI_(BOOL) RegQueryLoadHandlerOnEvent(TCHAR *pszClsid,DWORD dwSyncFlags,
  859. TCHAR *pConnectionName)
  860. {
  861. BOOL fLoadHandler = FALSE;
  862. DWORD dwSyncType;
  863. switch(dwSyncFlags & SYNCMGRFLAG_EVENTMASK)
  864. {
  865. case SYNCMGRFLAG_CONNECT:
  866. case SYNCMGRFLAG_PENDINGDISCONNECT:
  867. {
  868. dwSyncType = SYNCTYPE_AUTOSYNC;
  869. }
  870. break;
  871. case SYNCMGRFLAG_IDLE:
  872. {
  873. dwSyncType = SYNCTYPE_IDLE;
  874. }
  875. break;
  876. default:
  877. AssertSz(0,"Unknown SyncType");
  878. return FALSE;
  879. break;
  880. }
  881. // walk done the list openning keys.
  882. HKEY hkeySyncType;
  883. if (hkeySyncType = RegGetCurrentUserKey(dwSyncType,KEY_READ,FALSE))
  884. {
  885. HKEY hkeyConnectionName;
  886. if (ERROR_SUCCESS == RegOpenKeyEx(hkeySyncType,pConnectionName,0,KEY_READ,&hkeyConnectionName))
  887. {
  888. HKEY hkeyClsid;
  889. if (ERROR_SUCCESS == RegOpenKeyEx(hkeyConnectionName,pszClsid,0,KEY_READ,&hkeyClsid))
  890. {
  891. DWORD dwType = REG_DWORD;
  892. DWORD dwDataSize = sizeof(DWORD);
  893. DWORD fQueryResult;
  894. if (ERROR_SUCCESS == RegQueryValueEx(hkeyClsid,TEXT("ItemsChecked"),NULL, &dwType, (LPBYTE) &fQueryResult, &dwDataSize))
  895. {
  896. fLoadHandler = fQueryResult;
  897. }
  898. RegCloseKey(hkeyClsid);
  899. }
  900. RegCloseKey(hkeyConnectionName);
  901. }
  902. RegCloseKey(hkeySyncType);
  903. }
  904. return fLoadHandler;
  905. }
  906. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  907. Function: RegSetSyncHandlerSettings( )
  908. Summary: Sets the handler settings for syncType and Connection.
  909. Returns: Returns TRUE if successful, FALSE otherwise
  910. ------------------------------------------------------------------------F-F*/
  911. STDAPI_(BOOL) RegSetSyncHandlerSettings(DWORD dwSyncType,
  912. const TCHAR *pszConnectionName,
  913. CLSID clsidHandler,
  914. BOOL fItemsChecked)
  915. {
  916. HKEY hKeyUser;
  917. hKeyUser = RegGetCurrentUserKey(dwSyncType,KEY_WRITE | KEY_READ,TRUE);
  918. if (NULL == hKeyUser)
  919. {
  920. return FALSE;
  921. }
  922. SCODE sc;
  923. HKEY hkeyConnection;
  924. HKEY hkeyCLSID;
  925. DWORD dwType = REG_DWORD;
  926. DWORD dwDataSize = sizeof(DWORD);
  927. TCHAR szCLSID[(GUID_SIZE+1)];
  928. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  929. CMutexRegistry.Enter();
  930. smChkTo(EH_Err2,RegCreateUserSubKey(hKeyUser,
  931. pszConnectionName,
  932. KEY_WRITE | KEY_READ,
  933. &hkeyConnection));
  934. StringFromGUID2(clsidHandler, szCLSID, 2*GUID_SIZE);
  935. // Write entries under CLSID.
  936. smChkTo(EH_Err3,RegCreateUserSubKey(hkeyConnection,
  937. szCLSID,KEY_WRITE | KEY_READ,
  938. &hkeyCLSID));
  939. RegSetValueEx(hkeyCLSID,TEXT("ItemsChecked"),NULL, REG_DWORD,
  940. (LPBYTE) &fItemsChecked,
  941. sizeof(DWORD));
  942. RegCloseKey(hKeyUser);
  943. RegCloseKey(hkeyConnection);
  944. RegCloseKey(hkeyCLSID);
  945. CMutexRegistry.Leave();
  946. return TRUE;
  947. EH_Err3:
  948. RegCloseKey(hkeyConnection);
  949. EH_Err2:
  950. RegCloseKey(hKeyUser);
  951. CMutexRegistry.Leave();
  952. return FALSE;
  953. }
  954. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  955. Function: RegLookupSettings(const TCHAR *hkeyName,
  956. CLSID clsidHandler,
  957. SYNCMGRITEMID ItemID,
  958. const TCHAR *pszMachineName,
  959. const TCHAR *pszConnectionName,
  960. DWORD pdwCheckState)
  961. Summary: Gets the settings per handler, itemID, and connection name.
  962. Returns: Returns TRUE if there are settings for this item and
  963. connection, flase otherwise.
  964. ------------------------------------------------------------------------F-F*/
  965. STDAPI_(BOOL) RegLookupSettings(HKEY hKeyUser,
  966. CLSID clsidHandler,
  967. SYNCMGRITEMID ItemID,
  968. const TCHAR *pszConnectionName,
  969. DWORD *pdwCheckState)
  970. {
  971. SCODE sc;
  972. HKEY hkeyConnection;
  973. HKEY hkeyItem;
  974. HKEY hKeyHandler;
  975. DWORD dwType = REG_SZ;
  976. DWORD dwDataSize = MAX_PATH + 1;
  977. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  978. CMutexRegistry.Enter();
  979. TCHAR szID[GUID_SIZE+1];
  980. TCHAR szCLSID[2*(GUID_SIZE+1)];
  981. dwType = REG_DWORD;
  982. dwDataSize = sizeof(DWORD);
  983. smChkTo(EH_Err2,RegOpenKeyEx(hKeyUser,
  984. pszConnectionName,0,KEY_READ,
  985. &hkeyConnection));
  986. StringFromGUID2(clsidHandler, szCLSID, 2*GUID_SIZE);
  987. StringFromGUID2(ItemID, szID, GUID_SIZE);
  988. // Read entries under CLSID.
  989. smChkTo(EH_Err3,RegOpenKeyEx((hkeyConnection),
  990. szCLSID, 0, KEY_READ,
  991. &hKeyHandler));
  992. smChkTo(EH_Err4,RegOpenKeyEx((hKeyHandler),
  993. szID, 0, KEY_READ,
  994. &hkeyItem));
  995. RegQueryValueEx(hkeyItem,TEXT("CheckState"),NULL, &dwType, (LPBYTE)pdwCheckState, &dwDataSize);
  996. RegCloseKey(hkeyConnection);
  997. RegCloseKey(hkeyItem);
  998. RegCloseKey(hKeyHandler);
  999. CMutexRegistry.Leave();
  1000. return TRUE;
  1001. EH_Err4:
  1002. RegCloseKey(hKeyHandler);
  1003. EH_Err3:
  1004. RegCloseKey(hkeyConnection);
  1005. EH_Err2:
  1006. CMutexRegistry.Leave();
  1007. return FALSE;
  1008. }
  1009. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1010. Function: RegWriteOutSettings(const TCHAR *hkeyName,
  1011. CLSID clsidHandler,
  1012. SYNCMGRITEMID ItemID,
  1013. const TCHAR *pszConnectionName,
  1014. DWORD dwCheckState)
  1015. Summary: Sets the settings per handler, itemID, and connection name.
  1016. Returns: Returns TRUE if we can set them, FALSE if there is an error
  1017. ------------------------------------------------------------------------F-F*/
  1018. STDAPI_(BOOL) RegWriteOutSettings(HKEY hKeyUser,
  1019. CLSID clsidHandler,
  1020. SYNCMGRITEMID ItemID,
  1021. const TCHAR *pszConnectionName,
  1022. DWORD dwCheckState)
  1023. {
  1024. SCODE sc;
  1025. HKEY hkeyConnection;
  1026. HKEY hKeyHandler;
  1027. HKEY hkeyItem;
  1028. DWORD dwType = REG_SZ;
  1029. DWORD dwDataSize = MAX_PATH;
  1030. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  1031. CMutexRegistry.Enter();
  1032. TCHAR szID[GUID_SIZE+1];
  1033. TCHAR szCLSID[2*(GUID_SIZE+1)];
  1034. smChkTo(EH_Err2,RegCreateUserSubKey(hKeyUser,
  1035. pszConnectionName,KEY_WRITE | KEY_READ,
  1036. &hkeyConnection));
  1037. StringFromGUID2(clsidHandler, szCLSID, 2*GUID_SIZE);
  1038. StringFromGUID2(ItemID, szID, GUID_SIZE);
  1039. smChkTo(EH_Err3,RegCreateUserSubKey(hkeyConnection,
  1040. szCLSID,KEY_WRITE | KEY_READ,
  1041. &hKeyHandler));
  1042. // Write entries under CLSID.
  1043. smChkTo(EH_Err4,RegCreateUserSubKey(hKeyHandler,
  1044. szID,KEY_WRITE | KEY_READ,
  1045. &hkeyItem));
  1046. RegSetValueEx(hkeyItem,TEXT("CheckState"),NULL, REG_DWORD,
  1047. (LPBYTE) &dwCheckState,
  1048. sizeof(DWORD));
  1049. RegCloseKey(hkeyConnection);
  1050. RegCloseKey(hkeyItem);
  1051. RegCloseKey(hKeyHandler);
  1052. CMutexRegistry.Leave();
  1053. return TRUE;
  1054. EH_Err4:
  1055. RegCloseKey(hKeyHandler);
  1056. EH_Err3:
  1057. RegCloseKey(hkeyConnection);
  1058. EH_Err2:
  1059. CMutexRegistry.Leave();
  1060. return FALSE;
  1061. }
  1062. /****************************************************************************
  1063. AutoSync Registry Functions
  1064. ***************************************************************************F-F*/
  1065. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1066. Function: RegGetAutoSyncSettings( LPCONNECTIONSETTINGS lpConnectionSettings)
  1067. Summary: Gets the logon, logoff and prompt me first user selections.
  1068. If no selections on this connection, the default is ?
  1069. Returns: Returns TRUE if successful, FALSE otherwise
  1070. ------------------------------------------------------------------------F-F*/
  1071. STDAPI_(BOOL) RegGetAutoSyncSettings(LPCONNECTIONSETTINGS lpConnectionSettings)
  1072. {
  1073. SCODE sc;
  1074. HKEY hkeyConnection;
  1075. DWORD dwType = REG_SZ ;
  1076. DWORD dwDataSize = MAX_PATH;
  1077. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  1078. CMutexRegistry.Enter();
  1079. // Set these first to default values, in case there are no current
  1080. // user preferences in the registry
  1081. lpConnectionSettings->dwLogon = FALSE;
  1082. lpConnectionSettings->dwLogoff = FALSE;
  1083. lpConnectionSettings->dwPromptMeFirst = FALSE;
  1084. HKEY hKeyUser;
  1085. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_AUTOSYNC,KEY_READ,FALSE);
  1086. if (NULL == hKeyUser)
  1087. {
  1088. goto EH_Err;
  1089. }
  1090. dwType = REG_DWORD ;
  1091. dwDataSize = sizeof(DWORD);
  1092. smChkTo(EH_Err3,RegOpenKeyEx(hKeyUser, lpConnectionSettings->pszConnectionName,0,KEY_READ,
  1093. &hkeyConnection));
  1094. RegQueryValueEx(hkeyConnection,TEXT("Logon"),NULL, &dwType,
  1095. (LPBYTE) &(lpConnectionSettings->dwLogon),
  1096. &dwDataSize);
  1097. dwDataSize = sizeof(DWORD);
  1098. RegQueryValueEx(hkeyConnection,TEXT("Logoff"),NULL, &dwType,
  1099. (LPBYTE) &(lpConnectionSettings->dwLogoff),
  1100. &dwDataSize);
  1101. dwDataSize = sizeof(DWORD);
  1102. RegQueryValueEx(hkeyConnection,TEXT("PromptMeFirst"),NULL, &dwType,
  1103. (LPBYTE) &(lpConnectionSettings->dwPromptMeFirst),
  1104. &dwDataSize);
  1105. RegCloseKey(hkeyConnection);
  1106. RegCloseKey(hKeyUser);
  1107. CMutexRegistry.Leave();
  1108. return TRUE;
  1109. EH_Err3:
  1110. RegCloseKey(hKeyUser);
  1111. EH_Err:
  1112. CMutexRegistry.Leave();
  1113. return FALSE;
  1114. }
  1115. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1116. Function: RegUpdateUserAutosyncKey
  1117. Summary: given an Autosync User Key makes sure it is in the latest
  1118. format and if not updates it.
  1119. Returns:
  1120. ------------------------------------------------------------------------F-F*/
  1121. void RegUpdateUserAutosyncKey(HKEY hkeyUser,BOOL fForce)
  1122. {
  1123. DWORD dwType = REG_DWORD ;
  1124. DWORD dwDataSize = sizeof(DWORD);
  1125. DWORD dwUserLogonLogoff;
  1126. DWORD dwIndex = 0;
  1127. TCHAR lpName[MAX_PATH];
  1128. DWORD cbName = MAX_PATH;
  1129. DWORD dwLogon = 0;
  1130. DWORD dwLogoff = 0;
  1131. if (!fForce && (ERROR_SUCCESS == RegQueryValueEx(hkeyUser,TEXT("Logon"),NULL, &dwType,
  1132. (LPBYTE) &dwUserLogonLogoff,
  1133. &dwDataSize)) )
  1134. {
  1135. // if can open Logon Key this is up to date.
  1136. return;
  1137. }
  1138. // need to enum connection names and update the toplevel information.
  1139. while ( ERROR_SUCCESS == RegEnumKey(hkeyUser,dwIndex,
  1140. lpName,cbName) )
  1141. {
  1142. LONG lRet;
  1143. HKEY hKeyConnection;
  1144. lRet = RegOpenKeyEx( hkeyUser,
  1145. lpName,
  1146. NULL,
  1147. KEY_READ,
  1148. &hKeyConnection );
  1149. if (ERROR_SUCCESS == lRet)
  1150. {
  1151. dwDataSize = sizeof(DWORD);
  1152. if (ERROR_SUCCESS == RegQueryValueEx(hKeyConnection,TEXT("Logon"),NULL, &dwType,
  1153. (LPBYTE) &dwUserLogonLogoff,
  1154. &dwDataSize) )
  1155. {
  1156. dwLogon |= dwUserLogonLogoff;
  1157. }
  1158. dwDataSize = sizeof(DWORD);
  1159. if (ERROR_SUCCESS == RegQueryValueEx(hKeyConnection,TEXT("Logoff"),NULL, &dwType,
  1160. (LPBYTE) &dwUserLogonLogoff,
  1161. &dwDataSize) )
  1162. {
  1163. dwLogoff |= dwUserLogonLogoff;
  1164. }
  1165. RegCloseKey(hKeyConnection);
  1166. }
  1167. dwIndex++;
  1168. }
  1169. // write out new flags even if errors occured. work thing that happens is
  1170. // we don't set up someones autosync automatically.
  1171. RegSetValueEx(hkeyUser,TEXT("Logon"),NULL, REG_DWORD,
  1172. (LPBYTE) &(dwLogon), sizeof(DWORD));
  1173. RegSetValueEx(hkeyUser,TEXT("Logoff"),NULL, REG_DWORD,
  1174. (LPBYTE) &(dwLogoff), sizeof(DWORD));
  1175. RegWriteTimeStamp(hkeyUser);
  1176. }
  1177. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1178. Function: RegUpdateAutoSyncKeyValueSettings
  1179. Summary: walks through the UserList Updating the AutoSync
  1180. Returns:
  1181. ------------------------------------------------------------------------F-F*/
  1182. void RegUpdateAutoSyncKeyValue(HKEY hkeyAutoSync,DWORD dwLogonDefault,DWORD dwLogoffDefault)
  1183. {
  1184. DWORD dwIndex = 0;
  1185. WCHAR lpName[MAX_PATH];
  1186. DWORD cbName = MAX_PATH;
  1187. LONG lRet;
  1188. DWORD dwLogon = 0;
  1189. DWORD dwLogoff = 0;
  1190. BOOL fSetLogon,fSetLogoff;
  1191. // need to walk the autosync user key and set the top level information
  1192. // based on whther someone logon/logoff
  1193. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  1194. CMutexRegistry.Enter();
  1195. while ( ERROR_SUCCESS == (lRet = RegEnumKey(hkeyAutoSync,dwIndex,
  1196. lpName,cbName) ))
  1197. {
  1198. DWORD dwType = REG_DWORD ;
  1199. DWORD dwDataSize = sizeof(DWORD);
  1200. DWORD dwUserLogonLogoff;
  1201. HKEY hKeyDomainUser;
  1202. lRet = RegOpenKeyEx( hkeyAutoSync,
  1203. lpName,
  1204. NULL,
  1205. KEY_READ,
  1206. &hKeyDomainUser );
  1207. if (ERROR_SUCCESS == lRet)
  1208. {
  1209. // If Query fails don't want to count this as a failed to enum
  1210. // error so don't set lRet
  1211. if (ERROR_SUCCESS == (RegQueryValueEx(hKeyDomainUser,TEXT("Logon"),NULL, &dwType,
  1212. (LPBYTE) &dwUserLogonLogoff,
  1213. &dwDataSize) ))
  1214. {
  1215. dwLogon |= dwUserLogonLogoff;
  1216. }
  1217. dwDataSize = sizeof(DWORD);
  1218. if (ERROR_SUCCESS == lRet)
  1219. {
  1220. if (ERROR_SUCCESS == (RegQueryValueEx(hKeyDomainUser,TEXT("Logoff"),NULL, &dwType,
  1221. (LPBYTE) &dwUserLogonLogoff,
  1222. &dwDataSize) ) )
  1223. {
  1224. dwLogoff |= dwUserLogonLogoff;
  1225. }
  1226. }
  1227. RegCloseKey(hKeyDomainUser);
  1228. }
  1229. if (ERROR_SUCCESS != lRet)
  1230. {
  1231. break;
  1232. }
  1233. dwIndex++;
  1234. }
  1235. fSetLogon = FALSE;
  1236. fSetLogoff = FALSE;
  1237. // if an error occured, then use the passed in defaults,
  1238. // if set to 1, else don't set.
  1239. if ( (ERROR_SUCCESS != lRet) && (ERROR_NO_MORE_ITEMS != lRet))
  1240. {
  1241. if ( (-1 != dwLogonDefault) && (0 != dwLogonDefault))
  1242. {
  1243. fSetLogon = TRUE;
  1244. dwLogon = dwLogonDefault;
  1245. }
  1246. if ( (-1 != dwLogoffDefault) && (0 != dwLogoffDefault))
  1247. {
  1248. fSetLogoff = TRUE;
  1249. dwLogoff = dwLogoffDefault;
  1250. }
  1251. }
  1252. else
  1253. {
  1254. fSetLogon = TRUE;
  1255. fSetLogoff = TRUE;
  1256. }
  1257. // write out new flags even if errors occured. work thing that happens is
  1258. // we don't set up someones autosync automatically.
  1259. if (fSetLogon)
  1260. {
  1261. RegSetValueEx(hkeyAutoSync,TEXT("Logon"),NULL, REG_DWORD,
  1262. (LPBYTE) &(dwLogon), sizeof(DWORD));
  1263. }
  1264. if (fSetLogoff)
  1265. {
  1266. RegSetValueEx(hkeyAutoSync,TEXT("Logoff"),NULL, REG_DWORD,
  1267. (LPBYTE) &(dwLogoff), sizeof(DWORD));
  1268. }
  1269. RegWriteTimeStamp(hkeyAutoSync);
  1270. CMutexRegistry.Leave();
  1271. }
  1272. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1273. Function: RegUpdateUserIdleKey
  1274. Summary: given an Idle User Key makes sure it is in the latest
  1275. format and if not updates it.
  1276. Returns:
  1277. ------------------------------------------------------------------------F-F*/
  1278. void RegUpdateUserIdleKey(HKEY hkeyUser,BOOL fForce)
  1279. {
  1280. DWORD dwType = REG_DWORD ;
  1281. DWORD dwDataSize = sizeof(DWORD);
  1282. DWORD dwUserIdleEnabled;
  1283. DWORD dwIndex = 0;
  1284. TCHAR lpName[MAX_PATH];
  1285. DWORD cbName = MAX_PATH;
  1286. DWORD dwIdleEnabled = 0;
  1287. if (!fForce && (ERROR_SUCCESS == RegQueryValueEx(hkeyUser,TEXT("IdleEnabled"),NULL, &dwType,
  1288. (LPBYTE) &dwUserIdleEnabled,
  1289. &dwDataSize)) )
  1290. {
  1291. // if can open Logon Key this is up to date.
  1292. return;
  1293. }
  1294. // need to enum connection names and update the toplevel information.
  1295. while ( ERROR_SUCCESS == RegEnumKey(hkeyUser,dwIndex,
  1296. lpName,cbName) )
  1297. {
  1298. LONG lRet;
  1299. HKEY hKeyConnection;
  1300. lRet = RegOpenKeyEx( hkeyUser,
  1301. lpName,
  1302. NULL,
  1303. KEY_READ,
  1304. &hKeyConnection );
  1305. if (ERROR_SUCCESS == lRet)
  1306. {
  1307. dwDataSize = sizeof(DWORD);
  1308. if (ERROR_SUCCESS == RegQueryValueEx(hKeyConnection,TEXT("IdleEnabled"),NULL, &dwType,
  1309. (LPBYTE) &dwUserIdleEnabled,
  1310. &dwDataSize) )
  1311. {
  1312. dwIdleEnabled |= dwUserIdleEnabled;
  1313. }
  1314. RegCloseKey(hKeyConnection);
  1315. }
  1316. dwIndex++;
  1317. }
  1318. // write out new flags even if errors occured. work thing that happens is
  1319. // we don't set up someones autosync automatically.
  1320. RegSetValueEx(hkeyUser,TEXT("IdleEnabled"),NULL, REG_DWORD,
  1321. (LPBYTE) &(dwIdleEnabled), sizeof(DWORD));
  1322. RegWriteTimeStamp(hkeyUser);
  1323. }
  1324. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1325. Function: RegUpdateIdleKeyValue
  1326. Summary: walks through the UserList Updating the Idle RegKey
  1327. Returns:
  1328. ------------------------------------------------------------------------F-F*/
  1329. void RegUpdateIdleKeyValue(HKEY hkeyIdle,DWORD dwDefault)
  1330. {
  1331. DWORD dwIndex = 0;
  1332. WCHAR lpName[MAX_PATH];
  1333. DWORD cbName = MAX_PATH;
  1334. DWORD dwIdleEnabled = 0;
  1335. LONG lRet = -1;
  1336. BOOL fSetDefault;
  1337. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  1338. CMutexRegistry.Enter();
  1339. // need to walk the idle user key and set the top level information
  1340. // based on whther someone logon/logoff
  1341. fSetDefault = FALSE;
  1342. while ( ERROR_SUCCESS == (lRet = RegEnumKey(hkeyIdle,dwIndex,
  1343. lpName,cbName)) )
  1344. {
  1345. DWORD dwType = REG_DWORD ;
  1346. DWORD dwDataSize = sizeof(DWORD);
  1347. DWORD dwUserIdleEnabled;
  1348. HKEY hKeyDomainUser;
  1349. lRet = RegOpenKeyEx( hkeyIdle,
  1350. lpName,
  1351. NULL,
  1352. KEY_READ,
  1353. &hKeyDomainUser );
  1354. if (ERROR_SUCCESS == lRet)
  1355. {
  1356. // if query fails don't consider this an error as far as
  1357. // setDefault goes.
  1358. if (ERROR_SUCCESS == (RegQueryValueEx(hKeyDomainUser,TEXT("IdleEnabled"),NULL, &dwType,
  1359. (LPBYTE) &dwUserIdleEnabled,
  1360. &dwDataSize) ))
  1361. {
  1362. dwIdleEnabled |= dwUserIdleEnabled;
  1363. }
  1364. RegCloseKey(hKeyDomainUser);
  1365. }
  1366. if (ERROR_SUCCESS != lRet)
  1367. {
  1368. break;
  1369. }
  1370. dwIndex++;
  1371. }
  1372. // if an error occured, then use the passed in defaults,
  1373. // if set to 1, else don't set.
  1374. if ( (ERROR_SUCCESS != lRet) && (ERROR_NO_MORE_ITEMS != lRet))
  1375. {
  1376. if ( (-1 != dwDefault) && (0 != dwDefault))
  1377. {
  1378. fSetDefault = TRUE;
  1379. dwIdleEnabled = dwDefault;
  1380. }
  1381. }
  1382. else
  1383. {
  1384. fSetDefault = TRUE;
  1385. }
  1386. // write out new flags even if errors occured. work thing that happens is
  1387. // we don't set up someones autosync automatically.
  1388. if (fSetDefault)
  1389. {
  1390. RegSetValueEx(hkeyIdle,TEXT("IdleEnabled"),NULL, REG_DWORD,
  1391. (LPBYTE) &(dwIdleEnabled), sizeof(DWORD));
  1392. }
  1393. RegWriteTimeStamp(hkeyIdle);
  1394. CMutexRegistry.Leave();
  1395. }
  1396. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1397. Function: RegSetAutoSyncSettings( LPCONNECTIONSETTINGS lpConnectionSettings,
  1398. int iNumConnections)
  1399. Summary: Sets the logon, logoff and prompt me first user selections.
  1400. Returns: Returns TRUE if successful, FALSE otherwise
  1401. ------------------------------------------------------------------------F-F*/
  1402. STDAPI_(BOOL) RegSetAutoSyncSettings(LPCONNECTIONSETTINGS lpConnectionSettings,
  1403. int iNumConnections,
  1404. CRasUI *pRas,
  1405. BOOL fCleanReg,
  1406. BOOL fSetMachineState,
  1407. BOOL fPerUser)
  1408. {
  1409. SCODE sc = S_OK;
  1410. HKEY hAutoSync;
  1411. HKEY hKeyUser;
  1412. HKEY hkeyConnection;
  1413. DWORD dwUserConfigured = 1;
  1414. DWORD dwLogonDefault = -1;
  1415. DWORD dwLogoffDefault = -1;
  1416. int i;
  1417. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  1418. CMutexRegistry.Enter();
  1419. // make sure keys are converted to newest format
  1420. RegUpdateTopLevelKeys();
  1421. Assert(-1 != TRUE); // we rely on TRUE boolean being something other than -1
  1422. hAutoSync = RegGetSyncTypeKey(SYNCTYPE_AUTOSYNC,KEY_WRITE| KEY_READ,TRUE);
  1423. if (NULL == hAutoSync)
  1424. {
  1425. goto EH_Err;
  1426. }
  1427. hKeyUser = RegOpenUserKey(hAutoSync,KEY_WRITE| KEY_READ,TRUE,fCleanReg);
  1428. if (NULL == hKeyUser)
  1429. {
  1430. goto EH_Err2;
  1431. }
  1432. if (fPerUser)
  1433. {
  1434. smChkTo(EH_Err3,RegSetValueEx(hKeyUser,TEXT("UserConfigured"),NULL, REG_DWORD,
  1435. (LPBYTE) &dwUserConfigured,
  1436. sizeof(DWORD)));
  1437. }
  1438. else
  1439. {
  1440. DWORD dwType = REG_DWORD;
  1441. DWORD dwDataSize = sizeof(DWORD);
  1442. //If this value isn't added yet,
  1443. if (ERROR_SUCCESS == RegQueryValueEx(hKeyUser,TEXT("UserConfigured"),NULL, &dwType,
  1444. (LPBYTE) &dwUserConfigured,
  1445. &dwDataSize))
  1446. {
  1447. //if the user setup their configuration, we won't override on a
  1448. //subsequent handler registration.
  1449. if (dwUserConfigured)
  1450. {
  1451. RegCloseKey(hKeyUser);
  1452. RegCloseKey(hAutoSync);
  1453. CMutexRegistry.Leave();
  1454. return TRUE;
  1455. }
  1456. }
  1457. }
  1458. for (i=0; i<iNumConnections; i++)
  1459. {
  1460. smChkTo(EH_Err3,RegCreateUserSubKey (hKeyUser,
  1461. lpConnectionSettings[i].pszConnectionName,
  1462. KEY_WRITE | KEY_READ,
  1463. &hkeyConnection));
  1464. if (-1 != lpConnectionSettings[i].dwLogon)
  1465. {
  1466. if (0 != lpConnectionSettings[i].dwLogon)
  1467. {
  1468. dwLogonDefault = lpConnectionSettings[i].dwLogon;
  1469. }
  1470. smChkTo(EH_Err4,RegSetValueEx(hkeyConnection,TEXT("Logon"),NULL, REG_DWORD,
  1471. (LPBYTE) &(lpConnectionSettings[i].dwLogon),
  1472. sizeof(DWORD)));
  1473. }
  1474. if (-1 != lpConnectionSettings[i].dwLogoff)
  1475. {
  1476. if (0 != lpConnectionSettings[i].dwLogoff)
  1477. {
  1478. dwLogoffDefault = lpConnectionSettings[i].dwLogoff;
  1479. }
  1480. smChkTo(EH_Err4,RegSetValueEx(hkeyConnection,TEXT("Logoff"),NULL, REG_DWORD,
  1481. (LPBYTE) &(lpConnectionSettings[i].dwLogoff),
  1482. sizeof(DWORD)));
  1483. }
  1484. if (-1 != lpConnectionSettings[i].dwPromptMeFirst)
  1485. {
  1486. smChkTo(EH_Err4,RegSetValueEx(hkeyConnection,TEXT("PromptMeFirst"),NULL, REG_DWORD,
  1487. (LPBYTE) &(lpConnectionSettings[i].dwPromptMeFirst),
  1488. sizeof(DWORD)));
  1489. }
  1490. RegCloseKey(hkeyConnection);
  1491. }
  1492. // update the toplevel User information
  1493. RegUpdateUserAutosyncKey(hKeyUser,TRUE /* fForce */);
  1494. // update the top-level key
  1495. RegUpdateAutoSyncKeyValue(hAutoSync,dwLogonDefault,dwLogoffDefault);
  1496. RegCloseKey(hKeyUser);
  1497. RegCloseKey(hAutoSync);
  1498. // update our global sens state based on Autosync and Registration
  1499. if (fSetMachineState)
  1500. {
  1501. RegRegisterForEvents(FALSE /* fUninstall */);
  1502. }
  1503. CMutexRegistry.Leave();
  1504. return TRUE;
  1505. EH_Err4:
  1506. RegCloseKey(hkeyConnection);
  1507. EH_Err3:
  1508. RegCloseKey(hKeyUser);
  1509. EH_Err2:
  1510. RegCloseKey(hAutoSync);
  1511. EH_Err:
  1512. CMutexRegistry.Leave();
  1513. return FALSE;
  1514. }
  1515. /****************************************************************************
  1516. Scheduled Sync Registry Functions
  1517. ***************************************************************************F-F*/
  1518. //--------------------------------------------------------------------------------
  1519. //
  1520. // FUNCTION: RegGetSchedFriendlyName(LPCTSTR ptszScheduleGUIDName,
  1521. // LPTSTR ptstrFriendlyName)
  1522. //
  1523. // PURPOSE: Get the friendly name of this Schedule.
  1524. //
  1525. // History: 27-Feb-98 susia Created.
  1526. //
  1527. //--------------------------------------------------------------------------------
  1528. STDAPI_(BOOL) RegGetSchedFriendlyName(LPCTSTR ptszScheduleGUIDName,
  1529. LPTSTR ptstrFriendlyName)
  1530. {
  1531. BOOL fResult = FALSE;
  1532. HKEY hkeySchedName;
  1533. DWORD dwType = REG_SZ;
  1534. DWORD dwDataSize = (MAX_PATH + 1) * sizeof(TCHAR);
  1535. HKEY hKeyUser;
  1536. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_SCHEDULED,KEY_READ,FALSE);
  1537. if (NULL == hKeyUser)
  1538. {
  1539. return FALSE;
  1540. }
  1541. if (NOERROR == RegOpenKeyEx (hKeyUser, ptszScheduleGUIDName, 0,KEY_READ,
  1542. &hkeySchedName))
  1543. {
  1544. if (ERROR_SUCCESS == RegQueryValueEx(hkeySchedName,TEXT("FriendlyName"),NULL, &dwType,
  1545. (LPBYTE) ptstrFriendlyName, &dwDataSize))
  1546. {
  1547. fResult = TRUE;
  1548. }
  1549. RegCloseKey(hkeySchedName);
  1550. }
  1551. RegCloseKey(hKeyUser);
  1552. return fResult;
  1553. }
  1554. //--------------------------------------------------------------------------------
  1555. //
  1556. // FUNCTION: RegSetSchedFriendlyName(LPCTSTR ptszScheduleGUIDName,
  1557. // LPCTSTR ptstrFriendlyName)
  1558. //
  1559. // PURPOSE: Set the friendly name of this Schedule.
  1560. //
  1561. // History: 27-Feb-98 susia Created.
  1562. //
  1563. //--------------------------------------------------------------------------------
  1564. STDAPI_(BOOL) RegSetSchedFriendlyName(LPCTSTR ptszScheduleGUIDName,
  1565. LPCTSTR ptstrFriendlyName)
  1566. {
  1567. SCODE sc;
  1568. HKEY hkeySchedName;
  1569. DWORD dwType = REG_SZ;
  1570. HKEY hKeyUser;
  1571. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  1572. CMutexRegistry.Enter();
  1573. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_SCHEDULED,KEY_WRITE | KEY_READ,TRUE);
  1574. if (NULL == hKeyUser)
  1575. {
  1576. goto EH_Err;
  1577. }
  1578. smChkTo(EH_Err3,RegCreateUserSubKey (hKeyUser, ptszScheduleGUIDName,
  1579. KEY_WRITE | KEY_READ,
  1580. &hkeySchedName));
  1581. smChkTo(EH_Err4,RegSetValueEx (hkeySchedName,TEXT("FriendlyName"),NULL,
  1582. dwType,
  1583. (LPBYTE) ptstrFriendlyName,
  1584. (lstrlen(ptstrFriendlyName) + 1)*sizeof(TCHAR)));
  1585. RegCloseKey(hkeySchedName);
  1586. RegCloseKey(hKeyUser);
  1587. CMutexRegistry.Leave();
  1588. return TRUE;
  1589. EH_Err4:
  1590. RegCloseKey(hkeySchedName);
  1591. EH_Err3:
  1592. RegCloseKey(hKeyUser);
  1593. EH_Err:
  1594. CMutexRegistry.Leave();
  1595. return FALSE;
  1596. }
  1597. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1598. Function: RegGetSchedSyncSettings( LPCONNECTIONSETTINGS lpConnectionSettings,
  1599. TCHAR *pszSchedName)
  1600. Summary: Gets the MakeAutoConnection user selections.
  1601. If no selections on this connection, the default is FALSE
  1602. Returns: Returns TRUE if successful, FALSE otherwise
  1603. ------------------------------------------------------------------------F-F*/
  1604. STDAPI_(BOOL) RegGetSchedSyncSettings(LPCONNECTIONSETTINGS lpConnectionSettings,
  1605. TCHAR *pszSchedName)
  1606. {
  1607. SCODE sc;
  1608. HKEY hkeySchedName,
  1609. hkeyConnection;
  1610. DWORD dwType = REG_DWORD;
  1611. DWORD dwDataSize = sizeof(DWORD);
  1612. HKEY hKeyUser;
  1613. // Set these first to default values, in case there are no current
  1614. // user preferences in the registry
  1615. lpConnectionSettings->dwConnType = SYNCSCHEDINFO_FLAGS_CONNECTION_LAN;
  1616. lpConnectionSettings->dwMakeConnection = FALSE;
  1617. lpConnectionSettings->dwHidden = FALSE;
  1618. lpConnectionSettings->dwReadOnly = FALSE;
  1619. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_SCHEDULED,KEY_READ,FALSE);
  1620. if (NULL == hKeyUser)
  1621. {
  1622. goto EH_Err2;
  1623. }
  1624. smChkTo(EH_Err3,RegOpenKeyEx (hKeyUser,
  1625. pszSchedName,0,KEY_READ,
  1626. &hkeySchedName));
  1627. smChkTo(EH_Err4,RegQueryValueEx(hkeySchedName,TEXT("ScheduleHidden"),NULL, &dwType,
  1628. (LPBYTE) &(lpConnectionSettings->dwHidden),
  1629. &dwDataSize));
  1630. dwDataSize = sizeof(DWORD);
  1631. smChkTo(EH_Err4,RegQueryValueEx(hkeySchedName,TEXT("ScheduleReadOnly"),NULL, &dwType,
  1632. (LPBYTE) &(lpConnectionSettings->dwReadOnly),
  1633. &dwDataSize));
  1634. smChkTo(EH_Err4,RegOpenKeyEx (hkeySchedName,
  1635. lpConnectionSettings->pszConnectionName,
  1636. 0,KEY_READ,
  1637. &hkeyConnection));
  1638. dwDataSize = sizeof(DWORD);
  1639. smChkTo(EH_Err5,RegQueryValueEx(hkeyConnection,TEXT("MakeAutoConnection"),NULL, &dwType,
  1640. (LPBYTE) &(lpConnectionSettings->dwMakeConnection),
  1641. &dwDataSize));
  1642. dwDataSize = sizeof(DWORD);
  1643. smChkTo(EH_Err5,RegQueryValueEx(hkeyConnection,TEXT("Connection Type"),NULL, &dwType,
  1644. (LPBYTE) &(lpConnectionSettings->dwConnType),
  1645. &dwDataSize));
  1646. RegCloseKey(hkeyConnection);
  1647. RegCloseKey(hkeySchedName);
  1648. RegCloseKey(hKeyUser);
  1649. return TRUE;
  1650. EH_Err5:
  1651. RegCloseKey(hkeyConnection);
  1652. EH_Err4:
  1653. RegCloseKey(hkeySchedName);
  1654. EH_Err3:
  1655. RegCloseKey(hKeyUser);
  1656. EH_Err2:
  1657. return FALSE;
  1658. }
  1659. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1660. Function: RegSetSchedSyncSettings( LPCONNECTIONSETTINGS lpConnectionSettings,
  1661. TCHAR *pszSchedName)
  1662. Summary: Sets the hidden and readonly schedule flags.
  1663. Returns: Returns TRUE if successful, FALSE otherwise
  1664. ------------------------------------------------------------------------F-F*/
  1665. STDAPI_(BOOL) RegSetSchedSyncSettings(LPCONNECTIONSETTINGS lpConnectionSettings,
  1666. TCHAR *pszSchedName)
  1667. {
  1668. SCODE sc;
  1669. HKEY hKeyUser,
  1670. hkeySchedName,
  1671. hkeyConnection;
  1672. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  1673. CMutexRegistry.Enter();
  1674. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_SCHEDULED,KEY_READ | KEY_WRITE,TRUE);
  1675. if (NULL == hKeyUser)
  1676. {
  1677. goto EH_Err;
  1678. }
  1679. smChkTo(EH_Err3,RegCreateUserSubKey (hKeyUser,
  1680. pszSchedName,
  1681. KEY_WRITE | KEY_READ,
  1682. &hkeySchedName));
  1683. smChkTo(EH_Err4,RegSetValueEx(hkeySchedName,TEXT("ScheduleHidden"),NULL, REG_DWORD,
  1684. (LPBYTE) &(lpConnectionSettings->dwHidden),
  1685. sizeof(DWORD)));
  1686. smChkTo(EH_Err4,RegSetValueEx(hkeySchedName,TEXT("ScheduleReadOnly"),NULL, REG_DWORD,
  1687. (LPBYTE) &(lpConnectionSettings->dwReadOnly),
  1688. sizeof(DWORD)));
  1689. smChkTo(EH_Err4,RegCreateUserSubKey ( hkeySchedName,
  1690. lpConnectionSettings->pszConnectionName,
  1691. KEY_WRITE | KEY_READ,
  1692. &hkeyConnection));
  1693. smChkTo(EH_Err5,RegSetValueEx(hkeyConnection,TEXT("MakeAutoConnection"),NULL, REG_DWORD,
  1694. (LPBYTE) &(lpConnectionSettings->dwMakeConnection),
  1695. sizeof(DWORD)));
  1696. smChkTo(EH_Err5,RegSetValueEx(hkeyConnection,TEXT("Connection Type"),NULL, REG_DWORD,
  1697. (LPBYTE) &(lpConnectionSettings->dwConnType),
  1698. sizeof(DWORD)));
  1699. RegCloseKey(hkeyConnection);
  1700. RegCloseKey(hkeySchedName);
  1701. RegCloseKey(hKeyUser);
  1702. CMutexRegistry.Leave();
  1703. return TRUE;
  1704. EH_Err5:
  1705. RegCloseKey(hkeyConnection);
  1706. EH_Err4:
  1707. RegCloseKey(hkeySchedName);
  1708. EH_Err3:
  1709. RegCloseKey(hKeyUser);
  1710. EH_Err:
  1711. CMutexRegistry.Leave();
  1712. return FALSE;
  1713. }
  1714. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1715. Function: RegGetSchedConnectionName(TCHAR *pszSchedName,
  1716. TCHAR *pszConnectionName,
  1717. DWORD cbConnectionName)
  1718. Summary: returns the Connection Name for the Scheduled Item.
  1719. Returns: Returns TRUE if successful, FALSE otherwise
  1720. ------------------------------------------------------------------------F-F*/
  1721. STDAPI_(BOOL) RegGetSchedConnectionName(TCHAR *pszSchedName,TCHAR *pszConnectionName,
  1722. DWORD cbConnectionName)
  1723. {
  1724. SCODE sc;
  1725. BOOL fResult = FALSE;
  1726. HKEY hKeyUser,hkeySchedName;
  1727. DWORD dwIndex = 0;
  1728. DWORD cb = cbConnectionName;
  1729. DWORD dwType = REG_DWORD;
  1730. DWORD dwDataSize = sizeof(DWORD);
  1731. //First Set the connectionName to a default.
  1732. // for now we always assume a LAN card is present.
  1733. // if add support for connection manager should
  1734. // update this.
  1735. LoadString(g_hmodThisDll, IDS_LAN_CONNECTION, pszConnectionName, cbConnectionName);
  1736. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_SCHEDULED,KEY_READ,FALSE);
  1737. if (NULL == hKeyUser)
  1738. {
  1739. goto EH_Err;
  1740. }
  1741. smChkTo(EH_Err3, RegOpenKeyEx (hKeyUser,pszSchedName,
  1742. 0,KEY_READ, &hkeySchedName));
  1743. // next enumeration of keys is the names of the Schedules connection
  1744. // currently just have one so only get the first.
  1745. if ( ERROR_SUCCESS == (sc = RegEnumKeyEx(hkeySchedName,dwIndex,
  1746. pszConnectionName,&cb,NULL,NULL,NULL,NULL)) )
  1747. {
  1748. fResult = TRUE;
  1749. }
  1750. RegCloseKey(hkeySchedName);
  1751. RegCloseKey(hKeyUser);
  1752. return fResult;
  1753. EH_Err3:
  1754. RegCloseKey(hKeyUser);
  1755. EH_Err:
  1756. return FALSE;
  1757. }
  1758. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1759. Function: BOOL RegSetSIDForSchedule( TCHAR *pszSchedName)
  1760. Summary: sets the SID for this schedule
  1761. Returns: Returns TRUE if successful, FALSE otherwise
  1762. ------------------------------------------------------------------------F-F*/
  1763. STDAPI_(BOOL) RegSetSIDForSchedule(TCHAR *pszSchedName)
  1764. {
  1765. SCODE sc;
  1766. HKEY hkeySchedName;
  1767. TCHAR pszSID[MAX_PATH + 1];
  1768. DWORD dwSize = MAX_PATH;
  1769. DWORD dwType = REG_SZ;
  1770. if (!GetUserTextualSid(pszSID, &dwSize))
  1771. {
  1772. return FALSE;
  1773. }
  1774. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  1775. CMutexRegistry.Enter();
  1776. HKEY hKeyUser = RegGetCurrentUserKey(SYNCTYPE_SCHEDULED,KEY_READ | KEY_WRITE,TRUE);
  1777. if (NULL == hKeyUser)
  1778. {
  1779. goto EH_Err2;
  1780. }
  1781. smChkTo(EH_Err3,RegCreateUserSubKey (hKeyUser, pszSchedName,
  1782. KEY_WRITE | KEY_READ,
  1783. &hkeySchedName));
  1784. smChkTo(EH_Err4,RegSetValueEx (hkeySchedName,TEXT("SID"),NULL,
  1785. dwType,
  1786. (LPBYTE) pszSID,
  1787. (lstrlen(pszSID) + 1)*sizeof(TCHAR)));
  1788. RegCloseKey(hkeySchedName);
  1789. RegCloseKey(hKeyUser);
  1790. CMutexRegistry.Leave();
  1791. return TRUE;
  1792. EH_Err4:
  1793. RegCloseKey(hkeySchedName);
  1794. EH_Err3:
  1795. RegCloseKey(hKeyUser);
  1796. EH_Err2:
  1797. CMutexRegistry.Leave();
  1798. return FALSE;
  1799. }
  1800. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1801. Function: BOOL RegGetSIDForSchedule( TCHAR *ptszTextualSidSched,
  1802. DWORD *dwSizeSid, TCHAR *pszSchedName)
  1803. Summary: returns the SID for this schedule
  1804. Returns: Returns TRUE if successful, FALSE otherwise
  1805. ------------------------------------------------------------------------F-F*/
  1806. STDAPI_(BOOL) RegGetSIDForSchedule(TCHAR *ptszTextualSidSched, DWORD *pdwSizeSid, TCHAR *pszSchedName)
  1807. {
  1808. SCODE sc;
  1809. HKEY hkeySchedName;
  1810. DWORD dwIndex = 0;
  1811. DWORD dwType = REG_SZ;
  1812. DWORD dwSizeSidSave = *pdwSizeSid,
  1813. dwSizeSidSave2 = *pdwSizeSid;
  1814. DWORD dwTouched = TRUE;
  1815. ptszTextualSidSched[0] = TEXT('\0');
  1816. *pdwSizeSid = 0;
  1817. HKEY hKeyUser = RegGetCurrentUserKey(SYNCTYPE_SCHEDULED,KEY_READ,FALSE);
  1818. if (NULL == hKeyUser)
  1819. {
  1820. goto EH_Err2;
  1821. }
  1822. smChkTo(EH_Err3, RegOpenKeyEx (hKeyUser,pszSchedName,
  1823. 0,KEY_READ, &hkeySchedName));
  1824. if (ERROR_SUCCESS != (sc = RegQueryValueEx(hkeySchedName,TEXT("SID"),NULL, &dwType,
  1825. (LPBYTE) ptszTextualSidSched, &dwSizeSidSave)))
  1826. {
  1827. //handle migration from schedules without SIDs
  1828. // like from Beta to current builds
  1829. RegSetSIDForSchedule(pszSchedName);
  1830. RegQueryValueEx(hkeySchedName,TEXT("SID"),NULL, &dwType,
  1831. (LPBYTE) ptszTextualSidSched, &dwSizeSidSave2);
  1832. *pdwSizeSid = dwSizeSidSave2;
  1833. }
  1834. else
  1835. {
  1836. *pdwSizeSid = dwSizeSidSave;
  1837. }
  1838. RegCloseKey(hkeySchedName);
  1839. RegCloseKey(hKeyUser);
  1840. return TRUE;
  1841. EH_Err3:
  1842. RegCloseKey(hKeyUser);
  1843. EH_Err2:
  1844. return FALSE;
  1845. }
  1846. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1847. BOOL RemoveScheduledJobFile(TCHAR *pszTaskName)
  1848. Summary: Remove the TaskScheduler .job file
  1849. Note: Try to use ITask->Delete first
  1850. Call only when TaskScheduler isn't present or if ITask->Delete failed
  1851. Returns: Returns TRUE always
  1852. ------------------------------------------------------------------------F-F*/
  1853. STDAPI_(BOOL) RemoveScheduledJobFile(TCHAR *pszTaskName)
  1854. {
  1855. SCODE sc;
  1856. TCHAR pszFullFileName[MAX_PATH+1];
  1857. TCHAR pszTaskFolderPath[MAX_PATH+1];
  1858. HKEY hkeyTaskSchedulerPath;
  1859. DWORD dwType = REG_SZ;
  1860. DWORD dwDataSize = MAX_PATH * sizeof(TCHAR);
  1861. if (ERROR_SUCCESS == (sc = RegOpenKeyEx (HKEY_LOCAL_MACHINE,
  1862. TEXT("SOFTWARE\\Microsoft\\SchedulingAgent"),
  1863. NULL,KEY_READ,&hkeyTaskSchedulerPath)))
  1864. {
  1865. sc = RegQueryValueEx(hkeyTaskSchedulerPath,TEXT("TasksFolder"),
  1866. NULL, &dwType,
  1867. (LPBYTE) pszTaskFolderPath, &dwDataSize);
  1868. RegCloseKey(hkeyTaskSchedulerPath);
  1869. }
  1870. //If this get doesn't exist then bail.
  1871. if (ERROR_SUCCESS != sc)
  1872. {
  1873. return FALSE;
  1874. }
  1875. ExpandEnvironmentStrings(pszTaskFolderPath,pszFullFileName,MAX_PATH);
  1876. wcscat(pszFullFileName, L"\\");
  1877. wcscat(pszFullFileName,pszTaskName);
  1878. //if we fail this, ignore the error. We tried, there isn't much else we can do.
  1879. //So we have a turd file.
  1880. sc = DeleteFile(pszFullFileName);
  1881. return TRUE;
  1882. }
  1883. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1884. BOOL RegRemoveScheduledTask(TCHAR *pszTaskName)
  1885. Summary: Remove the Scheduled task info from the registry.
  1886. Returns: Returns TRUE if successful, FALSE otherwise
  1887. ------------------------------------------------------------------------F-F*/
  1888. STDAPI_(BOOL) RegRemoveScheduledTask(TCHAR *pszTaskName)
  1889. {
  1890. HKEY hKeyUser;
  1891. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  1892. CMutexRegistry.Enter();
  1893. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_SCHEDULED,KEY_READ | KEY_WRITE,FALSE);
  1894. if (hKeyUser)
  1895. {
  1896. RegDeleteKeyNT(hKeyUser, pszTaskName);
  1897. RegCloseKey(hKeyUser);
  1898. }
  1899. CMutexRegistry.Leave();
  1900. return TRUE;
  1901. }
  1902. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1903. BOOL RegUninstallSchedules()
  1904. Summary: Uninstall the scheduled tasks.
  1905. Returns: Returns TRUE if successful, FALSE otherwise
  1906. ------------------------------------------------------------------------F-F*/
  1907. STDAPI_(BOOL) RegUninstallSchedules()
  1908. {
  1909. SCODE sc;
  1910. ITaskScheduler *pITaskScheduler = NULL;
  1911. HKEY hkeySchedSync;
  1912. // Obtain a task scheduler class instance.
  1913. sc = CoCreateInstance(
  1914. CLSID_CTaskScheduler,
  1915. NULL,
  1916. CLSCTX_INPROC_SERVER,
  1917. IID_ITaskScheduler,
  1918. (VOID **)&pITaskScheduler);
  1919. if (NOERROR != sc)
  1920. {
  1921. pITaskScheduler = NULL;
  1922. }
  1923. //now go through and delete the schedules
  1924. hkeySchedSync = RegGetSyncTypeKey(SYNCTYPE_SCHEDULED,KEY_READ,FALSE);
  1925. if (hkeySchedSync)
  1926. {
  1927. int iUserCount = 0;
  1928. while (sc == ERROR_SUCCESS )
  1929. {
  1930. HKEY hkeyDomainUser;
  1931. TCHAR szDomainUser[MAX_KEY_LENGTH];
  1932. SCODE sc2;
  1933. DWORD dwDataSize;
  1934. dwDataSize = MAX_KEY_LENGTH;
  1935. sc = RegEnumKeyEx(hkeySchedSync,iUserCount,szDomainUser,&dwDataSize,NULL,NULL,NULL,NULL);
  1936. iUserCount++;
  1937. if(sc == ERROR_NO_MORE_ITEMS)
  1938. {
  1939. break;
  1940. }
  1941. sc2 = RegOpenKeyEx (hkeySchedSync,szDomainUser,0,KEY_READ, &hkeyDomainUser);
  1942. if (ERROR_SUCCESS == sc2)
  1943. {
  1944. int iScheduleCount = 0;
  1945. while (sc2 == ERROR_SUCCESS )
  1946. {
  1947. TCHAR ptszScheduleGUIDName[MAX_KEY_LENGTH];
  1948. //Add 4 to ensure that we can hold the .job extension if necessary
  1949. WCHAR pwszScheduleGUIDName[MAX_SCHEDULENAMESIZE + 4];
  1950. dwDataSize = MAX_KEY_LENGTH;
  1951. sc2 = RegEnumKeyEx(hkeyDomainUser,iScheduleCount,ptszScheduleGUIDName,&dwDataSize,NULL,NULL,NULL,NULL);
  1952. iScheduleCount++;
  1953. if(sc2 == ERROR_NO_MORE_ITEMS)
  1954. {
  1955. continue;
  1956. }
  1957. else
  1958. {
  1959. ConvertString(pwszScheduleGUIDName,ptszScheduleGUIDName,MAX_SCHEDULENAMESIZE);
  1960. if ((!pITaskScheduler) ||
  1961. FAILED(pITaskScheduler->Delete(pwszScheduleGUIDName)))
  1962. {
  1963. wcscat(pwszScheduleGUIDName, L".job");
  1964. RemoveScheduledJobFile(pwszScheduleGUIDName);
  1965. }
  1966. }
  1967. }
  1968. RegCloseKey(hkeyDomainUser);
  1969. }
  1970. }
  1971. RegCloseKey(hkeySchedSync);
  1972. }
  1973. if (pITaskScheduler)
  1974. {
  1975. pITaskScheduler->Release();
  1976. }
  1977. RegDeleteKeyNT(HKEY_LOCAL_MACHINE, SCHEDSYNC_REGKEY);
  1978. return TRUE;
  1979. }
  1980. /****************************************************************************
  1981. Idle Registry Functions
  1982. ***************************************************************************F-F*/
  1983. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1984. Function: RegGetIdleSyncSettings( LPCONNECTIONSETTINGS lpConnectionSettings)
  1985. Summary: Gets the Idle Specific settings.
  1986. Returns: Returns TRUE if successful, FALSE otherwise
  1987. ------------------------------------------------------------------------F-F*/
  1988. STDAPI_(BOOL) RegGetIdleSyncSettings(LPCONNECTIONSETTINGS lpConnectionSettings)
  1989. {
  1990. SCODE sc;
  1991. HKEY hkeyConnection;
  1992. DWORD dwType = REG_SZ ;
  1993. DWORD dwDataSize = MAX_PATH;
  1994. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  1995. CMutexRegistry.Enter();
  1996. // set up defaults
  1997. lpConnectionSettings->dwIdleEnabled = 0;
  1998. // following are really per user not per connection
  1999. lpConnectionSettings->ulIdleWaitMinutes = UL_DEFAULTIDLEWAITMINUTES;
  2000. lpConnectionSettings->ulIdleRetryMinutes = UL_DEFAULTIDLERETRYMINUTES;
  2001. lpConnectionSettings->ulDelayIdleShutDownTime = UL_DELAYIDLESHUTDOWNTIME;
  2002. lpConnectionSettings->dwRepeatSynchronization = UL_DEFAULTREPEATSYNCHRONIZATION;
  2003. lpConnectionSettings->dwRunOnBatteries = UL_DEFAULTFRUNONBATTERIES;
  2004. HKEY hKeyUser = RegGetCurrentUserKey(SYNCTYPE_IDLE,KEY_READ,FALSE);
  2005. if (NULL == hKeyUser)
  2006. {
  2007. goto EH_Err2;
  2008. }
  2009. dwType = REG_DWORD ;
  2010. dwDataSize = sizeof(DWORD);
  2011. // if got the Idle key open then fill in global settings
  2012. RegQueryValueEx(hKeyUser,SZ_IDLEWAITAFTERIDLEMINUTESKEY,NULL, &dwType,
  2013. (LPBYTE) &(lpConnectionSettings->ulIdleWaitMinutes),
  2014. &dwDataSize);
  2015. dwDataSize = sizeof(DWORD);
  2016. RegQueryValueEx(hKeyUser,SZ_IDLEREPEATESYNCHRONIZATIONKEY,NULL, &dwType,
  2017. (LPBYTE) &(lpConnectionSettings->dwRepeatSynchronization),
  2018. &dwDataSize);
  2019. dwDataSize = sizeof(DWORD);
  2020. RegQueryValueEx(hKeyUser,SZ_IDLERETRYMINUTESKEY,NULL, &dwType,
  2021. (LPBYTE) &(lpConnectionSettings->ulIdleRetryMinutes),
  2022. &dwDataSize);
  2023. dwDataSize = sizeof(DWORD);
  2024. RegQueryValueEx(hKeyUser,SZ_IDLEDELAYSHUTDOWNTIMEKEY,NULL, &dwType,
  2025. (LPBYTE) &(lpConnectionSettings->ulDelayIdleShutDownTime),
  2026. &dwDataSize);
  2027. dwDataSize = sizeof(DWORD);
  2028. RegQueryValueEx(hKeyUser,SZ_IDLERUNONBATTERIESKEY,NULL, &dwType,
  2029. (LPBYTE) &(lpConnectionSettings->dwRunOnBatteries),
  2030. &dwDataSize);
  2031. smChkTo(EH_Err3,RegOpenKeyEx(hKeyUser, lpConnectionSettings->pszConnectionName,0,KEY_READ,
  2032. &hkeyConnection));
  2033. dwDataSize = sizeof(DWORD);
  2034. RegQueryValueEx(hkeyConnection,TEXT("IdleEnabled"),NULL, &dwType,
  2035. (LPBYTE) &(lpConnectionSettings->dwIdleEnabled),
  2036. &dwDataSize);
  2037. RegCloseKey(hkeyConnection);
  2038. RegCloseKey(hKeyUser);
  2039. CMutexRegistry.Leave();
  2040. return TRUE;
  2041. EH_Err3:
  2042. RegCloseKey(hKeyUser);
  2043. EH_Err2:
  2044. CMutexRegistry.Leave();
  2045. return FALSE;
  2046. }
  2047. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2048. Function: RegSetIdleSyncSettings(LPCONNECTIONSETTINGS lpConnectionSettings,
  2049. int iNumConnections,
  2050. CRasUI *pRas,
  2051. BOOL fCleanReg,
  2052. BOOL fPerUser)
  2053. Summary: Sets the Idle Information user selections.
  2054. Returns: Returns TRUE if successful, FALSE otherwise
  2055. ------------------------------------------------------------------------F-F*/
  2056. STDAPI_(BOOL) RegSetIdleSyncSettings(LPCONNECTIONSETTINGS lpConnectionSettings,
  2057. int iNumConnections,
  2058. CRasUI *pRas,
  2059. BOOL fCleanReg,
  2060. BOOL fPerUser)
  2061. {
  2062. HKEY hkeyIdleSync = NULL;
  2063. HKEY hKeyUser;;
  2064. HKEY hkeyConnection;
  2065. HRESULT hr;
  2066. ULONG ulWaitMinutes = UL_DEFAULTWAITMINUTES;
  2067. DWORD dwIdleEnabled;
  2068. BOOL fRunOnBatteries = UL_DEFAULTFRUNONBATTERIES;
  2069. int i;
  2070. DWORD dwType;
  2071. DWORD dwDataSize;
  2072. DWORD dwUserConfigured;
  2073. DWORD dwTopLevelDefaultValue = -1;
  2074. RegUpdateTopLevelKeys(); // make sure top-level keys are latest version
  2075. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  2076. CMutexRegistry.Enter();
  2077. Assert(-1 != TRUE); // we rely on TRUE boolean being something other than -1
  2078. hkeyIdleSync = RegGetSyncTypeKey(SYNCTYPE_IDLE,KEY_WRITE| KEY_READ,TRUE);
  2079. if (NULL == hkeyIdleSync)
  2080. {
  2081. goto EH_Err;
  2082. }
  2083. hKeyUser = RegOpenUserKey(hkeyIdleSync,KEY_WRITE| KEY_READ,TRUE,fCleanReg);
  2084. if (NULL == hKeyUser)
  2085. {
  2086. goto EH_Err2;
  2087. }
  2088. if (fPerUser)
  2089. {
  2090. dwUserConfigured = 1;
  2091. if (ERROR_SUCCESS != RegSetValueEx(hKeyUser,TEXT("UserConfigured"),NULL, REG_DWORD,
  2092. (LPBYTE) &dwUserConfigured,
  2093. sizeof(DWORD)))
  2094. {
  2095. goto EH_Err3;
  2096. }
  2097. }
  2098. else
  2099. {
  2100. dwType = REG_DWORD;
  2101. dwDataSize = sizeof(DWORD);
  2102. //If this value isn't added yet,
  2103. if (ERROR_SUCCESS == RegQueryValueEx(hKeyUser,TEXT("UserConfigured"),NULL, &dwType,
  2104. (LPBYTE) &dwUserConfigured,
  2105. &dwDataSize))
  2106. {
  2107. //if the user setup their configuration, we won't override on a
  2108. //subsequent handler registration.
  2109. if (dwUserConfigured)
  2110. {
  2111. RegCloseKey(hkeyIdleSync);
  2112. RegCloseKey(hKeyUser);
  2113. CMutexRegistry.Leave();
  2114. return TRUE;
  2115. }
  2116. }
  2117. }
  2118. for (i=0; i<iNumConnections; i++)
  2119. {
  2120. if (ERROR_SUCCESS != RegCreateUserSubKey (hKeyUser,
  2121. lpConnectionSettings[i].pszConnectionName,
  2122. KEY_WRITE | KEY_READ,
  2123. &hkeyConnection))
  2124. {
  2125. goto EH_Err3;
  2126. }
  2127. hr = RegSetValueEx(hkeyConnection,TEXT("IdleEnabled"),NULL, REG_DWORD,
  2128. (LPBYTE) &(lpConnectionSettings[i].dwIdleEnabled),
  2129. sizeof(DWORD));
  2130. if (lpConnectionSettings[i].dwIdleEnabled)
  2131. {
  2132. dwTopLevelDefaultValue = lpConnectionSettings[i].dwIdleEnabled;
  2133. }
  2134. RegCloseKey(hkeyConnection);
  2135. }
  2136. // write out the global idle information for Retry minutes and DelayIdleShutDown.
  2137. // then call function to reg/unregister with TS.
  2138. Assert(hkeyIdleSync); // should have already returned if this failed.
  2139. if (iNumConnections) // make sure at least one connection
  2140. {
  2141. // ONLY UPDATE SETTINGS IF NOT -1;
  2142. if (-1 != lpConnectionSettings[0].ulIdleRetryMinutes)
  2143. {
  2144. hr = RegSetValueEx(hKeyUser,SZ_IDLERETRYMINUTESKEY,NULL, REG_DWORD,
  2145. (LPBYTE) &(lpConnectionSettings[0].ulIdleRetryMinutes),
  2146. sizeof(DWORD));
  2147. }
  2148. if (-1 != lpConnectionSettings[0].ulDelayIdleShutDownTime)
  2149. {
  2150. hr = RegSetValueEx(hKeyUser,SZ_IDLEDELAYSHUTDOWNTIMEKEY,NULL, REG_DWORD,
  2151. (LPBYTE) &(lpConnectionSettings[0].ulDelayIdleShutDownTime),
  2152. sizeof(DWORD));
  2153. }
  2154. if (-1 != lpConnectionSettings[0].dwRepeatSynchronization)
  2155. {
  2156. hr = RegSetValueEx(hKeyUser,SZ_IDLEREPEATESYNCHRONIZATIONKEY,NULL, REG_DWORD,
  2157. (LPBYTE) &(lpConnectionSettings[0].dwRepeatSynchronization),
  2158. sizeof(DWORD));
  2159. }
  2160. if (-1 != lpConnectionSettings[0].ulIdleWaitMinutes)
  2161. {
  2162. hr = RegSetValueEx(hKeyUser,SZ_IDLEWAITAFTERIDLEMINUTESKEY,NULL, REG_DWORD,
  2163. (LPBYTE) &(lpConnectionSettings[0].ulIdleWaitMinutes),
  2164. sizeof(DWORD));
  2165. }
  2166. if (-1 != lpConnectionSettings[0].dwRunOnBatteries)
  2167. {
  2168. hr = RegSetValueEx(hKeyUser,SZ_IDLERUNONBATTERIESKEY,NULL, REG_DWORD,
  2169. (LPBYTE) &(lpConnectionSettings[0].dwRunOnBatteries),
  2170. sizeof(DWORD));
  2171. }
  2172. ulWaitMinutes = lpConnectionSettings[0].ulIdleWaitMinutes;
  2173. fRunOnBatteries = lpConnectionSettings[0].dwRunOnBatteries;
  2174. // if -1 is passed in for ulWait or fRun on Batteries we need to
  2175. // get these and set them up so they can be passed onto Task Schedule
  2176. if (-1 == ulWaitMinutes)
  2177. {
  2178. dwType = REG_DWORD ;
  2179. dwDataSize = sizeof(ulWaitMinutes);
  2180. if (!(ERROR_SUCCESS == RegQueryValueEx(hKeyUser,SZ_IDLEWAITAFTERIDLEMINUTESKEY,NULL, &dwType,
  2181. (LPBYTE) &ulWaitMinutes,
  2182. &dwDataSize)) )
  2183. {
  2184. ulWaitMinutes = UL_DEFAULTIDLEWAITMINUTES;
  2185. }
  2186. }
  2187. if (-1 == fRunOnBatteries)
  2188. {
  2189. dwType = REG_DWORD ;
  2190. dwDataSize = sizeof(fRunOnBatteries);
  2191. if (!(ERROR_SUCCESS == RegQueryValueEx(hKeyUser,SZ_IDLERUNONBATTERIESKEY,NULL, &dwType,
  2192. (LPBYTE) &fRunOnBatteries,
  2193. &dwDataSize)) )
  2194. {
  2195. fRunOnBatteries = UL_DEFAULTFRUNONBATTERIES;
  2196. }
  2197. }
  2198. }
  2199. RegUpdateUserIdleKey(hKeyUser,TRUE /* fForce */); // set userlevel IdleFlags
  2200. // read in dwIdleEnabled key now that the UserKey is Updated.
  2201. dwType = REG_DWORD ;
  2202. dwDataSize = sizeof(DWORD);
  2203. if (!(ERROR_SUCCESS == RegQueryValueEx(hKeyUser,TEXT("IdleEnabled"),NULL, &dwType,
  2204. (LPBYTE) &dwIdleEnabled,
  2205. &dwDataSize)) )
  2206. {
  2207. AssertSz(0,"Unable to query User IdleEnabledKey");
  2208. dwIdleEnabled = FALSE;
  2209. }
  2210. RegCloseKey(hKeyUser);
  2211. // update the toplevel IdleSyncInfo
  2212. RegUpdateIdleKeyValue(hkeyIdleSync,dwTopLevelDefaultValue);
  2213. RegCloseKey(hkeyIdleSync);
  2214. CMutexRegistry.Leave();
  2215. RegRegisterForIdleTrigger(dwIdleEnabled,ulWaitMinutes,fRunOnBatteries);
  2216. return TRUE;
  2217. EH_Err3:
  2218. RegCloseKey(hKeyUser);
  2219. EH_Err2:
  2220. RegCloseKey(hkeyIdleSync);
  2221. EH_Err:
  2222. CMutexRegistry.Leave();
  2223. return FALSE;
  2224. }
  2225. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2226. Function: RegRegisterForIdleTrigger()
  2227. Summary: Sets or removes the Idle trigger.
  2228. Returns: Returns TRUE if successful, FALSE otherwise
  2229. ------------------------------------------------------------------------F-F*/
  2230. STDAPI_(BOOL) RegRegisterForIdleTrigger(BOOL fRegister,ULONG ulWaitMinutes,BOOL fRunOnBatteries)
  2231. {
  2232. HRESULT hr;
  2233. CSyncMgrSynchronize *pSyncMgrSynchronize;
  2234. LPSYNCSCHEDULEMGR pScheduleMgr;
  2235. // Review - Currently the mobsync dll is registered as apartment
  2236. // and this function can be called from Logon/Logoff which is FreeThreaded
  2237. // Correct fix is to change DLL to be registered as BOTH but until then
  2238. // just create the class directly.
  2239. #ifdef _WHENREGUPDATED
  2240. hr = CoCreateInstance(CLSID_SyncMgr,NULL,CLSCTX_ALL,
  2241. IID_ISyncScheduleMgr,(void **) &pScheduleMgr);
  2242. #endif // _WHENREGUPDATED
  2243. pSyncMgrSynchronize = new CSyncMgrSynchronize;
  2244. hr = E_OUTOFMEMORY;
  2245. if (pSyncMgrSynchronize)
  2246. {
  2247. hr = pSyncMgrSynchronize->QueryInterface(IID_ISyncScheduleMgr,(void **) &pScheduleMgr);
  2248. pSyncMgrSynchronize->Release();
  2249. }
  2250. if (NOERROR != hr)
  2251. {
  2252. return FALSE;
  2253. }
  2254. if (fRegister)
  2255. {
  2256. ISyncSchedule *pSyncSchedule = NULL;
  2257. SYNCSCHEDULECOOKIE SyncScheduleCookie;
  2258. BOOL fNewlyCreated = FALSE;
  2259. HRESULT hr = E_FAIL;
  2260. SyncScheduleCookie = GUID_IDLESCHEDULE;
  2261. // if there isn't an existing schedule create one, else update
  2262. // the existing.
  2263. if (HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) == (hr = pScheduleMgr->OpenSchedule(&SyncScheduleCookie,0,&pSyncSchedule)))
  2264. {
  2265. SyncScheduleCookie = GUID_IDLESCHEDULE;
  2266. // see if it is an exiting schedule.
  2267. hr = pScheduleMgr->CreateSchedule(L"Idle",0,&SyncScheduleCookie,&pSyncSchedule);
  2268. fNewlyCreated = TRUE;
  2269. }
  2270. // If created or found a schedule update the trigger settings.
  2271. if (NOERROR == hr)
  2272. {
  2273. ITaskTrigger *pTrigger;
  2274. pSyncSchedule->SetFlags(SYNCSCHEDINFO_FLAGS_READONLY | SYNCSCHEDINFO_FLAGS_HIDDEN);
  2275. if (NOERROR == pSyncSchedule->GetTrigger(&pTrigger))
  2276. {
  2277. TASK_TRIGGER trigger;
  2278. ITask *pTask;
  2279. trigger.cbTriggerSize = sizeof(TASK_TRIGGER);
  2280. if (SUCCEEDED(pTrigger->GetTrigger(&trigger)))
  2281. {
  2282. DWORD dwFlags;
  2283. // need to set Idle, ULONG ulWaitMinutes,BOOL fRunOnBatteries
  2284. trigger.cbTriggerSize = sizeof(TASK_TRIGGER);
  2285. trigger.TriggerType = TASK_EVENT_TRIGGER_ON_IDLE;
  2286. trigger.rgFlags = 0;
  2287. pTrigger->SetTrigger(&trigger);
  2288. if (SUCCEEDED(pSyncSchedule->GetITask(&pTask)))
  2289. {
  2290. // set up if run on battery.
  2291. if (SUCCEEDED(pTask->GetFlags(&dwFlags)))
  2292. {
  2293. dwFlags &= ~TASK_FLAG_DONT_START_IF_ON_BATTERIES;
  2294. dwFlags |= !fRunOnBatteries ? TASK_FLAG_DONT_START_IF_ON_BATTERIES : 0;
  2295. dwFlags |= TASK_FLAG_RUN_ONLY_IF_LOGGED_ON; // don't require password.
  2296. pTask->SetFlags(dwFlags);
  2297. }
  2298. // if this schedule was just created, get the current user name and reset
  2299. // account information password to NULL, If existing schedule don't change
  2300. // since user may have added a password for schedule to run while not logged on.
  2301. if (fNewlyCreated)
  2302. {
  2303. TCHAR szAccountName[MAX_DOMANDANDMACHINENAMESIZE];
  2304. WCHAR *pszAccountName = NULL;
  2305. DWORD dwAccountName = sizeof(szAccountName);
  2306. #ifndef _UNICODE
  2307. WCHAR pwszDomainAndUser[MAX_DOMANDANDMACHINENAMESIZE];
  2308. #endif // _UNICODE
  2309. // Review, this never returns an errorl
  2310. *szAccountName = TCHAR('\0');
  2311. GetDefaultDomainAndUserName( (LPTSTR) &szAccountName,TEXT("\\"),dwAccountName);
  2312. #ifndef _UNICODE
  2313. // if we are compiled as ansi need to convert to wchar
  2314. pszAccountName = pwszDomainAndUser;
  2315. MultiByteToWideChar(CP_ACP, 0, szAccountName, -1, pwszDomainAndUser,sizeof(pwszDomainAndUser));
  2316. #else
  2317. pszAccountName = szAccountName;
  2318. #endif _UNICODE
  2319. Assert(pszAccountName);
  2320. if (pszAccountName)
  2321. {
  2322. pTask->SetAccountInformation(pszAccountName,NULL);
  2323. }
  2324. }
  2325. // set up the IdleWaitTime.
  2326. pTask->SetIdleWait((WORD) ulWaitMinutes,1);
  2327. // turn off the option to kill task after xxx minutes.
  2328. pTask->SetMaxRunTime(INFINITE);
  2329. pTask->Release();
  2330. }
  2331. pTrigger->Release();
  2332. }
  2333. pSyncSchedule->Save();
  2334. }
  2335. pSyncSchedule->Release();
  2336. }
  2337. }
  2338. else
  2339. {
  2340. SYNCSCHEDULECOOKIE SyncScheduleCookie = GUID_IDLESCHEDULE;
  2341. // see if there is an existing schedule and if so remove it.
  2342. pScheduleMgr->RemoveSchedule(&SyncScheduleCookie);
  2343. }
  2344. pScheduleMgr->Release();
  2345. // set the temporary sens flags according so it can start on an idle trigger.
  2346. // not an error to not be able to get and set key since sens will
  2347. // be running anyways eventually.
  2348. HKEY hkeyAutoSync;
  2349. DWORD dwFlags = 0;
  2350. DWORD dwType = REG_DWORD;
  2351. DWORD dwDataSize = sizeof(DWORD);
  2352. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  2353. CMutexRegistry.Enter();
  2354. hkeyAutoSync = RegGetSyncTypeKey(SYNCTYPE_AUTOSYNC,KEY_WRITE | KEY_READ,TRUE);
  2355. if (NULL == hkeyAutoSync)
  2356. {
  2357. CMutexRegistry.Leave();
  2358. return FALSE;
  2359. }
  2360. if (ERROR_SUCCESS == RegQueryValueEx(hkeyAutoSync,TEXT("Flags"),NULL, &dwType,
  2361. (LPBYTE) &(dwFlags),&dwDataSize))
  2362. {
  2363. // Here we are setting Idle only so retail the other settings.
  2364. dwFlags &= ~AUTOSYNC_IDLE;
  2365. dwFlags |= (fRegister? AUTOSYNC_IDLE : 0);
  2366. RegSetValueEx(hkeyAutoSync,TEXT("Flags"),NULL, REG_DWORD,
  2367. (LPBYTE) &(dwFlags), sizeof(DWORD));
  2368. }
  2369. RegCloseKey(hkeyAutoSync);
  2370. CMutexRegistry.Leave();
  2371. return TRUE;
  2372. }
  2373. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2374. BOOL RegGetSyncSettings(DWORD dwSyncType,LPCONNECTIONSETTINGS lpConnectionSettings)
  2375. Summary: Get ConnectionSettings appropriate to the synctype..
  2376. Returns: Returns TRUE if successful, FALSE otherwise
  2377. ------------------------------------------------------------------------F-F*/
  2378. STDAPI_(BOOL) RegGetSyncSettings(DWORD dwSyncType,LPCONNECTIONSETTINGS lpConnectionSettings)
  2379. {
  2380. switch(dwSyncType)
  2381. {
  2382. case SYNCTYPE_AUTOSYNC:
  2383. return RegGetAutoSyncSettings(lpConnectionSettings);
  2384. break;
  2385. case SYNCTYPE_IDLE:
  2386. return RegGetIdleSyncSettings(lpConnectionSettings);
  2387. break;
  2388. default:
  2389. AssertSz(0,"Unknown SyncType in RegGetSyncSettings");
  2390. break;
  2391. }
  2392. return FALSE;
  2393. }
  2394. /****************************************************************************
  2395. Manual Sync Registry Functions
  2396. ***************************************************************************F-F*/
  2397. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2398. BOOL RegRemoveManualSyncSettings(TCHAR *pszTaskName)
  2399. Summary: Remove the manual settings info from the registry.
  2400. Returns: Returns TRUE if successful, FALSE otherwise
  2401. ------------------------------------------------------------------------F-F*/
  2402. STDAPI_(BOOL) RegRemoveManualSyncSettings(TCHAR *pszConnectionName)
  2403. {
  2404. HKEY hkeyUser;
  2405. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  2406. CMutexRegistry.Enter();
  2407. hkeyUser = RegGetCurrentUserKey(SYNCTYPE_MANUAL,KEY_WRITE | KEY_READ,FALSE);
  2408. if (hkeyUser)
  2409. {
  2410. RegDeleteKeyNT(hkeyUser, pszConnectionName);
  2411. RegCloseKey(hkeyUser);
  2412. }
  2413. CMutexRegistry.Leave();
  2414. return TRUE;
  2415. }
  2416. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2417. BOOL RegWriteEvents(BOOL fWanLogon,BOOL fWanLogoff,BOOL fLanLogon,BOOL fLanLogoff)
  2418. Summary: Write out the Wan/Lan Logon/Logoff preferences fo SENS knows whether to invoke us.
  2419. Returns: Returns TRUE if successful, FALSE otherwise
  2420. ------------------------------------------------------------------------F-F*/
  2421. // Run key under HKLM
  2422. const WCHAR wszRunKey[] = TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
  2423. const WCHAR wszRunKeyCommandLine[] = TEXT("%SystemRoot%\\system32\\mobsync.exe /logon");
  2424. STDAPI_(BOOL) RegWriteEvents(BOOL Logon,BOOL Logoff)
  2425. {
  2426. HRESULT hr;
  2427. HKEY hkeyAutoSync;
  2428. DWORD dwFlags = 0;
  2429. DWORD dwType = REG_DWORD;
  2430. DWORD dwDataSize = sizeof(DWORD);
  2431. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  2432. CMutexRegistry.Enter();
  2433. hkeyAutoSync = RegGetSyncTypeKey(SYNCTYPE_AUTOSYNC,KEY_WRITE | KEY_READ,TRUE);
  2434. if (NULL == hkeyAutoSync)
  2435. {
  2436. CMutexRegistry.Leave();
  2437. return FALSE;
  2438. }
  2439. RegQueryValueEx(hkeyAutoSync,TEXT("Flags"),NULL, &dwType,
  2440. (LPBYTE) &(dwFlags),
  2441. &dwDataSize);
  2442. // Review, Shouldn't need to worry about schedule/idle once IsNetworkAlive
  2443. // is setup properly. Leave for now first time anyone sets idle or schedule
  2444. // stuck
  2445. // Here we are setting autosync only,
  2446. // so retain the registry settings for scheduled and idle.
  2447. dwFlags &= ~(AUTOSYNC_WAN_LOGON | AUTOSYNC_LAN_LOGON | AUTOSYNC_LOGONWITHRUNKEY
  2448. | AUTOSYNC_WAN_LOGOFF | AUTOSYNC_LAN_LOGOFF);
  2449. // don't set logoff flags unless on platform logoff is supported
  2450. //
  2451. // !!! warning, if you change this also need to update settings
  2452. // and registry which are the other places we block this.
  2453. if ((VER_PLATFORM_WIN32_NT == g_OSVersionInfo.dwPlatformId
  2454. && g_OSVersionInfo.dwMajorVersion >= 5) )
  2455. {
  2456. dwFlags |= (Logoff ? AUTOSYNC_WAN_LOGOFF : 0);
  2457. dwFlags |= (Logoff ? AUTOSYNC_LAN_LOGOFF : 0);
  2458. }
  2459. // Since now use Run key instead of SENS always set both Logon Flags
  2460. // that SENS looks for to do a CreateProcess on us to FALSE.
  2461. // Then set the AUTOSYNC_LOGONWITHRUNKEY key to true so sens still gets loaded.
  2462. dwFlags |= (Logon ? AUTOSYNC_LOGONWITHRUNKEY : 0);
  2463. hr = RegSetValueEx(hkeyAutoSync,TEXT("Flags"),NULL, REG_DWORD,
  2464. (LPBYTE) &(dwFlags), sizeof(DWORD));
  2465. RegCloseKey(hkeyAutoSync);
  2466. // now add /delete the run key appropriately.
  2467. HKEY hKeyRun;
  2468. // call private RegOpen since don't want to set security on RunKey
  2469. if (ERROR_SUCCESS == RegOpenKeyExXp(HKEY_LOCAL_MACHINE,wszRunKey,
  2470. NULL,KEY_READ | KEY_WRITE,&hKeyRun,FALSE /*fSetSecurity*/))
  2471. {
  2472. if (Logon)
  2473. {
  2474. RegSetValueEx(hKeyRun, SZ_SYNCMGRNAME, 0, REG_EXPAND_SZ,
  2475. (BYTE *) wszRunKeyCommandLine,(lstrlen(wszRunKeyCommandLine) + 1)*sizeof(TCHAR));
  2476. }
  2477. else
  2478. {
  2479. RegDeleteValue(hKeyRun, SZ_SYNCMGRNAME);
  2480. }
  2481. RegCloseKey(hKeyRun);
  2482. }
  2483. else
  2484. {
  2485. // if can't open run key try calling SENS if that fails give up.
  2486. SyncMgrExecCmd_UpdateRunKey(Logon);
  2487. }
  2488. CMutexRegistry.Leave();
  2489. return TRUE;
  2490. }
  2491. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2492. BOOL RegFixRunKey(BOOL fScheduled)
  2493. Summary: The original version of SyncMgr for WinMe/Win2000 wrote the
  2494. "run" value as "mobsync.exe /logon". Since this is not a fully-
  2495. qualified path to the mobsync.exe image, the system's search
  2496. path is utilized to locate the image. This can create an
  2497. opportunity for someone to build a 'trojan' mobsync.exe, place
  2498. it in the search path ahead of the real mobsync.exe and have
  2499. the 'trojan' code run whenever a synchronization is invoked.
  2500. To fix this, the path must be stored in the registry using
  2501. fully-qualified syntax.
  2502. i.e. "%SystemRoot%\System32\mobsync.exe /logon"
  2503. This function is called from DllRegisterServer to correct this
  2504. registry entry during setup.
  2505. Returns: Always returns TRUE.
  2506. ------------------------------------------------------------------------F-F*/
  2507. STDAPI_(BOOL) RegFixRunKey(void)
  2508. {
  2509. HKEY hKeyRun;
  2510. // call private RegOpen since don't want to set security on RunKey
  2511. if (ERROR_SUCCESS == RegOpenKeyExXp(HKEY_LOCAL_MACHINE,
  2512. wszRunKey,
  2513. NULL,
  2514. KEY_READ | KEY_WRITE,
  2515. &hKeyRun,
  2516. FALSE /*fSetSecurity*/))
  2517. {
  2518. TCHAR szRunValue[MAX_PATH];
  2519. DWORD cbValue = sizeof(szRunValue);
  2520. DWORD dwType;
  2521. if (ERROR_SUCCESS == RegQueryValueEx(hKeyRun,
  2522. SZ_SYNCMGRNAME,
  2523. NULL,
  2524. &dwType,
  2525. (LPBYTE)szRunValue,
  2526. &cbValue))
  2527. {
  2528. if (REG_SZ == dwType && 0 == lstrcmp(szRunValue, TEXT("mobsync.exe /logon")))
  2529. {
  2530. //
  2531. // Upgrade only if it's our original value.
  2532. //
  2533. RegSetValueEx(hKeyRun,
  2534. SZ_SYNCMGRNAME,
  2535. 0,
  2536. REG_EXPAND_SZ,
  2537. (BYTE *)wszRunKeyCommandLine,
  2538. (lstrlen(wszRunKeyCommandLine) + 1) * sizeof(TCHAR));
  2539. }
  2540. }
  2541. RegCloseKey(hKeyRun);
  2542. }
  2543. return TRUE;
  2544. }
  2545. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2546. BOOL RegRegisterForScheduledTasks(BOOL fScheduled)
  2547. Summary: Register/unregister for scheduled tasks
  2548. so SENS knows whether to invoke us.
  2549. Returns: Returns TRUE if successful, FALSE otherwise
  2550. ------------------------------------------------------------------------F-F*/
  2551. STDAPI_(BOOL) RegRegisterForScheduledTasks(BOOL fScheduled)
  2552. {
  2553. HKEY hkeyAutoSync;
  2554. DWORD dwFlags = 0;
  2555. DWORD dwType = REG_DWORD;
  2556. DWORD dwDataSize = sizeof(DWORD);
  2557. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  2558. CMutexRegistry.Enter();
  2559. hkeyAutoSync = RegGetSyncTypeKey(SYNCTYPE_AUTOSYNC,KEY_WRITE | KEY_READ,TRUE);
  2560. if (NULL == hkeyAutoSync)
  2561. {
  2562. CMutexRegistry.Leave();
  2563. return FALSE;
  2564. }
  2565. RegQueryValueEx(hkeyAutoSync,TEXT("Flags"),NULL, &dwType,
  2566. (LPBYTE) &(dwFlags), &dwDataSize);
  2567. // Here we are setting schedsync only,
  2568. // so retain the registry settings for autosync and idle.
  2569. dwFlags &= AUTOSYNC_WAN_LOGON |
  2570. AUTOSYNC_WAN_LOGOFF |
  2571. AUTOSYNC_LAN_LOGON |
  2572. AUTOSYNC_LAN_LOGOFF |
  2573. AUTOSYNC_IDLE;
  2574. dwFlags |= (fScheduled? AUTOSYNC_SCHEDULED : 0);
  2575. RegSetValueEx(hkeyAutoSync,TEXT("Flags"),NULL, REG_DWORD,
  2576. (LPBYTE) &(dwFlags), sizeof(DWORD));
  2577. RegCloseKey(hkeyAutoSync);
  2578. CMutexRegistry.Leave();
  2579. return TRUE;
  2580. }
  2581. //+---------------------------------------------------------------------------
  2582. //
  2583. // Member: RegGetCombinedUserRegFlags, private
  2584. //
  2585. // Synopsis: Gets an or'ing together of user settings for setting up globals.
  2586. //
  2587. // Arguments: [dwSyncMgrRegisterFlags] - On Success gets set to flags
  2588. // on failure they are set to zero
  2589. //
  2590. // Returns: Appropriate status code
  2591. //
  2592. // Modifies:
  2593. //
  2594. // History: 24-Aug-98 rogerg Created.
  2595. //
  2596. //----------------------------------------------------------------------------
  2597. BOOL RegGetCombinedUserRegFlags(DWORD *pdwSyncMgrRegisterFlags)
  2598. {
  2599. HKEY hkey;
  2600. BOOL fResult = TRUE;
  2601. DWORD dwType = REG_DWORD ;
  2602. DWORD dwDataSize = sizeof(DWORD);
  2603. *pdwSyncMgrRegisterFlags = 0;
  2604. // update the AutoSync Key
  2605. hkey = RegGetSyncTypeKey(SYNCTYPE_AUTOSYNC,KEY_READ,FALSE);
  2606. if (hkey)
  2607. {
  2608. DWORD dwUserLogonLogoff;
  2609. if (ERROR_SUCCESS == RegQueryValueEx(hkey,TEXT("Logon"),NULL, &dwType,
  2610. (LPBYTE) &dwUserLogonLogoff,
  2611. &dwDataSize) )
  2612. {
  2613. *pdwSyncMgrRegisterFlags |= dwUserLogonLogoff ? SYNCMGRREGISTERFLAG_CONNECT : 0;
  2614. }
  2615. dwDataSize = sizeof(DWORD);
  2616. if (ERROR_SUCCESS == RegQueryValueEx(hkey,TEXT("Logoff"),NULL, &dwType,
  2617. (LPBYTE) &dwUserLogonLogoff,
  2618. &dwDataSize) )
  2619. {
  2620. *pdwSyncMgrRegisterFlags |= dwUserLogonLogoff ? SYNCMGRREGISTERFLAG_PENDINGDISCONNECT : 0;
  2621. }
  2622. RegCloseKey(hkey);
  2623. }
  2624. // update the Idle Key
  2625. hkey = RegGetSyncTypeKey(SYNCTYPE_IDLE,KEY_READ,FALSE);
  2626. if (hkey)
  2627. {
  2628. DWORD dwIdleEnabled;
  2629. dwDataSize = sizeof(DWORD);
  2630. if (ERROR_SUCCESS == RegQueryValueEx(hkey,TEXT("IdleEnabled"),NULL, &dwType,
  2631. (LPBYTE) &dwIdleEnabled,
  2632. &dwDataSize) )
  2633. {
  2634. *pdwSyncMgrRegisterFlags |= dwIdleEnabled ? SYNCMGRREGISTERFLAG_IDLE : 0;
  2635. }
  2636. RegCloseKey(hkey);
  2637. }
  2638. return TRUE; // always return true but don't set flags on error.
  2639. }
  2640. //+---------------------------------------------------------------------------
  2641. //
  2642. // Member: RegGetCombinedHandlerRegFlags, private
  2643. //
  2644. // Synopsis: Gets an or'ing together of handler registration Keys
  2645. //
  2646. // Arguments: [dwSyncMgrRegisterFlags] - On Success gets set to flags
  2647. // on failure they are set to zero
  2648. // [ft] - On Success filed with timestamp
  2649. //
  2650. // Returns: Appropriate status code
  2651. //
  2652. // Modifies:
  2653. //
  2654. // History: 24-Aug-98 rogerg Created.
  2655. //
  2656. //----------------------------------------------------------------------------
  2657. BOOL RegGetCombinedHandlerRegFlags(DWORD *pdwSyncMgrRegisterFlags,FILETIME *pft)
  2658. {
  2659. HKEY hkey;
  2660. BOOL fResult = TRUE;
  2661. DWORD dwType = REG_DWORD ;
  2662. DWORD dwDataSize = sizeof(DWORD);
  2663. *pdwSyncMgrRegisterFlags = 0;
  2664. hkey = RegGetHandlerTopLevelKey(KEY_READ);
  2665. if (hkey)
  2666. {
  2667. DWORD dwRegistrationFlags;
  2668. if (ERROR_SUCCESS == RegQueryValueEx(hkey,SZ_REGISTRATIONFLAGSKEY,NULL, &dwType,
  2669. (LPBYTE) &dwRegistrationFlags,
  2670. &dwDataSize) )
  2671. {
  2672. *pdwSyncMgrRegisterFlags = dwRegistrationFlags;
  2673. }
  2674. RegGetTimeStamp(hkey,pft);
  2675. RegCloseKey(hkey);
  2676. }
  2677. return TRUE; // always return true but don't set flags on error.
  2678. }
  2679. //+---------------------------------------------------------------------------
  2680. //
  2681. // Member: RegGetChangedHandlerFlags, private
  2682. //
  2683. // Synopsis: Gets an or'ing together of handler registration Keys
  2684. // that have changed since the given FILETIME
  2685. //
  2686. // Arguments: [pft] - Pointer to FileTime for Compare
  2687. // [pdwChangedFlags] - On Success filed with flags that channged
  2688. //
  2689. // Returns: TRUE if could gather flags.
  2690. //
  2691. // Modifies:
  2692. //
  2693. // History: 24-Aug-98 rogerg Created.
  2694. //
  2695. //----------------------------------------------------------------------------
  2696. BOOL RegGetChangedHandlerFlags(FILETIME *pft,DWORD *pdwHandlerChandedFlags)
  2697. {
  2698. HKEY hkeyHandler;
  2699. *pdwHandlerChandedFlags = 0;
  2700. hkeyHandler = RegGetHandlerTopLevelKey(KEY_READ);
  2701. if (hkeyHandler)
  2702. {
  2703. TCHAR lpName[MAX_PATH + 1];
  2704. DWORD cbName = MAX_PATH;
  2705. DWORD dwRegistrationFlags = 0;
  2706. FILETIME ftHandlerReg;
  2707. DWORD dwIndex = 0;
  2708. DWORD dwType = REG_DWORD ;
  2709. DWORD dwDataSize = sizeof(DWORD);
  2710. DWORD dwHandlerRegFlags;
  2711. LONG lRet;
  2712. HKEY hKeyClsid;
  2713. // enumerate the keys
  2714. while ( ERROR_SUCCESS == RegEnumKey(hkeyHandler,dwIndex,lpName,cbName) )
  2715. {
  2716. lRet = RegOpenKeyEx( hkeyHandler,
  2717. lpName,
  2718. NULL,
  2719. KEY_READ,
  2720. &hKeyClsid );
  2721. if (ERROR_SUCCESS == lRet)
  2722. {
  2723. RegGetTimeStamp(hKeyClsid,&ftHandlerReg);
  2724. // handler reg is new time than our gvien time add it to the flags.
  2725. if (CompareFileTime(pft,&ftHandlerReg) < 0)
  2726. {
  2727. if (ERROR_SUCCESS == RegQueryValueEx(hKeyClsid,SZ_REGISTRATIONFLAGSKEY,NULL, &dwType,
  2728. (LPBYTE) &dwHandlerRegFlags,
  2729. &dwDataSize) )
  2730. {
  2731. dwRegistrationFlags |= dwHandlerRegFlags;
  2732. }
  2733. }
  2734. RegCloseKey(hKeyClsid);
  2735. }
  2736. dwIndex++;
  2737. }
  2738. *pdwHandlerChandedFlags = dwRegistrationFlags;
  2739. RegCloseKey(hkeyHandler);
  2740. }
  2741. return TRUE;
  2742. }
  2743. //+---------------------------------------------------------------------------
  2744. //
  2745. // Member: RegRegisterForEvents, private
  2746. //
  2747. // Synopsis: Registers/UnRegisters for appropriate SENS and WinLogon Events.
  2748. // and any other per machine registration we need to do
  2749. //
  2750. // Arguments: [fUninstall] - set to true by uninstall to force us to unregister
  2751. // regardless of current machine state.
  2752. //
  2753. // Returns: Appropriate status code
  2754. //
  2755. // Modifies:
  2756. //
  2757. // History: 05-Nov-97 rogerg Created.
  2758. //
  2759. //----------------------------------------------------------------------------
  2760. // !!!Warning - Assumes toplevel key information is up to date.
  2761. STDAPI RegRegisterForEvents(BOOL fUninstall)
  2762. {
  2763. HRESULT hr = NOERROR;
  2764. BOOL fLogon = FALSE;
  2765. BOOL fLogoff = FALSE;
  2766. BOOL fIdle = FALSE;
  2767. #ifdef _SENS
  2768. IEventSystem *pEventSystem;
  2769. #endif // _SENS
  2770. CCriticalSection cCritSect(&g_DllCriticalSection,GetCurrentThreadId());
  2771. cCritSect.Enter();
  2772. if (!fUninstall)
  2773. {
  2774. FILETIME ftHandlerReg;
  2775. DWORD dwSyncMgrUsersRegisterFlags; // or'ing of all Users settings
  2776. DWORD dwSyncMgrHandlerRegisterFlags; // or'ing of all handler settings.
  2777. DWORD dwCombinedFlags; // or together user and handler.
  2778. // if not an uninstall determine the true machine state
  2779. // if Logon set for handler or user set or if handler
  2780. // wants an idle set.
  2781. // If Logoff set we register for Logoff.
  2782. RegGetCombinedUserRegFlags(&dwSyncMgrUsersRegisterFlags);
  2783. RegGetCombinedHandlerRegFlags(&dwSyncMgrHandlerRegisterFlags,&ftHandlerReg);
  2784. dwCombinedFlags = dwSyncMgrUsersRegisterFlags | dwSyncMgrHandlerRegisterFlags;
  2785. if ( (dwCombinedFlags & SYNCMGRREGISTERFLAG_CONNECT)
  2786. || (dwSyncMgrHandlerRegisterFlags & SYNCMGRREGISTERFLAG_IDLE) )
  2787. {
  2788. fLogon = TRUE;
  2789. }
  2790. if ( (dwCombinedFlags & SYNCMGRREGISTERFLAG_PENDINGDISCONNECT) )
  2791. {
  2792. fLogoff = TRUE;
  2793. }
  2794. }
  2795. // update Registry entries for SENS to lookup
  2796. RegWriteEvents(fLogon,fLogoff);
  2797. #ifdef _SENS
  2798. // we were able to load ole automation so reg/unreg with the event system.
  2799. hr = CoCreateInstance(CLSID_CEventSystem,NULL,CLSCTX_SERVER,IID_IEventSystem,
  2800. (LPVOID *) &pEventSystem);
  2801. if (SUCCEEDED(hr))
  2802. {
  2803. IEventSubscription *pIEventSubscription;
  2804. WCHAR szGuid[GUID_SIZE+1];
  2805. BSTR bstrSubscriberID = NULL;
  2806. BSTR bstrPROGID_EventSubscription = NULL;
  2807. bstrPROGID_EventSubscription = SysAllocString(PROGID_EventSubscription);
  2808. StringFromGUID2(GUID_SENSSUBSCRIBER_SYNCMGRP,szGuid, GUID_SIZE);
  2809. bstrSubscriberID = SysAllocString(szGuid);
  2810. if (bstrSubscriberID && bstrPROGID_EventSubscription)
  2811. {
  2812. // register for RasConnect
  2813. hr = CoCreateInstance(
  2814. CLSID_CEventSubscription,
  2815. NULL,
  2816. CLSCTX_SERVER,
  2817. IID_IEventSubscription,
  2818. (LPVOID *) &pIEventSubscription
  2819. );
  2820. }
  2821. else
  2822. {
  2823. hr = E_OUTOFMEMORY;
  2824. }
  2825. if (SUCCEEDED(hr))
  2826. {
  2827. BSTR bstrPublisherID = NULL;
  2828. BSTR bstrSubscriptionID = NULL;
  2829. BSTR bstrSubscriptionName = NULL;
  2830. BSTR bstrSubscriberCLSID = NULL;
  2831. BSTR bstrEventID = NULL;
  2832. BSTR bstrEventClassID = NULL;
  2833. BSTR bstrIID = NULL;
  2834. // if there are any events, register with ens for messages.
  2835. if (fLogon)
  2836. {
  2837. StringFromGUID2(GUID_SENSLOGONSUBSCRIPTION_SYNCMGRP,szGuid, GUID_SIZE);
  2838. bstrSubscriptionID = SysAllocString(szGuid);
  2839. if (bstrSubscriptionID && SUCCEEDED(hr))
  2840. {
  2841. hr = pIEventSubscription->put_SubscriptionID(bstrSubscriptionID);
  2842. }
  2843. StringFromGUID2(CLSID_SyncMgrp,szGuid, GUID_SIZE);
  2844. bstrSubscriberCLSID = SysAllocString(szGuid);
  2845. if (bstrSubscriberCLSID && SUCCEEDED(hr))
  2846. {
  2847. hr = pIEventSubscription->put_SubscriberCLSID(bstrSubscriberCLSID);
  2848. }
  2849. StringFromGUID2(SENSGUID_PUBLISHER,szGuid, GUID_SIZE);
  2850. bstrPublisherID = SysAllocString(szGuid);
  2851. if (bstrPublisherID && SUCCEEDED(hr))
  2852. {
  2853. hr = pIEventSubscription->put_PublisherID(bstrPublisherID);
  2854. }
  2855. bstrSubscriptionName = SysAllocString(SZ_SYNCMGRNAME);
  2856. if (bstrSubscriptionName && SUCCEEDED(hr))
  2857. {
  2858. hr = pIEventSubscription->put_SubscriptionName(bstrSubscriptionName);
  2859. }
  2860. bstrEventID = SysAllocString(L"ConnectionMade");
  2861. if (bstrEventID && SUCCEEDED(hr))
  2862. {
  2863. hr = pIEventSubscription->put_MethodName(bstrEventID);
  2864. }
  2865. StringFromGUID2(SENSGUID_EVENTCLASS_NETWORK,szGuid,GUID_SIZE);
  2866. bstrEventClassID = SysAllocString(szGuid);
  2867. if (bstrEventClassID && SUCCEEDED(hr))
  2868. {
  2869. hr = pIEventSubscription->put_EventClassID(bstrEventClassID);
  2870. }
  2871. // set this up for roaming
  2872. if (SUCCEEDED(hr))
  2873. {
  2874. // hr = pIEventSubscription->put_PerUser(TRUE); // don't register PerUser for Nw
  2875. }
  2876. StringFromGUID2(IID_ISensNetwork,szGuid,GUID_SIZE);
  2877. bstrIID = SysAllocString(szGuid);
  2878. if (bstrIID && SUCCEEDED(hr))
  2879. {
  2880. hr = pIEventSubscription->put_InterfaceID(bstrIID);
  2881. }
  2882. if (SUCCEEDED(hr))
  2883. {
  2884. hr = pEventSystem->Store(bstrPROGID_EventSubscription,pIEventSubscription);
  2885. }
  2886. if (bstrIID)
  2887. {
  2888. SysFreeString(bstrIID);
  2889. }
  2890. if (bstrPublisherID)
  2891. SysFreeString(bstrPublisherID);
  2892. if (bstrSubscriberCLSID)
  2893. SysFreeString(bstrSubscriberCLSID);
  2894. if (bstrEventClassID)
  2895. SysFreeString(bstrEventClassID);
  2896. if (bstrEventID)
  2897. SysFreeString(bstrEventID);
  2898. if (bstrSubscriptionID)
  2899. SysFreeString(bstrSubscriptionID);
  2900. if (bstrSubscriptionName)
  2901. SysFreeString(bstrSubscriptionName);
  2902. }
  2903. else // don't need to be registered, remove.
  2904. {
  2905. if (NOERROR == hr)
  2906. {
  2907. int errorIndex;
  2908. bstrSubscriptionID = SysAllocString(L"SubscriptionID={6295df30-35ee-11d1-8707-00C04FD93327}");
  2909. if (bstrSubscriptionID)
  2910. {
  2911. hr = pEventSystem->Remove(bstrPROGID_EventSubscription,bstrSubscriptionID /* QUERY */,&errorIndex);
  2912. SysFreeString(bstrSubscriptionID);
  2913. }
  2914. }
  2915. }
  2916. pIEventSubscription->Release();
  2917. }
  2918. if (bstrSubscriberID)
  2919. {
  2920. SysFreeString(bstrSubscriberID);
  2921. }
  2922. if (bstrPROGID_EventSubscription)
  2923. {
  2924. SysFreeString(bstrPROGID_EventSubscription);
  2925. }
  2926. pEventSystem->Release();
  2927. }
  2928. #endif // _SENS
  2929. cCritSect.Leave();
  2930. return hr;
  2931. }
  2932. // helper functions for handler registration
  2933. STDAPI_(BOOL) RegGetTimeStamp(HKEY hKey, FILETIME *pft)
  2934. {
  2935. DWORD dwType;
  2936. FILETIME ft;
  2937. LONG lr;
  2938. DWORD dwSize = sizeof(FILETIME);
  2939. Assert(pft);
  2940. lr = RegQueryValueEx( hKey,
  2941. SZ_REGISTRATIONTIMESTAMPKEY,
  2942. NULL,
  2943. &dwType,
  2944. (BYTE *)&ft,
  2945. &dwSize );
  2946. if ( lr == ERROR_SUCCESS )
  2947. {
  2948. Assert( dwSize == sizeof(FILETIME) && dwType == REG_BINARY );
  2949. *pft = ft;
  2950. }
  2951. else
  2952. {
  2953. // set the filetime to way back when to
  2954. // any compares will just say older instead
  2955. // of having to check success code
  2956. (*pft).dwLowDateTime = 0;
  2957. (*pft).dwHighDateTime = 0;
  2958. }
  2959. return TRUE;
  2960. }
  2961. STDAPI_(BOOL) RegWriteTimeStamp(HKEY hkey)
  2962. {
  2963. SYSTEMTIME sysTime;
  2964. FILETIME ft;
  2965. LONG lr = -1;
  2966. GetSystemTime(&sysTime); // void can't check for errors
  2967. if (SystemTimeToFileTime(&sysTime,&ft) )
  2968. {
  2969. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  2970. CMutexRegistry.Enter();
  2971. // write out the UpdateTime
  2972. lr = RegSetValueEx( hkey,
  2973. SZ_REGISTRATIONTIMESTAMPKEY,
  2974. NULL,
  2975. REG_BINARY,
  2976. (BYTE *)&ft,
  2977. sizeof(ft) );
  2978. CMutexRegistry.Leave();
  2979. }
  2980. return (ERROR_SUCCESS == lr) ? TRUE : FALSE;
  2981. }
  2982. //+---------------------------------------------------------------------------
  2983. //
  2984. // Function: UpdateHandlerKeyInformation
  2985. //
  2986. // Synopsis: Updates the top-level handler key information
  2987. //
  2988. // Arguments:
  2989. //
  2990. // Returns: void
  2991. //
  2992. // Modifies: enumerates the handlers underneath the given key
  2993. // updating the registrationFlags which is an || or
  2994. // all registered handler flags and then updates the
  2995. // timestamp on this key
  2996. //
  2997. // History: 05-Nov-97 rogerg Created.
  2998. //
  2999. //----------------------------------------------------------------------------
  3000. void UpdateHandlerKeyInformation(HKEY hKeyHandler)
  3001. {
  3002. DWORD dwSyncMgrTopLevelRegisterFlags = 0;
  3003. DWORD dwIndex = 0;
  3004. TCHAR lpName[MAX_PATH];
  3005. DWORD cbName = MAX_PATH;
  3006. while ( ERROR_SUCCESS == RegEnumKey(hKeyHandler,dwIndex,
  3007. lpName,cbName) )
  3008. {
  3009. DWORD dwType = REG_DWORD ;
  3010. DWORD dwDataSize = sizeof(DWORD);
  3011. DWORD dwHandlerRegFlags;
  3012. LONG lRet;
  3013. HKEY hKeyClsid;
  3014. lRet = RegOpenKeyEx( hKeyHandler,
  3015. lpName,
  3016. NULL,
  3017. KEY_READ,
  3018. &hKeyClsid );
  3019. if (ERROR_SUCCESS == lRet)
  3020. {
  3021. if (ERROR_SUCCESS == RegQueryValueEx(hKeyClsid,SZ_REGISTRATIONFLAGSKEY,NULL, &dwType,
  3022. (LPBYTE) &dwHandlerRegFlags,
  3023. &dwDataSize) )
  3024. {
  3025. dwSyncMgrTopLevelRegisterFlags |= dwHandlerRegFlags;
  3026. }
  3027. RegCloseKey(hKeyClsid);
  3028. }
  3029. dwIndex++;
  3030. }
  3031. // not much we can do if RegFlags are messed up other than assert and mask out
  3032. Assert(dwSyncMgrTopLevelRegisterFlags <= SYNCMGRREGISTERFLAGS_MASK);
  3033. dwSyncMgrTopLevelRegisterFlags &= SYNCMGRREGISTERFLAGS_MASK;
  3034. // write out new flags even if errors occured. work thing that happens is
  3035. // we don't set up someones autosync automatically.
  3036. RegSetValueEx(hKeyHandler,SZ_REGISTRATIONFLAGSKEY,NULL, REG_DWORD,
  3037. (LPBYTE) &(dwSyncMgrTopLevelRegisterFlags), sizeof(DWORD));
  3038. RegWriteTimeStamp(hKeyHandler);
  3039. }
  3040. //+---------------------------------------------------------------------------
  3041. //
  3042. // Function: RegUpdateTopLevelKeys
  3043. //
  3044. // Synopsis: Looks at toplevel AutoSync,Idle, etc. keys and determines
  3045. // if they need to be updated and if so goes for it.
  3046. //
  3047. // Arguments:
  3048. //
  3049. // Returns: Appropriate status code
  3050. //
  3051. // Modifies: set pfFirstRegistration out param to true if this is
  3052. // the first handler that has registered so we can setup defaults.
  3053. //
  3054. // History: 24-Aug-98 rogerg Created.
  3055. //
  3056. //----------------------------------------------------------------------------
  3057. STDAPI_(void) RegUpdateTopLevelKeys()
  3058. {
  3059. HKEY hkey;
  3060. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  3061. CMutexRegistry.Enter();
  3062. // update the AutoSync Key
  3063. hkey = RegGetSyncTypeKey(SYNCTYPE_AUTOSYNC,KEY_READ | KEY_WRITE,TRUE);
  3064. if (hkey)
  3065. {
  3066. DWORD dwType = REG_DWORD ;
  3067. DWORD dwDataSize = sizeof(DWORD);
  3068. DWORD dwUserLogonLogoff;
  3069. // see if has a logon value and if it is either newly created or
  3070. // old format. Call Update to setthings up
  3071. if (ERROR_SUCCESS != RegQueryValueEx(hkey,TEXT("Logon"),NULL, &dwType,
  3072. (LPBYTE) &dwUserLogonLogoff,
  3073. &dwDataSize) )
  3074. {
  3075. RegUpdateAutoSyncKeyValue(hkey,-1,-1);
  3076. }
  3077. RegCloseKey(hkey);
  3078. }
  3079. // update the Idle Key
  3080. hkey = RegGetSyncTypeKey(SYNCTYPE_IDLE,KEY_READ | KEY_WRITE,TRUE);
  3081. if (hkey)
  3082. {
  3083. DWORD dwType = REG_DWORD ;
  3084. DWORD dwDataSize = sizeof(DWORD);
  3085. DWORD dwIdleEnabled;
  3086. // see if has a Idle value and if it is either newly created or
  3087. // old format. Call Update to setthings up
  3088. if (ERROR_SUCCESS != RegQueryValueEx(hkey,TEXT("IdleEnabled"),NULL, &dwType,
  3089. (LPBYTE) &dwIdleEnabled,
  3090. &dwDataSize) )
  3091. {
  3092. RegUpdateIdleKeyValue(hkey,-1);
  3093. }
  3094. RegCloseKey(hkey);
  3095. }
  3096. CMutexRegistry.Leave();
  3097. }
  3098. //+---------------------------------------------------------------------------
  3099. //
  3100. // Function: RegRegisterHandler
  3101. //
  3102. // Synopsis: Registers Handlers with SyncMgr.
  3103. //
  3104. // Arguments:
  3105. //
  3106. // Returns: Appropriate status code
  3107. //
  3108. // Modifies: set pfFirstRegistration out param to true if this is
  3109. // the first handler that has registered so we can setup defaults.
  3110. //
  3111. // History: 05-Nov-97 rogerg Created.
  3112. //
  3113. //----------------------------------------------------------------------------
  3114. STDAPI_(BOOL) RegRegisterHandler(REFCLSID rclsidHandler,
  3115. WCHAR const* pwszDescription,
  3116. DWORD dwSyncMgrRegisterFlags,
  3117. BOOL *pfFirstRegistration)
  3118. {
  3119. LONG lRet;
  3120. RegUpdateTopLevelKeys(); // make sure other top-level keys are up to date.
  3121. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  3122. CMutexRegistry.Enter();
  3123. *pfFirstRegistration = FALSE;
  3124. HKEY hKeyHandler;
  3125. hKeyHandler = RegGetHandlerTopLevelKey(KEY_READ | KEY_WRITE);
  3126. if (NULL == hKeyHandler)
  3127. {
  3128. CMutexRegistry.Leave();
  3129. return FALSE;
  3130. }
  3131. XRegKey xRegKeyHandler( hKeyHandler );
  3132. //
  3133. // Check if this is the first handler being registerd
  3134. //
  3135. TCHAR szGuid[GUID_SIZE+1];
  3136. DWORD cbGuid = GUID_SIZE+1;
  3137. lRet = RegEnumKeyEx( hKeyHandler,
  3138. 0,
  3139. szGuid,
  3140. &cbGuid,
  3141. NULL,
  3142. NULL,
  3143. NULL,
  3144. NULL );
  3145. if ( lRet != ERROR_SUCCESS )
  3146. *pfFirstRegistration = TRUE;
  3147. //
  3148. // Convert guid and description to TCHAR
  3149. //
  3150. TCHAR *pszDesc;
  3151. BOOL fOk = FALSE;
  3152. StringFromGUID2( rclsidHandler, szGuid, GUID_SIZE+1 );
  3153. pszDesc = (TCHAR *)pwszDescription;
  3154. // write out the registration flags. If fail go ahead
  3155. // and succed registration anyways.
  3156. if (hKeyHandler)
  3157. {
  3158. HKEY hKeyClsid;
  3159. hKeyClsid = RegGetHandlerKey(hKeyHandler,szGuid,KEY_WRITE | KEY_READ,TRUE);
  3160. if (hKeyClsid)
  3161. {
  3162. fOk = TRUE; // if make handle key say registered okay
  3163. if (pszDesc)
  3164. {
  3165. RegSetValueEx(hKeyClsid,NULL,NULL, REG_SZ,
  3166. (LPBYTE) pszDesc,
  3167. (lstrlen(pszDesc) +1)*sizeof(TCHAR));
  3168. }
  3169. RegSetValueEx(hKeyClsid,SZ_REGISTRATIONFLAGSKEY,NULL, REG_DWORD,
  3170. (LPBYTE) &(dwSyncMgrRegisterFlags), sizeof(DWORD));
  3171. // update the TimeStamp on the handler clsid
  3172. RegWriteTimeStamp(hKeyClsid);
  3173. RegCloseKey( hKeyClsid );
  3174. // update the toplevel key
  3175. UpdateHandlerKeyInformation(hKeyHandler);
  3176. }
  3177. }
  3178. // update the user information.
  3179. RegSetUserDefaults();
  3180. RegRegisterForEvents(FALSE /* fUninstall */);
  3181. CMutexRegistry.Leave();
  3182. return fOk;
  3183. }
  3184. //+---------------------------------------------------------------------------
  3185. //
  3186. // Function: RegRegRemoveHandler
  3187. //
  3188. // Synopsis: UnRegisters Handlers with SyncMgr.
  3189. //
  3190. // Arguments:
  3191. //
  3192. // Returns: Appropriate status code
  3193. //
  3194. // Modifies: set pfAllHandlerUnRegistered out param to true if this is
  3195. // the last handler that needs to be unregistered before
  3196. // turning off our defaults..
  3197. //
  3198. // History: 05-Nov-97 rogerg Created.
  3199. //
  3200. //----------------------------------------------------------------------------
  3201. STDAPI_(BOOL) RegRegRemoveHandler(REFCLSID rclsidHandler)
  3202. {
  3203. HKEY hKeyHandler;
  3204. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  3205. CMutexRegistry.Enter();
  3206. hKeyHandler = RegGetHandlerTopLevelKey(KEY_WRITE | KEY_READ);
  3207. if (NULL == hKeyHandler)
  3208. {
  3209. //
  3210. // Non-existent key, so nothing to remove
  3211. //
  3212. CMutexRegistry.Leave();
  3213. return TRUE;
  3214. }
  3215. XRegKey xKeyHandler( hKeyHandler );
  3216. TCHAR szGuid[GUID_SIZE+1];
  3217. #ifdef _UNICODE
  3218. StringFromGUID2( rclsidHandler, szGuid, GUID_SIZE+1 );
  3219. #else
  3220. WCHAR wszGuid[GUID_SIZE+1];
  3221. StringFromGUID2( rclsidHandler, wszGuid, GUID_SIZE+1 );
  3222. BOOL fOk = ConvertString( szGuid, wszGuid, GUID_SIZE+1 );
  3223. Assert( fOk );
  3224. #endif
  3225. HKEY hKeyClsid;
  3226. hKeyClsid = RegGetHandlerKey(hKeyHandler,szGuid,KEY_WRITE | KEY_READ,FALSE);
  3227. if (hKeyClsid)
  3228. {
  3229. RegCloseKey( hKeyClsid );
  3230. RegDeleteKey( hKeyHandler, szGuid );
  3231. // update the toplevel key
  3232. UpdateHandlerKeyInformation(hKeyHandler);
  3233. }
  3234. else
  3235. {
  3236. //
  3237. // Non-existent key, so nothing to remove
  3238. //
  3239. }
  3240. RegRegisterForEvents(FALSE /* fUninstall */); // UPDATE EVENT REGISTRATION.
  3241. CMutexRegistry.Leave();
  3242. return TRUE;
  3243. }
  3244. //+---------------------------------------------------------------------------
  3245. //
  3246. // Function: RegGetHandlerRegistrationInfo
  3247. //
  3248. // Synopsis: Gets Information of the specified handler.
  3249. //
  3250. // Arguments:
  3251. //
  3252. // Returns: Appropriate status code
  3253. //
  3254. // Modifies: pdwSyncMgrRegisterFlags
  3255. //
  3256. // History: 20-Aug-98 rogerg Created.
  3257. //
  3258. //----------------------------------------------------------------------------
  3259. STDAPI_(BOOL) RegGetHandlerRegistrationInfo(REFCLSID rclsidHandler,LPDWORD pdwSyncMgrRegisterFlags)
  3260. {
  3261. HKEY hKeyHandler;
  3262. *pdwSyncMgrRegisterFlags = 0;
  3263. hKeyHandler = RegGetHandlerTopLevelKey(KEY_READ);
  3264. if (NULL == hKeyHandler)
  3265. {
  3266. //
  3267. // Non-existent key
  3268. //
  3269. return FALSE;
  3270. }
  3271. XRegKey xKeyHandler( hKeyHandler );
  3272. TCHAR szGuid[GUID_SIZE+1];
  3273. #ifdef _UNICODE
  3274. StringFromGUID2( rclsidHandler, szGuid, GUID_SIZE+1 );
  3275. #else
  3276. WCHAR wszGuid[GUID_SIZE+1];
  3277. StringFromGUID2( rclsidHandler, wszGuid, GUID_SIZE+1 );
  3278. BOOL fOk = ConvertString( szGuid, wszGuid, GUID_SIZE+1 );
  3279. Assert( fOk );
  3280. #endif
  3281. HKEY hKeyClsid;
  3282. BOOL fResult = FALSE;
  3283. hKeyClsid = RegGetHandlerKey(hKeyHandler,szGuid,KEY_READ,FALSE);
  3284. if (hKeyClsid)
  3285. {
  3286. DWORD dwType = REG_DWORD ;
  3287. DWORD dwDataSize = sizeof(DWORD);
  3288. LONG lRet;
  3289. lRet = RegQueryValueEx(hKeyClsid,SZ_REGISTRATIONFLAGSKEY,NULL, &dwType,
  3290. (LPBYTE) pdwSyncMgrRegisterFlags,
  3291. &dwDataSize);
  3292. RegCloseKey( hKeyClsid );
  3293. fResult = (ERROR_SUCCESS == lRet) ? TRUE : FALSE;
  3294. }
  3295. else
  3296. {
  3297. //
  3298. // Non-existent key, so nothing to remove
  3299. //
  3300. }
  3301. return fResult;
  3302. }
  3303. //+---------------------------------------------------------------------------
  3304. //
  3305. // Function: RegSetUserDefaults
  3306. //
  3307. // Synopsis: Registers default values for auto and idle sync
  3308. //
  3309. // Setup based on Handler and UserPreferences
  3310. //
  3311. // History: 20-May-98 SitaramR Created
  3312. //
  3313. //----------------------------------------------------------------------------
  3314. STDAPI_(void) RegSetUserDefaults()
  3315. {
  3316. HKEY hKeyUser = NULL;
  3317. BOOL fLogon = FALSE;
  3318. BOOL fLogoff = FALSE;
  3319. FILETIME ftHandlerReg;
  3320. DWORD dwHandlerRegistrationFlags;
  3321. CMutex CMutexRegistry(NULL, FALSE,SZ_REGSITRYMUTEXNAME);
  3322. CMutexRegistry.Enter();
  3323. // get the combined handler registration toplevel flags and timeStamp
  3324. // to see if should bother enumerating the rest.
  3325. if (!RegGetCombinedHandlerRegFlags(&dwHandlerRegistrationFlags,&ftHandlerReg))
  3326. {
  3327. CMutexRegistry.Leave();
  3328. return;
  3329. }
  3330. if (0 != (dwHandlerRegistrationFlags &
  3331. (SYNCMGRREGISTERFLAG_CONNECT | SYNCMGRREGISTERFLAG_PENDINGDISCONNECT) ) )
  3332. {
  3333. // See if AutoSync key needs to be updated
  3334. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_AUTOSYNC,KEY_WRITE | KEY_READ,TRUE);
  3335. if (hKeyUser)
  3336. {
  3337. FILETIME ftUserAutoSync;
  3338. // if got the User get the timestamp and see if it is older than the handlers
  3339. // If this is a new user filetime will be 0
  3340. RegGetTimeStamp(hKeyUser,&ftUserAutoSync);
  3341. if (CompareFileTime(&ftUserAutoSync,&ftHandlerReg) < 0)
  3342. {
  3343. DWORD dwHandlerChangedFlags;
  3344. // need to walk through handlers and update what we need to set based
  3345. // on each handlers timestamp since we don't want a handler that registered
  3346. // for idle to cause us to turn AutoSync back on and vis-a-versa
  3347. if (RegGetChangedHandlerFlags(&ftUserAutoSync,&dwHandlerChangedFlags))
  3348. {
  3349. BOOL fLogon = (dwHandlerChangedFlags & SYNCMGRREGISTERFLAG_CONNECT) ? TRUE : FALSE;
  3350. BOOL fLogoff = (dwHandlerChangedFlags & SYNCMGRREGISTERFLAG_PENDINGDISCONNECT) ? TRUE : FALSE;
  3351. RegSetAutoSyncDefaults(fLogon,fLogoff);
  3352. }
  3353. }
  3354. RegCloseKey(hKeyUser);
  3355. hKeyUser = NULL;
  3356. }
  3357. }
  3358. if (0 != (dwHandlerRegistrationFlags & SYNCMGRREGISTERFLAG_IDLE ) )
  3359. {
  3360. // now check for Idle same logic as above could probably combine
  3361. // into a function
  3362. // See if AutoSync key needs to be updated
  3363. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_IDLE, KEY_WRITE | KEY_READ,TRUE);
  3364. if (hKeyUser)
  3365. {
  3366. FILETIME ftUserIdleSync;
  3367. // if got the User get the timestamp and see if it is older than the handlers
  3368. // If this is a new user filetime will be 0
  3369. RegGetTimeStamp(hKeyUser,&ftUserIdleSync);
  3370. if (CompareFileTime(&ftUserIdleSync,&ftHandlerReg) < 0)
  3371. {
  3372. DWORD dwHandlerChangedFlags;
  3373. // need to walk through handlers and update what we need to set based
  3374. // on each handlers timestamp since we don't want a handler that registered
  3375. // for AutoSync to cause us to turn Idle back on and vis-a-versa
  3376. if (RegGetChangedHandlerFlags(&ftUserIdleSync,&dwHandlerChangedFlags))
  3377. {
  3378. if (dwHandlerChangedFlags & SYNCMGRREGISTERFLAG_IDLE)
  3379. {
  3380. RegSetIdleSyncDefaults(TRUE);
  3381. }
  3382. }
  3383. }
  3384. RegCloseKey(hKeyUser);
  3385. hKeyUser = NULL;
  3386. }
  3387. }
  3388. CMutexRegistry.Leave();
  3389. }
  3390. //+---------------------------------------------------------------------------
  3391. //
  3392. // Function: RegSetAutoSyncDefaults
  3393. //
  3394. // Synopsis: Registers default values for auto sync
  3395. //
  3396. // History: 20-May-98 SitaramR Created
  3397. //
  3398. //----------------------------------------------------------------------------
  3399. STDAPI_(void) RegSetAutoSyncDefaults(BOOL fLogon,BOOL fLogoff)
  3400. {
  3401. CONNECTIONSETTINGS *pConnection = (LPCONNECTIONSETTINGS)
  3402. ALLOC(sizeof(CONNECTIONSETTINGS));
  3403. if ( pConnection == 0 )
  3404. return;
  3405. XPtr<CONNECTIONSETTINGS> xConnection( pConnection );
  3406. INT iRet = LoadString(g_hmodThisDll,
  3407. IDS_LAN_CONNECTION,
  3408. pConnection->pszConnectionName,
  3409. ARRAYLEN(pConnection->pszConnectionName) );
  3410. Assert( iRet != 0 );
  3411. // -1 values are ignored by RegSetAutoSyncSettings.
  3412. // if not turning on leave the User Preferences alone,
  3413. pConnection->dwConnType = 0;
  3414. pConnection->dwLogon = fLogon ? TRUE : -1;
  3415. pConnection->dwLogoff = fLogoff ? TRUE : -1;
  3416. pConnection->dwPromptMeFirst = -1;
  3417. pConnection->dwMakeConnection = -1;
  3418. pConnection->dwIdleEnabled = -1;
  3419. // since this bases settings on what is already set no need to
  3420. // do a cleanreg or update the machine state
  3421. RegSetAutoSyncSettings(pConnection, 1, 0,
  3422. FALSE /* fCleanReg */,
  3423. FALSE /* fSetMachineState */,
  3424. FALSE /* fPerUser */);
  3425. }
  3426. //+---------------------------------------------------------------------------
  3427. //
  3428. // Function: RegSetUserAutoSyncDefaults
  3429. //
  3430. // Synopsis: Registers user default values for auto sync
  3431. //
  3432. // History: 39-March-99 rogerg Created
  3433. //
  3434. //----------------------------------------------------------------------------
  3435. STDAPI RegSetUserAutoSyncDefaults(DWORD dwSyncMgrRegisterMask,
  3436. DWORD dwSyncMgrRegisterFlags)
  3437. {
  3438. // if not changing either logon or logoff just return
  3439. if (!(dwSyncMgrRegisterMask & SYNCMGRREGISTERFLAG_CONNECT)
  3440. && !(dwSyncMgrRegisterMask & SYNCMGRREGISTERFLAG_PENDINGDISCONNECT) )
  3441. {
  3442. return NOERROR;
  3443. }
  3444. CONNECTIONSETTINGS *pConnection = (LPCONNECTIONSETTINGS)
  3445. ALLOC(sizeof(CONNECTIONSETTINGS));
  3446. if ( pConnection == 0 )
  3447. return E_OUTOFMEMORY;
  3448. XPtr<CONNECTIONSETTINGS> xConnection( pConnection );
  3449. INT iRet = LoadString(g_hmodThisDll,
  3450. IDS_LAN_CONNECTION,
  3451. pConnection->pszConnectionName,
  3452. ARRAYLEN(pConnection->pszConnectionName) );
  3453. Assert( iRet != 0 );
  3454. // -1 values are ignored by RegSetAutoSyncSettings.
  3455. // if not turning on leave the User Preferences alone,
  3456. pConnection->dwConnType = 0;
  3457. pConnection->dwLogon = -1;
  3458. pConnection->dwLogoff = -1;
  3459. pConnection->dwPromptMeFirst = -1;
  3460. pConnection->dwMakeConnection = -1;
  3461. pConnection->dwIdleEnabled = -1;
  3462. if (dwSyncMgrRegisterMask & SYNCMGRREGISTERFLAG_CONNECT)
  3463. {
  3464. pConnection->dwLogon = (dwSyncMgrRegisterFlags & SYNCMGRREGISTERFLAG_CONNECT)
  3465. ? TRUE : FALSE;
  3466. }
  3467. if (dwSyncMgrRegisterMask & SYNCMGRREGISTERFLAG_PENDINGDISCONNECT)
  3468. {
  3469. pConnection->dwLogoff = (dwSyncMgrRegisterFlags & SYNCMGRREGISTERFLAG_PENDINGDISCONNECT)
  3470. ? TRUE : FALSE;
  3471. }
  3472. // since this bases settings on what is already set no need to
  3473. // do a cleanreg or update the machine state
  3474. RegSetAutoSyncSettings(pConnection, 1, 0,
  3475. FALSE /* fCleanReg */,
  3476. TRUE /* fSetMachineState */,
  3477. TRUE /* fPerUser */);
  3478. return NOERROR;
  3479. }
  3480. //+---------------------------------------------------------------------------
  3481. //
  3482. // Function: RegSetIdleSyncDefaults
  3483. //
  3484. // Synopsis: Registers default values for idle sync
  3485. //
  3486. // History: 20-May-98 SitaramR Created
  3487. //
  3488. //----------------------------------------------------------------------------
  3489. STDAPI_(void) RegSetIdleSyncDefaults(BOOL fIdle)
  3490. {
  3491. Assert(fIdle); // for now this should only be called when true;
  3492. if (!fIdle)
  3493. {
  3494. return;
  3495. }
  3496. CONNECTIONSETTINGS *pConnection = (LPCONNECTIONSETTINGS)
  3497. ALLOC(sizeof(CONNECTIONSETTINGS));
  3498. if ( pConnection == 0 )
  3499. return;
  3500. XPtr<CONNECTIONSETTINGS> xConnection( pConnection );
  3501. INT iRet = LoadString(g_hmodThisDll,
  3502. IDS_LAN_CONNECTION,
  3503. pConnection->pszConnectionName,
  3504. ARRAYLEN(pConnection->pszConnectionName) );
  3505. Assert( iRet != 0 );
  3506. pConnection->dwConnType = 0;
  3507. pConnection->dwLogon = -1;
  3508. pConnection->dwLogoff = -1;
  3509. pConnection->dwPromptMeFirst = -1;
  3510. pConnection->dwMakeConnection = -1;
  3511. pConnection->dwIdleEnabled = TRUE;
  3512. // set all userLevel items to -1 so user gets the defaults if new
  3513. // but keep their settings if have already tweaked them.
  3514. pConnection->ulIdleRetryMinutes = -1;
  3515. pConnection->ulDelayIdleShutDownTime = -1;
  3516. pConnection->dwRepeatSynchronization = -1;
  3517. pConnection->ulIdleWaitMinutes = -1;
  3518. pConnection->dwRunOnBatteries = -1;
  3519. RegSetIdleSyncSettings(pConnection, 1, 0,
  3520. FALSE /* fCleanReg */,
  3521. FALSE /* fPerUser */);
  3522. }
  3523. //+---------------------------------------------------------------------------
  3524. //
  3525. // Function: RegSetIdleSyncDefaults
  3526. //
  3527. // Synopsis: Registers default values for idle sync
  3528. //
  3529. // History: 30-March-99 ROGERG Created
  3530. //
  3531. //----------------------------------------------------------------------------
  3532. STDAPI RegSetUserIdleSyncDefaults(DWORD dwSyncMgrRegisterMask,
  3533. DWORD dwSyncMgrRegisterFlags)
  3534. {
  3535. // RegSetIdleSyncSettings doesn't handle idle enabled of -1 so only
  3536. // call if Idle actually is set in the flags, if not just return
  3537. if (!(dwSyncMgrRegisterMask & SYNCMGRREGISTERFLAG_IDLE))
  3538. {
  3539. return NOERROR;
  3540. }
  3541. CONNECTIONSETTINGS *pConnection = (LPCONNECTIONSETTINGS)
  3542. ALLOC(sizeof(CONNECTIONSETTINGS));
  3543. if ( pConnection == 0 )
  3544. return E_OUTOFMEMORY;
  3545. XPtr<CONNECTIONSETTINGS> xConnection( pConnection );
  3546. INT iRet = LoadString(g_hmodThisDll,
  3547. IDS_LAN_CONNECTION,
  3548. pConnection->pszConnectionName,
  3549. ARRAYLEN(pConnection->pszConnectionName) );
  3550. Assert( iRet != 0 );
  3551. pConnection->dwConnType = 0;
  3552. pConnection->dwLogon = -1;
  3553. pConnection->dwLogoff = -1;
  3554. pConnection->dwPromptMeFirst = -1;
  3555. pConnection->dwMakeConnection = -1;
  3556. pConnection->dwIdleEnabled = (SYNCMGRREGISTERFLAG_IDLE & dwSyncMgrRegisterFlags)
  3557. ? TRUE : FALSE;
  3558. // set all userLevel items to -1 so user gets the defaults if new
  3559. // but keep their settings if have already tweaked them.
  3560. pConnection->ulIdleRetryMinutes = -1;
  3561. pConnection->ulDelayIdleShutDownTime = -1;
  3562. pConnection->dwRepeatSynchronization = -1;
  3563. pConnection->ulIdleWaitMinutes = -1;
  3564. pConnection->dwRunOnBatteries = -1;
  3565. RegSetIdleSyncSettings(pConnection, 1, 0,
  3566. FALSE /* fCleanReg */,
  3567. TRUE /* fPerUser */);
  3568. return NOERROR;
  3569. }
  3570. //+---------------------------------------------------------------------------
  3571. //
  3572. // Function: RegGetUserRegisterFlags
  3573. //
  3574. // Synopsis: returns current registration flags for the User.
  3575. //
  3576. // History: 30-March-99 ROGERG Created
  3577. //
  3578. //----------------------------------------------------------------------------
  3579. STDAPI RegGetUserRegisterFlags(LPDWORD pdwSyncMgrRegisterFlags)
  3580. {
  3581. CONNECTIONSETTINGS connectSettings;
  3582. *pdwSyncMgrRegisterFlags = 0;
  3583. INT iRet = LoadString(g_hmodThisDll,
  3584. IDS_LAN_CONNECTION,
  3585. connectSettings.pszConnectionName,
  3586. ARRAYLEN(connectSettings.pszConnectionName) );
  3587. if (0 == iRet)
  3588. {
  3589. Assert( iRet != 0 );
  3590. return E_UNEXPECTED;
  3591. }
  3592. RegGetSyncSettings(SYNCTYPE_AUTOSYNC,&connectSettings);
  3593. if (connectSettings.dwLogon)
  3594. {
  3595. *pdwSyncMgrRegisterFlags |= (SYNCMGRREGISTERFLAG_CONNECT);
  3596. }
  3597. if (connectSettings.dwLogoff)
  3598. {
  3599. *pdwSyncMgrRegisterFlags |= (SYNCMGRREGISTERFLAG_PENDINGDISCONNECT);
  3600. }
  3601. RegGetSyncSettings(SYNCTYPE_IDLE,&connectSettings);
  3602. if (connectSettings.dwIdleEnabled)
  3603. {
  3604. *pdwSyncMgrRegisterFlags |= (SYNCMGRREGISTERFLAG_IDLE);
  3605. }
  3606. return NOERROR;
  3607. }
  3608. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  3609. Function: BOOL RegSchedHandlerItemsChecked(TCHAR *pszHandlerName,
  3610. TCHAR *pszConnectionName,
  3611. TCHAR *pszScheduleName)
  3612. Summary: Determine if any items are checked on this handler for this schedule
  3613. Returns: Returns TRUE if one or more are checked, FALSE otherwise
  3614. ------------------------------------------------------------------------F-F*/
  3615. BOOL RegSchedHandlerItemsChecked(TCHAR *pszHandlerName,
  3616. TCHAR *pszConnectionName,
  3617. TCHAR *pszScheduleName)
  3618. {
  3619. SCODE sc;
  3620. HKEY hKeyUser,
  3621. hkeySchedName,
  3622. hkeyConnection,
  3623. hkeyHandler,
  3624. hkeyItem;
  3625. DWORD cbName = MAX_PATH,
  3626. dwIndex = 0,
  3627. dwCheckState = 0,
  3628. dwType = REG_DWORD,
  3629. dwDataSize = sizeof(DWORD);
  3630. BOOL fItemsChecked = FALSE;
  3631. TCHAR lpName[MAX_PATH + 1];
  3632. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_SCHEDULED,KEY_READ,FALSE);
  3633. if (NULL == hKeyUser)
  3634. {
  3635. return FALSE;
  3636. }
  3637. smChkTo(EH_Err3,RegOpenKeyEx (hKeyUser,
  3638. pszScheduleName,0,KEY_READ,
  3639. &hkeySchedName));
  3640. smChkTo(EH_Err4,RegOpenKeyEx (hkeySchedName,
  3641. pszConnectionName,
  3642. 0,KEY_READ,
  3643. &hkeyConnection));
  3644. smChkTo(EH_Err5,RegOpenKeyEx (hkeyConnection,
  3645. pszHandlerName,
  3646. 0,KEY_READ,
  3647. &hkeyHandler));
  3648. // need to enum handler items.
  3649. while ( ERROR_SUCCESS == RegEnumKey(hkeyHandler,dwIndex,
  3650. lpName,cbName) )
  3651. {
  3652. LONG lRet;
  3653. lRet = RegOpenKeyEx( hkeyHandler,
  3654. lpName,
  3655. NULL,
  3656. KEY_READ,
  3657. &hkeyItem);
  3658. if (ERROR_SUCCESS == lRet)
  3659. {
  3660. RegQueryValueEx(hkeyItem,TEXT("CheckState"),
  3661. NULL, &dwType, (LPBYTE) &dwCheckState, &dwDataSize);
  3662. RegCloseKey(hkeyItem);
  3663. }
  3664. else
  3665. {
  3666. goto EH_Err5;
  3667. }
  3668. if (dwCheckState)
  3669. {
  3670. fItemsChecked = TRUE;
  3671. break;
  3672. }
  3673. dwIndex++;
  3674. }
  3675. RegCloseKey(hkeyHandler);
  3676. RegCloseKey(hkeyConnection);
  3677. RegCloseKey(hkeySchedName);
  3678. RegCloseKey(hKeyUser);
  3679. return fItemsChecked;
  3680. EH_Err5:
  3681. RegCloseKey(hkeyConnection);
  3682. EH_Err4:
  3683. RegCloseKey(hkeySchedName);
  3684. EH_Err3:
  3685. RegCloseKey(hKeyUser);
  3686. return fItemsChecked;
  3687. }