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
10 KiB

  1. /****************************************************************************/
  2. // misc.c
  3. //
  4. // TermSrv general code.
  5. //
  6. // Copyright (C) 1997-2000 Microsoft Corporation
  7. /****************************************************************************/
  8. #include "precomp.h"
  9. #pragma hdrstop
  10. #include <rpc.h>
  11. #include <msaudite.h>
  12. #include <ntlsa.h>
  13. #include <authz.h>
  14. #include <authzi.h>
  15. //external procedures used
  16. extern NTSTATUS
  17. AuthzReportEventW( IN PAUTHZ_AUDIT_EVENT_TYPE_HANDLE pHAET,
  18. IN DWORD Flags,
  19. IN ULONG EventId,
  20. IN PSID pUserID,
  21. IN USHORT NumStrings,
  22. IN ULONG DataSize OPTIONAL, //Future - DO NOT USE
  23. IN PUNICODE_STRING* Strings,
  24. IN PVOID Data OPTIONAL //Future - DO NOT USE
  25. );
  26. extern BOOL
  27. AuthzInit( IN DWORD Flags,
  28. IN USHORT CategoryID,
  29. IN USHORT AuditID,
  30. IN USHORT ParameterCount,
  31. OUT PAUTHZ_AUDIT_EVENT_TYPE_HANDLE phAuditEventType
  32. );
  33. NTSTATUS ConfigureEnable(
  34. IN PWSTR ValueName,
  35. IN ULONG ValueType,
  36. IN PVOID ValueData,
  37. IN ULONG ValueLength,
  38. IN PVOID Context,
  39. IN PVOID EntryContext)
  40. {
  41. if (ValueType == REG_DWORD && *(PULONG)ValueData != 0)
  42. return STATUS_SUCCESS;
  43. return STATUS_UNSUCCESSFUL;
  44. }
  45. RTL_QUERY_REGISTRY_TABLE WinStationEnableTable[] = {
  46. { ConfigureEnable, RTL_QUERY_REGISTRY_REQUIRED, WIN_ENABLEWINSTATION,
  47. NULL, REG_NONE, NULL, 0},
  48. { NULL, 0, NULL, NULL, REG_NONE, NULL, 0}
  49. };
  50. NTSTATUS CheckWinStationEnable(LPWSTR WinStationName)
  51. {
  52. NTSTATUS Status;
  53. PWCHAR PathBuf = MemAlloc((wcslen(REG_TSERVER_WINSTATIONS L"\\") + wcslen(WinStationName) + 1) * sizeof(WCHAR));
  54. if (!PathBuf)
  55. {
  56. return STATUS_NO_MEMORY;
  57. }
  58. wcscpy(PathBuf, REG_TSERVER_WINSTATIONS L"\\");
  59. wcscat(PathBuf, WinStationName);
  60. /*
  61. * Check if WinStation is enabled, and return error if not.
  62. */
  63. Status = RtlQueryRegistryValues(RTL_REGISTRY_CONTROL, PathBuf,
  64. WinStationEnableTable, NULL, NULL);
  65. MemFree(PathBuf);
  66. return Status;
  67. }
  68. void InitializeSystemTrace(HKEY hKeyTermSrv)
  69. {
  70. ICA_TRACE Trace;
  71. NTSTATUS Status;
  72. WCHAR SystemDir[256];
  73. DWORD ValueType;
  74. DWORD ValueSize;
  75. ULONG fDebugger;
  76. UINT uiWinDirSize;
  77. ASSERT(hKeyTermSrv != NULL);
  78. RtlZeroMemory( &Trace , sizeof( ICA_TRACE ) );
  79. /*
  80. * Query trace enable flag
  81. */
  82. ValueSize = sizeof(Trace.TraceEnable);
  83. Status = RegQueryValueEx(hKeyTermSrv, WIN_TRACEENABLE, NULL, &ValueType,
  84. (LPBYTE) &Trace.TraceEnable, &ValueSize);
  85. if (Status == ERROR_SUCCESS && Trace.TraceEnable != 0) {
  86. /*
  87. * Query trace class flag
  88. */
  89. ValueSize = sizeof(Trace.TraceClass);
  90. Status = RegQueryValueEx(hKeyTermSrv, WIN_TRACECLASS, NULL,
  91. &ValueType, (LPBYTE)&Trace.TraceClass, &ValueSize);
  92. if (Status != ERROR_SUCCESS) {
  93. Trace.TraceClass = 0xffffffff;
  94. }
  95. /*
  96. * Query trace to debugger flag
  97. */
  98. ValueSize = sizeof(fDebugger);
  99. Status = RegQueryValueEx(hKeyTermSrv, WIN_TRACEDEBUGGER, NULL,
  100. &ValueType, (LPBYTE)&fDebugger, &ValueSize);
  101. if (Status != ERROR_SUCCESS) {
  102. fDebugger = FALSE;
  103. }
  104. Trace.fDebugger = (BOOLEAN)fDebugger;
  105. Trace.fTimestamp = TRUE;
  106. uiWinDirSize = GetWindowsDirectory(SystemDir, sizeof(SystemDir)/sizeof(WCHAR));
  107. if ((uiWinDirSize == 0) ||
  108. ((uiWinDirSize + wcslen(L"\\ICADD.log") + 1) > sizeof(Trace.TraceFile)/sizeof(WCHAR)))
  109. {
  110. // we failed to get the windows directory or we dont have enough buffer for the logfile.
  111. Trace.TraceEnable = 0;
  112. }
  113. else
  114. {
  115. wsprintf(Trace.TraceFile, L"%s\\ICADD.log", SystemDir);
  116. /*
  117. * Open TermDD.
  118. */
  119. Status = IcaOpen(&hTrace);
  120. if (NT_SUCCESS(Status)) {
  121. Status = IcaIoControl(hTrace, IOCTL_ICA_SET_SYSTEM_TRACE, &Trace,
  122. sizeof(Trace), NULL, 0, NULL);
  123. if (!NT_SUCCESS(Status)) {
  124. IcaClose(hTrace);
  125. hTrace = NULL;
  126. }
  127. }
  128. KdPrintEx((DPFLTR_TERMSRV_ID, DPFLTR_TRACE_LEVEL, "TRACE: %S, c:%x, e:%x d:%d, Status=0x%x\n", Trace.TraceFile,
  129. Trace.TraceClass, Trace.TraceEnable, Trace.fDebugger, Status ));
  130. }
  131. }
  132. }
  133. void InitializeTrace(
  134. PWINSTATION pWinStation,
  135. BOOLEAN fListen,
  136. PICA_TRACE pTrace)
  137. {
  138. PWINSTATIONNAME pWinStationName;
  139. NTSTATUS Status;
  140. WCHAR SystemDir[256];
  141. ULONG fDebugger;
  142. ULONG ulSize;
  143. /*
  144. * Use WinStation name if set, else try ListenName,
  145. * otherwise nothing to be done.
  146. */
  147. if (pWinStation->WinStationName[0])
  148. pWinStationName = pWinStation->WinStationName;
  149. else if (pWinStation->ListenName[0])
  150. pWinStationName = pWinStation->ListenName;
  151. else
  152. return;
  153. /*
  154. * Check if trace should be enabled for this WinStation
  155. */
  156. Status = RegWinStationQueryNumValue(SERVERNAME_CURRENT, pWinStationName,
  157. WIN_TRACEENABLE, &pTrace->TraceEnable);
  158. KdPrintEx((DPFLTR_TERMSRV_ID, DPFLTR_TRACE_LEVEL, "TERMSRV: InitializeTrace: LogonId %d, fListen %u, Status=0x%x\n",
  159. pWinStation->LogonId, fListen, Status ));
  160. if (Status == ERROR_SUCCESS && pTrace->TraceEnable != 0) {
  161. /*
  162. * Enable trace for this WinStation
  163. */
  164. if (RegWinStationQueryNumValue(SERVERNAME_CURRENT, pWinStationName,
  165. WIN_TRACECLASS, &pTrace->TraceClass))
  166. pTrace->TraceClass = 0xffffffff;
  167. if (RegWinStationQueryNumValue(SERVERNAME_CURRENT, pWinStationName,
  168. WIN_TRACEDEBUGGER, &fDebugger))
  169. fDebugger = FALSE;
  170. pTrace->fDebugger = (BOOLEAN)fDebugger;
  171. pTrace->fTimestamp = TRUE;
  172. if (RegWinStationQueryValue(SERVERNAME_CURRENT, pWinStationName,
  173. WIN_TRACEOPTION, pTrace->TraceOption,
  174. sizeof(pTrace->TraceOption), &ulSize))
  175. memset(pTrace->TraceOption, 0, sizeof(pTrace->TraceOption));
  176. if (GetWindowsDirectory(SystemDir, sizeof(SystemDir)/sizeof(WCHAR)) == 0) {
  177. return;
  178. }
  179. if (fListen)
  180. wsprintf(pTrace->TraceFile, L"%s\\%s.log", SystemDir,
  181. pWinStationName);
  182. else
  183. wsprintf(pTrace->TraceFile, L"%s\\%u.log", SystemDir,
  184. pWinStation->LogonId);
  185. Status = IcaIoControl(pWinStation->hIca, IOCTL_ICA_SET_TRACE, pTrace,
  186. sizeof(ICA_TRACE), NULL, 0, NULL);
  187. KdPrintEx((DPFLTR_TERMSRV_ID, DPFLTR_TRACE_LEVEL, "TRACE: %S, c:%x, e:%x d:%d, Status=0x%x\n", pTrace->TraceFile,
  188. pTrace->TraceClass, pTrace->TraceEnable, pTrace->fDebugger, Status ));
  189. }
  190. }
  191. /*
  192. * Retrieves non-trace systemwide registry entries and conveys them to TermDD.
  193. * Single location for configuration params.
  194. */
  195. void GetSetSystemParameters(HKEY hKeyTermSrv)
  196. {
  197. TERMSRV_SYSTEM_PARAMS SysParams;
  198. NTSTATUS Status;
  199. DWORD ValueType;
  200. DWORD ValueSize;
  201. HANDLE hTermDD;
  202. ASSERT(hKeyTermSrv != NULL);
  203. // Read the mouse throttle size.
  204. ValueSize = sizeof(SysParams.MouseThrottleSize);
  205. if (RegQueryValueEx(hKeyTermSrv, REG_MOUSE_THROTTLE_SIZE, NULL,
  206. &ValueType, (PCHAR)&SysParams.MouseThrottleSize, &ValueSize) ==
  207. ERROR_SUCCESS) {
  208. // Round the retrieved value up to the next multiple of the
  209. // input size.
  210. SysParams.MouseThrottleSize = (SysParams.MouseThrottleSize +
  211. sizeof(MOUSE_INPUT_DATA) - 1) &
  212. ~(sizeof(MOUSE_INPUT_DATA) - 1);
  213. }
  214. else {
  215. // Set default value.
  216. SysParams.MouseThrottleSize = DEFAULT_MOUSE_THROTTLE_SIZE;
  217. }
  218. // Read the keyboard throttle size.
  219. ValueSize = sizeof(SysParams.KeyboardThrottleSize);
  220. if (RegQueryValueEx(hKeyTermSrv, REG_KEYBOARD_THROTTLE_SIZE, NULL,
  221. &ValueType, (PCHAR)&SysParams.KeyboardThrottleSize, &ValueSize) ==
  222. ERROR_SUCCESS) {
  223. // Round the retrieved value up to the next multiple of the
  224. // input size.
  225. SysParams.KeyboardThrottleSize = (SysParams.KeyboardThrottleSize +
  226. sizeof(KEYBOARD_INPUT_DATA) - 1) &
  227. ~(sizeof(KEYBOARD_INPUT_DATA) - 1);
  228. }
  229. else {
  230. // Set default value.
  231. SysParams.KeyboardThrottleSize = DEFAULT_KEYBOARD_THROTTLE_SIZE;
  232. }
  233. // Open TermDD and send IOCTL.
  234. Status = IcaOpen(&hTermDD);
  235. if (NT_SUCCESS(Status)) {
  236. Status = IcaIoControl(hTermDD, IOCTL_ICA_SET_SYSTEM_PARAMETERS,
  237. &SysParams, sizeof(SysParams), NULL, 0, NULL);
  238. IcaClose(hTermDD);
  239. }
  240. KdPrintEx((DPFLTR_TERMSRV_ID, DPFLTR_TRACE_LEVEL, "SysParams: MouseThrottle=%u, KbdThrottle=%u, Status=0x%x\n",
  241. SysParams.MouseThrottleSize, SysParams.KeyboardThrottleSize,
  242. Status));
  243. }
  244. VOID AuditShutdownEvent(void)
  245. {
  246. RPC_STATUS RPCStatus;
  247. NTSTATUS NtStatus;
  248. AUTHZ_AUDIT_EVENT_TYPE_HANDLE hAET = NULL;
  249. RPCStatus = RpcImpersonateClient(NULL);
  250. if (RPCStatus != RPC_S_OK)
  251. {
  252. DBGPRINT(("TERMSRV: AuditShutdownEvent: Not impersonating! RpcStatus 0x%x\n",RPCStatus));
  253. return;
  254. }
  255. //
  256. //authz Changes
  257. //
  258. if( !AuthzInit( 0, SE_CATEGID_SYSTEM, SE_AUDITID_SYSTEM_SHUTDOWN, 0, &hAET ))
  259. goto ExitFunc;
  260. NtStatus = AuthzReportEventW( &hAET,
  261. APF_AuditSuccess,
  262. 0,
  263. NULL,
  264. 0,
  265. 0,
  266. NULL,
  267. NULL
  268. );
  269. //end authz changes
  270. if (!NT_SUCCESS(NtStatus)) {
  271. KdPrintEx((DPFLTR_TERMSRV_ID, DPFLTR_ERROR_LEVEL, "TERMSRV: Failed to report shutdown event.\n"));
  272. }
  273. ExitFunc:
  274. if( hAET != NULL )
  275. AuthziFreeAuditEventType( hAET );
  276. if (RPCStatus == RPC_S_OK)
  277. RpcRevertToSelf();
  278. }