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.

314 lines
7.2 KiB

  1. //*************************************************************
  2. //
  3. // Copyright (c) Microsoft Corporation 1998
  4. // All rights reserved
  5. //
  6. // util.cxx
  7. //
  8. //*************************************************************
  9. #include "appmgext.hxx"
  10. SRSETRESTOREPOINTW * gpfnSRSetRetorePointW = 0;
  11. BOOL
  12. IsMemberOfAdminGroup(
  13. HANDLE hUserToken
  14. )
  15. {
  16. SID_IDENTIFIER_AUTHORITY AuthorityNT = SECURITY_NT_AUTHORITY;
  17. PSID pSidAdmin;
  18. BOOL bStatus;
  19. BOOL bIsAdmin;
  20. bIsAdmin = FALSE;
  21. pSidAdmin = 0;
  22. bStatus = AllocateAndInitializeSid( &AuthorityNT, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &pSidAdmin );
  23. if ( bStatus )
  24. bStatus = CheckTokenMembership( hUserToken, pSidAdmin, &bIsAdmin );
  25. FreeSid( pSidAdmin );
  26. return bIsAdmin;
  27. }
  28. DWORD
  29. GetPreviousSid(
  30. HANDLE hUserToken,
  31. WCHAR * pwszCurrentScriptPath,
  32. WCHAR ** ppwszPreviousSid
  33. )
  34. {
  35. HANDLE hFind;
  36. WIN32_FIND_DATA FindData;
  37. PSID pSid;
  38. WCHAR * pwszSlash1;
  39. WCHAR * pwszSlash2;
  40. WCHAR * pwszSearchPath;
  41. DWORD Length;
  42. DWORD Status;
  43. BOOL bMember;
  44. BOOL bStatus;
  45. *ppwszPreviousSid = 0;
  46. //
  47. // Script dir paths created by GetScriptDirPath have '\' at the end.
  48. //
  49. pwszSlash1 = wcsrchr( pwszCurrentScriptPath, L'\\' );
  50. *pwszSlash1 = 0;
  51. pwszSlash2 = wcsrchr( pwszCurrentScriptPath, L'\\' );
  52. *pwszSlash2 = 0;
  53. Length = lstrlen(pwszCurrentScriptPath);
  54. pwszSearchPath = new WCHAR[Length + 3];
  55. if ( pwszSearchPath )
  56. {
  57. memcpy( pwszSearchPath, pwszCurrentScriptPath, Length * sizeof(WCHAR) );
  58. pwszSearchPath[Length] = L'\\';
  59. pwszSearchPath[Length+1] = L'*';
  60. pwszSearchPath[Length+2] = 0;
  61. }
  62. *pwszSlash1 = *pwszSlash2 = L'\\';
  63. if ( ! pwszSearchPath )
  64. return ERROR_OUTOFMEMORY;
  65. //
  66. // We've constructed a search path of %systemroot%\system32\appmgmt\*.
  67. //
  68. hFind = FindFirstFile( pwszSearchPath, &FindData );
  69. delete [] pwszSearchPath;
  70. if ( INVALID_HANDLE_VALUE == hFind )
  71. return ERROR_SUCCESS;
  72. Status = ERROR_SUCCESS;
  73. do
  74. {
  75. if ( ! (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
  76. continue;
  77. if ( (0 == lstrcmp( FindData.cFileName, L"." )) ||
  78. (0 == lstrcmp( FindData.cFileName, L".." )) ||
  79. (CompareString(LOCALE_INVARIANT, NORM_IGNORECASE, FindData.cFileName, -1, L"MACHINE", -1) == CSTR_EQUAL))
  80. continue;
  81. bMember = FALSE;
  82. pSid = 0;
  83. bStatus = ConvertStringSidToSid( FindData.cFileName, &pSid );
  84. if ( bStatus )
  85. {
  86. bStatus = CheckTokenMembership( hUserToken, pSid, &bMember );
  87. if ( bStatus && bMember )
  88. bStatus = ConvertSidToStringSid( pSid, ppwszPreviousSid );
  89. LocalFree( pSid );
  90. }
  91. if ( ! bStatus )
  92. {
  93. Status = GetLastError();
  94. break;
  95. }
  96. if ( bMember )
  97. break;
  98. } while ( FindNextFile( hFind, &FindData ) );
  99. FindClose( hFind );
  100. return Status;
  101. }
  102. DWORD
  103. RenameScriptDir(
  104. WCHAR * pwszPreviousSid,
  105. WCHAR * pwszCurrentScriptPath
  106. )
  107. {
  108. WCHAR * pwszSlash1;
  109. WCHAR * pwszSlash2;
  110. WCHAR * pwszOldScriptPath;
  111. DWORD Length;
  112. BOOL bStatus;
  113. //
  114. // Script dir paths created by GetScriptDirPath have '\' at the end.
  115. //
  116. pwszSlash1 = wcsrchr( pwszCurrentScriptPath, L'\\' );
  117. *pwszSlash1 = 0;
  118. pwszSlash2 = wcsrchr( pwszCurrentScriptPath, L'\\' );
  119. *pwszSlash2 = 0;
  120. Length = lstrlen( pwszCurrentScriptPath );
  121. pwszOldScriptPath = new WCHAR[Length + 1 + lstrlen(pwszPreviousSid) + 1];
  122. if ( pwszOldScriptPath )
  123. {
  124. HRESULT hr;
  125. memcpy( pwszOldScriptPath, pwszCurrentScriptPath, Length * sizeof(WCHAR) );
  126. pwszOldScriptPath[Length] = L'\\';
  127. hr = StringCchCopy( &pwszOldScriptPath[Length+1], lstrlen(pwszPreviousSid) + 1, pwszPreviousSid );
  128. if (FAILED(hr))
  129. {
  130. delete [] pwszOldScriptPath;
  131. return hr;
  132. }
  133. }
  134. *pwszSlash1 = *pwszSlash2 = L'\\';
  135. if ( ! pwszOldScriptPath )
  136. return ERROR_OUTOFMEMORY;
  137. bStatus = MoveFileEx( pwszOldScriptPath, pwszCurrentScriptPath, 0 );
  138. delete [] pwszOldScriptPath;
  139. if ( ! bStatus )
  140. return GetLastError();
  141. return ERROR_SUCCESS;
  142. }
  143. DWORD
  144. GetCurrentUserGPOList(
  145. OUT PGROUP_POLICY_OBJECT* ppGpoList // Free this with the FreeGPOList API
  146. )
  147. {
  148. GUID AppmgmtExtension = {0xc6dc5466, 0x785a, 0x11d2,
  149. 0x84, 0xd0,
  150. 0x00, 0xc0, 0x4f, 0xb1, 0x69, 0xf7};
  151. return GetAppliedGPOList(
  152. 0,
  153. NULL,
  154. NULL,
  155. &AppmgmtExtension,
  156. ppGpoList);
  157. }
  158. DWORD GetWin32ErrFromHResult( HRESULT hr )
  159. {
  160. DWORD Status = ERROR_SUCCESS;
  161. if (S_OK != hr)
  162. {
  163. if (FACILITY_WIN32 == HRESULT_FACILITY(hr))
  164. {
  165. Status = HRESULT_CODE(hr);
  166. }
  167. else
  168. {
  169. Status = GetLastError();
  170. if (ERROR_SUCCESS == Status)
  171. {
  172. //an error had occurred but nobody called SetLastError
  173. //should not be mistaken as a success.
  174. Status = (DWORD) hr;
  175. }
  176. }
  177. }
  178. return Status;
  179. }
  180. void ClearManagedApp( MANAGED_APP* pManagedApp )
  181. {
  182. if (pManagedApp->pszPackageName)
  183. {
  184. midl_user_free(pManagedApp->pszPackageName);
  185. }
  186. if (pManagedApp->pszSupportUrl)
  187. {
  188. midl_user_free(pManagedApp->pszSupportUrl);
  189. }
  190. if (pManagedApp->pszPolicyName)
  191. {
  192. midl_user_free(pManagedApp->pszPolicyName);
  193. }
  194. if (pManagedApp->pszPublisher)
  195. {
  196. midl_user_free(pManagedApp->pszPublisher);
  197. }
  198. //
  199. // Make sure to clear the structure if there is a failure
  200. // so we won't try to marshal bogus data
  201. //
  202. memset(pManagedApp, 0, sizeof(*pManagedApp));
  203. }
  204. CLoadSfc::CLoadSfc( DWORD &Status )
  205. {
  206. hSfc = LoadLibrary( L"sfc.dll" );
  207. if ( ! hSfc )
  208. {
  209. Status = GetLastError();
  210. return;
  211. }
  212. gpfnSRSetRetorePointW = (SRSETRESTOREPOINTW *) GetProcAddress( hSfc, "SRSetRestorePointW" );
  213. if ( ! gpfnSRSetRetorePointW )
  214. {
  215. Status = ERROR_PROC_NOT_FOUND;
  216. return;
  217. }
  218. Status = ERROR_SUCCESS;
  219. }
  220. CLoadSfc::~CLoadSfc()
  221. {
  222. if ( hSfc )
  223. FreeLibrary( hSfc );
  224. }
  225. //
  226. // Force policy to be synchronous at next refresh --
  227. // use a token for user policy, NULL for machine policy
  228. //
  229. DWORD ForceSynchronousRefresh( HANDLE hUserToken )
  230. {
  231. LONG Status;
  232. UNICODE_STRING SidString;
  233. Status = GetSidString( hUserToken, &SidString );
  234. if ( ERROR_SUCCESS == Status )
  235. {
  236. //
  237. // Inform the gp engine to give us a sync refresh
  238. //
  239. Status = ForceSyncFgPolicy( SidString.Buffer );
  240. RtlFreeUnicodeString( &SidString );
  241. }
  242. return Status;
  243. }