Source code of Windows XP (NT5)
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.

344 lines
9.4 KiB

  1. //================================================================================
  2. // Copyright (C) 1997 Microsoft Corporation
  3. // Author: RameshV
  4. // Description: command line tool to dump the ds in a way that could be
  5. // understood by the dsini program.
  6. //================================================================================
  7. //BeginExport(overview)
  8. //DOC DSDMP is a command line too very similar to the REGDMP.
  9. //DOC It takes as parameter the LDAP path of the DS object to root the dumping,
  10. //DOC followed by the list of attributes to look for each object found
  11. //DOC The output of this is in the same format as that of the DSINI program's input.
  12. //EndExport(overview)
  13. //BeginImports(headers)
  14. #include <hdrmacro.h>
  15. #include <store.h>
  16. #include <stdio.h>
  17. //EndImports(headers)
  18. //BeginInternal(globals)
  19. static DWORD nTabs = 0;
  20. static LPWSTR RecurseFilter = L"(name=*)" ;
  21. static WCHAR Buffer[1000];
  22. static WCHAR Strings[30][100];
  23. //EndExport(globals)
  24. VOID
  25. ErrorPrint(
  26. IN LPSTORE_HANDLE hStore,
  27. IN DWORD Result,
  28. IN LPWSTR Comment
  29. )
  30. {
  31. if( NULL != hStore ) {
  32. printf("\n#%ws\n#%ws (errror %ld, 0x%lx)\n", hStore->Location, Comment, Result, Result );
  33. return;
  34. }
  35. printf("\n#%ws (errror %ld, 0x%lx)\n", Comment, Result, Result);
  36. }
  37. LPWSTR
  38. ConvertDWORD(
  39. IN DWORD d
  40. )
  41. {
  42. swprintf(Buffer, L"0x%08lx", d);
  43. return Buffer;
  44. }
  45. #define HEX(ch) ( ((ch) < 10)? ((ch) + L'0') : ((ch) + L'A' - 10))
  46. LPWSTR
  47. ConvertBINARY(
  48. IN DWORD l,
  49. IN LPBYTE s
  50. )
  51. {
  52. DWORD i;
  53. i = 0;
  54. while( l ) {
  55. Buffer[i++] = HEX(((*s)>>4));
  56. Buffer[i++] = HEX(((*s) & 0x0F));
  57. l --; s ++;
  58. }
  59. Buffer[i] = L'\0';
  60. return Buffer;
  61. }
  62. VOID
  63. Print(
  64. IN LPWSTR Operation,
  65. IN DWORD Type,
  66. IN LPWSTR AttributeName
  67. )
  68. {
  69. DWORD i;
  70. putchar('\n');
  71. for(i = 1; i < nTabs; i ++ ) printf(" ");
  72. printf("%ws %ld :%ws", Operation, Type, AttributeName);
  73. }
  74. VOID
  75. PrintMore(
  76. IN LPWSTR String
  77. )
  78. {
  79. printf("=%ws", String);
  80. }
  81. VOID
  82. PrintName(
  83. IN LPSTORE_HANDLE hStore
  84. )
  85. {
  86. DWORD i;
  87. if( 1 == nTabs ) return;
  88. putchar('\n');
  89. for(i = 1; i < nTabs; i ++ ) printf(" ");
  90. printf("object");
  91. }
  92. DWORD
  93. AttributeDmp(
  94. IN ADS_ATTR_INFO Attribute
  95. )
  96. {
  97. DWORD Result;
  98. DWORD i;
  99. if( 0 == Attribute.dwNumValues ) {
  100. Print(L"clear", Attribute.dwADsType, Attribute.pszAttrName);
  101. return ERROR_SUCCESS;
  102. }
  103. for( i = 0; i < Attribute.dwNumValues; i ++ ) {
  104. if( ADSTYPE_INVALID == Attribute.pADsValues[i].dwType )
  105. continue;
  106. Print((i==0) ? L"update" : L"append", Attribute.dwADsType, Attribute.pszAttrName);
  107. switch(Attribute.pADsValues[i].dwType) {
  108. case ADSTYPE_INVALID:
  109. // error!
  110. break;
  111. case ADSTYPE_DN_STRING:
  112. PrintMore(Attribute.pADsValues[i].DNString); break;
  113. case ADSTYPE_CASE_EXACT_STRING:
  114. PrintMore(Attribute.pADsValues[i].CaseExactString); break;
  115. case ADSTYPE_CASE_IGNORE_STRING:
  116. PrintMore(Attribute.pADsValues[i].CaseIgnoreString); break;
  117. case ADSTYPE_PRINTABLE_STRING:
  118. PrintMore(Attribute.pADsValues[i].PrintableString); break;
  119. case ADSTYPE_NUMERIC_STRING:
  120. PrintMore(Attribute.pADsValues[i].NumericString); break;
  121. case ADSTYPE_BOOLEAN:
  122. PrintMore(ConvertDWORD(Attribute.pADsValues[i].Boolean)); break;
  123. case ADSTYPE_INTEGER:
  124. PrintMore(ConvertDWORD(Attribute.pADsValues[i].Integer)); break;
  125. case ADSTYPE_OCTET_STRING:
  126. PrintMore(ConvertBINARY(Attribute.pADsValues[i].OctetString.dwLength,
  127. Attribute.pADsValues[i].OctetString.lpValue
  128. )); break;
  129. case ADSTYPE_LARGE_INTEGER:
  130. case ADSTYPE_PROV_SPECIFIC:
  131. break;
  132. case ADSTYPE_OBJECT_CLASS:
  133. PrintMore(Attribute.pADsValues[i].ClassName); break;
  134. }
  135. }
  136. return ERROR_SUCCESS;
  137. }
  138. DWORD
  139. AttributesDmp(
  140. IN LPSTORE_HANDLE hStore,
  141. IN DWORD nAttribs,
  142. IN LPWSTR *Attribs
  143. )
  144. {
  145. HRESULT hResult;
  146. DWORD Result, i ;
  147. DWORD nAttributes;
  148. PADS_ATTR_INFO Attributes;
  149. Attributes = NULL; nAttributes = 0;
  150. hResult = ADSIGetObjectAttributes(
  151. hStore->ADSIHandle,
  152. Attribs,
  153. nAttribs,
  154. &Attributes,
  155. &nAttributes
  156. );
  157. if( FAILED(hResult) ) {
  158. return ERROR_GEN_FAILURE;
  159. }
  160. for( i = 0; i < nAttributes ; i ++ ) {
  161. Result = AttributeDmp(Attributes[i]);
  162. if( ERROR_SUCCESS != Result ) {
  163. ErrorPrint(NULL, Result, L"AttributeDmp");
  164. }
  165. }
  166. if( Attributes ) FreeADsMem(Attributes);
  167. return ERROR_SUCCESS;
  168. }
  169. DWORD
  170. RecurseDmp(
  171. IN OUT LPSTORE_HANDLE hStore,
  172. IN DWORD nAttribs,
  173. IN LPWSTR Attribs[]
  174. )
  175. {
  176. DWORD Result;
  177. STORE_HANDLE hStore2;
  178. nTabs ++;
  179. PrintName(hStore);
  180. Result = AttributesDmp(hStore,nAttribs,Attribs);
  181. if( ERROR_SUCCESS != Result ) {
  182. ErrorPrint(hStore, Result, L"AttributesDmp failed");
  183. }
  184. Result = StoreSetSearchOneLevel(hStore, 0);
  185. if( ERROR_SUCCESS != Result ) {
  186. ErrorPrint(hStore, Result, L"StoreSetSearchOneLevel failed");
  187. nTabs --;
  188. return Result;
  189. }
  190. Result = StoreBeginSearch(hStore, 0, RecurseFilter);
  191. if( ERROR_SUCCESS != Result ) {
  192. ErrorPrint(hStore, Result, L"StoreBeginSearch failed");
  193. } else {
  194. while ( ERROR_SUCCESS == Result ) {
  195. Result = StoreSearchGetNext(hStore, 0, &hStore2);
  196. if( ERROR_SUCCESS != Result ) {
  197. if( ERROR_FILE_NOT_FOUND == Result ) break;
  198. if( ERROR_NO_MORE_ITEMS == Result ) break;
  199. ErrorPrint(hStore, Result, L"StoreSearchGetNext failed");
  200. break;
  201. }
  202. Result = RecurseDmp(&hStore2, nAttribs, Attribs);
  203. if( ERROR_SUCCESS != Result ) {
  204. ErrorPrint(&hStore2, Result, L"RecurseDmp failed");
  205. break;
  206. }
  207. StoreCleanupHandle(&hStore2, 0);
  208. }
  209. StoreEndSearch(hStore, 0);
  210. }
  211. nTabs --;
  212. return ERROR_SUCCESS;
  213. }
  214. LPWSTR
  215. ConvertToLPWSTR(
  216. IN LPSTR s
  217. )
  218. {
  219. LPWSTR u, v;
  220. if( NULL == s ) return L"";
  221. u = LocalAlloc(LMEM_FIXED, (strlen(s)+1)*sizeof(WCHAR));
  222. if( NULL == u ) return L"";
  223. v = u;
  224. while( *v++ = *s++)
  225. ;
  226. return u;
  227. }
  228. void _cdecl main(
  229. int argc,
  230. char *argv[]
  231. )
  232. {
  233. LPWSTR RootName;
  234. LPWSTR *Attribs;
  235. STORE_HANDLE Store, Store2;
  236. DWORD nNames, i;
  237. DWORD Result;
  238. Attribs = NULL;
  239. nNames = argc-2 +1;
  240. if( argc == 1 ) {
  241. printf("Usage: %s Root-DN-To-DUMP Attribute-names-To-DUMP\n", argv[0]);
  242. return;
  243. }
  244. RootName = ConvertToLPWSTR(argv[1]);
  245. if( NULL == RootName ) {
  246. printf("RootDN confusing\n");
  247. return;
  248. }
  249. Attribs = LocalAlloc(LMEM_FIXED, nNames * sizeof(LPWSTR));
  250. if( NULL == Attribs ) {
  251. printf("not enough memory\n");
  252. return;
  253. }
  254. Result = StoreInitHandle(
  255. &Store, 0, NULL, NULL, NULL, NULL,
  256. ADS_SECURE_AUTHENTICATION
  257. );
  258. if( ERROR_SUCCESS != Result ) {
  259. printf("StoreInitHandle: %ld\n", Result);
  260. return;
  261. }
  262. if( wcslen(RootName) == 0 ) {
  263. Result = StoreDuplicateHandle(&Store, 0, &Store2);
  264. } else {
  265. Result = StoreGetHandle(&Store, 0, StoreGetChildType, RootName, &Store2);
  266. if( ERROR_SUCCESS != Result )
  267. Result = StoreGetHandle(&Store, 0, StoreGetAbsoluteSameServerType, RootName, &Store2);
  268. if( ERROR_SUCCESS != Result )
  269. Result = StoreGetHandle(&Store, 0, StoreGetAbsoluteOtherServerType, RootName, &Store2);
  270. }
  271. if( ERROR_SUCCESS != Result ) {
  272. printf("could not find %ws\n", RootName);
  273. return;
  274. }
  275. Result = StoreCleanupHandle(&Store, 0);
  276. printf("################################################################################\n");
  277. printf("## G E N E R E R A T E D D S D U M P ##\n");
  278. printf("## command line : (will be filled in future versions) ##\n");
  279. printf("## DSDSMP.EXE 1.0 ##\n");
  280. printf("################################################################################\n");
  281. printf("object %ws", RootName);
  282. Attribs[0] = L"name";
  283. for( i = 1; i < nNames ; i ++ ) {
  284. Attribs[i] = ConvertToLPWSTR(argv[i+1]);
  285. }
  286. RecurseDmp(&Store2, nNames, Attribs);
  287. putchar('\n');
  288. exit(0);
  289. }
  290. //================================================================================
  291. // end of file
  292. //================================================================================