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.

426 lines
13 KiB

  1. //--------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1999, Microsoft Corporation
  4. //
  5. // File: info.cxx
  6. //
  7. //--------------------------------------------------------------------------
  8. #define UNICODE 1
  9. #include <stdio.h>
  10. #include <nt.h>
  11. #include <ntrtl.h>
  12. #include <nturtl.h>
  13. #include <windows.h>
  14. #include <shellapi.h>
  15. #include <winldap.h>
  16. #include <stdlib.h>
  17. #include <dsgetdc.h>
  18. #include <lm.h>
  19. #include <lmdfs.h>
  20. #include <dfsfsctl.h>
  21. #include "struct.hxx"
  22. #include "misc.hxx"
  23. #include <dfsutil.hxx>
  24. #define OFFSET_TO_POINTER(f,b) \
  25. ((VOID *)((PCHAR)(f) + (ULONG_PTR)(b)))
  26. CHAR *OutBuf = NULL;
  27. ULONG OutBufSize = 0x1000;
  28. HANDLE DriverHandle = NULL;
  29. CHAR InBuf[0x1000];
  30. CHAR OutBuf2[0x1000];
  31. VOID
  32. GetDCs(
  33. LPWSTR Name);
  34. DWORD
  35. PktInfo(
  36. BOOLEAN fSwDfs,
  37. LPWSTR pwszHexValue)
  38. {
  39. DWORD dwErr = ERROR_SUCCESS;
  40. NTSTATUS NtStatus;
  41. IO_STATUS_BLOCK IoStatusBlock;
  42. OBJECT_ATTRIBUTES objectAttributes;
  43. UNICODE_STRING DfsDriverName;
  44. ULONG Type;
  45. ULONG State;
  46. ULONG Entry;
  47. ULONG Service;
  48. ULONG Level = 0;
  49. PDFS_GET_PKT_ARG pGetPkt = NULL;
  50. OutBuf = (PCHAR) malloc(OutBufSize);
  51. if (OutBuf == NULL) {
  52. dwErr = ERROR_OUTOFMEMORY;
  53. goto Cleanup;
  54. }
  55. if (fSwDfs == TRUE) {
  56. MyPrintf(L"--dfs.sys--\r\n");
  57. RtlInitUnicodeString(&DfsDriverName, DFS_SERVER_NAME);
  58. } else {
  59. MyPrintf(L"--mup.sys--\r\n");
  60. RtlInitUnicodeString(&DfsDriverName, DFS_DRIVER_NAME);
  61. }
  62. if (pwszHexValue != NULL) {
  63. Level = AtoHex(pwszHexValue, &dwErr);
  64. if (dwErr != ERROR_SUCCESS) {
  65. DebugInformation((L"Bad Level %ws\r\n", pwszHexValue));
  66. goto Cleanup;
  67. }
  68. }
  69. InitializeObjectAttributes(
  70. &objectAttributes,
  71. &DfsDriverName,
  72. OBJ_CASE_INSENSITIVE,
  73. NULL,
  74. NULL);
  75. NtStatus = NtCreateFile(
  76. &DriverHandle,
  77. SYNCHRONIZE,
  78. &objectAttributes,
  79. &IoStatusBlock,
  80. NULL,
  81. FILE_ATTRIBUTE_NORMAL,
  82. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  83. FILE_OPEN_IF,
  84. FILE_CREATE_TREE_CONNECTION | FILE_SYNCHRONOUS_IO_NONALERT,
  85. NULL,
  86. 0);
  87. if (!NT_SUCCESS(NtStatus)) {
  88. dwErr = RtlNtStatusToDosError(NtStatus);
  89. DebugInformation((L"NtCreateFile returned 0x%x\r\n", NtStatus));
  90. goto Cleanup;
  91. }
  92. TryAgain:
  93. NtStatus = NtFsControlFile(
  94. DriverHandle,
  95. NULL, // Event,
  96. NULL, // ApcRoutine,
  97. NULL, // ApcContext,
  98. &IoStatusBlock,
  99. FSCTL_DFS_GET_PKT,
  100. NULL,
  101. 0,
  102. OutBuf,
  103. OutBufSize);
  104. if (NtStatus == STATUS_BUFFER_OVERFLOW) {
  105. OutBufSize = *((ULONG *)OutBuf);
  106. free(OutBuf);
  107. OutBuf = (PCHAR) malloc(OutBufSize);
  108. if (OutBuf == NULL) {
  109. dwErr = ERROR_OUTOFMEMORY;
  110. goto Cleanup;
  111. }
  112. goto TryAgain;
  113. }
  114. dwErr = RtlNtStatusToDosError(NtStatus);
  115. if (!NT_SUCCESS(NtStatus))
  116. goto Cleanup;
  117. pGetPkt = (PDFS_GET_PKT_ARG)OutBuf;
  118. MyPrintf(L"%d entries...\r\n", pGetPkt->EntryCount);
  119. for (Entry = 0; Entry < pGetPkt->EntryCount; Entry++) {
  120. pGetPkt->EntryObject[Entry].Prefix = (LPWSTR) OFFSET_TO_POINTER(
  121. pGetPkt->EntryObject[Entry].Prefix,
  122. OutBuf);
  123. pGetPkt->EntryObject[Entry].ShortPrefix = (LPWSTR) OFFSET_TO_POINTER(
  124. pGetPkt->EntryObject[Entry].ShortPrefix,
  125. OutBuf);
  126. pGetPkt->EntryObject[Entry].Address = (PDFS_PKT_ADDRESS_OBJECT *) OFFSET_TO_POINTER(
  127. pGetPkt->EntryObject[Entry].Address,
  128. OutBuf);
  129. for (Service = 0; Service < pGetPkt->EntryObject[Entry].ServiceCount; Service++) {
  130. pGetPkt->EntryObject[Entry].Address[Service] = (PDFS_PKT_ADDRESS_OBJECT)
  131. OFFSET_TO_POINTER(
  132. pGetPkt->EntryObject[Entry].Address[Service],
  133. OutBuf);
  134. }
  135. }
  136. for (Entry = 0; Entry < pGetPkt->EntryCount; Entry++) {
  137. MyPrintf(L"Entry: %ws\r\n", pGetPkt->EntryObject[Entry].Prefix);
  138. MyPrintf(L"ShortEntry: %ws\r\n", pGetPkt->EntryObject[Entry].ShortPrefix);
  139. MyPrintf(L"Expires in %d seconds\r\n", pGetPkt->EntryObject[Entry].ExpireTime);
  140. MyPrintf(L"UseCount: %d Type:0x%x (",
  141. pGetPkt->EntryObject[Entry].UseCount,
  142. pGetPkt->EntryObject[Entry].Type);
  143. Type = pGetPkt->EntryObject[Entry].Type;
  144. if (Type & PKT_ENTRY_TYPE_OFFLINE)
  145. MyPrintf(L" OFFLINE");
  146. if (Type & PKT_ENTRY_TYPE_LOCAL)
  147. MyPrintf(L" LOCAL");
  148. if (Type & PKT_ENTRY_TYPE_STALE)
  149. MyPrintf(L" STALE");
  150. if (Type & PKT_ENTRY_TYPE_LOCAL_XPOINT)
  151. MyPrintf(L" LOCAL_XPOINT");
  152. if (Type & PKT_ENTRY_TYPE_DELETE_PENDING)
  153. MyPrintf(L" DELETE_PENDING");
  154. if (Type & PKT_ENTRY_TYPE_PERMANENT)
  155. MyPrintf(L" PERMANENT");
  156. if (Type & PKT_ENTRY_TYPE_REFERRAL_SVC)
  157. MyPrintf(L" REFERRAL_SVC");
  158. if (Type & PKT_ENTRY_TYPE_SYSVOL)
  159. MyPrintf(L" SYSVOL");
  160. if (Type & PKT_ENTRY_TYPE_OUTSIDE_MY_DOM)
  161. MyPrintf(L" OUTSIDE_MY_DOM");
  162. if (Type & PKT_ENTRY_TYPE_LEAFONLY)
  163. MyPrintf(L" LEAFONLY");
  164. if (Type & PKT_ENTRY_TYPE_NONDFS)
  165. MyPrintf(L" NONDFS");
  166. if (Type & PKT_ENTRY_TYPE_MACHINE)
  167. MyPrintf(L" MACHINE");
  168. if (Type & PKT_ENTRY_TYPE_DFS)
  169. MyPrintf(L" DFS");
  170. if (Type & PKT_ENTRY_TYPE_INSITE_ONLY)
  171. MyPrintf(L" INSITE");
  172. MyPrintf(L" )\r\n");
  173. for (Service = 0; Service < pGetPkt->EntryObject[Entry].ServiceCount; Service++) {
  174. MyPrintf(L"%4d:[%ws] State:0x%02x ",
  175. Service,
  176. pGetPkt->EntryObject[Entry].Address[Service]->ServerShare,
  177. pGetPkt->EntryObject[Entry].Address[Service]->State);
  178. State = pGetPkt->EntryObject[Entry].Address[Service]->State;
  179. //
  180. // Ugly - used to have State == 0x1 mean 'active'. Now we return an
  181. // ULONG with multiple bits set, so 0x1 indicates the old way and
  182. // more than 1 bit set means the new way.
  183. //
  184. if (State == 0x0) {
  185. MyPrintf(L"\r\n");
  186. } else if (State == 0x1) {
  187. MyPrintf(L"( ACTIVE )\r\n");
  188. } else {
  189. MyPrintf(L"(");
  190. if (State & DFS_SERVICE_TYPE_ACTIVE)
  191. MyPrintf(L" ACTIVE");
  192. if (State & DFS_SERVICE_TYPE_OFFLINE)
  193. MyPrintf(L" OFFLINE");
  194. if (Level >= 1) {
  195. if (State & DFS_SERVICE_TYPE_MASTER)
  196. MyPrintf(L" MASTER");
  197. if (State & DFS_SERVICE_TYPE_READONLY)
  198. MyPrintf(L" READONLY");
  199. if (State & DFS_SERVICE_TYPE_LOCAL)
  200. MyPrintf(L" LOCAL");
  201. if (State & DFS_SERVICE_TYPE_REFERRAL)
  202. MyPrintf(L" REFERRAL");
  203. if (State & DFS_SERVICE_TYPE_DOWN_LEVEL)
  204. MyPrintf(L" DOWNLEVEL");
  205. if (State & DFS_SERVICE_TYPE_COSTLIER)
  206. MyPrintf(L" COSTLIER");
  207. }
  208. MyPrintf(L" )\r\n");
  209. }
  210. }
  211. MyPrintf(L"\r\n");
  212. }
  213. Cleanup:
  214. if (OutBuf != NULL) {
  215. free(OutBuf);
  216. OutBuf = NULL;
  217. }
  218. if (DriverHandle != NULL) {
  219. NTSTATUS st;
  220. st = NtClose(DriverHandle);
  221. DriverHandle = NULL;
  222. }
  223. if (dwErr == ERROR_SUCCESS) {
  224. CommandSucceeded = TRUE;
  225. }
  226. return dwErr;
  227. }
  228. DWORD
  229. SpcInfo(
  230. BOOLEAN fSwAll)
  231. {
  232. DWORD dwErr = ERROR_SUCCESS;
  233. NTSTATUS NtStatus;
  234. IO_STATUS_BLOCK IoStatusBlock;
  235. OBJECT_ATTRIBUTES objectAttributes;
  236. UNICODE_STRING DfsDriverName;
  237. WCHAR *wCp;
  238. OutBuf = (PCHAR) malloc(OutBufSize);
  239. if (OutBuf == NULL) {
  240. dwErr = ERROR_OUTOFMEMORY;
  241. goto Cleanup;
  242. }
  243. RtlInitUnicodeString(&DfsDriverName, DFS_DRIVER_NAME);
  244. InitializeObjectAttributes(
  245. &objectAttributes,
  246. &DfsDriverName,
  247. OBJ_CASE_INSENSITIVE,
  248. NULL,
  249. NULL);
  250. NtStatus = NtCreateFile(
  251. &DriverHandle,
  252. SYNCHRONIZE,
  253. &objectAttributes,
  254. &IoStatusBlock,
  255. NULL,
  256. FILE_ATTRIBUTE_NORMAL,
  257. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  258. FILE_OPEN_IF,
  259. FILE_CREATE_TREE_CONNECTION | FILE_SYNCHRONOUS_IO_NONALERT,
  260. NULL,
  261. 0);
  262. if (!NT_SUCCESS(NtStatus)) {
  263. dwErr = RtlNtStatusToDosError(NtStatus);
  264. DebugInformation((L"NtCreateFile returned 0x%x\r\n", NtStatus));
  265. ErrorMessage(dwErr);
  266. goto Cleanup;
  267. } else {
  268. CommandSucceeded = TRUE;
  269. }
  270. TryAgain:
  271. wcscpy((WCHAR *)InBuf, L"*");
  272. NtStatus = NtFsControlFile(
  273. DriverHandle,
  274. NULL, // Event,
  275. NULL, // ApcRoutine,
  276. NULL, // ApcContext,
  277. &IoStatusBlock,
  278. FSCTL_DFS_GET_SPC_TABLE,
  279. InBuf,
  280. sizeof(InBuf),
  281. OutBuf,
  282. OutBufSize);
  283. if (NtStatus == STATUS_BUFFER_OVERFLOW) {
  284. OutBufSize = *((ULONG *)OutBuf);
  285. free(OutBuf);
  286. OutBuf = (PCHAR)malloc(OutBufSize);
  287. if (OutBuf == NULL) {
  288. dwErr = ERROR_OUTOFMEMORY;
  289. goto Cleanup;
  290. }
  291. goto TryAgain;
  292. }
  293. dwErr = RtlNtStatusToDosError(NtStatus);
  294. if (NtStatus == STATUS_SUCCESS) {
  295. wCp = (WCHAR *)OutBuf;
  296. while (*wCp) {
  297. MyPrintf(L"[%c][%ws]\r\n", *wCp, (wCp+1));
  298. wCp += wcslen(wCp) + 1;
  299. }
  300. }
  301. wcscpy((WCHAR *)InBuf, L"");
  302. NtStatus = NtFsControlFile(
  303. DriverHandle,
  304. NULL, // Event,
  305. NULL, // ApcRoutine,
  306. NULL, // ApcContext,
  307. &IoStatusBlock,
  308. FSCTL_DFS_GET_SPC_TABLE,
  309. InBuf,
  310. sizeof(InBuf),
  311. OutBuf,
  312. OutBufSize);
  313. if (NtStatus == STATUS_BUFFER_OVERFLOW) {
  314. OutBufSize = *((ULONG *)OutBuf);
  315. OutBuf = (PCHAR)malloc(OutBufSize);
  316. if (OutBuf == NULL) {
  317. dwErr = ERROR_OUTOFMEMORY;
  318. goto Cleanup;
  319. }
  320. goto TryAgain;
  321. }
  322. dwErr = RtlNtStatusToDosError(NtStatus);
  323. if (NtStatus == STATUS_SUCCESS) {
  324. wCp = (WCHAR *)OutBuf;
  325. while (*wCp) {
  326. MyPrintf(L"[%c][%ws]\r\n", *wCp, (wCp+1));
  327. if (fSwAll == TRUE || *wCp == '+') {
  328. GetDCs((wCp+1));
  329. }
  330. wCp += wcslen(wCp) + 1;
  331. }
  332. }
  333. Cleanup:
  334. if (DriverHandle != NULL) {
  335. NtClose(DriverHandle);
  336. DriverHandle = NULL;
  337. }
  338. if (OutBuf != NULL) {
  339. free(OutBuf);
  340. OutBuf = NULL;
  341. }
  342. if (dwErr == ERROR_SUCCESS) {
  343. CommandSucceeded = TRUE;
  344. }
  345. return dwErr;
  346. }
  347. VOID
  348. GetDCs(
  349. LPWSTR Name)
  350. {
  351. NTSTATUS NtStatus;
  352. IO_STATUS_BLOCK IoStatusBlock;
  353. WCHAR *wCp;
  354. NtStatus = NtFsControlFile(
  355. DriverHandle,
  356. NULL, // Event,
  357. NULL, // ApcRoutine,
  358. NULL, // ApcContext,
  359. &IoStatusBlock,
  360. FSCTL_DFS_GET_SPC_TABLE,
  361. Name,
  362. (wcslen(Name) + 1) * sizeof(WCHAR),
  363. OutBuf2,
  364. sizeof(OutBuf2));
  365. if (NtStatus == STATUS_SUCCESS) {
  366. wCp = (WCHAR *)OutBuf2;
  367. while (*wCp) {
  368. MyPrintf(L"\t[%ws]\r\n", wCp);
  369. wCp += wcslen(wCp) + 1;
  370. }
  371. }
  372. }