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.

206 lines
5.5 KiB

  1. #include "shsrvice.h"
  2. #include "dbg.h"
  3. #include "tfids.h"
  4. #pragma warning(push)
  5. // FALSE positive below: gss
  6. #pragma warning(disable : 4101)
  7. // for now
  8. static SECURITY_ATTRIBUTES _sa = {0};
  9. static ACL* _pacl = NULL;
  10. static SID* _psidLocalUsers = NULL;
  11. static SECURITY_DESCRIPTOR* _psd = NULL;
  12. HRESULT _InitSecurityDescriptor();
  13. // for now
  14. #ifdef UNICODE
  15. extern "C"
  16. {
  17. int __cdecl wmain(int argc, wchar_t* argv[])
  18. #else
  19. int __cdecl main(int argc, char* argv[])
  20. #endif
  21. {
  22. TRACE(TF_SERVICE, TEXT("Entered main"));
  23. HRESULT hres = E_INVALIDARG;
  24. #ifdef DEBUG
  25. CGenericServiceManager::_fRunAsService = FALSE;
  26. #endif
  27. if (argc > 1)
  28. {
  29. hres = CGenericServiceManager::Init();
  30. if (!lstrcmpi(argv[1], TEXT("-i")) ||
  31. !lstrcmpi(argv[1], TEXT("/i")))
  32. {
  33. TRACE(TF_SERVICE, TEXT("Installing"));
  34. hres = CGenericServiceManager::Install();
  35. if (SUCCEEDED(hres))
  36. {
  37. TRACE(TF_SERVICE, TEXT("Install SUCCEEDED"));
  38. }
  39. else
  40. {
  41. TRACE(TF_SERVICE, TEXT("Install FAILED"));
  42. }
  43. }
  44. else
  45. {
  46. if (!lstrcmpi(argv[1], TEXT("-u")) ||
  47. !lstrcmpi(argv[1], TEXT("/u")))
  48. {
  49. TRACE(TF_SERVICE, TEXT("UnInstalling"));
  50. hres = CGenericServiceManager::UnInstall();
  51. if (SUCCEEDED(hres))
  52. {
  53. TRACE(TF_SERVICE, TEXT("UnInstall SUCCEEDED"));
  54. }
  55. else
  56. {
  57. TRACE(TF_SERVICE, TEXT("UnInstall FAILED"));
  58. }
  59. }
  60. else
  61. {
  62. hres = E_INVALIDARG;
  63. }
  64. }
  65. CGenericServiceManager::Cleanup();
  66. }
  67. else
  68. {
  69. hres = CoInitializeEx(NULL, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE);
  70. if (SUCCEEDED(hres))
  71. {
  72. // hres = _InitSecurityDescriptor();
  73. if (SUCCEEDED(hres))
  74. {
  75. hres = CoInitializeSecurity(_psd, -1, NULL, NULL,
  76. RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL,
  77. EOAC_NONE, NULL);
  78. if (SUCCEEDED(hres))
  79. {
  80. // need to be here at most 2 seconds after being launched
  81. hres = CGenericServiceManager::StartServiceCtrlDispatcher();
  82. }
  83. else
  84. {
  85. TRACE(TF_SERVICE, TEXT("CoInitializeSecurity failed: 0x%08X"), hres);
  86. }
  87. }
  88. CoUninitialize();
  89. }
  90. }
  91. return hres;
  92. }
  93. #ifdef UNICODE
  94. }
  95. #endif
  96. #pragma warning(pop)
  97. HRESULT _InitSecurityDescriptor()
  98. {
  99. HRESULT hres;
  100. if (_pacl)
  101. {
  102. hres = S_OK;
  103. }
  104. else
  105. {
  106. hres = E_FAIL;
  107. // This is for "Everyone":
  108. //
  109. // SID_IDENTIFIER_AUTHORITY sidAuthNT = SECURITY_WORLD_SID_AUTHORITY;
  110. //
  111. // if (AllocateAndInitializeSid(&sidAuthNT, 1, SECURITY_WORLD_RID,
  112. // 0, 0, 0, 0, 0, 0, 0, (void**)&_psidLocalUsers))
  113. // This is for local entities only
  114. SID_IDENTIFIER_AUTHORITY sidAuthNT = SECURITY_NT_AUTHORITY;
  115. if (AllocateAndInitializeSid(&sidAuthNT, 1, SECURITY_INTERACTIVE_RID,
  116. 0, 0, 0, 0, 0, 0, 0, (void**)&_psidLocalUsers))
  117. {
  118. DWORD cbacl = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) -
  119. sizeof(DWORD/*ACCESS_ALLOWED_ACE.SidStart*/) +
  120. GetLengthSid(_psidLocalUsers);
  121. _pacl = (ACL*)LocalAlloc(LPTR, cbacl);
  122. if (_pacl)
  123. {
  124. if (InitializeAcl(_pacl, cbacl, ACL_REVISION))
  125. {
  126. if (AddAccessAllowedAce(_pacl, ACL_REVISION, FILE_ALL_ACCESS,
  127. _psidLocalUsers))
  128. {
  129. _psd = (SECURITY_DESCRIPTOR*)LocalAlloc(LPTR,
  130. sizeof(SECURITY_DESCRIPTOR));
  131. if (_psd)
  132. {
  133. if (InitializeSecurityDescriptor(_psd,
  134. SECURITY_DESCRIPTOR_REVISION))
  135. {
  136. if (SetSecurityDescriptorDacl(_psd, TRUE,
  137. _pacl, FALSE))
  138. {
  139. if (IsValidSecurityDescriptor(_psd))
  140. {
  141. _sa.nLength = sizeof(_sa);
  142. _sa.lpSecurityDescriptor = _psd;
  143. _sa.bInheritHandle = TRUE;
  144. hres = S_OK;
  145. }
  146. }
  147. }
  148. }
  149. else
  150. {
  151. hres = E_OUTOFMEMORY;
  152. }
  153. }
  154. }
  155. }
  156. else
  157. {
  158. hres = E_OUTOFMEMORY;
  159. }
  160. }
  161. if (FAILED(hres))
  162. {
  163. if (_psidLocalUsers)
  164. {
  165. FreeSid(_psidLocalUsers);
  166. }
  167. if (_pacl)
  168. {
  169. LocalFree((HLOCAL)_pacl);
  170. }
  171. if (_psd)
  172. {
  173. LocalFree((HLOCAL)_psd);
  174. }
  175. }
  176. }
  177. return hres;
  178. }