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.

271 lines
6.8 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1997 - 1999
  3. Module Name:
  4. onestop.hxx
  5. Abstract:
  6. This file contains the common functions that are helpful in notifying
  7. OneStop of logon/logoff events.
  8. Author:
  9. Gopal Parupudi <GopalP>
  10. Notes:
  11. a. This is being used in senslogn.dll on NT5.
  12. b. It is also being used in sens.dll on NT4 and Win9x.
  13. Revision History:
  14. GopalP 4/29/1998 Start.
  15. --*/
  16. #include <mobsyncp.h>
  17. #include "onestop.hxx"
  18. HRESULT
  19. SensNotifyOneStop(
  20. HANDLE hToken,
  21. TCHAR *pCommandLine,
  22. BOOL bSync
  23. )
  24. {
  25. TCHAR szCommandLine[256];
  26. DWORD dwLastError;
  27. STARTUPINFO si;
  28. PROCESS_INFORMATION ProcessInformation;
  29. dwLastError = 0;
  30. // CreateProcess* APIs require an editable buffer for command-line parameter
  31. ASSERT(_tcslen(pCommandLine) < 255);
  32. _tcscpy(szCommandLine, pCommandLine);
  33. // Fill in the STARTUPINFO structure.
  34. memset(&si, 0x0, sizeof(STARTUPINFO));
  35. si.cb = sizeof(STARTUPINFO);
  36. si.lpReserved = NULL;
  37. si.lpTitle = NULL;
  38. si.lpDesktop = NULL;
  39. si.dwX = 0x0;
  40. si.dwY = 0x0;
  41. si.dwXSize = 0x0;
  42. si.dwYSize = 0x0;
  43. si.dwFlags = 0x0;
  44. si.wShowWindow = SW_SHOW;
  45. si.lpReserved2 = NULL;
  46. si.cbReserved2 = 0;
  47. LogMessage((SENSLOGN "[%d] Launching OneStop...\n", GetTickCount()));
  48. #if !defined(SENS_CHICAGO)
  49. if (CreateProcessAsUser(
  50. hToken, // Handle to the Token of the logged-on user
  51. #else // SENS_CHICAGO
  52. if (CreateProcess(
  53. #endif // SENS_CHICAGO
  54. NULL, // Name of the executable module
  55. szCommandLine, // Command-line string
  56. NULL, // Security attributes
  57. NULL, // Thread security attributes
  58. FALSE, // Don't inherit handles
  59. 0, // Creation flags
  60. NULL, // New environment block
  61. NULL, // Current directory name
  62. &si, // Startup info
  63. &ProcessInformation // Process information
  64. ))
  65. {
  66. //
  67. // Wait until the process terminates
  68. //
  69. if (bSync)
  70. {
  71. LogMessage((SENSLOGN "[%d] Waiting for OneStop to return...\n", GetTickCount()));
  72. WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
  73. LogMessage((SENSLOGN "[%d] OneStop returned successfully.\n", GetTickCount()));
  74. }
  75. CloseHandle(ProcessInformation.hProcess);
  76. CloseHandle(ProcessInformation.hThread);
  77. return S_OK;
  78. }
  79. else
  80. {
  81. dwLastError = GetLastError();
  82. SensPrintToDebugger(SENS_DBG, (SENSLOGN "SensNotifyOneStop() - CreateProcessXXX() "
  83. "failed with 0x%x\n", dwLastError));
  84. return HRESULT_FROM_WIN32(dwLastError);
  85. }
  86. LogMessage((SENSLOGN "[%d] Successfully notified OneStop.\n", GetTickCount()));
  87. return S_OK;
  88. }
  89. BOOL
  90. IsAutoSyncEnabled(
  91. HANDLE hToken,
  92. DWORD dwMask
  93. )
  94. {
  95. HKEY hKeyAutoSync;
  96. LONG lResult;
  97. BOOL bEnabled;
  98. BOOL bImpersonated;
  99. DWORD dwType;
  100. DWORD dwAutoSyncFlags;
  101. DWORD cbData;
  102. LPBYTE lpbData;
  103. hKeyAutoSync = NULL;
  104. lResult = 0;
  105. bEnabled = FALSE;
  106. bImpersonated = FALSE;
  107. dwType = 0x0;
  108. dwAutoSyncFlags = 0x0;
  109. cbData = 0x0;
  110. lpbData = NULL;
  111. //
  112. // Impersonate the Logged on user so that we can access the user-specific
  113. // registry entries.
  114. //
  115. #if !defined(SENS_CHICAGO)
  116. bImpersonated = ImpersonateLoggedOnUser(hToken);
  117. if (bImpersonated == FALSE)
  118. {
  119. LogMessage((SENSLOGN "ImpersonateLoggedOnUser(token = 0x%x) failed - "
  120. "0x%x\n", hToken, GetLastError()));
  121. }
  122. else
  123. {
  124. LogMessage((SENSLOGN "ImpersonateLoggedUser() succeeded!\n"));
  125. }
  126. #endif // SENS_CHICAGO
  127. //
  128. // Open AutoSync sub-key for this user.
  129. //
  130. lResult = RegOpenKeyEx(
  131. HKEY_LOCAL_MACHINE, // Handle of the open Key
  132. AUTOSYNC_KEY, // Name of the sub-key
  133. 0, // Reserved (MBZ)
  134. KEY_QUERY_VALUE, // Security access mask
  135. &hKeyAutoSync // Address of the handle of new key
  136. );
  137. if (lResult != ERROR_SUCCESS)
  138. {
  139. SensPrintToDebugger(SENS_DBG, (SENSLOGN "RegOpenKeyEx(AUTOSYNC) failed with 0x%x\n", lResult));
  140. goto Cleanup;
  141. }
  142. //
  143. // Query the Flags value
  144. //
  145. lpbData = (LPBYTE) &dwAutoSyncFlags;
  146. cbData = sizeof(DWORD);
  147. lResult = RegQueryValueEx(
  148. hKeyAutoSync, // Handle of the sub-key
  149. AUTOSYNC_FLAGS, // Name of the Value
  150. NULL, // Reserved (MBZ)
  151. &dwType, // Address of the type of the Value
  152. lpbData, // Address of the data of the Value
  153. &cbData // Address of size of data of the Value
  154. );
  155. if (lResult != ERROR_SUCCESS)
  156. {
  157. LogMessage((SENSLOGN "RegQueryValueEx(AUTOSYNC_FLAGS) failed with 0x%x\n", lResult));
  158. goto Cleanup;
  159. }
  160. ASSERT(dwType == REG_DWORD);
  161. //
  162. // Check to see if the Mask bit is set
  163. //
  164. if (dwMask == AUTOSYNC_ON_STARTSHELL)
  165. {
  166. if ( (dwAutoSyncFlags & AUTOSYNC_LAN_LOGON)
  167. || (dwAutoSyncFlags & AUTOSYNC_WAN_LOGON))
  168. {
  169. LogMessage((SENSLOGN "AutoSync is enabled for StartShell\n"));
  170. bEnabled = TRUE;
  171. goto Cleanup;
  172. }
  173. else
  174. {
  175. LogMessage((SENSLOGN "AutoSync is NOT enabled for Logon\n"));
  176. }
  177. }
  178. else
  179. if (dwMask == AUTOSYNC_ON_LOGOFF)
  180. {
  181. if ( (dwAutoSyncFlags & AUTOSYNC_LAN_LOGOFF)
  182. || (dwAutoSyncFlags & AUTOSYNC_WAN_LOGOFF))
  183. {
  184. LogMessage((SENSLOGN "AutoSync is enabled for Logoff\n"));
  185. bEnabled = TRUE;
  186. goto Cleanup;
  187. }
  188. else
  189. {
  190. LogMessage((SENSLOGN "AutoSync is NOT enabled for Logoff\n"));
  191. }
  192. }
  193. else
  194. if (dwMask == AUTOSYNC_ON_SCHEDULE)
  195. {
  196. if (dwAutoSyncFlags != NULL)
  197. {
  198. LogMessage((SENSLOGN "AutoSync is enabled for Schedule\n"));
  199. bEnabled = TRUE;
  200. goto Cleanup;
  201. }
  202. else
  203. {
  204. LogMessage((SENSLOGN "AutoSync is NOT enabled for Schedule\n"));
  205. }
  206. }
  207. //
  208. // Autosync is not enabled.
  209. //
  210. Cleanup:
  211. //
  212. // Cleanup
  213. //
  214. if (hKeyAutoSync)
  215. {
  216. RegCloseKey(hKeyAutoSync);
  217. }
  218. #if !defined(SENS_CHICAGO)
  219. // Stop Impersonating
  220. if (bImpersonated)
  221. {
  222. RevertToSelf();
  223. }
  224. #endif // SENS_CHICAGO
  225. return bEnabled;
  226. }