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

237 lines
6.2 KiB

  1. //*************************************************************
  2. //
  3. // Copyright (c)1999 Microsoft Corporation, All Rights Reserved
  4. //
  5. // gpdas.h
  6. //
  7. // Module: Rsop Planning mode Provider
  8. //
  9. // History: 11-Jul-99 MickH Created
  10. //
  11. //*************************************************************
  12. #include "stdafx.h"
  13. #include "resource.h"
  14. #include <initguid.h>
  15. #include "planprov.h"
  16. #include <sddl.h>
  17. #include "GPDAS.h"
  18. #include "events.h"
  19. #include "rsopdbg.h"
  20. #include "rsopsec.h"
  21. const WCHAR* RSOP_PLANNING_SERVICENAME = L"rsopprov";
  22. CServiceModule _Module;
  23. BEGIN_OBJECT_MAP(ObjectMap)
  24. OBJECT_ENTRY(CLSID_RsopPlanningModeProvider, RsopPlanningModeProvider)
  25. END_OBJECT_MAP()
  26. inline void CServiceModule::Init(_ATL_OBJMAP_ENTRY* p, HINSTANCE h, UINT nServiceNameID, const GUID* plibid)
  27. {
  28. CComModule::Init(p, h, plibid);
  29. // set up the initial service status
  30. m_hServiceStatus = NULL;
  31. m_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  32. m_status.dwCurrentState = SERVICE_STOPPED;
  33. m_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  34. m_status.dwWin32ExitCode = 0;
  35. m_status.dwServiceSpecificExitCode = 0;
  36. m_status.dwCheckPoint = 0;
  37. m_status.dwWaitHint = 0;
  38. }
  39. //////////////////////////////////////////////////////////////////////////////////////////////
  40. // Service startup and registration
  41. inline void CServiceModule::Start()
  42. {
  43. SERVICE_TABLE_ENTRY st[] =
  44. {
  45. { (LPWSTR) RSOP_PLANNING_SERVICENAME, _ServiceMain },
  46. { NULL, NULL }
  47. };
  48. ::StartServiceCtrlDispatcher(st);
  49. }
  50. inline void CServiceModule::ServiceMain(DWORD /* dwArgc */, LPWSTR* /* lpszArgv */)
  51. {
  52. // Register the control request handler
  53. m_status.dwCurrentState = SERVICE_START_PENDING;
  54. m_hServiceStatus = RegisterServiceCtrlHandler(RSOP_PLANNING_SERVICENAME, _Handler);
  55. if (m_hServiceStatus == NULL)
  56. {
  57. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CServiceModule::ServiceMain failed to Register ServiceCtrlHandler with error %d."), GetLastError() );
  58. return;
  59. }
  60. SetServiceStatus(SERVICE_START_PENDING);
  61. m_status.dwWin32ExitCode = S_OK;
  62. m_status.dwCheckPoint = 0;
  63. m_status.dwWaitHint = 0;
  64. // When the Run function returns, the service has stopped.
  65. Run();
  66. SetServiceStatus(SERVICE_STOPPED);
  67. }
  68. inline void CServiceModule::Handler(DWORD dwOpcode)
  69. {
  70. switch (dwOpcode)
  71. {
  72. case SERVICE_CONTROL_STOP:
  73. SetServiceStatus(SERVICE_STOP_PENDING);
  74. PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
  75. break;
  76. case SERVICE_CONTROL_PAUSE:
  77. break;
  78. case SERVICE_CONTROL_CONTINUE:
  79. break;
  80. case SERVICE_CONTROL_INTERROGATE:
  81. break;
  82. case SERVICE_CONTROL_SHUTDOWN:
  83. break;
  84. default:
  85. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("CServiceModule::Handler Wrong opcode passed to handler %d."), dwOpcode );
  86. }
  87. }
  88. void WINAPI CServiceModule::_ServiceMain(DWORD dwArgc, LPWSTR* lpszArgv)
  89. {
  90. _Module.ServiceMain(dwArgc, lpszArgv);
  91. }
  92. void WINAPI CServiceModule::_Handler(DWORD dwOpcode)
  93. {
  94. _Module.Handler(dwOpcode);
  95. }
  96. void CServiceModule::SetServiceStatus(DWORD dwState)
  97. {
  98. m_status.dwCurrentState = dwState;
  99. ::SetServiceStatus(m_hServiceStatus, &m_status);
  100. }
  101. void CServiceModule::Run()
  102. {
  103. _Module.dwThreadID = GetCurrentThreadId();
  104. HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  105. _ASSERTE(SUCCEEDED(hr));
  106. if ( ! SUCCEEDED(hr) )
  107. return;
  108. //
  109. // Get a security descriptor to pass to CoInitializeSecurity -- note
  110. // that this also returns sids and acls that are part of the security
  111. // descriptor -- we need to free these once the service shuts down, but
  112. // we cannot free them before then.
  113. //
  114. XPtrLF<SECURITY_DESCRIPTOR> xAbsoluteSD;
  115. CSecDesc RelativeSD;
  116. RelativeSD.AddAdministrators( COM_RIGHTS_EXECUTE );
  117. RelativeSD.AddAuthUsers( COM_RIGHTS_EXECUTE );
  118. RelativeSD.AddLocalSystem( COM_RIGHTS_EXECUTE );
  119. RelativeSD.AddAdministratorsAsGroup();
  120. RelativeSD.AddAdministratorsAsOwner();
  121. xAbsoluteSD = RelativeSD.MakeSD();
  122. if ( ! xAbsoluteSD )
  123. {
  124. DWORD Status = GetLastError();
  125. hr = HRESULT_FROM_WIN32(Status);
  126. }
  127. if (SUCCEEDED(hr))
  128. {
  129. hr = CoInitializeSecurity(xAbsoluteSD, -1, NULL, NULL,
  130. RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
  131. _ASSERTE(SUCCEEDED(hr));
  132. }
  133. if (FAILED(hr))
  134. {
  135. goto Run_Exit;
  136. }
  137. if ( SUCCEEDED(hr) )
  138. {
  139. hr = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE);
  140. _ASSERTE(SUCCEEDED(hr));
  141. }
  142. if ( ! SUCCEEDED(hr) )
  143. {
  144. goto Run_Exit;
  145. }
  146. SetServiceStatus(SERVICE_RUNNING);
  147. MSG msg;
  148. BOOL bRet;
  149. while ((bRet = GetMessage(&msg, 0, 0, 0)) != 0)
  150. {
  151. if (-1 == bRet)
  152. {
  153. // Error occured while receiving message
  154. break;
  155. }
  156. DispatchMessage(&msg);
  157. }
  158. _Module.RevokeClassObjects();
  159. Run_Exit:
  160. CoUninitialize();
  161. }
  162. LONG CServiceModule::IncrementServiceCount()
  163. {
  164. LONG l;
  165. l = CoAddRefServerProcess();
  166. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("CServiceModule::IncrementServiceCount. Ref count = %d."), l);
  167. return l;
  168. }
  169. LONG CServiceModule::DecrementServiceCount()
  170. {
  171. LONG srvRefCount = CoReleaseServerProcess();
  172. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("CServiceModule::DecrementServiceCount. Ref count = %d. "), srvRefCount);
  173. if (srvRefCount == 0) {
  174. PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
  175. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("CServiceModule::Unlock Ref count came down to zero. Exitting."));
  176. }
  177. return srvRefCount;
  178. }
  179. /////////////////////////////////////////////////////////////////////////////
  180. //
  181. extern "C" int WINAPI wWinMain(HINSTANCE hInstance,
  182. HINSTANCE /*hPrevInstance*/, LPWSTR lpCmdLine, int /*nShowCmd*/)
  183. {
  184. _Module.Init(ObjectMap, hInstance, IDS_SERVICENAME, &LIBID_RSOPPROVLib);
  185. _Module.Start();
  186. ShutdownEvents();
  187. // When we get here, the service has been stopped
  188. return _Module.m_status.dwWin32ExitCode;
  189. }