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.

421 lines
11 KiB

  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <winver.h>
  4. #include <tchar.h>
  5. #include <ole2.h>
  6. #include <shlobj.h>
  7. #include <winnt.h>
  8. #include <subauth.h>
  9. #include <lmaccess.h>
  10. #include <lmserver.h>
  11. #include <lmapibuf.h>
  12. #include <lmerr.h>
  13. #include <basetsd.h>
  14. #define SECURITY_WIN32
  15. #define ISSP_LEVEL 32
  16. #define ISSP_MODE 1
  17. #include <sspi.h>
  18. #include "other.h"
  19. //#ifdef UNICODE
  20. BOOL ValidatePassword(IN LPCWSTR UserName,IN LPCWSTR Domain,IN LPCWSTR Password)
  21. /*++
  22. Routine Description:
  23. Uses SSPI to validate the specified password
  24. Arguments:
  25. UserName - Supplies the user name
  26. Domain - Supplies the user's domain
  27. Password - Supplies the password
  28. Return Value:
  29. TRUE if the password is valid.
  30. FALSE otherwise.
  31. --*/
  32. {
  33. SECURITY_STATUS SecStatus;
  34. SECURITY_STATUS AcceptStatus;
  35. SECURITY_STATUS InitStatus;
  36. CredHandle ClientCredHandle;
  37. CredHandle ServerCredHandle;
  38. BOOL ClientCredAllocated = FALSE;
  39. BOOL ServerCredAllocated = FALSE;
  40. CtxtHandle ClientContextHandle;
  41. CtxtHandle ServerContextHandle;
  42. TimeStamp Lifetime;
  43. ULONG ContextAttributes;
  44. PSecPkgInfo PackageInfo = NULL;
  45. ULONG ClientFlags;
  46. ULONG ServerFlags;
  47. TCHAR TargetName[100];
  48. SEC_WINNT_AUTH_IDENTITY_W AuthIdentity;
  49. BOOL Validated = FALSE;
  50. SecBufferDesc NegotiateDesc;
  51. SecBuffer NegotiateBuffer;
  52. SecBufferDesc ChallengeDesc;
  53. SecBuffer ChallengeBuffer;
  54. SecBufferDesc AuthenticateDesc;
  55. SecBuffer AuthenticateBuffer;
  56. AuthIdentity.User = (LPWSTR)UserName;
  57. AuthIdentity.UserLength = lstrlenW(UserName);
  58. AuthIdentity.Domain = (LPWSTR)Domain;
  59. AuthIdentity.DomainLength = lstrlenW(Domain);
  60. AuthIdentity.Password = (LPWSTR)Password;
  61. AuthIdentity.PasswordLength = lstrlenW(Password);
  62. AuthIdentity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
  63. NegotiateBuffer.pvBuffer = NULL;
  64. ChallengeBuffer.pvBuffer = NULL;
  65. AuthenticateBuffer.pvBuffer = NULL;
  66. //
  67. // Get info about the security packages.
  68. //
  69. SecStatus = QuerySecurityPackageInfo( _T("NTLM"), &PackageInfo );
  70. if ( SecStatus != STATUS_SUCCESS ) {
  71. goto error_exit;
  72. }
  73. //
  74. // Acquire a credential handle for the server side
  75. //
  76. SecStatus = AcquireCredentialsHandle(
  77. NULL,
  78. _T("NTLM"),
  79. SECPKG_CRED_INBOUND,
  80. NULL,
  81. &AuthIdentity,
  82. NULL,
  83. NULL,
  84. &ServerCredHandle,
  85. &Lifetime );
  86. if ( SecStatus != STATUS_SUCCESS ) {
  87. goto error_exit;
  88. }
  89. ServerCredAllocated = TRUE;
  90. //
  91. // Acquire a credential handle for the client side
  92. //
  93. SecStatus = AcquireCredentialsHandle(
  94. NULL, // New principal
  95. _T("NTLM"),
  96. SECPKG_CRED_OUTBOUND,
  97. NULL,
  98. &AuthIdentity,
  99. NULL,
  100. NULL,
  101. &ClientCredHandle,
  102. &Lifetime );
  103. if ( SecStatus != STATUS_SUCCESS ) {
  104. goto error_exit;
  105. }
  106. ClientCredAllocated = TRUE;
  107. //
  108. // Get the NegotiateMessage (ClientSide)
  109. //
  110. NegotiateDesc.ulVersion = 0;
  111. NegotiateDesc.cBuffers = 1;
  112. NegotiateDesc.pBuffers = &NegotiateBuffer;
  113. NegotiateBuffer.cbBuffer = PackageInfo->cbMaxToken;
  114. NegotiateBuffer.BufferType = SECBUFFER_TOKEN;
  115. NegotiateBuffer.pvBuffer = LocalAlloc( 0, NegotiateBuffer.cbBuffer );
  116. if ( NegotiateBuffer.pvBuffer == NULL ) {
  117. goto error_exit;
  118. }
  119. ClientFlags = ISC_REQ_MUTUAL_AUTH | ISC_REQ_REPLAY_DETECT;
  120. InitStatus = InitializeSecurityContext(
  121. &ClientCredHandle,
  122. NULL, // No Client context yet
  123. NULL,
  124. ClientFlags,
  125. 0, // Reserved 1
  126. SECURITY_NATIVE_DREP,
  127. NULL, // No initial input token
  128. 0, // Reserved 2
  129. &ClientContextHandle,
  130. &NegotiateDesc,
  131. &ContextAttributes,
  132. &Lifetime );
  133. if ( !NT_SUCCESS(InitStatus) ) {
  134. goto error_exit;
  135. }
  136. //
  137. // Get the ChallengeMessage (ServerSide)
  138. //
  139. NegotiateBuffer.BufferType |= SECBUFFER_READONLY;
  140. ChallengeDesc.ulVersion = 0;
  141. ChallengeDesc.cBuffers = 1;
  142. ChallengeDesc.pBuffers = &ChallengeBuffer;
  143. ChallengeBuffer.cbBuffer = PackageInfo->cbMaxToken;
  144. ChallengeBuffer.BufferType = SECBUFFER_TOKEN;
  145. ChallengeBuffer.pvBuffer = LocalAlloc( 0, ChallengeBuffer.cbBuffer );
  146. if ( ChallengeBuffer.pvBuffer == NULL ) {
  147. goto error_exit;
  148. }
  149. ServerFlags = ASC_REQ_EXTENDED_ERROR;
  150. AcceptStatus = AcceptSecurityContext(
  151. &ServerCredHandle,
  152. NULL, // No Server context yet
  153. &NegotiateDesc,
  154. ServerFlags,
  155. SECURITY_NATIVE_DREP,
  156. &ServerContextHandle,
  157. &ChallengeDesc,
  158. &ContextAttributes,
  159. &Lifetime );
  160. if ( !NT_SUCCESS(AcceptStatus) ) {
  161. goto error_exit;
  162. }
  163. if (InitStatus != STATUS_SUCCESS)
  164. {
  165. //
  166. // Get the AuthenticateMessage (ClientSide)
  167. //
  168. ChallengeBuffer.BufferType |= SECBUFFER_READONLY;
  169. AuthenticateDesc.ulVersion = 0;
  170. AuthenticateDesc.cBuffers = 1;
  171. AuthenticateDesc.pBuffers = &AuthenticateBuffer;
  172. AuthenticateBuffer.cbBuffer = PackageInfo->cbMaxToken;
  173. AuthenticateBuffer.BufferType = SECBUFFER_TOKEN;
  174. AuthenticateBuffer.pvBuffer = LocalAlloc( 0, AuthenticateBuffer.cbBuffer );
  175. if ( AuthenticateBuffer.pvBuffer == NULL ) {
  176. goto error_exit;
  177. }
  178. SecStatus = InitializeSecurityContext(
  179. NULL,
  180. &ClientContextHandle,
  181. TargetName,
  182. 0,
  183. 0, // Reserved 1
  184. SECURITY_NATIVE_DREP,
  185. &ChallengeDesc,
  186. 0, // Reserved 2
  187. &ClientContextHandle,
  188. &AuthenticateDesc,
  189. &ContextAttributes,
  190. &Lifetime );
  191. if ( !NT_SUCCESS(SecStatus) ) {
  192. goto error_exit;
  193. }
  194. if (AcceptStatus != STATUS_SUCCESS) {
  195. //
  196. // Finally authenticate the user (ServerSide)
  197. //
  198. AuthenticateBuffer.BufferType |= SECBUFFER_READONLY;
  199. SecStatus = AcceptSecurityContext(
  200. NULL,
  201. &ServerContextHandle,
  202. &AuthenticateDesc,
  203. ServerFlags,
  204. SECURITY_NATIVE_DREP,
  205. &ServerContextHandle,
  206. NULL,
  207. &ContextAttributes,
  208. &Lifetime );
  209. if ( !NT_SUCCESS(SecStatus) ) {
  210. goto error_exit;
  211. }
  212. Validated = TRUE;
  213. }
  214. }
  215. error_exit:
  216. if (ServerCredAllocated) {
  217. FreeCredentialsHandle( &ServerCredHandle );
  218. }
  219. if (ClientCredAllocated) {
  220. FreeCredentialsHandle( &ClientCredHandle );
  221. }
  222. //
  223. // Final Cleanup
  224. //
  225. if ( NegotiateBuffer.pvBuffer != NULL ) {
  226. (VOID) LocalFree( NegotiateBuffer.pvBuffer );
  227. }
  228. if ( ChallengeBuffer.pvBuffer != NULL ) {
  229. (VOID) LocalFree( ChallengeBuffer.pvBuffer );
  230. }
  231. if ( AuthenticateBuffer.pvBuffer != NULL ) {
  232. (VOID) LocalFree( AuthenticateBuffer.pvBuffer );
  233. }
  234. return(Validated);
  235. }
  236. //#endif
  237. BOOL IsUserExist(LPWSTR strUsername)
  238. {
  239. BYTE *pBuffer;
  240. INT err = NERR_Success;
  241. do
  242. {
  243. const unsigned short *pMachineName = NULL;
  244. // make sure we are not backup docmain first
  245. if (( err = NetServerGetInfo( NULL, 101, &pBuffer )) != NERR_Success )
  246. {
  247. printf("NetServerGetInfo:failed.Do not call this on PDC or BDC takes too long.This must be a PDC or BDC.");
  248. break;
  249. }
  250. //
  251. // Check if domain controller or backup domain controller
  252. //
  253. LPSERVER_INFO_101 pInfo = (LPSERVER_INFO_101)pBuffer;
  254. if (( pInfo->sv101_type & SV_TYPE_DOMAIN_BAKCTRL ) != 0 )
  255. {
  256. printf("Backupdomaincontroller.NetGetDCName.start.");
  257. NetGetDCName( NULL, NULL, (LPBYTE*)&pMachineName );
  258. printf((char*) pMachineName);
  259. printf("NetGetDCName.end.");
  260. }
  261. else
  262. {
  263. if (( pInfo->sv101_type & SV_TYPE_DOMAIN_CTRL ) != 0 )
  264. {
  265. printf("Domaincontroller.NetGetDCName.start.");
  266. NetGetDCName( NULL, NULL, (LPBYTE*)&pMachineName );
  267. printf((char*) pMachineName);
  268. printf("NetGetDCName.end.");
  269. }
  270. }
  271. NetApiBufferFree( pBuffer );
  272. // old for testing
  273. /*
  274. char buf[ CNLEN + 10 ];
  275. DWORD dwLen = CNLEN + 10;
  276. if ( GetComputerName( buf, &dwLen ))
  277. {
  278. printf((char*) buf);
  279. pMachineName = (const unsigned short *) buf;
  280. printf((char*) buf);
  281. }
  282. */
  283. if (pMachineName)
  284. {
  285. printf("MachineName="); printf((char*) pMachineName);
  286. printf("Username="); //printf((char*) strUsername);
  287. }
  288. else
  289. {
  290. printf("MachineName=(null)");
  291. printf("Username="); //printf((char*) strUsername);
  292. }
  293. printf("\n");
  294. err = NetUserGetInfo( pMachineName, strUsername, 3, &pBuffer );
  295. char szTheError[255];
  296. sprintf(szTheError, "TheErrCode=0x%x\n",err);
  297. printf(szTheError);
  298. if (err == ERROR_ACCESS_DENIED)
  299. {
  300. printf("ERROR_ACCESS_DENIED.The user does not have access to the requested information. \n");
  301. printf("\n");
  302. }
  303. if (err == NERR_InvalidComputer)
  304. {
  305. printf("ERROR_ACCESS_DENIED.The computer name is invalid.\n");
  306. printf("\n");
  307. }
  308. if (err == NERR_UserNotFound)
  309. {
  310. printf("NERR_UserNotFound.The user name could not be found.\n");
  311. printf("\n");
  312. }
  313. //if (pMachineName){iisDebugOut((_T("NetUserGetInfo:[%s\\%s].End.Ret=0x%x.\n"),pMachineName,strUsername,err));}
  314. //else{iisDebugOut((_T("NetUserGetInfo:[(null)\\%s].End.\n"),strUsername));}
  315. if ( err == NERR_Success ){NetApiBufferFree( pBuffer );}
  316. if ( pMachineName != NULL ){NetApiBufferFree( (void*) pMachineName );}
  317. } while (FALSE);
  318. if (err == NERR_Success )
  319. {
  320. return TRUE;
  321. }
  322. else
  323. {
  324. return FALSE;
  325. }
  326. }
  327. void DoStuff99(LPCTSTR lpUserName)
  328. {
  329. //printf("DoStuff99.Start.\n");
  330. WCHAR wchUsername[UNLEN+1];
  331. MultiByteToWideChar(CP_ACP, 0, (LPCSTR)lpUserName, -1, (LPWSTR)wchUsername, UNLEN);
  332. //if (TRUE == IsUserExist((const unsigned short *) lpUserName))
  333. if (TRUE == IsUserExist(wchUsername))
  334. {
  335. printf("IsUserExist.TRUE.\n");
  336. }
  337. else
  338. {
  339. printf("IsUserExist.FAILED.\n");
  340. }
  341. //printf("DoStuff99.End.\n");
  342. return;
  343. }