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.

400 lines
10 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. crti.c
  5. Abstract:
  6. Component test for marshaling and unmarshaling target info
  7. Author:
  8. Cliff Van Dyke (CliffV) October 14, 2000
  9. Environment:
  10. Revision History:
  11. --*/
  12. #define SECURITY_WIN32
  13. #define UNICODE
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <windows.h>
  18. #include <wincred.h>
  19. #include <security.h>
  20. #include <secpkg.h>
  21. #include <stdio.h>
  22. #include <align.h>
  23. // #include <winnetwk.h>
  24. // #include <lmerr.h>
  25. #define MAX_PRINTF_LEN 1024 // Arbitrary.
  26. #define NlPrint(_x_) NlPrintRoutine _x_
  27. VOID
  28. NlPrintRoutine(
  29. IN DWORD DebugFlag,
  30. IN LPSTR Format,
  31. ...
  32. )
  33. {
  34. va_list arglist;
  35. char OutputBuffer[MAX_PRINTF_LEN];
  36. //
  37. // Put a the information requested by the caller onto the line
  38. //
  39. va_start(arglist, Format);
  40. (VOID) vsprintf(OutputBuffer, Format, arglist);
  41. va_end(arglist);
  42. printf( "%s", OutputBuffer );
  43. return;
  44. UNREFERENCED_PARAMETER( DebugFlag );
  45. }
  46. VOID
  47. NlpDumpBuffer(
  48. PVOID Buffer,
  49. DWORD BufferSize
  50. )
  51. /*++
  52. Routine Description:
  53. Dumps the buffer content on to the debugger output.
  54. Arguments:
  55. DebugFlag: Debug flag to pass on to NlPrintRoutine
  56. Buffer: buffer pointer.
  57. BufferSize: size of the buffer.
  58. Return Value:
  59. none
  60. --*/
  61. {
  62. #define NUM_CHARS 16
  63. IN DWORD DebugFlag;
  64. DWORD i, limit;
  65. CHAR TextBuffer[NUM_CHARS + 1];
  66. LPBYTE BufferPtr = Buffer;
  67. //
  68. // Hex dump of the bytes
  69. //
  70. limit = ((BufferSize - 1) / NUM_CHARS + 1) * NUM_CHARS;
  71. for (i = 0; i < limit; i++) {
  72. if (i < BufferSize) {
  73. NlPrint((0,"%02x ", BufferPtr[i]));
  74. if (BufferPtr[i] < 31 ) {
  75. TextBuffer[i % NUM_CHARS] = '.';
  76. } else if (BufferPtr[i] == '\0') {
  77. TextBuffer[i % NUM_CHARS] = ' ';
  78. } else {
  79. TextBuffer[i % NUM_CHARS] = (CHAR) BufferPtr[i];
  80. }
  81. } else {
  82. NlPrint((0," "));
  83. TextBuffer[i % NUM_CHARS] = ' ';
  84. }
  85. if ((i + 1) % NUM_CHARS == 0) {
  86. TextBuffer[NUM_CHARS] = 0;
  87. NlPrint((0," %s\n", TextBuffer));
  88. }
  89. }
  90. UNREFERENCED_PARAMETER( DebugFlag );
  91. }
  92. LPWSTR TypeArray[] = {
  93. L"Generic",
  94. L"Password",
  95. L"Certificate",
  96. L"VisiblePassword",
  97. NULL
  98. };
  99. #define TYPE_COUNT (sizeof(TypeArray)/sizeof(TypeArray[0]))
  100. VOID
  101. PrintTargetInfo(
  102. PCREDENTIAL_TARGET_INFORMATION TargetInformation
  103. )
  104. /*++
  105. Routine Description:
  106. Print a Target Info
  107. Arguments:
  108. TargetInfo to print
  109. Return Value:
  110. None
  111. --*/
  112. {
  113. ULONG i;
  114. printf( "TargetInformation:\n" );
  115. if ( TargetInformation->TargetName != NULL ) {
  116. printf( " TargetName: %ls\n", TargetInformation->TargetName );
  117. }
  118. if ( TargetInformation->NetbiosServerName != NULL ) {
  119. printf( " NetbiosServerName: %ls\n", TargetInformation->NetbiosServerName );
  120. }
  121. if ( TargetInformation->DnsServerName != NULL ) {
  122. printf( " DnsServerName: %ls\n", TargetInformation->DnsServerName );
  123. }
  124. if ( TargetInformation->NetbiosDomainName != NULL ) {
  125. printf( " NetbiosDomainName: %ls\n", TargetInformation->NetbiosDomainName );
  126. }
  127. if ( TargetInformation->DnsDomainName != NULL ) {
  128. printf( " DnsDomainName: %ls\n", TargetInformation->DnsDomainName );
  129. }
  130. if ( TargetInformation->DnsTreeName != NULL ) {
  131. printf( " DnsTreeName: %ls\n", TargetInformation->DnsTreeName );
  132. }
  133. if ( TargetInformation->PackageName != NULL ) {
  134. printf( " PackageName: %ls\n", TargetInformation->PackageName );
  135. }
  136. if ( TargetInformation->Flags != 0 ) {
  137. DWORD LocalFlags = TargetInformation->Flags;
  138. printf( " Flags:" );
  139. if ( LocalFlags & CRED_TI_SERVER_FORMAT_UNKNOWN ) {
  140. printf(" ServerFormatUnknown");
  141. LocalFlags &= ~CRED_TI_SERVER_FORMAT_UNKNOWN;
  142. }
  143. if ( LocalFlags & CRED_TI_DOMAIN_FORMAT_UNKNOWN ) {
  144. printf(" DomainFormatUnknown");
  145. LocalFlags &= ~CRED_TI_DOMAIN_FORMAT_UNKNOWN;
  146. }
  147. if ( LocalFlags != 0 ) {
  148. printf( " 0x%lx", LocalFlags );
  149. }
  150. printf( "\n" );
  151. }
  152. if ( TargetInformation->CredTypeCount != 0 ) {
  153. printf( " Types:" );
  154. for ( i=0; i<TargetInformation->CredTypeCount; i++ ) {
  155. if ( TargetInformation->CredTypes[i] >= 1 && TargetInformation->CredTypes[i] <= TYPE_COUNT ) {
  156. printf(" %ls", TypeArray[TargetInformation->CredTypes[i]-1]);
  157. } else {
  158. printf("<Unknown>");
  159. }
  160. }
  161. printf( "\n" );
  162. }
  163. }
  164. BOOLEAN
  165. DoOne(
  166. PCREDENTIAL_TARGET_INFORMATION TargetInformation
  167. )
  168. /*++
  169. Routine Description:
  170. Test marhsalling and unmarshalling a single Target Info
  171. Arguments:
  172. TargetInfo to test
  173. Return Value:
  174. TRUE on success
  175. --*/
  176. {
  177. NTSTATUS Status;
  178. PCREDENTIAL_TARGET_INFORMATIONW RetTargetInfo;
  179. ULONG RetSize;
  180. PUSHORT Buffer;
  181. ULONG BufferSize;
  182. WCHAR BigBuffer[40000];
  183. printf( "\n\nInput:\n");
  184. PrintTargetInfo( TargetInformation );
  185. Status = CredMarshalTargetInfo( TargetInformation, &Buffer, &BufferSize );
  186. if ( !NT_SUCCESS(Status) ) {
  187. fprintf( stderr, "Cannot convert 0x%lx\n", Status );
  188. return FALSE;
  189. }
  190. printf( "\nBinary:\n");
  191. NlpDumpBuffer( Buffer, BufferSize );
  192. //
  193. // Test passing NULL in all optional parameters
  194. //
  195. Status = CredUnmarshalTargetInfo ( Buffer,
  196. BufferSize,
  197. NULL,
  198. NULL );
  199. if ( !NT_SUCCESS(Status) ) {
  200. fprintf( stderr, "Cannot check format 0x%lx\n", Status );
  201. return FALSE;
  202. }
  203. //
  204. // Test passing NULL for just the allocated buffer
  205. //
  206. Status = CredUnmarshalTargetInfo ( Buffer,
  207. BufferSize,
  208. NULL,
  209. &RetSize );
  210. if ( !NT_SUCCESS(Status) ) {
  211. fprintf( stderr, "Cannot check size 0x%lx\n", Status );
  212. return FALSE;
  213. }
  214. //
  215. // Actually convert the data
  216. //
  217. Status = CredUnmarshalTargetInfo ( Buffer,
  218. BufferSize,
  219. &RetTargetInfo,
  220. &RetSize );
  221. if ( !NT_SUCCESS(Status) ) {
  222. fprintf( stderr, "Cannot convert back 0x%lx\n", Status );
  223. return FALSE;
  224. } else {
  225. printf( "\n\nOutput: %ld\n", RetSize );
  226. PrintTargetInfo( RetTargetInfo );
  227. }
  228. //
  229. // Try again with a preconcatenated string
  230. //
  231. wcscpy( BigBuffer, L"SpnInFrontOfBuffer" );
  232. wcscat( BigBuffer, Buffer );
  233. Status = CredUnmarshalTargetInfo ( BigBuffer,
  234. wcslen(BigBuffer)*sizeof(WCHAR),
  235. &RetTargetInfo,
  236. &RetSize );
  237. if ( !NT_SUCCESS(Status) ) {
  238. fprintf( stderr, "Cannot convert back with prepended 0x%lx\n", Status );
  239. return FALSE;
  240. } else {
  241. printf( "\n\nOutput: %ld\n", RetSize );
  242. PrintTargetInfo( RetTargetInfo );
  243. }
  244. return TRUE;
  245. }
  246. int __cdecl
  247. main ()
  248. {
  249. NTSTATUS Status;
  250. CREDENTIAL_TARGET_INFORMATIONW Ti1;
  251. PCREDENTIAL_TARGET_INFORMATIONW RetTargetInfo;
  252. ULONG RetSize;
  253. PUSHORT Buffer;
  254. ULONG BufferSize;
  255. ULONG CredTypes[] = { 1, 3, 0, 7, 2 };
  256. //
  257. // Do a trivial target info
  258. //
  259. RtlZeroMemory( &Ti1, sizeof(Ti1) );
  260. Ti1.TargetName = L"The name";
  261. if (!DoOne( &Ti1 ) ) {
  262. return 1;
  263. }
  264. //
  265. // More complicated
  266. //
  267. RtlZeroMemory( &Ti1, sizeof(Ti1) );
  268. Ti1.TargetName = L"TargetName";
  269. Ti1.NetbiosServerName = L"NetbiosServerName";
  270. Ti1.DnsServerName = L"DnsServerName";
  271. Ti1.NetbiosDomainName = L"NetbiosDomainName";
  272. Ti1.DnsDomainName = L"DnsDomainName";
  273. Ti1.DnsTreeName = L"DnsTreeName";
  274. Ti1.PackageName = L"PackageName";
  275. Ti1.Flags=0x12345678;
  276. Ti1.CredTypeCount = sizeof(CredTypes)/sizeof(DWORD);
  277. Ti1.CredTypes = CredTypes;
  278. if (!DoOne( &Ti1 ) ) {
  279. return 1;
  280. }
  281. //
  282. // Try one with max length strings
  283. //
  284. RtlZeroMemory( &Ti1, sizeof(Ti1) );
  285. Ti1.TargetName = L"TargetName111111111111111111111111111111111111111111111111111111111111111111222222222222222222222222222222222222222222222222222222221111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
  286. Ti1.NetbiosServerName = L"NetbiosServerName";
  287. Ti1.DnsServerName = L"DnsServerName111111111111111111111111111111111111111111111111111111111111111222222222222222222222222222222222222222222222222222222221111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
  288. Ti1.NetbiosDomainName = L"NetbiosDomainName";
  289. Ti1.DnsDomainName = L"DnsDomainName111111111111111111111111111111111111111111111111111111111111111222222222222222222222222222222222222222222222222222222221111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
  290. Ti1.DnsTreeName = L"DnsTreeName11111111111111111111111111111111111111111111111111111111111111111222222222222222222222222222222222222222222222222222222221111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
  291. Ti1.PackageName = L"PackageName11111111111111111111111111111111111111111111111111111111111111111222222222222222222222222222222222222222222222222222222221111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
  292. Ti1.Flags=0x87654321;
  293. Ti1.CredTypeCount = sizeof(CredTypes)/sizeof(DWORD);
  294. Ti1.CredTypes = CredTypes;
  295. if (!DoOne( &Ti1 ) ) {
  296. return 1;
  297. }
  298. return 0;
  299. }