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.

241 lines
4.7 KiB

  1. /*++
  2. Copyright (c) 1993 Microsoft Corporation
  3. Module Name:
  4. util.c
  5. Abstract:
  6. This module contains miscellaneous utility routines used by the
  7. NetWare Workstation service.
  8. Author:
  9. Rita Wong (ritaw) 08-Feb-1993
  10. Revision History:
  11. --*/
  12. #include <nw.h>
  13. #include <nwstatus.h>
  14. //
  15. // Debug trace flag for selecting which trace statements to output
  16. //
  17. #if DBG
  18. DWORD WorkstationTrace = 0;
  19. #endif // DBG
  20. // Terminal Server Addition -
  21. DWORD
  22. NwImpersonateClient(
  23. VOID
  24. )
  25. /*++
  26. Routine Description:
  27. This function calls RpcImpersonateClient to impersonate the current caller
  28. of an API.
  29. Arguments:
  30. None.
  31. Return Value:
  32. NO_ERROR or reason for failure.
  33. --*/
  34. {
  35. DWORD status;
  36. if ((status = RpcImpersonateClient(NULL)) != NO_ERROR) {
  37. KdPrint(("NWWORKSTATION: Fail to impersonate client %ld\n", status));
  38. }
  39. return status;
  40. }
  41. // Terminal Server Addition -
  42. DWORD
  43. NwRevertToSelf(
  44. VOID
  45. )
  46. /*++
  47. Routine Description:
  48. This function calls RpcRevertToSelf to undo an impersonation.
  49. Arguments:
  50. None.
  51. Return Value:
  52. NO_ERROR or reason for failure.
  53. --*/
  54. {
  55. DWORD status;
  56. if ((status = RpcRevertToSelf()) != NO_ERROR) {
  57. KdPrint(("NWWORKSTATION: Fail to revert to self %ld\n", status));
  58. ASSERT(FALSE);
  59. }
  60. return status;
  61. }
  62. VOID
  63. NwLogEvent(
  64. DWORD MessageId,
  65. DWORD NumberOfSubStrings,
  66. LPWSTR *SubStrings,
  67. DWORD ErrorCode
  68. )
  69. {
  70. HANDLE LogHandle;
  71. LogHandle = RegisterEventSourceW (
  72. NULL,
  73. NW_WORKSTATION_SERVICE
  74. );
  75. if (LogHandle == NULL) {
  76. KdPrint(("NWWORKSTATION: RegisterEventSourceW failed %lu\n",
  77. GetLastError()));
  78. return;
  79. }
  80. if (ErrorCode == NO_ERROR) {
  81. //
  82. // No error codes were specified
  83. //
  84. (void) ReportEventW(
  85. LogHandle,
  86. EVENTLOG_ERROR_TYPE,
  87. 0, // event category
  88. MessageId,
  89. (PSID) NULL,
  90. (WORD) NumberOfSubStrings,
  91. 0,
  92. SubStrings,
  93. (PVOID) NULL
  94. );
  95. }
  96. else {
  97. //
  98. // Log the error code specified as binary data
  99. //
  100. (void) ReportEventW(
  101. LogHandle,
  102. EVENTLOG_ERROR_TYPE,
  103. 0, // event category
  104. MessageId,
  105. (PSID) NULL,
  106. (WORD) NumberOfSubStrings,
  107. sizeof(DWORD),
  108. SubStrings,
  109. (PVOID) &ErrorCode
  110. );
  111. }
  112. DeregisterEventSource(LogHandle);
  113. }
  114. //---------------------------------------------------------------------------------//
  115. // NwGetSessionId - This routine assumed that the thread is already executed in //
  116. // the client context. It just get the session ID without impersonating. //
  117. //---------------------------------------------------------------------------------//
  118. NTSTATUS
  119. NwGetSessionId(
  120. OUT PULONG pSessionId
  121. )
  122. /*++
  123. Routine Description:
  124. This function gets the session id of the current thread.
  125. Arguments:
  126. pSessionId - Returns the session id of the current process.
  127. Return Value:
  128. DWORD - NERR_Success or reason for failure.
  129. --*/
  130. {
  131. NTSTATUS ntstatus;
  132. HANDLE CurrentThreadToken;
  133. ULONG SessionId;
  134. ULONG ReturnLength;
  135. ntstatus = NtOpenThreadToken(
  136. NtCurrentThread(),
  137. TOKEN_QUERY,
  138. TRUE, // Use workstation service's security
  139. // context to open thread token
  140. &CurrentThreadToken
  141. );
  142. if (! NT_SUCCESS(ntstatus)) {
  143. KdPrint(("[Wksta] Cannot open the current thread token %08lx\n",
  144. ntstatus));
  145. goto Exit;
  146. }
  147. //
  148. // Get the session id of the current thread
  149. //
  150. ntstatus = NtQueryInformationToken(
  151. CurrentThreadToken,
  152. TokenSessionId,
  153. &SessionId,
  154. sizeof(ULONG),
  155. &ReturnLength
  156. );
  157. if (! NT_SUCCESS(ntstatus)) {
  158. KdPrint(("[Wksta] Cannot query current thread's token %08lx\n",
  159. ntstatus));
  160. NtClose(CurrentThreadToken);
  161. goto Exit;
  162. }
  163. NtClose(CurrentThreadToken);
  164. *pSessionId = SessionId;
  165. Exit:
  166. return ntstatus;
  167. }