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.

425 lines
10 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Function: main
  4. //
  5. // Arguments:
  6. // argc, argv: the passed in argument list.
  7. //
  8. // Description: This routine initializes the dfs server, and creates
  9. // a worker thread that will be responsible for periodic
  10. // work (such as scavenging and refreshing). It then calls
  11. // into the RPC code to start processing client requests.
  12. //
  13. //--------------------------------------------------------------------------
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <windows.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <stddef.h>
  21. #include <shellapi.h>
  22. #include "..\..\lib\dfsgram\dfsobjectdef.h"
  23. #include <dfsprefix.h>
  24. #include <dfsadmin.h>
  25. DFSSTATUS
  26. ProcessCommandLineArg( LPWSTR Arg );
  27. ULONG AddLinks, RemoveLinks, AddTargets, RemoveTargets, ApiCalls;
  28. VOID
  29. ScriptUsage();
  30. VOID
  31. ProcessDfsRoots(
  32. PROOT_DEF pRoots,
  33. LPWSTR NameSpace,
  34. BOOLEAN Update);
  35. VOID
  36. VerifyDfsRoots(
  37. PROOT_DEF pRoots,
  38. LPWSTR NameSpace );
  39. VOID
  40. SetDfsRoots(
  41. PROOT_DEF pRoots,
  42. LPWSTR NameSpace );
  43. #define MAKEARG(x) \
  44. WCHAR Arg##x[] = L"/" L#x L":"; \
  45. LONG ArgLen##x = (sizeof(Arg##x) / sizeof(WCHAR)) - 1; \
  46. BOOLEAN fArg##x;
  47. #define SWITCH(x) \
  48. WCHAR Switch##x[] = L"/" L#x ; \
  49. BOOLEAN fSwitch##x;
  50. MAKEARG(Import);
  51. MAKEARG(Export);
  52. MAKEARG(Name);
  53. MAKEARG(DebugFile);
  54. SWITCH(Debug);
  55. SWITCH(Verify);
  56. SWITCH(Update);
  57. SWITCH(Set);
  58. LPWSTR ImportFile = NULL;
  59. LPWSTR NameSpace = NULL;
  60. LPWSTR ExportFile = NULL;
  61. LPWSTR DebugFile = NULL;
  62. FILE *ExportOut;
  63. FILE *DebugOut = NULL;
  64. extern int DumpRoots(PROOT_DEF pRoot,
  65. FILE *DebugOut );
  66. VOID
  67. DumpCurrentTime()
  68. {
  69. SYSTEMTIME CurrentTime;
  70. GetLocalTime( &CurrentTime );
  71. printf("Current Time: %d\\%d\\%d, %d:%d:%d:%d\n",
  72. CurrentTime.wMonth, CurrentTime.wDay, CurrentTime.wYear,
  73. CurrentTime.wHour, CurrentTime.wMinute, CurrentTime.wSecond,
  74. CurrentTime.wMilliseconds );
  75. }
  76. _cdecl
  77. main(
  78. int argc,
  79. char *argv[])
  80. {
  81. LPWSTR CommandLine;
  82. LPWSTR *argvw;
  83. DFSSTATUS Status = ERROR_SUCCESS;
  84. int argcw = 0;
  85. int i;
  86. PROOT_DEF pRoot = NULL;
  87. UNREFERENCED_PARAMETER(argv);
  88. UNREFERENCED_PARAMETER(argc);
  89. //
  90. // Get the command line in Unicode
  91. //
  92. CommandLine = GetCommandLine();
  93. argvw = CommandLineToArgvW(CommandLine, &argcw);
  94. ExportOut = stdout;
  95. //
  96. // Process each argument on the command line.
  97. //
  98. for (i = 1; i < argcw; i++) {
  99. Status = ProcessCommandLineArg(argvw[i]);
  100. if (Status != ERROR_SUCCESS)
  101. {
  102. ScriptUsage();
  103. exit(-1);
  104. }
  105. }
  106. if (DfsPrefixTableInit() != STATUS_SUCCESS)
  107. {
  108. printf("Prefix table init failed \n");
  109. exit(-1);
  110. }
  111. DumpCurrentTime();
  112. if (Status == ERROR_SUCCESS && fArgImport)
  113. {
  114. printf("input file is %wS\n", ImportFile );
  115. pRoot = GetDfsTrees( ImportFile );
  116. if ((pRoot != NULL) && (fSwitchDebug == TRUE))
  117. {
  118. DumpRoots( pRoot, DebugOut);
  119. }
  120. if (fSwitchVerify)
  121. {
  122. VerifyDfsRoots(pRoot, NameSpace);
  123. }
  124. else if (fSwitchSet)
  125. {
  126. SetDfsRoots(pRoot, NameSpace);
  127. }
  128. else if (fSwitchUpdate)
  129. {
  130. ProcessDfsRoots( pRoot, NameSpace, TRUE );
  131. }
  132. else {
  133. ProcessDfsRoots( pRoot, NameSpace, FALSE);
  134. }
  135. }
  136. else if (Status == ERROR_SUCCESS && fArgName)
  137. {
  138. DfsView(NameSpace, ExportOut);
  139. }
  140. else {
  141. ScriptUsage();
  142. }
  143. DumpCurrentTime();
  144. printf("DfsServer is exiting with status %x\n", Status);
  145. exit(0);
  146. }
  147. //+-------------------------------------------------------------------------
  148. //
  149. // Function: ProcessCommandLineArg - process the command line
  150. //
  151. // Arguments: Arg - the argument to process
  152. //
  153. // Returns: Status
  154. // ERROR_SUCCESS on success
  155. // ERROR status code otherwise
  156. //
  157. //
  158. // Description: This routine inteprets the passed in argument and
  159. // sets appropriate flags with which the server should
  160. // be initialized.
  161. //
  162. //--------------------------------------------------------------------------
  163. DFSSTATUS
  164. ProcessCommandLineArg( LPWSTR Arg )
  165. {
  166. LONG ArgLen;
  167. DFSSTATUS Status = ERROR_SUCCESS;
  168. if (Arg == NULL) {
  169. Status = ERROR_INVALID_PARAMETER;
  170. }
  171. if (Status == ERROR_SUCCESS)
  172. {
  173. ArgLen = wcslen(Arg);
  174. if (_wcsnicmp(Arg, ArgImport, ArgLenImport) == 0)
  175. {
  176. fArgImport = TRUE;
  177. ImportFile = &Arg[ArgLenImport];
  178. if (wcslen(ImportFile) == 0)
  179. {
  180. Status = ERROR_INVALID_PARAMETER;
  181. ImportFile = NULL;
  182. }
  183. }
  184. else if (_wcsnicmp(Arg, ArgExport, ArgLenExport) == 0)
  185. {
  186. fArgExport = TRUE;
  187. ExportFile = &Arg[ArgLenExport];
  188. if (wcslen(ExportFile) == 0)
  189. {
  190. Status = ERROR_INVALID_PARAMETER;
  191. ExportFile = NULL;
  192. }
  193. else
  194. {
  195. ExportOut = _wfopen(ExportFile, L"w");
  196. }
  197. }
  198. else if (_wcsnicmp(Arg, ArgName, ArgLenName) == 0)
  199. {
  200. fArgName = TRUE;
  201. NameSpace = &Arg[ArgLenName];
  202. printf("Namespace is %wS\n",NameSpace);
  203. if (wcslen(NameSpace) == 0)
  204. {
  205. Status = ERROR_INVALID_PARAMETER;
  206. NameSpace = NULL;
  207. }
  208. }
  209. else if (_wcsnicmp(Arg, ArgDebugFile, ArgLenDebugFile) == 0)
  210. {
  211. fArgDebugFile = TRUE;
  212. DebugFile = &Arg[ArgLenDebugFile];
  213. if (wcslen(DebugFile) == 0)
  214. {
  215. Status = ERROR_INVALID_PARAMETER;
  216. DebugFile = NULL;
  217. }
  218. else {
  219. DebugOut = _wfopen(DebugFile, L"w");
  220. fSwitchDebug = TRUE;
  221. }
  222. }
  223. else if (_wcsicmp(Arg, SwitchDebug) == 0)
  224. {
  225. fSwitchDebug = TRUE;
  226. if (DebugOut == NULL)
  227. {
  228. DebugOut = stdout;
  229. }
  230. }
  231. else if (_wcsicmp(Arg, SwitchVerify) == 0)
  232. {
  233. fSwitchVerify = TRUE;
  234. }
  235. else if (_wcsicmp(Arg, SwitchUpdate) == 0)
  236. {
  237. fSwitchUpdate = TRUE;
  238. }
  239. else if (_wcsicmp(Arg, SwitchSet) == 0)
  240. {
  241. fSwitchSet = TRUE;
  242. }
  243. else {
  244. Status = STATUS_INVALID_PARAMETER;
  245. }
  246. }
  247. return Status;
  248. }
  249. //
  250. // Function: ReferralServerUsage. Usage printout for the referral server.
  251. //
  252. VOID
  253. ScriptUsage()
  254. {
  255. printf("Usage:\n");
  256. printf("/Debug - Enable debug settings\n");
  257. printf("/DebugFile:<File> - name of file to send debug output\n");
  258. printf("/Name:<NameSpace> - namespace of interest\n");
  259. printf("/Verify - Verify the namespace with import file dfs information\n");
  260. printf("/Update - update the namespace with import file dfs information\n");
  261. printf("/Set - Set the import file dfs information in the namespace\n");
  262. printf("/Import:<File> - name of file to import\n");
  263. printf("/Export:<File> - name of file for export data\n");
  264. return;
  265. }
  266. VOID
  267. ProcessDfsRoots(
  268. PROOT_DEF pRoots,
  269. LPWSTR NameSpace,
  270. BOOLEAN Update )
  271. {
  272. PROOT_DEF pRoot;
  273. DFSSTATUS Status;
  274. LPWSTR UseRootName;
  275. for (pRoot = pRoots; pRoot != NULL; pRoot = NEXT_ROOT_OBJECT(pRoot))
  276. {
  277. UseRootName = (NameSpace == NULL) ? pRoot->RootObjectName : NameSpace;
  278. Status = AddNewRoot(pRoot, UseRootName, Update);
  279. if (Status != ERROR_SUCCESS)
  280. {
  281. printf("AddRoot failed for %wS, status %d\n", UseRootName, Status);
  282. }
  283. }
  284. }
  285. VOID
  286. VerifyDfsRoots(
  287. PROOT_DEF pRoots,
  288. LPWSTR NameSpace )
  289. {
  290. PROOT_DEF pRoot;
  291. DFSSTATUS Status, VerifyStatus;
  292. PLINK_DEF pLink;
  293. DWORD ErrorLinks = 0;
  294. LPWSTR UseRootName;
  295. for (pRoot = pRoots; pRoot != NULL; pRoot = NEXT_ROOT_OBJECT(pRoot))
  296. {
  297. UseRootName = (NameSpace == NULL) ? pRoot->RootObjectName : NameSpace;
  298. Status = DfsMerge(pRoot, UseRootName);
  299. if (Status != ERROR_SUCCESS)
  300. {
  301. printf("Verify failed for %wS, status %d\n", UseRootName, Status);
  302. }
  303. else {
  304. for (pLink = pRoot->pLinks; pLink != NULL; pLink = NEXT_LINK_OBJECT(pLink))
  305. {
  306. VerifyStatus = VerifyLink( pLink );
  307. if (VerifyStatus != ERROR_SUCCESS)
  308. {
  309. Status = VerifyStatus;
  310. ErrorLinks++;
  311. }
  312. }
  313. printf("Found %d links not verified (Status %x)\n", ErrorLinks, VerifyStatus);
  314. }
  315. }
  316. }
  317. VOID
  318. SetDfsRoots(
  319. PROOT_DEF pRoots,
  320. LPWSTR NameSpace )
  321. {
  322. PROOT_DEF pRoot;
  323. DFSSTATUS Status;
  324. PLINK_DEF pLink;
  325. DFSSTATUS SetStatus;
  326. LPWSTR UseRootName;
  327. DWORD ErrorLinks = 0;
  328. for (pRoot = pRoots; pRoot != NULL; pRoot = NEXT_ROOT_OBJECT(pRoot))
  329. {
  330. UseRootName = (NameSpace == NULL) ? pRoot->RootObjectName : NameSpace;
  331. Status = DfsMerge(pRoot, UseRootName);
  332. if (Status != ERROR_SUCCESS)
  333. {
  334. printf("Set failed for %wS, status %d\n", UseRootName, Status);
  335. }
  336. else {
  337. AddLinks = 0;
  338. RemoveLinks = 0;
  339. AddTargets = 0;
  340. RemoveTargets = 0;
  341. for (pLink = pRoot->pLinks; pLink != NULL; pLink = NEXT_LINK_OBJECT(pLink))
  342. {
  343. SetStatus = SetLink( UseRootName, pLink );
  344. if (SetStatus != ERROR_SUCCESS)
  345. {
  346. Status = SetStatus;
  347. ErrorLinks++;
  348. }
  349. }
  350. printf("Added %d links, Removed %d\n", AddLinks, RemoveLinks);
  351. printf("Added %d targets, Removed %d\n", AddTargets, RemoveTargets);
  352. printf("%d links failed\n", ErrorLinks);
  353. }
  354. }
  355. }