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.

337 lines
8.5 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. init.c
  5. Abstract:
  6. AdvApi32.dll initialization
  7. Author:
  8. Robert Reichel (RobertRe) 8-12-92
  9. Revision History:
  10. --*/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <windows.h>
  15. #include <marta.h>
  16. #include <winsvcp.h>
  17. #include "advapi.h"
  18. #include "tsappcmp.h"
  19. extern CRITICAL_SECTION FeClientLoadCritical;
  20. extern CRITICAL_SECTION SddlSidLookupCritical;
  21. extern CRITICAL_SECTION MSChapChangePassword;
  22. //
  23. // Local prototypes for functions that seem to have no prototypes.
  24. //
  25. BOOLEAN
  26. RegInitialize (
  27. IN HANDLE Handle,
  28. IN DWORD Reason,
  29. IN PVOID Reserved
  30. );
  31. BOOLEAN
  32. Sys003Initialize (
  33. IN HANDLE Handle,
  34. IN DWORD Reason,
  35. IN PVOID Reserved
  36. );
  37. BOOLEAN
  38. AppmgmtInitialize (
  39. IN HANDLE Handle,
  40. IN DWORD Reason,
  41. IN PVOID Reserved
  42. );
  43. BOOLEAN
  44. WmiDllInitialize (
  45. IN HANDLE Handle,
  46. IN DWORD Reason,
  47. IN PVOID Reserved
  48. );
  49. BOOLEAN
  50. CodeAuthzInitialize (
  51. IN HANDLE Handle,
  52. IN DWORD Reason,
  53. IN PVOID Reserved
  54. );
  55. // app server has two modes for app compat
  56. BOOLEAN
  57. AdvApi_InitializeTermsrvFpns(
  58. BOOLEAN *pIsInRelaxedSecurityMode,
  59. DWORD *pdwCompatFlags
  60. );
  61. #define ADVAPI_PROCESS_ATTACH ( 1 << DLL_PROCESS_ATTACH )
  62. #define ADVAPI_PROCESS_DETACH ( 1 << DLL_PROCESS_DETACH )
  63. #define ADVAPI_THREAD_ATTACH ( 1 << DLL_THREAD_ATTACH )
  64. #define ADVAPI_THREAD_DETACH ( 1 << DLL_THREAD_DETACH )
  65. typedef struct _ADVAPI_INIT_ROUTINE {
  66. PDLL_INIT_ROUTINE InitRoutine;
  67. ULONG Flags;
  68. ULONG CompletedFlags;
  69. } ADVAPI_INIT_ROUTINE, *PADVAPI_INIT_ROUTINE;
  70. typedef struct _ADVAPI_CRITICAL_SECTION {
  71. PRTL_CRITICAL_SECTION CriticalSection;
  72. BOOLEAN bInit;
  73. } ADVAPI_CRITICAL_SECTION, *PADVAPI_CREATE_SECTION;
  74. //
  75. // Place all ADVAPI32 initialization hooks in this
  76. // table.
  77. //
  78. ADVAPI_INIT_ROUTINE AdvapiInitRoutines[] = {
  79. { (PDLL_INIT_ROUTINE) RegInitialize,
  80. ADVAPI_PROCESS_ATTACH |
  81. ADVAPI_PROCESS_DETACH |
  82. ADVAPI_THREAD_ATTACH |
  83. ADVAPI_THREAD_DETACH,
  84. 0 },
  85. { (PDLL_INIT_ROUTINE) Sys003Initialize,
  86. ADVAPI_PROCESS_ATTACH |
  87. ADVAPI_PROCESS_DETACH,
  88. 0 },
  89. { (PDLL_INIT_ROUTINE) MartaDllInitialize,
  90. ADVAPI_PROCESS_ATTACH |
  91. ADVAPI_PROCESS_DETACH,
  92. 0 },
  93. { (PDLL_INIT_ROUTINE) AppmgmtInitialize,
  94. ADVAPI_PROCESS_ATTACH |
  95. ADVAPI_PROCESS_DETACH,
  96. 0 },
  97. { (PDLL_INIT_ROUTINE) WmiDllInitialize,
  98. ADVAPI_PROCESS_ATTACH |
  99. ADVAPI_PROCESS_DETACH,
  100. 0 },
  101. { (PDLL_INIT_ROUTINE) CodeAuthzInitialize,
  102. ADVAPI_PROCESS_ATTACH |
  103. ADVAPI_PROCESS_DETACH,
  104. 0 }
  105. };
  106. //
  107. // Place all critical sections used in advapi32 here:
  108. //
  109. ADVAPI_CRITICAL_SECTION AdvapiCriticalSections[] = {
  110. { &FeClientLoadCritical, FALSE },
  111. { &SddlSidLookupCritical, FALSE },
  112. { &Logon32Lock, FALSE },
  113. { &MSChapChangePassword, FALSE }
  114. };
  115. NTSTATUS
  116. InitializeAdvapiCriticalSections(
  117. )
  118. {
  119. ULONG i;
  120. NTSTATUS Status = STATUS_SUCCESS;
  121. for (i = 0; i < sizeof(AdvapiCriticalSections) / sizeof(ADVAPI_CRITICAL_SECTION); i++)
  122. {
  123. Status = RtlInitializeCriticalSection(AdvapiCriticalSections[i].CriticalSection);
  124. if (NT_SUCCESS(Status))
  125. {
  126. AdvapiCriticalSections[i].bInit = TRUE;
  127. }
  128. else
  129. {
  130. #if DBG
  131. DbgPrint("ADVAPI: Failed to initialize critical section %p at index %d\n", AdvapiCriticalSections[i], i);
  132. #endif
  133. break;
  134. }
  135. }
  136. return Status;
  137. }
  138. NTSTATUS
  139. DeleteAdvapiCriticalSections(
  140. )
  141. {
  142. ULONG i;
  143. NTSTATUS Status;
  144. NTSTATUS ReturnStatus = STATUS_SUCCESS;
  145. for (i = 0; i < sizeof(AdvapiCriticalSections) / sizeof(ADVAPI_CRITICAL_SECTION); i++)
  146. {
  147. if (AdvapiCriticalSections[i].bInit)
  148. {
  149. Status = RtlDeleteCriticalSection(AdvapiCriticalSections[i].CriticalSection);
  150. if (!NT_SUCCESS(Status))
  151. {
  152. #if DBG
  153. DbgPrint("Failed to delete critical section %p at index %d\n", AdvapiCriticalSections[i], i);
  154. #endif
  155. //
  156. // Don't exit if failed to delete. Keep trying to free all the critsects that
  157. // we can. Record the failure status.
  158. //
  159. ReturnStatus = Status;
  160. }
  161. }
  162. }
  163. return ReturnStatus;
  164. }
  165. BOOLEAN
  166. DllInitialize(
  167. IN PVOID hmod,
  168. IN ULONG Reason,
  169. IN PCONTEXT Context
  170. )
  171. {
  172. NTSTATUS Status;
  173. BOOLEAN Result;
  174. ULONG ReasonMask;
  175. LONG i;
  176. //
  177. // First, handle all the critical sections
  178. //
  179. if (Reason == DLL_PROCESS_ATTACH)
  180. {
  181. Status = InitializeAdvapiCriticalSections();
  182. if (!NT_SUCCESS(Status))
  183. {
  184. Result = FALSE;
  185. //
  186. // If any crit sects failed to init then delete all that
  187. // may have succeeded.
  188. //
  189. (VOID) DeleteAdvapiCriticalSections();
  190. goto Return;
  191. }
  192. if (IsTerminalServer())
  193. {
  194. BOOLEAN isInRelaxedSecurityMode = FALSE; // app server is in standard or relaxed security mode
  195. DWORD dwCompatFlags = 0;
  196. if(AdvApi_InitializeTermsrvFpns(&isInRelaxedSecurityMode, &dwCompatFlags))
  197. {
  198. if (isInRelaxedSecurityMode)
  199. {
  200. //
  201. // If TS reg key redirection is enabled, then get our special reg key extention flag for this app
  202. // called "gdwRegistryExtensionFlags" which is used in screg\winreg\server\ files.
  203. // This flag control HKCR per user virtualization and HKLM\SW\Classes per user virtualization and
  204. // also modification to access mask.
  205. //
  206. // Basically, only non-system, non-ts-aware apps on the app server will have this enabled.
  207. // Also, we only provide this feature in the relaxed security mode.
  208. //
  209. // Future work, add DISABLE mask support on per app basis, so that we can turn off this
  210. // reg extenion feature on per app basis (just in case).
  211. //
  212. GetRegistryExtensionFlags(dwCompatFlags);
  213. }
  214. }
  215. }
  216. }
  217. //
  218. // Now, run the subcomponents initialization routines
  219. //
  220. ReasonMask = 1 << Reason;
  221. Result = TRUE;
  222. for (i = 0; i < sizeof(AdvapiInitRoutines) / sizeof(ADVAPI_INIT_ROUTINE); i++)
  223. {
  224. if (AdvapiInitRoutines[i].Flags & ReasonMask)
  225. {
  226. //
  227. // Only run the routines for *DETACH if the routine successfully
  228. // completed for *ATTACH
  229. //
  230. #define FLAG_ON(dw,f) ((f) == ((dw) & (f)))
  231. if ((Reason == DLL_PROCESS_DETACH && !FLAG_ON(AdvapiInitRoutines[i].CompletedFlags, ADVAPI_PROCESS_ATTACH)) ||
  232. (Reason == DLL_THREAD_DETACH && !FLAG_ON(AdvapiInitRoutines[i].CompletedFlags, ADVAPI_THREAD_ATTACH)))
  233. {
  234. continue;
  235. }
  236. else
  237. {
  238. Result = AdvapiInitRoutines[i].InitRoutine(hmod, Reason, Context);
  239. if (Result)
  240. {
  241. //
  242. // The routine succeeded. Note for which reason it succeeded.
  243. //
  244. AdvapiInitRoutines[i].CompletedFlags |= ReasonMask;
  245. }
  246. else
  247. {
  248. #if DBG
  249. DbgPrint("ADVAPI: sub init routine %p failed for reason %d\n", AdvapiInitRoutines[i].InitRoutine, Reason);
  250. #endif
  251. break;
  252. }
  253. }
  254. }
  255. }
  256. //
  257. // If an initialization routine failed during DLL_PROCESS_ATTACH then clean up
  258. // and fail.
  259. // If this is DLL_PROCESS_DETACH, clean up all the critical sections
  260. // after the hooks are run.
  261. //
  262. if ((!Result && Reason == DLL_PROCESS_ATTACH) || (Reason == DLL_PROCESS_DETACH))
  263. {
  264. (VOID) DeleteAdvapiCriticalSections();
  265. goto Return;
  266. }
  267. #if DBG
  268. SccInit(Reason);
  269. #endif
  270. Return:
  271. return Result;
  272. }