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.

276 lines
5.8 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1997.
  5. //
  6. // File: util.c
  7. //
  8. // Contents: General utility functions
  9. //
  10. // Functions:
  11. //
  12. // History: 2-20-97 RichardW Created
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "xtcbpkg.h"
  16. #include "stdio.h"
  17. BOOL
  18. XtcbDupSecurityString(
  19. PSECURITY_STRING Dest,
  20. PSECURITY_STRING Source
  21. )
  22. {
  23. if ( Source->Buffer == NULL )
  24. {
  25. ZeroMemory( Dest, sizeof( SECURITY_STRING ) );
  26. return TRUE ;
  27. }
  28. Dest->Buffer = LocalAlloc( LMEM_FIXED, Source->Length + sizeof(WCHAR) );
  29. if ( Dest->Buffer )
  30. {
  31. Dest->MaximumLength = Source->Length + sizeof( WCHAR ) ;
  32. Dest->Length = Source->Length ;
  33. CopyMemory( Dest->Buffer, Source->Buffer, Source->Length);
  34. Dest->Buffer[ Dest->Length / sizeof( WCHAR ) ] = L'\0';
  35. return TRUE ;
  36. }
  37. return FALSE ;
  38. }
  39. BOOL
  40. XtcbAnsiStringToSecurityString(
  41. PSECURITY_STRING Dest,
  42. PSTRING Source
  43. )
  44. {
  45. int len;
  46. len = (Source->Length + 1) * sizeof(WCHAR) ;
  47. // overkill, but safe
  48. Dest->Buffer = LocalAlloc( LMEM_FIXED, len );
  49. if ( Dest->Buffer )
  50. {
  51. Dest->Length = (USHORT) (len - sizeof(WCHAR)) ;
  52. Dest->MaximumLength = (USHORT) len ;
  53. MultiByteToWideChar( CP_ACP, 0,
  54. Source->Buffer, -1,
  55. Dest->Buffer, len / sizeof(WCHAR) );
  56. return TRUE ;
  57. }
  58. return FALSE ;
  59. }
  60. BOOL
  61. XtcbSecurityStringToAnsiString(
  62. PSTRING Dest,
  63. PSECURITY_STRING Source
  64. )
  65. {
  66. int len ;
  67. len = (Source->Length / sizeof(WCHAR)) + 1 ;
  68. Dest->Buffer = LocalAlloc( LMEM_FIXED, len );
  69. if ( Dest->Buffer )
  70. {
  71. Dest->Length = (USHORT) (len - 1) ;
  72. Dest->MaximumLength = (USHORT) len ;
  73. WideCharToMultiByte( CP_ACP, 0,
  74. Source->Buffer, -1,
  75. Dest->Buffer, len,
  76. NULL, NULL );
  77. return TRUE ;
  78. }
  79. return FALSE ;
  80. }
  81. BOOL
  82. XtcbDupStringToSecurityString(
  83. PSECURITY_STRING Dest,
  84. PWSTR Source
  85. )
  86. {
  87. ULONG Len ;
  88. Len = (wcslen( Source ) + 1) * 2 ;
  89. Dest->Buffer = LocalAlloc( LMEM_FIXED, Len );
  90. if ( Dest->Buffer )
  91. {
  92. Dest->MaximumLength = (USHORT) Len ;
  93. Dest->Length = (USHORT) Len - 2 ;
  94. CopyMemory( Dest->Buffer, Source, Len );
  95. return TRUE ;
  96. }
  97. return FALSE ;
  98. }
  99. BOOL
  100. XtcbGenerateChallenge(
  101. PUCHAR Challenge,
  102. ULONG Length,
  103. PULONG Actual
  104. )
  105. {
  106. CHAR Temp[ MAX_PATH ];
  107. LUID Unique;
  108. ULONG Len ;
  109. AllocateLocallyUniqueId( &Unique );
  110. _snprintf( Temp, MAX_PATH, "<%x%x.%x%x@%s>",
  111. GetCurrentProcessId(), GetCurrentThreadId(),
  112. Unique.HighPart, Unique.LowPart,
  113. XtcbDnsName.Buffer
  114. );
  115. Len = strlen( Temp );
  116. if ( Len < Length )
  117. {
  118. strcpy( Challenge, Temp );
  119. *Actual = Len;
  120. return TRUE ;
  121. }
  122. *Actual = Len + 1;
  123. return FALSE ;
  124. }
  125. BOOL
  126. XtcbCaptureAuthData(
  127. PVOID pvAuthData,
  128. PSEC_WINNT_AUTH_IDENTITY * AuthData
  129. )
  130. {
  131. SEC_WINNT_AUTH_IDENTITY Auth ;
  132. PSEC_WINNT_AUTH_IDENTITY pAuth ;
  133. SECURITY_STATUS Status ;
  134. ULONG TotalSize ;
  135. PWSTR Current ;
  136. ZeroMemory( &Auth, sizeof( Auth ) );
  137. Status = LsaTable->CopyFromClientBuffer(
  138. NULL,
  139. sizeof( SEC_WINNT_AUTH_IDENTITY ),
  140. & Auth,
  141. pvAuthData );
  142. if ( !NT_SUCCESS( Status ) )
  143. {
  144. return FALSE ;
  145. }
  146. if ( Auth.Flags & SEC_WINNT_AUTH_IDENTITY_ANSI )
  147. {
  148. return FALSE ;
  149. }
  150. TotalSize = sizeof( SEC_WINNT_AUTH_IDENTITY ) +
  151. ( Auth.UserLength + 1 +
  152. Auth.DomainLength + 1 +
  153. Auth.PasswordLength + 1 ) * sizeof( WCHAR );
  154. pAuth = (PSEC_WINNT_AUTH_IDENTITY) LocalAlloc( LMEM_FIXED | LMEM_ZEROINIT,
  155. TotalSize );
  156. if ( !pAuth )
  157. {
  158. return FALSE ;
  159. }
  160. pAuth->Flags = Auth.Flags ;
  161. Current = (PWSTR) (pAuth + 1);
  162. if ( Auth.User )
  163. {
  164. pAuth->User = Current ;
  165. pAuth->UserLength = Auth.UserLength ;
  166. Status = LsaTable->CopyFromClientBuffer(
  167. NULL,
  168. (Auth.UserLength + 1) * sizeof(WCHAR) ,
  169. pAuth->User,
  170. Auth.User );
  171. if ( !NT_SUCCESS( Status ) )
  172. {
  173. goto Error_Cleanup ;
  174. }
  175. Current += Auth.UserLength + 1;
  176. }
  177. if ( Auth.Domain )
  178. {
  179. pAuth->Domain = Current ;
  180. pAuth->DomainLength = Auth.DomainLength ;
  181. Status = LsaTable->CopyFromClientBuffer(
  182. NULL,
  183. (Auth.DomainLength + 1) * sizeof( WCHAR ),
  184. pAuth->Domain,
  185. Auth.Domain );
  186. if ( !NT_SUCCESS( Status ) )
  187. {
  188. goto Error_Cleanup ;
  189. }
  190. Current += Auth.DomainLength + 1;
  191. }
  192. if ( Auth.Password )
  193. {
  194. pAuth->Password = Current ;
  195. pAuth->PasswordLength = Auth.PasswordLength ;
  196. Status = LsaTable->CopyFromClientBuffer(
  197. NULL,
  198. (Auth.PasswordLength + 1) * sizeof( WCHAR ),
  199. pAuth->Password,
  200. Auth.Password );
  201. if ( !NT_SUCCESS( Status ) )
  202. {
  203. goto Error_Cleanup ;
  204. }
  205. Current += Auth.PasswordLength + 1;
  206. }
  207. *AuthData = pAuth ;
  208. return TRUE ;
  209. Error_Cleanup:
  210. LocalFree( pAuth );
  211. return FALSE ;
  212. }