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.

334 lines
9.1 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. c2RegAcl.c
  5. Abstract:
  6. Registry ACL processing and display functions
  7. Author:
  8. Bob Watson (a-robw)
  9. Revision History:
  10. 23 Dec 94
  11. --*/
  12. #include <windows.h>
  13. #include <tchar.h>
  14. #include <stdio.h>
  15. #include <c2dll.h>
  16. #include <c2inc.h>
  17. #include <c2utils.h>
  18. #include <strings.h>
  19. #include "c2acls.h"
  20. #include "c2aclres.h"
  21. // define action codes here. They are only meaningful in the
  22. // context of this module.
  23. #define AC_REG_ACLS_MAKE_C2 1
  24. #define AC_REG_ACLS_MAKE_NOTC2 2
  25. #define SECURE C2DLL_C2
  26. static
  27. LONG
  28. ProcessRegistryInf (
  29. IN LPCTSTR szInfFileName
  30. )
  31. /*++
  32. Routine Description:
  33. Read the Registry INF file and update the registry key security
  34. Return Value:
  35. WIN32 status of function
  36. --*/
  37. {
  38. LONG lReturn;
  39. LONG lStatus;
  40. LPTSTR mszRegKeyList;
  41. DWORD dwKeyListSize;
  42. DWORD dwReturnSize;
  43. LPTSTR szThisKey;
  44. TCHAR mszThisSection[SMALL_BUFFER_SIZE];
  45. LPCTSTR szRegKey;
  46. BOOL bDoSubKeys;
  47. PACL paclKey;
  48. SECURITY_DESCRIPTOR sdKey;
  49. if (FileExists(szInfFileName)) {
  50. // file found, so continue
  51. dwReturnSize = 0;
  52. dwKeyListSize = 0;
  53. mszRegKeyList = NULL;
  54. do {
  55. // allocate buffer to hold key list
  56. dwKeyListSize += MAX_PATH * 1024; // add room for 1K keys
  57. // free any previous allocations
  58. GLOBAL_FREE_IF_ALLOC (mszRegKeyList);
  59. mszRegKeyList = (LPTSTR)GLOBAL_ALLOC(dwKeyListSize * sizeof(TCHAR));
  60. // read the keys to process (i.e. get a list of the section
  61. // headers in the .ini file.
  62. dwReturnSize = GetPrivateProfileString (
  63. NULL, // list all sections
  64. NULL, // not used
  65. cmszEmptyString, // empty string for default,
  66. mszRegKeyList,
  67. dwKeyListSize, // buffer size in characters
  68. szInfFileName); // file name
  69. } while (dwReturnSize == (dwKeyListSize -2)); // this value indicates truncation
  70. if (dwReturnSize != 0) {
  71. // process all keys in list
  72. for (szThisKey = mszRegKeyList;
  73. *szThisKey != 0;
  74. szThisKey += lstrlen(szThisKey)+1) {
  75. // read in all the ACEs for this key
  76. dwReturnSize = GetPrivateProfileSection (
  77. szThisKey,
  78. mszThisSection,
  79. SMALL_BUFFER_SIZE,
  80. szInfFileName);
  81. if (dwReturnSize != 0) {
  82. paclKey = (PACL)GLOBAL_ALLOC(SMALL_BUFFER_SIZE);
  83. if (paclKey != NULL) {
  84. InitializeSecurityDescriptor (&sdKey,
  85. SECURITY_DESCRIPTOR_REVISION);
  86. if (InitializeAcl(paclKey, SMALL_BUFFER_SIZE, ACL_REVISION)) {
  87. // make ACL from section
  88. lStatus = MakeAclFromRegSection (
  89. mszThisSection,
  90. paclKey);
  91. // add ACL to Security Descriptor
  92. if (SetSecurityDescriptorDacl (
  93. &sdKey,
  94. TRUE,
  95. paclKey,
  96. FALSE)) {
  97. // DACL built now update key
  98. szRegKey = GetKeyPath (szThisKey, &bDoSubKeys);
  99. lStatus = SetRegistryKeySecurity (
  100. GetRootKey(szThisKey),
  101. szRegKey,
  102. bDoSubKeys,
  103. &sdKey);
  104. } else {
  105. // unable to set securityDesc.
  106. }
  107. } else {
  108. // unable to initialize ACL
  109. }
  110. GLOBAL_FREE_IF_ALLOC (paclKey);
  111. } else {
  112. // unable to allocate ACL buffer
  113. }
  114. } else {
  115. // no entries found in this section
  116. }
  117. } // end while scanning list of sections
  118. } else {
  119. // no section list returned
  120. }
  121. GLOBAL_FREE_IF_ALLOC (mszRegKeyList);
  122. } else {
  123. lReturn = ERROR_FILE_NOT_FOUND;
  124. }
  125. return lReturn;
  126. }
  127. LONG
  128. C2QueryRegistryAcls (
  129. IN LPARAM lParam
  130. )
  131. /*++
  132. Routine Description:
  133. Function called to find out the current state of this configuration
  134. item. This function reads the current state of the item and
  135. sets the C2 Compliance flag and the Status string to reflect
  136. the current value of the configuration item.
  137. For the moment, the registry is not read and compared so no status
  138. is returned.
  139. Arguments:
  140. Pointer to the Dll data block passed as an LPARAM.
  141. ReturnValue:
  142. ERROR_SUCCESS if the function succeeds otherwise a
  143. WIN32 error is returned if an error occurs
  144. --*/
  145. {
  146. PC2DLL_DATA pC2Data;
  147. if (lParam != 0) {
  148. pC2Data = (PC2DLL_DATA)lParam;
  149. // return message based on flag for now
  150. pC2Data->lC2Compliance = C2DLL_UNKNOWN;
  151. lstrcpy (pC2Data->szStatusName,
  152. GetStringResource (GetDllInstance(), IDS_UNABLE_READ));
  153. return ERROR_SUCCESS;
  154. } else {
  155. return ERROR_BAD_ARGUMENTS;
  156. }
  157. }
  158. LONG
  159. C2SetRegistryAcls (
  160. IN LPARAM lParam
  161. )
  162. /*++
  163. Routine Description:
  164. Function called to change the current state of this configuration
  165. item based on an action code passed in the DLL data block. If
  166. this function successfully sets the state of the configuration
  167. item, then the C2 Compliance flag and the Status string to reflect
  168. the new value of the configuration item.
  169. Arguments:
  170. Pointer to the Dll data block passed as an LPARAM.
  171. ReturnValue:
  172. ERROR_SUCCESS if the function succeeds otherwise a
  173. WIN32 error is returned if an error occurs
  174. --*/
  175. {
  176. PC2DLL_DATA pC2Data;
  177. TCHAR szInfFileName[MAX_PATH];
  178. if (lParam != 0) {
  179. pC2Data = (PC2DLL_DATA)lParam;
  180. switch (pC2Data->lActionCode ) {
  181. case AC_REG_ACLS_MAKE_C2:
  182. if (DisplayDllMessageBox(
  183. pC2Data->hWnd,
  184. IDS_REG_ACLS_CONFIRM,
  185. IDS_REG_ACLS_CAPTION,
  186. MBOKCANCEL_QUESTION) == IDOK) {
  187. SET_WAIT_CURSOR;
  188. if (GetFilePath(
  189. GetStringResource(GetDllInstance(), IDS_REGISTRY_ACL_INF),
  190. szInfFileName)) {
  191. if (ProcessRegistryInf(szInfFileName) == ERROR_SUCCESS) {
  192. pC2Data->lC2Compliance = SECURE;
  193. lstrcpy (pC2Data->szStatusName,
  194. GetStringResource(GetDllInstance(), IDS_REG_ACLS_COMPLY));
  195. } else {
  196. // unable to set acl security
  197. }
  198. } else {
  199. // unable to get acl file path
  200. }
  201. SET_ARROW_CURSOR;
  202. } else {
  203. // user opted not to set acls
  204. }
  205. break;
  206. default:
  207. // no change;
  208. break;
  209. }
  210. } else {
  211. return ERROR_BAD_ARGUMENTS;
  212. }
  213. return ERROR_SUCCESS;
  214. }
  215. LONG
  216. C2DisplayRegistryAcls (
  217. IN LPARAM lParam
  218. )
  219. /*++
  220. Routine Description:
  221. Function called to display more information on the configuration
  222. item and provide the user with the option to change the current
  223. setting (if appropriate). If the User "OK's" out of the UI,
  224. then the action code field in the DLL data block is set to the
  225. appropriate (and configuration item-specific) action code so the
  226. "Set" function can be called to perform the desired action. If
  227. the user Cancels out of the UI, then the Action code field is
  228. set to 0 (no action) and no action is performed.
  229. Arguments:
  230. Pointer to the Dll data block passed as an LPARAM.
  231. ReturnValue:
  232. ERROR_SUCCESS if the function succeeds otherwise a
  233. WIN32 error is returned if an error occurs
  234. --*/
  235. {
  236. PC2DLL_DATA pC2Data;
  237. if (lParam != 0) {
  238. pC2Data = (PC2DLL_DATA)lParam;
  239. } else {
  240. return ERROR_BAD_ARGUMENTS;
  241. }
  242. if (pC2Data->lC2Compliance == SECURE) {
  243. DisplayDllMessageBox (
  244. pC2Data->hWnd,
  245. IDS_REG_ACLS_COMPLY,
  246. IDS_REG_ACLS_CAPTION,
  247. MBOK_INFO);
  248. } else {
  249. if (DisplayDllMessageBox (
  250. pC2Data->hWnd,
  251. IDS_REG_ACLS_QUERY_SET,
  252. IDS_REG_ACLS_CAPTION,
  253. MBOKCANCEL_QUESTION) == IDOK) {
  254. pC2Data->lActionCode = AC_REG_ACLS_MAKE_C2;
  255. pC2Data->lActionValue = 0; // not used
  256. } else {
  257. pC2Data->lActionCode = 0; // no action
  258. pC2Data->lActionValue = 0; // not used
  259. }
  260. }
  261. return ERROR_SUCCESS;
  262. }