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.

311 lines
7.7 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. frs.c
  5. Abstract:
  6. This module is a development tool. It exercises the dcpromo and poke
  7. APIs.
  8. Author:
  9. Billy J. Fuller 12-Dec-1997
  10. Environment
  11. User mode winnt
  12. --*/
  13. #define FREE(_x_) { if (_x_) LocalFree(_x_); _x_ = NULL; }
  14. #define WIN_SUCCESS(_x_) ((_x_) == ERROR_SUCCESS)
  15. //
  16. // Is a handle valid?
  17. // Some functions set the handle to NULL and some to
  18. // INVALID_HANDLE_VALUE (-1). This define handles both
  19. // cases.
  20. //
  21. #define HANDLE_IS_VALID(_Handle) ((_Handle) && \
  22. ((_Handle) != INVALID_HANDLE_VALUE))
  23. //
  24. // NT Headers
  25. //
  26. #include <nt.h>
  27. #include <ntrtl.h>
  28. #include <nturtl.h>
  29. //
  30. // UNICODE or ANSI compilation
  31. //
  32. #include <tchar.h>
  33. //
  34. // Windows Headers
  35. //
  36. #include <windows.h>
  37. #include <rpc.h>
  38. //
  39. // C-Runtime Header
  40. //
  41. #include <malloc.h>
  42. #include <memory.h>
  43. #include <process.h>
  44. #include <signal.h>
  45. #include <string.h>
  46. #include <stddef.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <time.h>
  50. #include <string.h>
  51. #include <excpt.h>
  52. #include <conio.h>
  53. #include <sys\types.h>
  54. #include <errno.h>
  55. #include <sys\stat.h>
  56. #include <ctype.h>
  57. #include <winsvc.h>
  58. DWORD
  59. UtilGetTokenInformation(
  60. IN HANDLE TokenHandle,
  61. IN TOKEN_INFORMATION_CLASS TokenInformationClass,
  62. IN DWORD InitialTokenBufSize,
  63. OUT DWORD *OutTokenBufSize,
  64. OUT PVOID *OutTokenBuf
  65. )
  66. /*++
  67. Routine Description:
  68. Retries GetTokenInformation() with larger buffers.
  69. Arguments:
  70. TokenHandle - From OpenCurrentProcess/Thread()
  71. TokenInformationClass - E.g., TokenUser
  72. InitialTokenBufSize - Initial buffer size; 0 = default
  73. OutTokenBufSize - Resultant returned buf size
  74. OutTokenBuf - free with with FrsFree()
  75. Return Value:
  76. OutTokenBufSize - Size of returned info (NOT THE BUFFER SIZE!)
  77. OutTokenBuf - info of type TokenInformationClass. Free with FrsFree().
  78. --*/
  79. {
  80. #undef DEBSUB
  81. #define DEBSUB "UtilGetTokenInformation:"
  82. DWORD WStatus;
  83. *OutTokenBuf = NULL;
  84. *OutTokenBufSize = 0;
  85. //
  86. // Check inputs
  87. //
  88. if (!HANDLE_IS_VALID(TokenHandle)) {
  89. return ERROR_INVALID_PARAMETER;
  90. }
  91. if (InitialTokenBufSize == 0 ||
  92. InitialTokenBufSize > (1024 * 1024)) {
  93. InitialTokenBufSize = 1024;
  94. }
  95. //
  96. // Retry if buffer is too small
  97. //
  98. *OutTokenBufSize = InitialTokenBufSize;
  99. AGAIN:
  100. *OutTokenBuf = LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT,
  101. *OutTokenBufSize);
  102. WStatus = ERROR_SUCCESS;
  103. if (!GetTokenInformation(TokenHandle,
  104. TokenInformationClass,
  105. *OutTokenBuf,
  106. *OutTokenBufSize,
  107. OutTokenBufSize)) {
  108. WStatus = GetLastError();
  109. printf("GetTokenInformation(Info %d, Size %d); WStatus %d\n",
  110. TokenInformationClass,
  111. *OutTokenBufSize,
  112. WStatus);
  113. FREE(*OutTokenBuf);
  114. if (WStatus == ERROR_INSUFFICIENT_BUFFER) {
  115. goto AGAIN;
  116. }
  117. }
  118. return WStatus;
  119. }
  120. VOID
  121. PrintUserName(
  122. VOID
  123. )
  124. /*++
  125. Routine Description:
  126. Print our user name
  127. Arguments:
  128. None.
  129. Return Value:
  130. None.
  131. --*/
  132. {
  133. WCHAR Uname[MAX_PATH + 1];
  134. ULONG Unamesize = MAX_PATH + 1;
  135. if (GetUserName(Uname, &Unamesize)) {
  136. printf("User name is %ws\n", Uname);
  137. } else {
  138. printf("ERROR - Getting user name; WStatus %d\n",
  139. GetLastError());
  140. }
  141. }
  142. #define PRIV_BUF_LENGTH (1024)
  143. VOID
  144. PrintInfo(
  145. )
  146. /*++
  147. Routine Description:
  148. Check if the caller is a member of Groups
  149. Arguments:
  150. ServerHandle
  151. Groups
  152. Return Value:
  153. Win32 Status
  154. --*/
  155. {
  156. DWORD i;
  157. TOKEN_PRIVILEGES *Tp;
  158. TOKEN_SOURCE *Ts;
  159. DWORD ComputerLen;
  160. WCHAR ComputerName[MAX_COMPUTERNAME_LENGTH + 1];
  161. DWORD WStatus;
  162. DWORD TokenBufSize;
  163. PVOID TokenBuf = NULL;
  164. HANDLE TokenHandle = NULL;
  165. HANDLE IdHandle = NULL;
  166. DWORD PrivLen;
  167. WCHAR PrivName[MAX_PATH + 1];
  168. CHAR SourceName[sizeof(Ts->SourceName) + 1];
  169. ComputerLen = MAX_COMPUTERNAME_LENGTH;
  170. ComputerName[0] = L'\0';
  171. if (!GetComputerName(ComputerName, &ComputerLen)) {
  172. printf("GetComputerName(); WStatus %d\n",
  173. GetLastError());
  174. return;
  175. }
  176. printf("Computer name is %ws\n", ComputerName);
  177. PrintUserName();
  178. //
  179. // For this process
  180. //
  181. IdHandle = GetCurrentProcess();
  182. if (!OpenProcessToken(IdHandle,
  183. TOKEN_QUERY | TOKEN_QUERY_SOURCE,
  184. &TokenHandle)) {
  185. WStatus = GetLastError();
  186. printf("Can't open process token; WStatus %d\n", WStatus);
  187. goto CLEANUP;
  188. }
  189. //
  190. // Get the Token privileges from the access token for this thread or process
  191. //
  192. WStatus = UtilGetTokenInformation(TokenHandle,
  193. TokenPrivileges,
  194. 0,
  195. &TokenBufSize,
  196. &TokenBuf);
  197. if (!WIN_SUCCESS(WStatus)) {
  198. printf("UtilGetTokenInformation(TokenPrivileges); WStatus %d\n",
  199. WStatus);
  200. goto CLEANUP;
  201. }
  202. Tp = (TOKEN_PRIVILEGES *)TokenBuf;
  203. for (i = 0; i < Tp->PrivilegeCount; ++i) {
  204. PrivLen = MAX_PATH + 1;
  205. if (!LookupPrivilegeName(NULL,
  206. &Tp->Privileges[i].Luid,
  207. PrivName,
  208. &PrivLen)) {
  209. printf("lookuppriv error %d\n", GetLastError());
  210. exit(0);
  211. }
  212. printf("Priv %2d is %ws :%s:%s:%s:\n",
  213. i,
  214. PrivName,
  215. (Tp->Privileges[i].Attributes & SE_PRIVILEGE_ENABLED_BY_DEFAULT) ? "Enabled by default" : "",
  216. (Tp->Privileges[i].Attributes & SE_PRIVILEGE_ENABLED) ? "Enabled" : "",
  217. (Tp->Privileges[i].Attributes & SE_PRIVILEGE_USED_FOR_ACCESS) ? "Used" : "");
  218. }
  219. FREE(TokenBuf);
  220. //
  221. // Source
  222. //
  223. //
  224. // Get the Token privileges from the access token for this thread or process
  225. //
  226. WStatus = UtilGetTokenInformation(TokenHandle,
  227. TokenSource,
  228. 0,
  229. &TokenBufSize,
  230. &TokenBuf);
  231. if (!WIN_SUCCESS(WStatus)) {
  232. printf("UtilGetTokenInformation(TokenSource); WStatus %d\n",
  233. WStatus);
  234. goto CLEANUP;
  235. }
  236. Ts = (TOKEN_SOURCE *)TokenBuf;
  237. CopyMemory(SourceName, Ts->SourceName, sizeof(Ts->SourceName));
  238. SourceName[sizeof(Ts->SourceName)] = '\0';
  239. printf("Source: %s\n", SourceName);
  240. FREE(TokenBuf);
  241. CLEANUP:
  242. if (HANDLE_IS_VALID(TokenHandle)) {
  243. CloseHandle(TokenHandle);
  244. }
  245. if (HANDLE_IS_VALID(IdHandle)) {
  246. CloseHandle(IdHandle);
  247. }
  248. FREE(TokenBuf);
  249. }
  250. VOID _cdecl
  251. main(
  252. IN DWORD argc,
  253. IN PCHAR *argv
  254. )
  255. /*++
  256. Routine Description:
  257. Process the command line.
  258. Arguments:
  259. argc
  260. argv
  261. Return Value:
  262. Exits with 0 if everything went okay. Otherwise, 1.
  263. --*/
  264. {
  265. PrintInfo();
  266. }