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.

316 lines
7.1 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. crack.cxx
  5. Abstract:
  6. crack
  7. Author:
  8. Larry Zhu (LZhu) June 1, 2002 Created
  9. Environment:
  10. User Mode
  11. Revision History:
  12. --*/
  13. #include "precomp.hxx"
  14. #pragma hdrstop
  15. #include "crack.hxx"
  16. #include <Ntdsapi.h>
  17. #define SKIP_WSPACE(s) while (*s && (*s == TEXT(' ') || *s == TEXT('\t'))) { ++s; }
  18. #define SKIP_NON_WSPACE(s) while (*s && (*s != TEXT(' ') && *s != TEXT('\t') && *s != TEXT('\n'))) { ++s; }
  19. VOID
  20. Usage(
  21. IN PCTSTR pszApp
  22. )
  23. {
  24. SspiPrint(SSPI_ERROR,
  25. TEXT("\n\nUsage: %s [-domaincontroller <domaincontroller>] [-dnsdomainname <dnsdomainname>]\n")
  26. TEXT("[-flags <flags>] [-formatoffered <formatoffered>] [-formatdesired <formatdesired>]\n")
  27. TEXT("[-names <names>]\n\n"), pszApp);
  28. exit(-1);
  29. }
  30. VOID
  31. ReleaseArgumentList(
  32. IN ULONG cArgs,
  33. IN PTSTR* ppszArgs
  34. )
  35. {
  36. if (ppszArgs)
  37. {
  38. for (ULONG i = 0; i < cArgs; i++)
  39. {
  40. delete [] ppszArgs[i];
  41. }
  42. delete [] ppszArgs;
  43. }
  44. }
  45. HRESULT
  46. String2ArgumentList(
  47. IN PTSTR pszArgs,
  48. OUT ULONG* pcArgs,
  49. OUT PTSTR** pppszArgs
  50. )
  51. {
  52. HRESULT hRetval = S_OK;
  53. ULONG cArgs = 0;
  54. PTSTR* ppszArgs = NULL;
  55. PTSTR pszSave = pszArgs;
  56. *pcArgs = NULL;
  57. *pppszArgs = NULL;
  58. while (pszArgs && *pszArgs)
  59. {
  60. SKIP_WSPACE(pszArgs);
  61. ++cArgs;
  62. // check for quote
  63. if (*pszArgs == TEXT('"'))
  64. {
  65. ++pszArgs;
  66. if (*pszArgs == TEXT('"'))
  67. {
  68. continue;
  69. }
  70. while (*pszArgs && (*pszArgs++ != TEXT('"'))) /* empty */;
  71. if (*(pszArgs - 1) != TEXT('"'))
  72. {
  73. hRetval = E_INVALIDARG;
  74. goto Cleanup;
  75. }
  76. }
  77. else
  78. {
  79. SKIP_NON_WSPACE(pszArgs);
  80. }
  81. }
  82. if (cArgs)
  83. {
  84. pszArgs = pszSave;
  85. ppszArgs = new PTSTR[cArgs];
  86. if (!ppszArgs)
  87. {
  88. hRetval = E_OUTOFMEMORY;
  89. goto Cleanup;
  90. }
  91. RtlZeroMemory(ppszArgs, cArgs * sizeof(PTSTR));
  92. ULONG argc = 0;
  93. while (pszArgs && *pszArgs)
  94. {
  95. SKIP_WSPACE(pszArgs);
  96. PTSTR pStart = pszArgs;
  97. PTSTR pEnd = pStart;
  98. // check for quote
  99. if (*pszArgs == TEXT('"'))
  100. {
  101. ++pszArgs;
  102. pStart = pszArgs;
  103. if (*pszArgs == TEXT('"'))
  104. {
  105. pEnd = pStart;
  106. }
  107. else
  108. {
  109. while (*pszArgs && (*pszArgs++ != TEXT('"'))) /* empty */;
  110. pEnd = pszArgs - 1;
  111. }
  112. }
  113. else
  114. {
  115. SKIP_NON_WSPACE(pszArgs);
  116. pEnd = pszArgs;
  117. }
  118. TCHAR* pszItem = new TCHAR[pEnd - pStart + 1];
  119. if (!pszItem)
  120. {
  121. hRetval = E_OUTOFMEMORY;
  122. goto Cleanup;
  123. }
  124. RtlCopyMemory(
  125. pszItem,
  126. pStart,
  127. (pEnd - pStart) * sizeof(TCHAR)
  128. );
  129. pszItem[pEnd - pStart] = TEXT('\0');
  130. ppszArgs[argc] = pszItem;
  131. ++argc;
  132. }
  133. }
  134. *pppszArgs = ppszArgs;
  135. *pcArgs = cArgs;
  136. cArgs = 0;
  137. ppszArgs = NULL;
  138. Cleanup:
  139. ReleaseArgumentList(cArgs, ppszArgs);
  140. return hRetval;
  141. }
  142. VOID __cdecl
  143. _tmain(
  144. IN INT argc,
  145. IN PTSTR argv[]
  146. )
  147. {
  148. THResult hRetval = S_OK;
  149. HANDLE hDs = NULL;
  150. PTSTR pszDomainController = NULL;
  151. PTSTR pszDnsDomainName = NULL;
  152. PTSTR pszNames = NULL;
  153. ULONG FormatOffered = DS_USER_PRINCIPAL_NAME;
  154. ULONG FormatDesired = DS_NT4_ACCOUNT_NAME;
  155. ULONG Flags = 0;
  156. ULONG cNames = 0;
  157. PTSTR* rpNames = NULL;
  158. DS_NAME_RESULT* pResult = NULL;
  159. ULONG mark = 1;
  160. argc--;
  161. while (argc)
  162. {
  163. if (!lstrcmp(argv[mark], TEXT("-domaincontroller")) && argc > 1)
  164. {
  165. argc--; mark++;
  166. pszDomainController = argv[mark];
  167. argc--; mark++;
  168. }
  169. else if (!lstrcmp(argv[mark], TEXT("-dnsdomainname")) && argc > 1)
  170. {
  171. argc--; mark++;
  172. pszDnsDomainName = argv[mark];
  173. argc--; mark++;
  174. }
  175. else if (!lstrcmp(argv[mark], TEXT("-formatoffered")) && argc > 1)
  176. {
  177. argc--; mark++;
  178. FormatOffered = lstrtol(argv[mark], NULL, 0);
  179. argc--; mark++;
  180. }
  181. else if (!lstrcmp(argv[mark], TEXT("-formatdesired")) && argc > 1)
  182. {
  183. argc--; mark++;
  184. FormatDesired = lstrtol(argv[mark], NULL, 0);
  185. argc--; mark++;
  186. }
  187. else if (!lstrcmp(argv[mark], TEXT("-flags")) && argc > 1)
  188. {
  189. argc--; mark++;
  190. Flags = lstrtol(argv[mark], NULL, 0);
  191. argc--; mark++;
  192. }
  193. else if (!lstrcmp(argv[mark], TEXT("-names")) && argc > 1)
  194. {
  195. argc--; mark++;
  196. pszNames = argv[mark];
  197. argc--; mark++;
  198. }
  199. else if (!lstrcmp(argv[mark], TEXT("-h"))
  200. || !lstrcmp(argv[mark], TEXT("-?"))
  201. || !lstrcmp(argv[mark], TEXT("/h"))
  202. || !lstrcmp(argv[mark], TEXT("/?")))
  203. {
  204. argc--; mark++;
  205. Usage(argv[0]);
  206. }
  207. else
  208. {
  209. Usage(argv[0]);
  210. }
  211. }
  212. hRetval DBGCHK = String2ArgumentList(pszNames, &cNames, &rpNames);
  213. SspiPrint(SSPI_LOG,
  214. TEXT("DC \"%s\", DnsDomain \"%s\", Flags %#x, Names \"%s\", FormatOffered %#x, FormatDesired %#x, cNames %#x\n"),
  215. pszDomainController,
  216. pszDnsDomainName,
  217. Flags,
  218. pszNames,
  219. FormatOffered,
  220. FormatDesired,
  221. cNames);
  222. if (SUCCEEDED(hRetval))
  223. {
  224. hRetval DBGCHK = HResultFromWin32(
  225. DsBind(
  226. pszDomainController,
  227. pszDnsDomainName,
  228. &hDs
  229. ));
  230. }
  231. if (SUCCEEDED(hRetval))
  232. {
  233. hRetval DBGCHK = HResultFromWin32(
  234. DsCrackNames(
  235. hDs,
  236. (DS_NAME_FLAGS) Flags,
  237. (DS_NAME_FORMAT) FormatOffered,
  238. (DS_NAME_FORMAT) FormatDesired,
  239. cNames ,
  240. rpNames,
  241. &pResult
  242. ));
  243. }
  244. if (pResult)
  245. {
  246. for (ULONG i = 0; i < pResult->cItems; i++)
  247. {
  248. SspiPrint(SSPI_LOG,
  249. TEXT("Item %#x: status %#x, domain \"%s\", name \"%s\"\n"),
  250. i,
  251. pResult->rItems[i].status,
  252. pResult->rItems[i].pDomain,
  253. pResult->rItems[i].pName);
  254. }
  255. DsFreeNameResult(pResult);
  256. }
  257. if (hDs)
  258. {
  259. DsUnBind(&hDs);
  260. }
  261. }