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.

420 lines
14 KiB

  1. #ifndef WPP_MOF_RESOURCENAME
  2. #define WPP_REG_TRACE_REGKEY L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Tracing"
  3. #define WPP_REG_TRACE_ENABLED L"EnableTracing"
  4. #define WPP_REG_TRACE_LOG_FILE_NAME L"LogFileName"
  5. #define WPP_REG_TRACE_LOG_SESSION_NAME L"LogSessionName"
  6. #define WPP_REG_TRACE_LOG_BUFFER_SIZE L"BufferSize"
  7. #define WPP_REG_TRACE_LOG_MIN_BUFFERS L"MinBuffers"
  8. #define WPP_REG_TRACE_LOG_MAX_BUFFERS L"MaxBuffers"
  9. #define WPP_REG_TRACE_LOG_MAX_FILESIZE L"MaxFileSize"
  10. #define WPP_REG_TRACE_LOG_MAX_HISTORY L"MaxHistorySize"
  11. #define WPP_REG_TRACE_LOG_MAX_BACKUPS L"MaxBackups"
  12. #define WPP_REG_TRACE_ACTIVE L"Active"
  13. #define WPP_REG_TRACE_CONTROL L"ControlFlags"
  14. #define WPP_REG_TRACE_LEVEL L"Level"
  15. #define WPP_REG_TRACE_GUID L"Guid"
  16. #define WPP_REG_TRACE_BITNAMES L"BitNames"
  17. #endif // #ifndef WPP_MOF_RESOURCENAME
  18. #define DEFAULT_LOGGER_NAME L"stdout"
  19. #if !defined(WppDebug)
  20. # define WppDebug(a,b)
  21. #endif
  22. #if !defined(WPPINIT_STATIC)
  23. # define WPPINIT_STATIC
  24. #endif
  25. #if !defined(WPPINIT_EXPORT)
  26. # define WPPINIT_EXPORT
  27. #endif
  28. #define WPP_GUID_FORMAT "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x"
  29. #define WPP_GUID_ELEMENTS(p) \
  30. p->Data1, p->Data2, p->Data3,\
  31. p->Data4[0], p->Data4[1], p->Data4[2], p->Data4[3],\
  32. p->Data4[4], p->Data4[5], p->Data4[6], p->Data4[7]
  33. WPPINIT_STATIC
  34. void WppIntToHex(
  35. LPWSTR Buf,
  36. unsigned int value,
  37. int digits
  38. )
  39. {
  40. static LPCWSTR hexDigit = L"0123456789abcdef";
  41. while (--digits >= 0) {
  42. Buf[digits] = hexDigit[ value & 15 ];
  43. value /= 16; // compiler is smart enough to change it to bitshift
  44. }
  45. }
  46. #define WPP_TEXTGUID_LEN 37
  47. // b1e5deaf-1524-4a04-82c4-c9dfbce6cf97<NULL>
  48. // 0 1 2 3
  49. // 0123456789012345678901234567890123456
  50. WPPINIT_STATIC
  51. void WppGuidToStr(LPWSTR buf, LPCGUID guid) {
  52. WppIntToHex(buf + 0, guid->Data1, 8);
  53. buf[8] = '-';
  54. WppIntToHex(buf + 9, guid->Data2, 4);
  55. buf[13] = '-';
  56. WppIntToHex(buf + 14, guid->Data3, 4);
  57. buf[18] = '-';
  58. WppIntToHex(buf + 19, guid->Data4[0], 2);
  59. WppIntToHex(buf + 21, guid->Data4[1], 2);
  60. buf[23] = '-';
  61. WppIntToHex(buf + 24, guid->Data4[2], 2);
  62. WppIntToHex(buf + 26, guid->Data4[3], 2);
  63. WppIntToHex(buf + 28, guid->Data4[4], 2);
  64. WppIntToHex(buf + 30, guid->Data4[5], 2);
  65. WppIntToHex(buf + 32, guid->Data4[6], 2);
  66. WppIntToHex(buf + 34, guid->Data4[7], 2);
  67. buf[36] = 0;
  68. }
  69. #ifndef WPP_MOF_RESOURCENAME
  70. WPPINIT_STATIC
  71. ULONG
  72. WppPublishTraceInfo(
  73. PWPP_REGISTRATION_BLOCK Head,
  74. LPCWSTR ProductName)
  75. {
  76. DWORD status;
  77. DWORD disposition= REG_OPENED_EXISTING_KEY;
  78. HKEY TracingKey = 0;
  79. HKEY ProductKey = 0;
  80. DWORD dwValue;
  81. const DWORD dwSizeLoggerName=sizeof(DEFAULT_LOGGER_NAME);
  82. //
  83. // Enforce security on Trace Registry Key
  84. // only system, admin usera are allowed to full access
  85. // LocalService and NetworkService R/W access
  86. // normal and power users are NOT allowed to R the key
  87. //
  88. SECURITY_ATTRIBUTES seAttrib = {0};
  89. PSECURITY_DESCRIPTOR pSD = NULL;
  90. #ifndef WPP_TRACE_W2K_COMPATABILITY
  91. #ifndef WPP_DLL
  92. TCHAR szSD[] = TEXT("D:") // DACL
  93. TEXT("(A;OICI;GA;;;SY)") // System Full Access
  94. TEXT("(A;OICI;GRGW;;;LS)") // Local service R/W Access
  95. TEXT("(A;OICI;GRGW;;;NS)") // Network service R/W Access
  96. TEXT("(A;OICI;GA;;;BA)"); // Built-in Adminstrator Full Access
  97. #endif
  98. #endif
  99. seAttrib.nLength = sizeof(SECURITY_ATTRIBUTES);
  100. seAttrib.bInheritHandle = FALSE;
  101. #ifndef WPP_TRACE_W2K_COMPATABILITY
  102. #ifndef WPP_DLL
  103. if (!ConvertStringSecurityDescriptorToSecurityDescriptor(
  104. szSD,
  105. SDDL_REVISION_1,
  106. &pSD,
  107. NULL )) {
  108. status = GetLastError();
  109. WppDebug(1,("[WppInit] Failed to create security descriptor, %1!d!", status) );
  110. goto exit_gracefully;
  111. }
  112. #endif
  113. #endif
  114. seAttrib.lpSecurityDescriptor = pSD;
  115. status = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
  116. WPP_REG_TRACE_REGKEY,
  117. 0,
  118. NULL, // Class
  119. REG_OPTION_NON_VOLATILE,
  120. KEY_ALL_ACCESS,
  121. &seAttrib, // Sec Attributes
  122. &TracingKey,
  123. &disposition
  124. );
  125. if (status != ERROR_SUCCESS) {
  126. WppDebug(1,("[WppInit] Failed to create Trace Key, %1!d!", status) );
  127. goto exit_gracefully;
  128. }
  129. status = RegCreateKeyExW(TracingKey,
  130. ProductName,
  131. 0,
  132. NULL, // Class
  133. REG_OPTION_NON_VOLATILE,
  134. KEY_WRITE,
  135. NULL, // Sec Attributes
  136. &ProductKey,
  137. &disposition
  138. );
  139. if (status != ERROR_SUCCESS) {
  140. WppDebug(1,("[WppInit] Failed to create Product Key, %1!d!", status) );
  141. goto exit_gracefully;
  142. }
  143. status = RegSetValueExW(ProductKey,
  144. WPP_REG_TRACE_LOG_SESSION_NAME,
  145. 0, // Reserved
  146. REG_EXPAND_SZ,
  147. (const BYTE*)DEFAULT_LOGGER_NAME,
  148. dwSizeLoggerName
  149. );
  150. if (status != ERROR_SUCCESS) {
  151. WppDebug(1,("[WppInit] Failed to create LogSession value, %1!d!", status) );
  152. goto exit_gracefully;
  153. }
  154. dwValue = 1;
  155. status = RegSetValueExW(ProductKey,
  156. WPP_REG_TRACE_ACTIVE,
  157. 0, // Reserved
  158. REG_DWORD,
  159. (const BYTE*)&dwValue,
  160. sizeof(dwValue) );
  161. if (status != ERROR_SUCCESS) {
  162. WppDebug(1, ("[WppInit] Failed to create Active value, %1!d!", status) );
  163. goto exit_gracefully;
  164. }
  165. dwValue = 1;
  166. status = RegSetValueExW(ProductKey,
  167. WPP_REG_TRACE_CONTROL,
  168. 0, // Reserved
  169. REG_DWORD,
  170. (const BYTE*)&dwValue,
  171. sizeof(dwValue) );
  172. if (status != ERROR_SUCCESS) {
  173. WppDebug(1,("[WppInit] Failed to create Control value, %1!d!", status) );
  174. goto exit_gracefully;
  175. }
  176. for(;Head;Head = Head->Next) {
  177. HKEY ComponentKey=0;
  178. status = RegCreateKeyExW(ProductKey,
  179. Head->FriendlyName,
  180. 0,
  181. NULL, // Class
  182. REG_OPTION_NON_VOLATILE,
  183. KEY_WRITE,
  184. NULL, // Sec Attributes
  185. &ComponentKey,
  186. &disposition
  187. );
  188. if (status != ERROR_SUCCESS) {
  189. WppDebug(1,("[WppInit] Failed to create %ws Key, %d",
  190. Head->FriendlyName, status) );
  191. } else {
  192. GUID guid;
  193. WCHAR guidBuf[WPP_TEXTGUID_LEN];
  194. guid = *Head->ControlGuid;
  195. WppGuidToStr(guidBuf, &guid);
  196. status = RegSetValueExW(ComponentKey,
  197. WPP_REG_TRACE_GUID,
  198. 0, // Reserved
  199. REG_SZ,
  200. (const BYTE*)guidBuf,
  201. sizeof(guidBuf) );
  202. if (status != ERROR_SUCCESS) {
  203. WppDebug(1,("[WppInit] Failed to create GUID value, %1!d!", status) );
  204. }
  205. status = RegSetValueExW(ComponentKey,
  206. WPP_REG_TRACE_BITNAMES,
  207. 0, // Reserved
  208. REG_SZ,
  209. (const BYTE*)Head->BitNames,
  210. (DWORD)(wcslen(Head->BitNames) * sizeof(WCHAR)) );
  211. if (status != ERROR_SUCCESS) {
  212. WppDebug(1,("[WppInit] Failed to create GUID value, %1!d!", status) );
  213. }
  214. }
  215. if (ComponentKey) {
  216. RegCloseKey(ComponentKey);
  217. }
  218. }
  219. exit_gracefully:
  220. LocalFree( pSD );
  221. if (ProductKey) {
  222. RegCloseKey(ProductKey);
  223. }
  224. if (TracingKey) {
  225. RegCloseKey(TracingKey);
  226. }
  227. return status;
  228. }
  229. #endif // #ifndef WPP_MOF_RESOURCENAME
  230. ULONG
  231. WINAPI
  232. WppControlCallback(
  233. IN WMIDPREQUESTCODE RequestCode,
  234. IN PVOID Context,
  235. IN OUT ULONG *InOutBufferSize,
  236. IN OUT PVOID Buffer
  237. )
  238. {
  239. PWPP_TRACE_CONTROL_BLOCK Ctx = (PWPP_TRACE_CONTROL_BLOCK)Context;
  240. TRACEHANDLE Logger;
  241. UCHAR Level;
  242. DWORD Flags;
  243. *InOutBufferSize = 0;
  244. switch (RequestCode)
  245. {
  246. case WMI_ENABLE_EVENTS:
  247. {
  248. Logger = GetTraceLoggerHandle( Buffer );
  249. Level = GetTraceEnableLevel(Logger);
  250. Flags = GetTraceEnableFlags(Logger);
  251. WppDebug(1, ("[WppInit] WMI_ENABLE_EVENTS Ctx %p Flags %x"
  252. " Lev %d Logger %I64x\n",
  253. Ctx, Flags, Level, Logger) );
  254. break;
  255. }
  256. case WMI_DISABLE_EVENTS:
  257. {
  258. Logger = 0;
  259. Flags = 0;
  260. Level = 0;
  261. WppDebug(1, ("[WppInit] WMI_DISABLE_EVENTS Ctx 0x%08p\n", Ctx));
  262. break;
  263. }
  264. default:
  265. {
  266. return(ERROR_INVALID_PARAMETER);
  267. }
  268. }
  269. if (Ctx->Options & WPP_VER_WIN2K_CB_FORWARD_PTR && Ctx->Win2kCb) {
  270. Ctx->Win2kCb->Logger = Logger;
  271. Ctx->Win2kCb->Level = Level;
  272. Ctx->Win2kCb->Flags = Flags;
  273. } else {
  274. if (Ctx->Options & WPP_VER_WHISTLER_CB_FORWARD_PTR && Ctx->Cb) {
  275. Ctx = Ctx->Cb; // use forwarding address
  276. }
  277. Ctx->Logger = Logger;
  278. Ctx->Level = Level;
  279. Ctx->Flags[0] = Flags;
  280. }
  281. return(ERROR_SUCCESS);
  282. }
  283. WPPINIT_EXPORT
  284. VOID WppInitUm(LPCWSTR AppName, PWPP_REGISTRATION_BLOCK Registration)
  285. {
  286. PWPP_REGISTRATION_BLOCK p = Registration;
  287. #ifdef WPP_MOF_RESOURCENAME
  288. #ifdef WPP_DLL
  289. HMODULE hModule = NULL ;
  290. #endif
  291. WCHAR ImagePath[MAX_PATH] = {UNICODE_NULL} ;
  292. WCHAR WppMofResourceName[] = WPP_MOF_RESOURCENAME ;
  293. #endif //#ifdef WPP_MOF_RESOURCENAME
  294. #ifndef WPP_MOF_RESOURCENAME
  295. if (AppName) {
  296. WppPublishTraceInfo(Registration, AppName);
  297. }
  298. #endif // #ifndef WPP_MOF_RESOURCENAME
  299. WppDebug(1, ("Registering %ws\n", AppName) );
  300. for(; p; p = p->Next) {
  301. TRACE_GUID_REGISTRATION FakeTraceGuid;
  302. WPP_REGISTRATION_BLOCK RegBlock = *p;
  303. PWPP_TRACE_CONTROL_BLOCK cb = (PWPP_TRACE_CONTROL_BLOCK)p;
  304. ULONG status;
  305. WppDebug(1,(WPP_GUID_FORMAT " %ws : %d:%ws\n",
  306. WPP_GUID_ELEMENTS(p->ControlGuid),
  307. p->FriendlyName, p->FlagsLen, p->BitNames));
  308. ZeroMemory(cb, sizeof(WPP_TRACE_CONTROL_BLOCK)
  309. + sizeof(ULONG) * (RegBlock.FlagsLen - 1) );
  310. p->Next = RegBlock.Next;
  311. cb->FlagsLen = RegBlock.FlagsLen;
  312. cb->Options = RegBlock.Options;
  313. cb->Ptr = RegBlock.Ptr;
  314. // Jee, do we need this fake trace guid? //
  315. FakeTraceGuid.Guid = RegBlock.ControlGuid;
  316. FakeTraceGuid.RegHandle = 0;
  317. #ifdef WPP_MOF_RESOURCENAME
  318. if (AppName != NULL) {
  319. DWORD Status ;
  320. #ifdef WPP_DLL
  321. if ((hModule = GetModuleHandleW(AppName)) != NULL) {
  322. Status = GetModuleFileNameW(hModule, ImagePath, MAX_PATH) ;
  323. ImagePath[MAX_PATH-1] = '\0';
  324. if (Status == 0) {
  325. WppDebug(1,("RegisterTraceGuids => GetModuleFileName(DLL) Failed 0x%08X\n",GetLastError()));
  326. }
  327. } else {
  328. WppDebug(1,("RegisterTraceGuids => GetModuleHandleW failed for %ws (0x%08X)\n",AppName,GetLastError()));
  329. }
  330. #else // #ifdef WPP_DLL
  331. Status = GetModuleFileNameW(NULL,ImagePath,MAX_PATH);
  332. if (Status == 0) {
  333. WppDebug(1,("GetModuleFileName(EXE) Failed 0x%08X\n",GetLastError()));
  334. }
  335. #endif // #ifdef WPP_DLL
  336. }
  337. WppDebug(1,("registerTraceGuids => registering with WMI, App=%ws, Mof=%ws, ImagePath=%ws\n",AppName,WppMofResourceName,ImagePath));
  338. status = RegisterTraceGuidsW( // Always use Unicode
  339. #else // ifndef WPP_MOF_RESOURCENAME
  340. status = RegisterTraceGuids( // So no change for existing users
  341. #endif // ifndef WPP_MOF_RESOURCENAME
  342. WppControlCallback,
  343. cb, // Context for the callback
  344. RegBlock.ControlGuid, // Control Guid
  345. 1, &FakeTraceGuid, // #TraceGuids, TraceGuid
  346. #ifndef WPP_MOF_RESOURCENAME
  347. 0, //ImagePath,
  348. 0, //ResourceName,
  349. #else // #ifndef WPP_MOF_RESOURCENAME
  350. ImagePath,
  351. WppMofResourceName,
  352. #endif // #ifndef WPP_MOF_RESOURCENAME
  353. &cb->UmRegistrationHandle
  354. );
  355. WppDebug(1, ("RegisterTraceGuid => %d\n", status) );
  356. }
  357. }
  358. WPPINIT_EXPORT
  359. VOID WppCleanupUm(PWPP_REGISTRATION_BLOCK Registration)
  360. {
  361. PWPP_TRACE_CONTROL_BLOCK x = (PWPP_TRACE_CONTROL_BLOCK)Registration;
  362. WppDebug(1, ("Cleanup\n") );
  363. for(; x; x = x->Next) {
  364. WppDebug(1,("UnRegistering %I64x\n", x->UmRegistrationHandle) );
  365. if (x->UmRegistrationHandle) {
  366. UnregisterTraceGuids(x->UmRegistrationHandle);
  367. x->UmRegistrationHandle = (TRACEHANDLE)NULL ;
  368. }
  369. }
  370. }