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.

448 lines
12 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <windows.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stddef.h>
  8. #include <shellapi.h>
  9. #include "dfsadmin.h"
  10. UpdateApiCalls()
  11. {
  12. if ((++ApiCalls % 500) == 0)
  13. {
  14. printf("Api calls completed: %d\n", ApiCalls);
  15. DumpCurrentTime();
  16. }
  17. }
  18. DFSSTATUS
  19. AddNewRoot(
  20. PROOT_DEF pRoot,
  21. LPWSTR NameSpace,
  22. BOOLEAN Update )
  23. {
  24. UNICODE_STRING ServerName;
  25. UNICODE_STRING ShareName;
  26. UNICODE_STRING RootName;
  27. LPWSTR ServerNameString = NULL;
  28. LPWSTR ShareNameString = NULL;
  29. DFSSTATUS Status;
  30. LPWSTR UseRootName;
  31. PLINK_DEF pLink;
  32. UseRootName = NameSpace;
  33. if (UseRootName == NULL)
  34. {
  35. UseRootName = pRoot->RootObjectName;
  36. }
  37. RtlInitUnicodeString(&RootName, UseRootName);
  38. Status = DfsGetPathComponents(&RootName,
  39. &ServerName,
  40. &ShareName,
  41. NULL );
  42. if (Status == ERROR_SUCCESS)
  43. {
  44. Status = DfsCreateWideString(&ServerName, &ServerNameString);
  45. }
  46. if (Status == ERROR_SUCCESS)
  47. {
  48. Status = DfsCreateWideString(&ShareName, &ShareNameString);
  49. }
  50. if (Status == ERROR_SUCCESS)
  51. {
  52. Status = NetDfsAddStdRoot( ServerNameString, ShareNameString, NULL, 0);
  53. if (Status != ERROR_SUCCESS)
  54. {
  55. printf("NetDfsAddStdRoot failed: Status 0x%x, Server %wS Share %wS\n",
  56. Status, ServerNameString, ShareNameString);
  57. }
  58. UpdateApiCalls();
  59. }
  60. if (ServerNameString != NULL)
  61. {
  62. free(ServerNameString);
  63. }
  64. if (ShareNameString != NULL)
  65. {
  66. free(ShareNameString);
  67. }
  68. if ((Status == ERROR_SUCCESS) ||
  69. ((Status == ERROR_FILE_EXISTS) && (Update == TRUE)))
  70. {
  71. for (pLink = pRoot->pLinks; pLink != NULL; pLink = NEXT_LINK_OBJECT(pLink))
  72. {
  73. Status = AddNewLink( UseRootName, pLink, Update);
  74. if ((Status != ERROR_SUCCESS) &&
  75. ((Status != ERROR_FILE_EXISTS) || (Update != TRUE)))
  76. {
  77. if (DebugOut)
  78. {
  79. fwprintf(DebugOut, L"Add link failed: Root %wS Link %wS\n", UseRootName,
  80. pLink->LinkObjectName);
  81. }
  82. break;
  83. }
  84. }
  85. }
  86. return Status;
  87. }
  88. DFSSTATUS
  89. AddNewLink(
  90. LPWSTR RootNameString,
  91. PLINK_DEF pLink,
  92. BOOLEAN Update )
  93. {
  94. UNICODE_STRING LinkName;
  95. DFSSTATUS Status;
  96. PTARGET_DEF pTarget;
  97. Status = DfsCreateUnicodePathString(&LinkName,
  98. 0, // not unc path: no leading seperators.
  99. RootNameString,
  100. pLink->LinkObjectName);
  101. if (Status == ERROR_SUCCESS)
  102. {
  103. // Status = NetDfsAdd( LinkName.Buffer, NULL, NULL, NULL, DFS_ADD_VOLUME);
  104. UpdateApiCalls();
  105. if ((++AddLinks % 400) == 0)
  106. {
  107. printf("Done with %d Links %d Targets %d Api calls\n",
  108. AddLinks, AddTargets, ApiCalls );
  109. }
  110. if (Status == ERROR_SUCCESS)
  111. {
  112. for (pTarget = pLink->LinkObjectTargets; pTarget != NULL; pTarget = pTarget->NextTarget)
  113. {
  114. Status = AddNewTarget( LinkName.Buffer, pTarget, pTarget == pLink->LinkObjectTargets);
  115. if ((Status != ERROR_SUCCESS) &&
  116. ((Status != ERROR_FILE_EXISTS) || (Update != TRUE)))
  117. {
  118. if (DebugOut)
  119. {
  120. fwprintf(DebugOut, L"Add target failed: Root %wS Link %wS Target %wS\n", RootNameString,
  121. pLink->LinkObjectName,
  122. pTarget->Name);
  123. }
  124. break;
  125. }
  126. }
  127. }
  128. if (Status == ERROR_SUCCESS)
  129. {
  130. Status = UpdateLinkMetaInformation(&LinkName,
  131. pLink);
  132. }
  133. DfsFreeUnicodeString(&LinkName);
  134. }
  135. //printf("Add new link status %x\n", Status);
  136. return Status;
  137. }
  138. DWORD
  139. UpdateLinkMetaInformation(
  140. PUNICODE_STRING pLinkName,
  141. PLINK_DEF pLink)
  142. {
  143. UNREFERENCED_PARAMETER(pLinkName);
  144. UNREFERENCED_PARAMETER(pLink);
  145. return ERROR_SUCCESS;
  146. #if 0
  147. DWORD Status = ERROR_SUCCESS;
  148. if (Status == ERROR_SUCCESS)
  149. {
  150. if (IsObjectCommentSet(pLink))
  151. {
  152. DFS_INFO_100 Info100;
  153. Info100.Comment = pLink->BaseObject.Comment;
  154. Status = NetDfsSetInfo( pLinkName->Buffer, NULL, NULL, 100, (LPBYTE)&Info100);
  155. }
  156. }
  157. if (Status == ERROR_SUCCESS)
  158. {
  159. if (IsObjectStateSet(pLink))
  160. {
  161. DFS_INFO_101 Info101;
  162. Info101.State = pLink->BaseObject.State;
  163. Status = NetDfsSetInfo( pLinkName->Buffer, NULL, NULL, 101, (LPBYTE)&Info101);
  164. }
  165. }
  166. if (Status == ERROR_SUCCESS)
  167. {
  168. if (IsObjectTimeoutSet(pLink))
  169. {
  170. DFS_INFO_102 Info102;
  171. Info102.Timeout = pLink->BaseObject.Timeout;
  172. Status = NetDfsSetInfo( pLinkName->Buffer, NULL, NULL, 102, (LPBYTE)&Info102);
  173. }
  174. }
  175. return Status;
  176. #endif
  177. }
  178. DFSSTATUS
  179. AddNewTarget(
  180. LPWSTR LinkOrRoot,
  181. PTARGET_DEF pTarget,
  182. BOOLEAN FirstTarget)
  183. {
  184. LPWSTR ServerNameString = NULL;
  185. LPWSTR ShareNameString = NULL;
  186. DFSSTATUS Status;
  187. ULONG Flags = 0;
  188. UNICODE_STRING ServerName;
  189. UNICODE_STRING ShareName;
  190. UNICODE_STRING TargetName;
  191. if (FirstTarget)
  192. {
  193. Flags = DFS_ADD_VOLUME;
  194. }
  195. RtlInitUnicodeString(&TargetName, pTarget->Name );
  196. Status = DfsGetFirstComponent(&TargetName,
  197. &ServerName,
  198. &ShareName );
  199. if (Status == ERROR_SUCCESS)
  200. Status = DfsCreateWideString(&ServerName, &ServerNameString);
  201. if (Status == ERROR_SUCCESS)
  202. Status = DfsCreateWideString(&ShareName, &ShareNameString);
  203. if (Status == ERROR_SUCCESS) {
  204. Status = NetDfsAdd( LinkOrRoot, ServerNameString, ShareNameString, NULL, Flags);
  205. if ((Status != ERROR_SUCCESS) && (Status != ERROR_NOT_FOUND))
  206. {
  207. printf("NetDfsAdd failed: Status 0x%x, Link %wS Server %wS Share %wS Flags 0x%x\n",
  208. Status, LinkOrRoot, ServerNameString, ShareNameString, Flags);
  209. }
  210. if (Status == ERROR_NOT_FOUND)
  211. {
  212. Status = NetDfsAdd( LinkOrRoot, ServerNameString, ShareNameString, NULL, DFS_ADD_VOLUME);
  213. if (Status != ERROR_SUCCESS)
  214. {
  215. printf("NetDfsAdd failed again!: Status 0x%x, Link %wS Server %wS Share %wS Flags 0x%x\n",
  216. Status, LinkOrRoot, ServerNameString, ShareNameString, Flags);
  217. }
  218. }
  219. UpdateApiCalls();
  220. }
  221. if (DebugOut)
  222. {
  223. fwprintf(DebugOut, L"Add new target %wS %wS %wS status %x\n",
  224. LinkOrRoot,
  225. ServerNameString,
  226. ShareNameString,
  227. Status);
  228. }
  229. if (ServerNameString != NULL)
  230. {
  231. free(ServerNameString);
  232. }
  233. if (ShareNameString != NULL)
  234. {
  235. free(ShareNameString);
  236. }
  237. AddTargets++;
  238. return Status;
  239. }
  240. DeleteRoot(
  241. PROOT_DEF pRoot )
  242. {
  243. LPWSTR ServerNameString = NULL;
  244. LPWSTR ShareNameString = NULL;
  245. DFSSTATUS Status;
  246. UNICODE_STRING ServerName;
  247. UNICODE_STRING ShareName;
  248. UNICODE_STRING RootName;
  249. RtlInitUnicodeString(&RootName, pRoot->RootObjectName);
  250. Status = DfsGetPathComponents(&RootName,
  251. &ServerName,
  252. &ShareName,
  253. NULL );
  254. if (Status == ERROR_SUCCESS)
  255. Status = DfsCreateWideString(&ServerName, &ServerNameString);
  256. if (Status == ERROR_SUCCESS)
  257. Status = DfsCreateWideString(&ShareName, &ShareNameString);
  258. if (Status == ERROR_SUCCESS)
  259. {
  260. Status = NetDfsRemoveStdRoot( ServerNameString, ShareNameString, 0);
  261. if (Status != ERROR_SUCCESS)
  262. {
  263. printf("NetDfsremovedStdRoot failed: Status 0x%x, Server %wS Share %wS\n",
  264. Status, ServerNameString, ShareNameString);
  265. }
  266. }
  267. if (DebugOut)
  268. {
  269. fwprintf(DebugOut, L"Remove root %wS, Status %x\n", RootName.Buffer, Status);
  270. }
  271. if (ServerNameString != NULL)
  272. {
  273. free(ServerNameString);
  274. }
  275. if (ShareNameString != NULL)
  276. {
  277. free(ShareNameString);
  278. }
  279. return Status;
  280. }
  281. DeleteLink(
  282. LPWSTR RootNameString,
  283. PLINK_DEF pLink )
  284. {
  285. UNICODE_STRING LinkName;
  286. DFSSTATUS Status;
  287. Status = DfsCreateUnicodePathString(&LinkName, FALSE, RootNameString, pLink->LinkObjectName);
  288. if (Status == ERROR_SUCCESS)
  289. {
  290. Status = NetDfsRemove( LinkName.Buffer, NULL, NULL);
  291. if (Status != ERROR_SUCCESS)
  292. {
  293. printf("NetDfsRemove Failed 0x%x, for link %wS\n",
  294. Status, LinkName.Buffer);
  295. }
  296. UpdateApiCalls();
  297. if (DebugOut)
  298. {
  299. fwprintf(DebugOut, L"Removed Link %wS, Status %x\n", LinkName.Buffer, Status);
  300. }
  301. DfsFreeUnicodeString(&LinkName);
  302. }
  303. RemoveLinks++;
  304. return Status;
  305. }
  306. DeleteTarget(
  307. LPWSTR LinkOrRoot,
  308. PTARGET_DEF pTarget )
  309. {
  310. LPWSTR ServerNameString = NULL;
  311. LPWSTR ShareNameString = NULL;
  312. DFSSTATUS Status = STATUS_SUCCESS;
  313. UNICODE_STRING ServerName;
  314. UNICODE_STRING ShareName;
  315. UNICODE_STRING TargetName;
  316. RtlInitUnicodeString( &TargetName, pTarget->Name);
  317. if (Status == ERROR_SUCCESS)
  318. {
  319. Status = DfsGetFirstComponent(&TargetName,
  320. &ServerName,
  321. &ShareName );
  322. }
  323. if (Status == ERROR_SUCCESS)
  324. Status = DfsCreateWideString(&ServerName, &ServerNameString);
  325. if (Status == ERROR_SUCCESS)
  326. Status = DfsCreateWideString(&ShareName, &ShareNameString);
  327. if (Status == ERROR_SUCCESS) {
  328. Status = NetDfsRemove( LinkOrRoot, ServerNameString, ShareNameString);
  329. if (Status != ERROR_SUCCESS)
  330. {
  331. printf("NetDfsRemove Failed 0x%x, for link %wS, Server %wS Share %wS\n",
  332. Status, LinkOrRoot, ServerNameString, ShareNameString);
  333. }
  334. UpdateApiCalls();
  335. }
  336. if (DebugOut)
  337. {
  338. fwprintf(DebugOut, L"Removed Link %wS target %wS, Status %x\n",
  339. LinkOrRoot,
  340. pTarget->Name,
  341. Status);
  342. }
  343. if (ServerNameString != NULL)
  344. {
  345. free(ServerNameString);
  346. }
  347. if (ShareNameString != NULL)
  348. {
  349. free(ShareNameString);
  350. }
  351. RemoveTargets++;
  352. return Status;
  353. }
  354. DFSSTATUS
  355. DfsCreateWideString(
  356. PUNICODE_STRING pName,
  357. LPWSTR *pString )
  358. {
  359. DFSSTATUS Status = ERROR_SUCCESS;
  360. *pString = malloc(pName->Length + sizeof(WCHAR));
  361. if (*pString != NULL)
  362. {
  363. RtlCopyMemory(*pString, pName->Buffer, pName->Length);
  364. (*pString)[pName->Length/sizeof(WCHAR)] = 0;
  365. }
  366. else {
  367. Status = ERROR_NOT_ENOUGH_MEMORY;
  368. }
  369. return Status;
  370. }