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.

4870 lines
142 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. checker.cxx
  5. Abstract:
  6. IIS Services IISADMIN Extension
  7. Unicode Metadata Sink.
  8. Author:
  9. Michael W. Thomas 11-19-98
  10. --*/
  11. #include <cominc.hxx>
  12. #include <lmaccess.h>
  13. #include <lmserver.h>
  14. #include <lmapibuf.h>
  15. #include <lmerr.h>
  16. #include <ntlsa.h>
  17. #include <time.h>
  18. #include <ntsam.h>
  19. #include <netlib.h>
  20. #include <resource.h>
  21. #define SECURITY_WIN32
  22. #define ISSP_LEVEL 32
  23. #define ISSP_MODE 1
  24. #include <sspi.h>
  25. #include <sspi.h>
  26. #include <eventlog.hxx>
  27. #include "extend.h"
  28. #include <comadmin.h>
  29. #include <sddl.h>
  30. #include "wmrgexp.h"
  31. typedef TCHAR USERNAME_STRING_TYPE[MAX_PATH];
  32. typedef TCHAR PASSWORD_STRING_TYPE[LM20_PWLEN+1];
  33. const LPCWSTR ROOTMDPath = L"/LM/W3SVC";
  34. #define IIS_WP_GROUP L"IIS_WPG"
  35. typedef enum {
  36. GUFM_SUCCESS,
  37. GUFM_NO_PATH,
  38. GUFM_NO_PASSWORD,
  39. GUFM_NO_USER_ID
  40. } GUFM_RETURN;
  41. BOOL ValidatePassword(IN LPCTSTR UserName,IN LPCTSTR Domain,IN LPCTSTR Password);
  42. void InitLsaString(PLSA_UNICODE_STRING LsaString,LPWSTR String);
  43. DWORD GetPrincipalSID (LPCTSTR Principal,PSID *Sid,BOOL *pbWellKnownSID);
  44. DWORD OpenPolicy(LPTSTR ServerName,DWORD DesiredAccess,PLSA_HANDLE PolicyHandle);
  45. DWORD AddRightToUserAccount(LPCTSTR szAccountName, LPTSTR PrivilegeName);
  46. DWORD DoesUserHaveThisRight(LPCTSTR szAccountName, LPTSTR PrivilegeName,BOOL *fHaveThatRight);
  47. HRESULT UpdateComApplications(IMDCOM * pcCom,LPCTSTR szWamUserName,LPCTSTR szWamUserPass);
  48. int IsDomainController(void);
  49. BOOL WaitForDCAvailability(void);
  50. HRESULT UpdateAdminAcl(IMDCOM *pcCom, LPCWSTR szPath, LPCWSTR szAccountName);
  51. // these two lines for logging event about account recreation
  52. EVENT_LOG *g_eventLogForAccountRecreation = NULL;
  53. BOOL CreateEventLogObject();
  54. VOID UpdateUserRights (LPCTSTR account,LPTSTR pstrRights[],DWORD dwNofRights)
  55. {
  56. DWORD status;
  57. BOOL fPresence;
  58. for (DWORD i=0;i<dwNofRights;i++)
  59. {
  60. status = DoesUserHaveThisRight(account,pstrRights[i],&fPresence);
  61. if (!NT_SUCCESS(status))
  62. {
  63. DBGPRINTF(( DBG_CONTEXT,"[UpdateAnonymousUser] DoesUserHaveThisRight returned err=0x%0X for account %s right %s\n",status,account,pstrRights[i]));
  64. }
  65. else
  66. {
  67. if (!fPresence)
  68. {
  69. status = AddRightToUserAccount(account,pstrRights[i]);
  70. if (!NT_SUCCESS(status))
  71. {
  72. DBGPRINTF(( DBG_CONTEXT,"[UpdateAnonymousUser] AddRightToUserAccount returned err=0x%0X for account %s right %s\n",status,account,pstrRights[i]));
  73. }
  74. }
  75. }
  76. }
  77. }
  78. DWORD AddRightToUserAccount(LPCTSTR szAccountName, LPTSTR PrivilegeName)
  79. {
  80. BOOL fEnabled = FALSE;
  81. NTSTATUS status;
  82. LSA_UNICODE_STRING UserRightString;
  83. LSA_HANDLE PolicyHandle = NULL;
  84. // Create a LSA_UNICODE_STRING for the privilege name.
  85. InitLsaString(&UserRightString, PrivilegeName);
  86. // get the sid of szAccountName
  87. PSID pSID = NULL;
  88. BOOL bWellKnownSID = FALSE;
  89. status = GetPrincipalSID ((LPTSTR)szAccountName, &pSID, &bWellKnownSID);
  90. if (status != ERROR_SUCCESS)
  91. {
  92. DBGPRINTF(( DBG_CONTEXT,"[AddRightToUserAccount] GetPrincipalSID returned err=0x%0X\n",status));
  93. return (status);
  94. }
  95. status = OpenPolicy(NULL, POLICY_ALL_ACCESS,&PolicyHandle);
  96. if ( status == NERR_Success )
  97. {
  98. UINT i;
  99. LSA_UNICODE_STRING *rgUserRights = NULL;
  100. ULONG cRights;
  101. status = LsaAddAccountRights (
  102. PolicyHandle,
  103. pSID,
  104. &UserRightString,
  105. 1);
  106. }
  107. if (PolicyHandle)
  108. {
  109. LsaClose(PolicyHandle);
  110. }
  111. if (pSID)
  112. {
  113. if (bWellKnownSID)
  114. {
  115. FreeSid (pSID);
  116. }
  117. else
  118. {
  119. free (pSID);
  120. }
  121. }
  122. return status;
  123. }
  124. DWORD DoesUserHaveThisRight(LPCTSTR szAccountName, LPTSTR PrivilegeName,BOOL *fHaveThatRight)
  125. {
  126. BOOL fEnabled = FALSE;
  127. NTSTATUS status;
  128. LSA_UNICODE_STRING UserRightString;
  129. LSA_HANDLE PolicyHandle = NULL;
  130. *fHaveThatRight = FALSE;
  131. // Create a LSA_UNICODE_STRING for the privilege name.
  132. InitLsaString(&UserRightString, PrivilegeName);
  133. // get the sid of szAccountName
  134. PSID pSID = NULL;
  135. BOOL bWellKnownSID = FALSE;
  136. status = GetPrincipalSID ((LPTSTR)szAccountName, &pSID, &bWellKnownSID);
  137. if (status != ERROR_SUCCESS)
  138. {
  139. return status;
  140. }
  141. status = OpenPolicy(NULL, POLICY_ALL_ACCESS,&PolicyHandle);
  142. if ( status == NERR_Success )
  143. {
  144. UINT i;
  145. LSA_UNICODE_STRING *rgUserRights = NULL;
  146. ULONG cRights;
  147. status = LsaEnumerateAccountRights(
  148. PolicyHandle,
  149. pSID,
  150. &rgUserRights,
  151. &cRights);
  152. if (status==STATUS_OBJECT_NAME_NOT_FOUND)
  153. {
  154. // no rights/privileges for this account
  155. status = ERROR_SUCCESS;
  156. fEnabled = FALSE;
  157. }
  158. else if (!NT_SUCCESS(status))
  159. {
  160. //iisDebugOut((LOG_TYPE_ERROR, _T("DoesUserHaveBasicRights:GetPrincipalSID:Failed to enumerate rights: status 0x%08lx\n"), status));
  161. goto DoesUserHaveBasicRights_Exit;
  162. }
  163. for(i=0; i < cRights; i++)
  164. {
  165. if ( RtlEqualUnicodeString(&rgUserRights[i],&UserRightString,FALSE) )
  166. {
  167. fEnabled = TRUE;
  168. break;
  169. }
  170. }
  171. if (rgUserRights)
  172. {
  173. LsaFreeMemory(rgUserRights);
  174. }
  175. }
  176. DoesUserHaveBasicRights_Exit:
  177. if (PolicyHandle)
  178. {
  179. LsaClose(PolicyHandle);
  180. }
  181. if (pSID)
  182. {
  183. if (bWellKnownSID)
  184. {
  185. FreeSid (pSID);
  186. }
  187. else
  188. {
  189. free (pSID);
  190. }
  191. }
  192. *fHaveThatRight = fEnabled;
  193. return status;
  194. }
  195. DWORD
  196. GetCurrentUserSID (
  197. PSID *Sid
  198. )
  199. {
  200. DWORD dwReturn = ERROR_SUCCESS;
  201. TOKEN_USER *tokenUser = NULL;
  202. HANDLE tokenHandle = NULL;
  203. DWORD tokenSize;
  204. DWORD sidLength;
  205. if (OpenProcessToken (GetCurrentProcess(), TOKEN_QUERY, &tokenHandle))
  206. {
  207. GetTokenInformation (tokenHandle, TokenUser, tokenUser, 0, &tokenSize);
  208. tokenUser = (TOKEN_USER *) malloc (tokenSize);
  209. if (!tokenUser)
  210. {
  211. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  212. return GetLastError();
  213. }
  214. if (GetTokenInformation (tokenHandle, TokenUser, tokenUser, tokenSize, &tokenSize))
  215. {
  216. sidLength = GetLengthSid (tokenUser->User.Sid);
  217. *Sid = (PSID) malloc (sidLength);
  218. if (*Sid)
  219. {
  220. memcpy (*Sid, tokenUser->User.Sid, sidLength);
  221. }
  222. CloseHandle (tokenHandle);
  223. } else
  224. dwReturn = GetLastError();
  225. if (tokenUser)
  226. free(tokenUser);
  227. } else
  228. dwReturn = GetLastError();
  229. return dwReturn;
  230. }
  231. DWORD
  232. CreateNewSD (
  233. SECURITY_DESCRIPTOR **SD
  234. )
  235. {
  236. PACL dacl;
  237. DWORD sidLength;
  238. PSID sid = NULL;
  239. PSID groupSID;
  240. PSID ownerSID;
  241. DWORD returnValue;
  242. *SD = NULL;
  243. returnValue = GetCurrentUserSID (&sid);
  244. if (returnValue != ERROR_SUCCESS) {
  245. if (sid)
  246. free(sid);
  247. return returnValue;
  248. }
  249. sidLength = GetLengthSid (sid);
  250. *SD = (SECURITY_DESCRIPTOR *) malloc (
  251. (sizeof (ACL)+sizeof (ACCESS_ALLOWED_ACE)+sidLength) +
  252. (2 * sidLength) +
  253. sizeof (SECURITY_DESCRIPTOR));
  254. if (!*SD)
  255. {
  256. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  257. return GetLastError();
  258. }
  259. groupSID = (SID *) (*SD + 1);
  260. ownerSID = (SID *) (((BYTE *) groupSID) + sidLength);
  261. dacl = (ACL *) (((BYTE *) ownerSID) + sidLength);
  262. if (!InitializeSecurityDescriptor (*SD, SECURITY_DESCRIPTOR_REVISION))
  263. {
  264. free (*SD);
  265. free (sid);
  266. return GetLastError();
  267. }
  268. if (!InitializeAcl (dacl,
  269. sizeof (ACL)+sizeof (ACCESS_ALLOWED_ACE)+sidLength,
  270. ACL_REVISION2))
  271. {
  272. free (*SD);
  273. free (sid);
  274. return GetLastError();
  275. }
  276. if (!AddAccessAllowedAce (dacl,
  277. ACL_REVISION2,
  278. COM_RIGHTS_EXECUTE,
  279. sid))
  280. {
  281. free (*SD);
  282. free (sid);
  283. return GetLastError();
  284. }
  285. if (!SetSecurityDescriptorDacl (*SD, TRUE, dacl, FALSE))
  286. {
  287. free (*SD);
  288. free (sid);
  289. return GetLastError();
  290. }
  291. memcpy (groupSID, sid, sidLength);
  292. if (!SetSecurityDescriptorGroup (*SD, groupSID, FALSE))
  293. {
  294. free (*SD);
  295. free (sid);
  296. return GetLastError();
  297. }
  298. memcpy (ownerSID, sid, sidLength);
  299. if (!SetSecurityDescriptorOwner (*SD, ownerSID, FALSE))
  300. {
  301. free (*SD);
  302. free (sid);
  303. return GetLastError();
  304. }
  305. if (sid)
  306. free(sid);
  307. return ERROR_SUCCESS;
  308. }
  309. DWORD
  310. GetNamedValueSD (
  311. HKEY RootKey,
  312. LPTSTR KeyName,
  313. LPTSTR ValueName,
  314. SECURITY_DESCRIPTOR **SD,
  315. BOOL *NewSD
  316. )
  317. {
  318. DWORD returnValue;
  319. HKEY registryKey;
  320. DWORD valueType;
  321. DWORD valueSize = 0;
  322. *NewSD = FALSE;
  323. //
  324. // Get the security descriptor from the named value. If it doesn't
  325. // exist, create a fresh one.
  326. //
  327. returnValue = RegOpenKeyEx (RootKey, KeyName, 0, KEY_ALL_ACCESS, &registryKey);
  328. if (returnValue != ERROR_SUCCESS)
  329. {
  330. if (returnValue == ERROR_FILE_NOT_FOUND)
  331. {
  332. *SD = NULL;
  333. returnValue = CreateNewSD (SD);
  334. if (returnValue != ERROR_SUCCESS) {
  335. if (*SD)
  336. free(*SD);
  337. return returnValue;
  338. }
  339. *NewSD = TRUE;
  340. return ERROR_SUCCESS;
  341. } else
  342. return returnValue;
  343. }
  344. returnValue = RegQueryValueEx (registryKey, ValueName, NULL, &valueType, NULL, &valueSize);
  345. if (returnValue && returnValue != ERROR_INSUFFICIENT_BUFFER)
  346. {
  347. *SD = NULL;
  348. returnValue = CreateNewSD (SD);
  349. if (returnValue != ERROR_SUCCESS) {
  350. if (*SD)
  351. free(*SD);
  352. return returnValue;
  353. }
  354. *NewSD = TRUE;
  355. } else
  356. {
  357. *SD = (SECURITY_DESCRIPTOR *) malloc (valueSize);
  358. if (!*SD)
  359. {
  360. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  361. return GetLastError();
  362. }
  363. returnValue = RegQueryValueEx (registryKey,
  364. ValueName,
  365. NULL,
  366. &valueType,
  367. (LPBYTE) *SD,
  368. &valueSize);
  369. if (returnValue)
  370. {
  371. if (*SD)
  372. free (*SD);
  373. *SD = NULL;
  374. returnValue = CreateNewSD (SD);
  375. if (returnValue != ERROR_SUCCESS) {
  376. if (*SD)
  377. free(*SD);
  378. return returnValue;
  379. }
  380. *NewSD = TRUE;
  381. }
  382. }
  383. RegCloseKey (registryKey);
  384. return ERROR_SUCCESS;
  385. }
  386. DWORD
  387. GetPrincipalSID (
  388. LPCTSTR Principal,
  389. PSID *Sid,
  390. BOOL *pbWellKnownSID
  391. )
  392. {
  393. DWORD returnValue=ERROR_SUCCESS;
  394. SID_IDENTIFIER_AUTHORITY SidIdentifierNTAuthority = SECURITY_NT_AUTHORITY;
  395. SID_IDENTIFIER_AUTHORITY SidIdentifierWORLDAuthority = SECURITY_WORLD_SID_AUTHORITY;
  396. PSID_IDENTIFIER_AUTHORITY pSidIdentifierAuthority;
  397. BYTE Count;
  398. DWORD dwRID[8];
  399. TCHAR pszPrincipal[MAX_PATH];
  400. *pbWellKnownSID = TRUE;
  401. memset(&(dwRID[0]), 0, 8 * sizeof(DWORD));
  402. DBG_ASSERT(wcslen(Principal) < MAX_PATH);
  403. wcscpy(pszPrincipal, Principal);
  404. _wcslwr(pszPrincipal);
  405. if ( wcsstr(pszPrincipal, TEXT("administrators")) != NULL ) {
  406. // Administrators group
  407. pSidIdentifierAuthority = &SidIdentifierNTAuthority;
  408. Count = 2;
  409. dwRID[0] = SECURITY_BUILTIN_DOMAIN_RID;
  410. dwRID[1] = DOMAIN_ALIAS_RID_ADMINS;
  411. } else if ( wcsstr(pszPrincipal, TEXT("system")) != NULL) {
  412. // SYSTEM
  413. pSidIdentifierAuthority = &SidIdentifierNTAuthority;
  414. Count = 1;
  415. dwRID[0] = SECURITY_LOCAL_SYSTEM_RID;
  416. } else if ( wcsstr(pszPrincipal, TEXT("interactive")) != NULL) {
  417. // INTERACTIVE
  418. pSidIdentifierAuthority = &SidIdentifierNTAuthority;
  419. Count = 1;
  420. dwRID[0] = SECURITY_INTERACTIVE_RID;
  421. } else if ( wcsstr(pszPrincipal, TEXT("everyone")) != NULL) {
  422. // Everyone
  423. pSidIdentifierAuthority = &SidIdentifierWORLDAuthority;
  424. Count = 1;
  425. dwRID[0] = SECURITY_WORLD_RID;
  426. } else {
  427. *pbWellKnownSID = FALSE;
  428. }
  429. if (*pbWellKnownSID) {
  430. if ( !AllocateAndInitializeSid(pSidIdentifierAuthority,
  431. (BYTE)Count,
  432. dwRID[0],
  433. dwRID[1],
  434. dwRID[2],
  435. dwRID[3],
  436. dwRID[4],
  437. dwRID[5],
  438. dwRID[6],
  439. dwRID[7],
  440. Sid) ) {
  441. returnValue = GetLastError();
  442. }
  443. } else {
  444. // get regular account sid
  445. DWORD sidSize;
  446. TCHAR refDomain [256];
  447. DWORD refDomainSize;
  448. SID_NAME_USE snu;
  449. sidSize = 0;
  450. refDomainSize = 255;
  451. LookupAccountName (NULL,
  452. pszPrincipal,
  453. *Sid,
  454. &sidSize,
  455. refDomain,
  456. &refDomainSize,
  457. &snu);
  458. returnValue = GetLastError();
  459. if (returnValue == ERROR_INSUFFICIENT_BUFFER) {
  460. *Sid = (PSID) malloc (sidSize);
  461. if (!*Sid)
  462. {
  463. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  464. return GetLastError();
  465. }
  466. refDomainSize = 255;
  467. if (!LookupAccountName (NULL,
  468. pszPrincipal,
  469. *Sid,
  470. &sidSize,
  471. refDomain,
  472. &refDomainSize,
  473. &snu))
  474. {
  475. returnValue = GetLastError();
  476. } else {
  477. returnValue = ERROR_SUCCESS;
  478. }
  479. }
  480. }
  481. return returnValue;
  482. }
  483. DWORD
  484. CopyACL (
  485. PACL OldACL,
  486. PACL NewACL
  487. )
  488. {
  489. ACL_SIZE_INFORMATION aclSizeInfo;
  490. LPVOID ace;
  491. ACE_HEADER *aceHeader;
  492. ULONG i;
  493. GetAclInformation (OldACL,
  494. (LPVOID) &aclSizeInfo,
  495. (DWORD) sizeof (aclSizeInfo),
  496. AclSizeInformation);
  497. //
  498. // Copy all of the ACEs to the new ACL
  499. //
  500. for (i = 0; i < aclSizeInfo.AceCount; i++)
  501. {
  502. //
  503. // Get the ACE and header info
  504. //
  505. if (!GetAce (OldACL, i, &ace))
  506. return GetLastError();
  507. aceHeader = (ACE_HEADER *) ace;
  508. //
  509. // Add the ACE to the new list
  510. //
  511. if (!AddAce (NewACL, ACL_REVISION, 0xffffffff, ace, aceHeader->AceSize))
  512. return GetLastError();
  513. }
  514. return ERROR_SUCCESS;
  515. }
  516. DWORD
  517. AddAccessAllowedACEToACL (
  518. PACL *Acl,
  519. DWORD PermissionMask,
  520. LPTSTR Principal
  521. )
  522. {
  523. ACL_SIZE_INFORMATION aclSizeInfo;
  524. int aclSize;
  525. DWORD returnValue = ERROR_SUCCESS;
  526. PSID principalSID = NULL;
  527. PACL oldACL, newACL;
  528. BOOL bWellKnownSID = FALSE;
  529. oldACL = *Acl;
  530. returnValue = GetPrincipalSID (Principal, &principalSID, &bWellKnownSID);
  531. if (returnValue != ERROR_SUCCESS)
  532. return returnValue;
  533. GetAclInformation (oldACL,
  534. (LPVOID) &aclSizeInfo,
  535. (DWORD) sizeof (ACL_SIZE_INFORMATION),
  536. AclSizeInformation);
  537. aclSize = aclSizeInfo.AclBytesInUse +
  538. sizeof (ACL) + sizeof (ACCESS_ALLOWED_ACE) +
  539. GetLengthSid (principalSID) - sizeof (DWORD);
  540. newACL = (PACL) new BYTE [aclSize];
  541. if (!InitializeAcl (newACL, aclSize, ACL_REVISION))
  542. {
  543. returnValue = GetLastError();
  544. goto cleanup;
  545. }
  546. returnValue = CopyACL (oldACL, newACL);
  547. if (returnValue != ERROR_SUCCESS)
  548. {
  549. goto cleanup;
  550. }
  551. if (!AddAccessAllowedAce (newACL, ACL_REVISION2, PermissionMask, principalSID))
  552. {
  553. returnValue = GetLastError();
  554. goto cleanup;
  555. }
  556. *Acl = newACL;
  557. newACL = NULL;
  558. cleanup:
  559. // BugFix: 57654 Whistler
  560. // Prefix bug leaking memory in error condition.
  561. // By setting the newACL to NULL above if we have
  562. // relinquished the memory to *Acl, we avoid releasing
  563. // memory we have passed back to the caller.
  564. // EBK 5/5/2000
  565. if (newACL)
  566. {
  567. delete [] newACL;
  568. newACL = NULL;
  569. }
  570. if (principalSID) {
  571. if (bWellKnownSID)
  572. FreeSid (principalSID);
  573. else
  574. free (principalSID);
  575. }
  576. return returnValue;
  577. }
  578. DWORD
  579. RemovePrincipalFromACL (
  580. PACL Acl,
  581. LPTSTR Principal
  582. )
  583. {
  584. ACL_SIZE_INFORMATION aclSizeInfo;
  585. ULONG i;
  586. LPVOID ace;
  587. ACCESS_ALLOWED_ACE *accessAllowedAce;
  588. ACCESS_DENIED_ACE *accessDeniedAce;
  589. SYSTEM_AUDIT_ACE *systemAuditAce;
  590. PSID principalSID = NULL;
  591. DWORD returnValue = ERROR_SUCCESS;
  592. ACE_HEADER *aceHeader;
  593. BOOL bWellKnownSID = FALSE;
  594. returnValue = GetPrincipalSID (Principal, &principalSID, &bWellKnownSID);
  595. if (returnValue != ERROR_SUCCESS)
  596. return returnValue;
  597. GetAclInformation (Acl,
  598. (LPVOID) &aclSizeInfo,
  599. (DWORD) sizeof (ACL_SIZE_INFORMATION),
  600. AclSizeInformation);
  601. for (i = 0; i < aclSizeInfo.AceCount; i++)
  602. {
  603. if (!GetAce (Acl, i, &ace))
  604. {
  605. returnValue = GetLastError();
  606. break;
  607. }
  608. aceHeader = (ACE_HEADER *) ace;
  609. if (aceHeader->AceType == ACCESS_ALLOWED_ACE_TYPE)
  610. {
  611. accessAllowedAce = (ACCESS_ALLOWED_ACE *) ace;
  612. if (EqualSid (principalSID, (PSID) &accessAllowedAce->SidStart))
  613. {
  614. DeleteAce (Acl, i);
  615. break;
  616. }
  617. } else
  618. if (aceHeader->AceType == ACCESS_DENIED_ACE_TYPE)
  619. {
  620. accessDeniedAce = (ACCESS_DENIED_ACE *) ace;
  621. if (EqualSid (principalSID, (PSID) &accessDeniedAce->SidStart))
  622. {
  623. DeleteAce (Acl, i);
  624. break;
  625. }
  626. } else
  627. if (aceHeader->AceType == SYSTEM_AUDIT_ACE_TYPE)
  628. {
  629. systemAuditAce = (SYSTEM_AUDIT_ACE *) ace;
  630. if (EqualSid (principalSID, (PSID) &systemAuditAce->SidStart))
  631. {
  632. DeleteAce (Acl, i);
  633. break;
  634. }
  635. }
  636. }
  637. if (principalSID) {
  638. if (bWellKnownSID)
  639. FreeSid (principalSID);
  640. else
  641. free (principalSID);
  642. }
  643. return returnValue;
  644. }
  645. DWORD
  646. MakeSDAbsolute (
  647. PSECURITY_DESCRIPTOR OldSD,
  648. PSECURITY_DESCRIPTOR *NewSD
  649. )
  650. {
  651. PSECURITY_DESCRIPTOR sd = NULL;
  652. DWORD descriptorSize;
  653. DWORD daclSize;
  654. DWORD saclSize;
  655. DWORD ownerSIDSize;
  656. DWORD groupSIDSize;
  657. PACL dacl = NULL;
  658. PACL sacl = NULL;
  659. PSID ownerSID = NULL;
  660. PSID groupSID = NULL;
  661. BOOL present;
  662. BOOL systemDefault;
  663. //
  664. // Get SACL
  665. //
  666. if (!GetSecurityDescriptorSacl (OldSD, &present, &sacl, &systemDefault))
  667. return GetLastError();
  668. if (sacl && present)
  669. {
  670. saclSize = sacl->AclSize;
  671. } else saclSize = 0;
  672. //
  673. // Get DACL
  674. //
  675. if (!GetSecurityDescriptorDacl (OldSD, &present, &dacl, &systemDefault))
  676. return GetLastError();
  677. if (dacl && present)
  678. {
  679. daclSize = dacl->AclSize;
  680. } else daclSize = 0;
  681. //
  682. // Get Owner
  683. //
  684. if (!GetSecurityDescriptorOwner (OldSD, &ownerSID, &systemDefault))
  685. return GetLastError();
  686. ownerSIDSize = GetLengthSid (ownerSID);
  687. //
  688. // Get Group
  689. //
  690. if (!GetSecurityDescriptorGroup (OldSD, &groupSID, &systemDefault))
  691. return GetLastError();
  692. groupSIDSize = GetLengthSid (groupSID);
  693. //
  694. // Do the conversion
  695. //
  696. descriptorSize = 0;
  697. MakeAbsoluteSD (OldSD, sd, &descriptorSize, dacl, &daclSize, sacl,
  698. &saclSize, ownerSID, &ownerSIDSize, groupSID,
  699. &groupSIDSize);
  700. sd = (PSECURITY_DESCRIPTOR) new BYTE [SECURITY_DESCRIPTOR_MIN_LENGTH];
  701. if (!sd)
  702. {
  703. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  704. return GetLastError();
  705. }
  706. if (!InitializeSecurityDescriptor (sd, SECURITY_DESCRIPTOR_REVISION))
  707. return GetLastError();
  708. if (!MakeAbsoluteSD (OldSD, sd, &descriptorSize, dacl, &daclSize, sacl,
  709. &saclSize, ownerSID, &ownerSIDSize, groupSID,
  710. &groupSIDSize))
  711. return GetLastError();
  712. *NewSD = sd;
  713. return ERROR_SUCCESS;
  714. }
  715. DWORD
  716. SetNamedValueSD (
  717. HKEY RootKey,
  718. LPTSTR KeyName,
  719. LPTSTR ValueName,
  720. SECURITY_DESCRIPTOR *SD
  721. )
  722. {
  723. DWORD returnValue;
  724. DWORD disposition;
  725. HKEY registryKey;
  726. //
  727. // Create new key or open existing key
  728. //
  729. returnValue = RegCreateKeyEx (RootKey,
  730. KeyName,
  731. 0,
  732. TEXT(""),
  733. 0,
  734. KEY_ALL_ACCESS,
  735. NULL,
  736. &registryKey,
  737. &disposition);
  738. if (returnValue != ERROR_SUCCESS)
  739. return returnValue;
  740. //
  741. // Write the security descriptor
  742. //
  743. returnValue = RegSetValueEx (registryKey,
  744. ValueName,
  745. 0,
  746. REG_BINARY,
  747. (LPBYTE) SD,
  748. GetSecurityDescriptorLength (SD));
  749. if (returnValue != ERROR_SUCCESS)
  750. return returnValue;
  751. RegCloseKey (registryKey);
  752. return ERROR_SUCCESS;
  753. }
  754. DWORD
  755. RemovePrincipalFromNamedValueSD (
  756. HKEY RootKey,
  757. LPTSTR KeyName,
  758. LPTSTR ValueName,
  759. LPTSTR Principal
  760. )
  761. {
  762. DWORD returnValue = ERROR_SUCCESS;
  763. SECURITY_DESCRIPTOR *sd = NULL;
  764. SECURITY_DESCRIPTOR *sdSelfRelative = NULL;
  765. SECURITY_DESCRIPTOR *sdAbsolute = NULL;
  766. DWORD secDescSize;
  767. BOOL present;
  768. BOOL defaultDACL;
  769. PACL dacl = NULL;
  770. BOOL newSD = FALSE;
  771. BOOL fFreeAbsolute = TRUE;
  772. returnValue = GetNamedValueSD (RootKey, KeyName, ValueName, &sd, &newSD);
  773. //
  774. // Get security descriptor from registry or create a new one
  775. //
  776. if (returnValue != ERROR_SUCCESS)
  777. return returnValue;
  778. if (!GetSecurityDescriptorDacl (sd, &present, &dacl, &defaultDACL)) {
  779. returnValue = GetLastError();
  780. goto Cleanup;
  781. }
  782. //
  783. // If the security descriptor is new, add the required Principals to it
  784. //
  785. if (newSD)
  786. {
  787. AddAccessAllowedACEToACL (&dacl, COM_RIGHTS_EXECUTE, TEXT("SYSTEM"));
  788. AddAccessAllowedACEToACL (&dacl, COM_RIGHTS_EXECUTE, TEXT("INTERACTIVE"));
  789. }
  790. //
  791. // Remove the Principal that the caller wants removed
  792. //
  793. returnValue = RemovePrincipalFromACL (dacl, Principal);
  794. if (returnValue != ERROR_SUCCESS)
  795. goto Cleanup;
  796. //
  797. // Make the security descriptor absolute if it isn't new
  798. //
  799. if (!newSD) {
  800. MakeSDAbsolute ((PSECURITY_DESCRIPTOR) sd, (PSECURITY_DESCRIPTOR *) &sdAbsolute);
  801. fFreeAbsolute = TRUE;
  802. } else {
  803. sdAbsolute = sd;
  804. fFreeAbsolute = FALSE;
  805. }
  806. //
  807. // Set the discretionary ACL on the security descriptor
  808. //
  809. if (!SetSecurityDescriptorDacl (sdAbsolute, TRUE, dacl, FALSE)) {
  810. returnValue = GetLastError();
  811. goto Cleanup;
  812. }
  813. //
  814. // Make the security descriptor self-relative so that we can
  815. // store it in the registry
  816. //
  817. secDescSize = 0;
  818. MakeSelfRelativeSD (sdAbsolute, sdSelfRelative, &secDescSize);
  819. sdSelfRelative = (SECURITY_DESCRIPTOR *) malloc (secDescSize);
  820. if (!sdSelfRelative)
  821. {
  822. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  823. returnValue = GetLastError();
  824. goto Cleanup;
  825. }
  826. if (!MakeSelfRelativeSD (sdAbsolute, sdSelfRelative, &secDescSize)) {
  827. returnValue = GetLastError();
  828. goto Cleanup;
  829. }
  830. //
  831. // Store the security descriptor in the registry
  832. //
  833. SetNamedValueSD (RootKey, KeyName, ValueName, sdSelfRelative);
  834. Cleanup:
  835. if (sd)
  836. free (sd);
  837. if (sdSelfRelative)
  838. free (sdSelfRelative);
  839. if (fFreeAbsolute && sdAbsolute)
  840. free (sdAbsolute);
  841. return returnValue;
  842. }
  843. DWORD
  844. AddAccessDeniedACEToACL (
  845. PACL *Acl,
  846. DWORD PermissionMask,
  847. LPTSTR Principal
  848. )
  849. {
  850. ACL_SIZE_INFORMATION aclSizeInfo;
  851. int aclSize;
  852. DWORD returnValue = ERROR_SUCCESS;
  853. PSID principalSID = NULL;
  854. PACL oldACL, newACL;
  855. BOOL bWellKnownSID = FALSE;
  856. oldACL = *Acl;
  857. returnValue = GetPrincipalSID (Principal, &principalSID, &bWellKnownSID);
  858. if (returnValue != ERROR_SUCCESS)
  859. return returnValue;
  860. GetAclInformation (oldACL,
  861. (LPVOID) &aclSizeInfo,
  862. (DWORD) sizeof (ACL_SIZE_INFORMATION),
  863. AclSizeInformation);
  864. aclSize = aclSizeInfo.AclBytesInUse +
  865. sizeof (ACL) + sizeof (ACCESS_DENIED_ACE) +
  866. GetLengthSid (principalSID) - sizeof (DWORD);
  867. newACL = (PACL) new BYTE [aclSize];
  868. if (!InitializeAcl (newACL, aclSize, ACL_REVISION))
  869. {
  870. returnValue = GetLastError();
  871. goto cleanup;
  872. }
  873. if (!AddAccessDeniedAce (newACL, ACL_REVISION2, PermissionMask, principalSID))
  874. {
  875. returnValue = GetLastError();
  876. goto cleanup;
  877. }
  878. returnValue = CopyACL (oldACL, newACL);
  879. if (returnValue != ERROR_SUCCESS)
  880. {
  881. goto cleanup;
  882. }
  883. *Acl = newACL;
  884. newACL = NULL;
  885. cleanup:
  886. // BugFix: 57654 Whistler
  887. // Prefix bug leaking memory in error condition.
  888. // By setting the newACL to NULL above if we have
  889. // relinquished the memory to *Acl, we avoid releasing
  890. // memory we have passed back to the caller.
  891. // EBK 5/5/2000
  892. if (newACL)
  893. {
  894. delete[] newACL;
  895. newACL = NULL;
  896. }
  897. if (principalSID) {
  898. if (bWellKnownSID)
  899. FreeSid (principalSID);
  900. else
  901. free (principalSID);
  902. }
  903. return returnValue;
  904. }
  905. DWORD
  906. AddPrincipalToNamedValueSD (
  907. HKEY RootKey,
  908. LPTSTR KeyName,
  909. LPTSTR ValueName,
  910. LPTSTR Principal,
  911. BOOL Permit
  912. )
  913. {
  914. DWORD returnValue = ERROR_SUCCESS;
  915. SECURITY_DESCRIPTOR *sd = NULL;
  916. SECURITY_DESCRIPTOR *sdSelfRelative = NULL;
  917. SECURITY_DESCRIPTOR *sdAbsolute = NULL;
  918. DWORD secDescSize;
  919. BOOL present;
  920. BOOL defaultDACL;
  921. PACL dacl;
  922. BOOL newSD = FALSE;
  923. BOOL fFreeAbsolute = TRUE;
  924. returnValue = GetNamedValueSD (RootKey, KeyName, ValueName, &sd, &newSD);
  925. //
  926. // Get security descriptor from registry or create a new one
  927. //
  928. if (returnValue != ERROR_SUCCESS)
  929. return returnValue;
  930. if (!GetSecurityDescriptorDacl (sd, &present, &dacl, &defaultDACL)) {
  931. returnValue = GetLastError();
  932. goto Cleanup;
  933. }
  934. if (newSD)
  935. {
  936. AddAccessAllowedACEToACL (&dacl, COM_RIGHTS_EXECUTE, TEXT("SYSTEM"));
  937. AddAccessAllowedACEToACL (&dacl, COM_RIGHTS_EXECUTE, TEXT("INTERACTIVE"));
  938. }
  939. //
  940. // Add the Principal that the caller wants added
  941. //
  942. if (Permit)
  943. returnValue = AddAccessAllowedACEToACL (&dacl, COM_RIGHTS_EXECUTE, Principal);
  944. else
  945. returnValue = AddAccessDeniedACEToACL (&dacl, GENERIC_ALL, Principal);
  946. if (returnValue != ERROR_SUCCESS)
  947. goto Cleanup;
  948. //
  949. // Make the security descriptor absolute if it isn't new
  950. //
  951. if (!newSD) {
  952. MakeSDAbsolute ((PSECURITY_DESCRIPTOR) sd, (PSECURITY_DESCRIPTOR *) &sdAbsolute);
  953. fFreeAbsolute = TRUE;
  954. } else {
  955. sdAbsolute = sd;
  956. fFreeAbsolute = FALSE;
  957. }
  958. //
  959. // Set the discretionary ACL on the security descriptor
  960. //
  961. if (!SetSecurityDescriptorDacl (sdAbsolute, TRUE, dacl, FALSE)) {
  962. returnValue = GetLastError();
  963. goto Cleanup;
  964. }
  965. //
  966. // Make the security descriptor self-relative so that we can
  967. // store it in the registry
  968. //
  969. secDescSize = 0;
  970. MakeSelfRelativeSD (sdAbsolute, sdSelfRelative, &secDescSize);
  971. sdSelfRelative = (SECURITY_DESCRIPTOR *) malloc (secDescSize);
  972. if (!sdSelfRelative)
  973. {
  974. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  975. returnValue = GetLastError();
  976. goto Cleanup;
  977. }
  978. if (!MakeSelfRelativeSD (sdAbsolute, sdSelfRelative, &secDescSize)) {
  979. returnValue = GetLastError();
  980. goto Cleanup;
  981. }
  982. //
  983. // Store the security descriptor in the registry
  984. //
  985. SetNamedValueSD (RootKey, KeyName, ValueName, sdSelfRelative);
  986. Cleanup:
  987. if (sd)
  988. free (sd);
  989. if (sdSelfRelative)
  990. free (sdSelfRelative);
  991. if (fFreeAbsolute && sdAbsolute)
  992. free (sdAbsolute);
  993. return returnValue;
  994. }
  995. DWORD
  996. ChangeDCOMAccessACL (
  997. LPTSTR Principal,
  998. BOOL SetPrincipal,
  999. BOOL Permit
  1000. )
  1001. {
  1002. DWORD err;
  1003. if (SetPrincipal)
  1004. {
  1005. err = RemovePrincipalFromNamedValueSD (HKEY_LOCAL_MACHINE,
  1006. TEXT("Software\\Microsoft\\OLE"),
  1007. TEXT("DefaultAccessPermission"),
  1008. Principal);
  1009. err = AddPrincipalToNamedValueSD (HKEY_LOCAL_MACHINE,
  1010. TEXT("Software\\Microsoft\\OLE"),
  1011. TEXT("DefaultAccessPermission"),
  1012. Principal,
  1013. Permit);
  1014. }
  1015. else
  1016. {
  1017. err = RemovePrincipalFromNamedValueSD (HKEY_LOCAL_MACHINE,
  1018. TEXT("Software\\Microsoft\\OLE"),
  1019. TEXT("DefaultAccessPermission"),
  1020. Principal);
  1021. }
  1022. return err;
  1023. }
  1024. DWORD
  1025. ChangeDCOMLaunchACL (
  1026. LPTSTR Principal,
  1027. BOOL SetPrincipal,
  1028. BOOL Permit
  1029. )
  1030. {
  1031. TCHAR keyName [256] = TEXT("Software\\Microsoft\\OLE");
  1032. DWORD err;
  1033. if (SetPrincipal)
  1034. {
  1035. err = RemovePrincipalFromNamedValueSD (HKEY_LOCAL_MACHINE,
  1036. keyName,
  1037. TEXT("DefaultLaunchPermission"),
  1038. Principal);
  1039. err = AddPrincipalToNamedValueSD (HKEY_LOCAL_MACHINE,
  1040. keyName,
  1041. TEXT("DefaultLaunchPermission"),
  1042. Principal,
  1043. Permit);
  1044. }
  1045. else
  1046. {
  1047. err = RemovePrincipalFromNamedValueSD (HKEY_LOCAL_MACHINE,
  1048. keyName,
  1049. TEXT("DefaultLaunchPermission"),
  1050. Principal);
  1051. }
  1052. return err;
  1053. }
  1054. GUFM_RETURN GetUserFromMetabase(IMDCOM *pcCom,
  1055. LPWSTR pszPath,
  1056. DWORD dwUserMetaId,
  1057. DWORD dwPasswordMetaId,
  1058. USERNAME_STRING_TYPE ustUserBuf,
  1059. PASSWORD_STRING_TYPE pstPasswordBuf)
  1060. {
  1061. HRESULT hresTemp;
  1062. GUFM_RETURN gufmReturn = GUFM_SUCCESS;
  1063. METADATA_RECORD mdrData;
  1064. DWORD dwRequiredDataLen;
  1065. METADATA_HANDLE mhOpenHandle;
  1066. hresTemp = pcCom->ComMDOpenMetaObject(METADATA_MASTER_ROOT_HANDLE,
  1067. pszPath,
  1068. METADATA_PERMISSION_READ,
  1069. OPEN_TIMEOUT_VALUE,
  1070. &mhOpenHandle);
  1071. if (FAILED(hresTemp)) {
  1072. gufmReturn = GUFM_NO_PATH;
  1073. }
  1074. else {
  1075. MD_SET_DATA_RECORD_EXT(&mdrData,
  1076. dwUserMetaId,
  1077. METADATA_NO_ATTRIBUTES,
  1078. ALL_METADATA,
  1079. STRING_METADATA,
  1080. MAX_PATH * sizeof(TCHAR),
  1081. (PBYTE)ustUserBuf)
  1082. hresTemp = pcCom->ComMDGetMetaData(mhOpenHandle,
  1083. NULL,
  1084. &mdrData,
  1085. &dwRequiredDataLen);
  1086. if (FAILED(hresTemp) || (ustUserBuf[0] == (TCHAR)'\0')) {
  1087. gufmReturn = GUFM_NO_USER_ID;
  1088. }
  1089. else {
  1090. MD_SET_DATA_RECORD_EXT(&mdrData,
  1091. dwPasswordMetaId,
  1092. METADATA_NO_ATTRIBUTES,
  1093. ALL_METADATA,
  1094. STRING_METADATA,
  1095. MAX_PATH * sizeof(TCHAR),
  1096. (PBYTE)pstPasswordBuf)
  1097. hresTemp = pcCom->ComMDGetMetaData(mhOpenHandle,
  1098. NULL,
  1099. &mdrData,
  1100. &dwRequiredDataLen);
  1101. if (FAILED(hresTemp)) {
  1102. gufmReturn = GUFM_NO_PASSWORD;
  1103. }
  1104. }
  1105. pcCom->ComMDCloseMetaObject(mhOpenHandle);
  1106. }
  1107. return gufmReturn;
  1108. }
  1109. BOOL WritePasswordToMetabase(IMDCOM *pcCom,
  1110. LPWSTR pszPath,
  1111. DWORD dwPasswordMetaId,
  1112. PASSWORD_STRING_TYPE pstPasswordBuf)
  1113. {
  1114. HRESULT hresReturn;
  1115. BOOL fReturn = FALSE;
  1116. METADATA_RECORD mdrData;
  1117. METADATA_HANDLE mhOpenHandle;
  1118. hresReturn = pcCom->ComMDOpenMetaObject(METADATA_MASTER_ROOT_HANDLE,
  1119. pszPath,
  1120. METADATA_PERMISSION_WRITE,
  1121. OPEN_TIMEOUT_VALUE,
  1122. &mhOpenHandle);
  1123. if (SUCCEEDED(hresReturn)) {
  1124. MD_SET_DATA_RECORD_EXT(&mdrData,
  1125. dwPasswordMetaId,
  1126. METADATA_INHERIT | METADATA_SECURE,
  1127. IIS_MD_UT_FILE,
  1128. STRING_METADATA,
  1129. sizeof(pstPasswordBuf),
  1130. (PBYTE)pstPasswordBuf)
  1131. hresReturn = pcCom->ComMDSetMetaData(mhOpenHandle,
  1132. NULL,
  1133. &mdrData);
  1134. pcCom->ComMDCloseMetaObject(mhOpenHandle);
  1135. }
  1136. return SUCCEEDED(hresReturn);
  1137. }
  1138. BOOL DoesUserExist( LPWSTR strUsername, BOOL *fDisabled )
  1139. {
  1140. BYTE *pBuffer;
  1141. INT err = NERR_Success;
  1142. BOOL fReturn = FALSE;
  1143. *fDisabled = FALSE;
  1144. err = NetUserGetInfo( NULL, strUsername, 3, &pBuffer );
  1145. if ( err == NERR_Success )
  1146. {
  1147. *fDisabled = !!(((PUSER_INFO_3)pBuffer)->usri3_flags & UF_ACCOUNTDISABLE);
  1148. NetApiBufferFree( pBuffer );
  1149. fReturn = TRUE;
  1150. }
  1151. return( fReturn );
  1152. }
  1153. NET_API_STATUS
  1154. NetpNtStatusToApiStatus (
  1155. IN NTSTATUS NtStatus
  1156. )
  1157. /*++
  1158. Routine Description:
  1159. This function takes an NT status code and maps it to the appropriate
  1160. LAN Man error code.
  1161. Arguments:
  1162. NtStatus - Supplies the NT status.
  1163. Return Value:
  1164. Returns the appropriate LAN Man error code for the NT status.
  1165. --*/
  1166. {
  1167. NET_API_STATUS error;
  1168. //
  1169. // A small optimization for the most common case.
  1170. //
  1171. if ( NtStatus == STATUS_SUCCESS ) {
  1172. return NERR_Success;
  1173. }
  1174. switch ( NtStatus ) {
  1175. case STATUS_BUFFER_TOO_SMALL :
  1176. return NERR_BufTooSmall;
  1177. case STATUS_FILES_OPEN :
  1178. return NERR_OpenFiles;
  1179. case STATUS_CONNECTION_IN_USE :
  1180. return NERR_DevInUse;
  1181. case STATUS_INVALID_LOGON_HOURS :
  1182. return NERR_InvalidLogonHours;
  1183. case STATUS_INVALID_WORKSTATION :
  1184. return NERR_InvalidWorkstation;
  1185. case STATUS_PASSWORD_EXPIRED :
  1186. return NERR_PasswordExpired;
  1187. case STATUS_ACCOUNT_EXPIRED :
  1188. return NERR_AccountExpired;
  1189. case STATUS_REDIRECTOR_NOT_STARTED :
  1190. return NERR_NetNotStarted;
  1191. case STATUS_GROUP_EXISTS:
  1192. return NERR_GroupExists;
  1193. case STATUS_INTERNAL_DB_CORRUPTION:
  1194. return NERR_InvalidDatabase;
  1195. case STATUS_INVALID_ACCOUNT_NAME:
  1196. return NERR_BadUsername;
  1197. case STATUS_INVALID_DOMAIN_ROLE:
  1198. case STATUS_INVALID_SERVER_STATE:
  1199. case STATUS_BACKUP_CONTROLLER:
  1200. return NERR_NotPrimary;
  1201. case STATUS_INVALID_DOMAIN_STATE:
  1202. return NERR_ACFNotLoaded;
  1203. case STATUS_MEMBER_IN_GROUP:
  1204. return NERR_UserInGroup;
  1205. case STATUS_MEMBER_NOT_IN_GROUP:
  1206. return NERR_UserNotInGroup;
  1207. case STATUS_NONE_MAPPED:
  1208. case STATUS_NO_SUCH_GROUP:
  1209. return NERR_GroupNotFound;
  1210. case STATUS_SPECIAL_GROUP:
  1211. case STATUS_MEMBERS_PRIMARY_GROUP:
  1212. return NERR_SpeGroupOp;
  1213. case STATUS_USER_EXISTS:
  1214. return NERR_UserExists;
  1215. case STATUS_NO_SUCH_USER:
  1216. return NERR_UserNotFound;
  1217. case STATUS_PRIVILEGE_NOT_HELD:
  1218. return ERROR_ACCESS_DENIED;
  1219. case STATUS_LOGON_SERVER_CONFLICT:
  1220. return NERR_LogonServerConflict;
  1221. case STATUS_TIME_DIFFERENCE_AT_DC:
  1222. return NERR_TimeDiffAtDC;
  1223. case STATUS_SYNCHRONIZATION_REQUIRED:
  1224. return NERR_SyncRequired;
  1225. case STATUS_WRONG_PASSWORD_CORE:
  1226. return NERR_BadPasswordCore;
  1227. case STATUS_DOMAIN_CONTROLLER_NOT_FOUND:
  1228. return NERR_DCNotFound;
  1229. case STATUS_PASSWORD_RESTRICTION:
  1230. return NERR_PasswordTooShort;
  1231. case STATUS_ALREADY_DISCONNECTED:
  1232. return NERR_Success;
  1233. default:
  1234. //
  1235. // Use the system routine to do the mapping to ERROR_ codes.
  1236. //
  1237. #ifndef WIN32_CHICAGO
  1238. error = RtlNtStatusToDosError( NtStatus );
  1239. if ( error != (NET_API_STATUS)NtStatus ) {
  1240. return error;
  1241. }
  1242. #endif // WIN32_CHICAGO
  1243. //
  1244. // Could not map the NT status to anything appropriate.
  1245. //
  1246. return NERR_InternalError;
  1247. }
  1248. } // NetpNtStatusToApiStatus
  1249. NET_API_STATUS
  1250. UaspGetDomainId(
  1251. IN LPCWSTR ServerName OPTIONAL,
  1252. OUT PSAM_HANDLE SamServerHandle OPTIONAL,
  1253. OUT PPOLICY_ACCOUNT_DOMAIN_INFO * AccountDomainInfo
  1254. )
  1255. /*++
  1256. Routine Description:
  1257. Return a domain ID of the account domain of a server.
  1258. Arguments:
  1259. ServerName - A pointer to a string containing the name of the
  1260. Domain Controller (DC) to query. A NULL pointer
  1261. or string specifies the local machine.
  1262. SamServerHandle - Returns the SAM connection handle if the caller wants it.
  1263. DomainId - Receives a pointer to the domain ID.
  1264. Caller must deallocate buffer using NetpMemoryFree.
  1265. Return Value:
  1266. Error code for the operation.
  1267. --*/
  1268. {
  1269. NET_API_STATUS NetStatus;
  1270. NTSTATUS Status;
  1271. SAM_HANDLE LocalSamHandle = NULL;
  1272. ACCESS_MASK LSADesiredAccess;
  1273. LSA_HANDLE LSAPolicyHandle = NULL;
  1274. OBJECT_ATTRIBUTES LSAObjectAttributes;
  1275. UNICODE_STRING ServerNameString;
  1276. //
  1277. // Connect to the SAM server
  1278. //
  1279. RtlInitUnicodeString( &ServerNameString, ServerName );
  1280. Status = SamConnect(
  1281. &ServerNameString,
  1282. &LocalSamHandle,
  1283. SAM_SERVER_LOOKUP_DOMAIN,
  1284. NULL);
  1285. if ( !NT_SUCCESS(Status))
  1286. {
  1287. LocalSamHandle = NULL;
  1288. NetStatus = NetpNtStatusToApiStatus( Status );
  1289. goto Cleanup;
  1290. }
  1291. //
  1292. // Open LSA to read account domain info.
  1293. //
  1294. if ( AccountDomainInfo != NULL) {
  1295. //
  1296. // set desired access mask.
  1297. //
  1298. LSADesiredAccess = POLICY_VIEW_LOCAL_INFORMATION;
  1299. InitializeObjectAttributes( &LSAObjectAttributes,
  1300. NULL, // Name
  1301. 0, // Attributes
  1302. NULL, // Root
  1303. NULL ); // Security Descriptor
  1304. Status = LsaOpenPolicy( &ServerNameString,
  1305. &LSAObjectAttributes,
  1306. LSADesiredAccess,
  1307. &LSAPolicyHandle );
  1308. if( !NT_SUCCESS(Status) ) {
  1309. NetStatus = NetpNtStatusToApiStatus( Status );
  1310. goto Cleanup;
  1311. }
  1312. //
  1313. // now read account domain info from LSA.
  1314. //
  1315. Status = LsaQueryInformationPolicy(
  1316. LSAPolicyHandle,
  1317. PolicyAccountDomainInformation,
  1318. (PVOID *) AccountDomainInfo );
  1319. if( !NT_SUCCESS(Status) ) {
  1320. NetStatus = NetpNtStatusToApiStatus( Status );
  1321. goto Cleanup;
  1322. }
  1323. }
  1324. //
  1325. // Return the SAM connection handle to the caller if he wants it.
  1326. // Otherwise, disconnect from SAM.
  1327. //
  1328. if ( ARGUMENT_PRESENT( SamServerHandle ) ) {
  1329. *SamServerHandle = LocalSamHandle;
  1330. LocalSamHandle = NULL;
  1331. }
  1332. NetStatus = NERR_Success;
  1333. //
  1334. // Cleanup locally used resources
  1335. //
  1336. Cleanup:
  1337. if ( LocalSamHandle != NULL ) {
  1338. (VOID) SamCloseHandle( LocalSamHandle );
  1339. }
  1340. if( LSAPolicyHandle != NULL ) {
  1341. LsaClose( LSAPolicyHandle );
  1342. }
  1343. return NetStatus;
  1344. } // UaspGetDomainId
  1345. NET_API_STATUS
  1346. SampCreateFullSid(
  1347. IN PSID DomainSid,
  1348. IN ULONG Rid,
  1349. OUT PSID *AccountSid
  1350. )
  1351. /*++
  1352. Routine Description:
  1353. This function creates a domain account sid given a domain sid and
  1354. the relative id of the account within the domain.
  1355. The returned Sid may be freed with LocalFree.
  1356. --*/
  1357. {
  1358. NET_API_STATUS NetStatus;
  1359. NTSTATUS IgnoreStatus;
  1360. UCHAR AccountSubAuthorityCount;
  1361. ULONG AccountSidLength;
  1362. PULONG RidLocation;
  1363. //
  1364. // Calculate the size of the new sid
  1365. //
  1366. AccountSubAuthorityCount = *RtlSubAuthorityCountSid(DomainSid) + (UCHAR)1;
  1367. AccountSidLength = RtlLengthRequiredSid(AccountSubAuthorityCount);
  1368. //
  1369. // Allocate space for the account sid
  1370. //
  1371. *AccountSid = LocalAlloc(LMEM_ZEROINIT,AccountSidLength);
  1372. if (*AccountSid == NULL)
  1373. {
  1374. NetStatus = ERROR_NOT_ENOUGH_MEMORY;
  1375. }
  1376. else
  1377. {
  1378. //
  1379. // Copy the domain sid into the first part of the account sid
  1380. //
  1381. IgnoreStatus = RtlCopySid(AccountSidLength, *AccountSid, DomainSid);
  1382. ASSERT(NT_SUCCESS(IgnoreStatus));
  1383. //
  1384. // Increment the account sid sub-authority count
  1385. //
  1386. *RtlSubAuthorityCountSid(*AccountSid) = AccountSubAuthorityCount;
  1387. //
  1388. // Add the rid as the final sub-authority
  1389. //
  1390. RidLocation = RtlSubAuthoritySid(*AccountSid, AccountSubAuthorityCount-1);
  1391. *RidLocation = Rid;
  1392. NetStatus = NERR_Success;
  1393. }
  1394. return(NetStatus);
  1395. }
  1396. int GetGuestUserNameForDomain_FastWay(LPTSTR szDomainToLookUp,LPTSTR lpGuestUsrName)
  1397. {
  1398. int iReturn = FALSE;
  1399. NET_API_STATUS NetStatus;
  1400. // for UaspGetDomainId()
  1401. SAM_HANDLE SamServerHandle = NULL;
  1402. PPOLICY_ACCOUNT_DOMAIN_INFO pAccountDomainInfo = NULL;
  1403. PSID pAccountSid = NULL;
  1404. PSID pDomainSid = NULL;
  1405. // for LookupAccountSid()
  1406. SID_NAME_USE sidNameUse = SidTypeUser;
  1407. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  1408. TCHAR szUserName[UNLEN+1];
  1409. DWORD cbName = UNLEN+1;
  1410. // use UNLEN because DNLEN is too small
  1411. TCHAR szReferencedDomainName[UNLEN+1];
  1412. DWORD cbReferencedDomainName = sizeof(szReferencedDomainName);
  1413. // make sure not to return back gobble-d-gook
  1414. wcscpy(lpGuestUsrName, TEXT(""));
  1415. //
  1416. // Get the Sid for the specified Domain
  1417. //
  1418. // szDomainToLookUp=NULL for local machine
  1419. NetStatus = UaspGetDomainId( szDomainToLookUp,&SamServerHandle,&pAccountDomainInfo );
  1420. if ( NetStatus != NERR_Success )
  1421. {
  1422. goto GetGuestUserNameForDomain_FastWay_Exit;
  1423. }
  1424. pDomainSid = pAccountDomainInfo->DomainSid;
  1425. //
  1426. // Use the Domain Sid and the well known Guest RID to create the Real Guest Sid
  1427. //
  1428. // Well-known users ...
  1429. // DOMAIN_USER_RID_ADMIN (0x000001F4L)
  1430. // DOMAIN_USER_RID_GUEST (0x000001F5L)
  1431. NetStatus = NERR_InternalError;
  1432. NetStatus = SampCreateFullSid(pDomainSid, DOMAIN_USER_RID_GUEST, &pAccountSid);
  1433. if ( NetStatus != NERR_Success )
  1434. {
  1435. goto GetGuestUserNameForDomain_FastWay_Exit;
  1436. }
  1437. //
  1438. // Check if the SID is valid
  1439. //
  1440. if (0 == IsValidSid(pAccountSid))
  1441. {
  1442. DWORD dwErr = GetLastError();
  1443. goto GetGuestUserNameForDomain_FastWay_Exit;
  1444. }
  1445. //
  1446. // Retrieve the UserName for the specified SID
  1447. //
  1448. wcscpy(szUserName, TEXT(""));
  1449. wcscpy(szReferencedDomainName, TEXT(""));
  1450. // szDomainToLookUp=NULL for local machine
  1451. if (!LookupAccountSid(szDomainToLookUp,
  1452. pAccountSid,
  1453. szUserName,
  1454. &cbName,
  1455. szReferencedDomainName,
  1456. &cbReferencedDomainName,
  1457. &sidNameUse))
  1458. {
  1459. DWORD dwErr = GetLastError();
  1460. goto GetGuestUserNameForDomain_FastWay_Exit;
  1461. }
  1462. // Return the guest user name that we got.
  1463. wcscpy(lpGuestUsrName, szUserName);
  1464. // Wow, after all that, we must have succeeded
  1465. iReturn = TRUE;
  1466. GetGuestUserNameForDomain_FastWay_Exit:
  1467. // Free the Domain info if we got some
  1468. if (pAccountDomainInfo) {NetpMemoryFree(pAccountDomainInfo);}
  1469. // Free the sid if we had allocated one
  1470. if (pAccountSid) {LocalFree(pAccountSid);}
  1471. return iReturn;
  1472. }
  1473. int GetGuestUserName_SlowWay(LPWSTR lpGuestUsrName)
  1474. {
  1475. LPWSTR ServerName = NULL; // default to local machine
  1476. DWORD Level = 1; // to retrieve info of all local and global normal user accounts
  1477. DWORD Index = 0;
  1478. DWORD EntriesRequested = 5;
  1479. DWORD PreferredMaxLength = 1024;
  1480. DWORD ReturnedEntryCount = 0;
  1481. PVOID SortedBuffer = NULL;
  1482. NET_DISPLAY_USER *p = NULL;
  1483. DWORD i=0;
  1484. int err = 0;
  1485. BOOL fStatus = TRUE;
  1486. while (fStatus)
  1487. {
  1488. err = NetQueryDisplayInformation(ServerName,
  1489. Level,
  1490. Index,
  1491. EntriesRequested,
  1492. PreferredMaxLength,
  1493. &ReturnedEntryCount,
  1494. &SortedBuffer);
  1495. if (err == NERR_Success)
  1496. fStatus = FALSE;
  1497. if (err == NERR_Success || err == ERROR_MORE_DATA)
  1498. {
  1499. p = (NET_DISPLAY_USER *)SortedBuffer;
  1500. i = 0;
  1501. while (i < ReturnedEntryCount && (p[i].usri1_user_id != DOMAIN_USER_RID_GUEST))
  1502. i++;
  1503. if (i == ReturnedEntryCount)
  1504. {
  1505. if (err == ERROR_MORE_DATA)
  1506. { // need to get more entries
  1507. Index = p[i-1].usri1_next_index;
  1508. }
  1509. }
  1510. else
  1511. {
  1512. wcscpy(lpGuestUsrName, p[i].usri1_name);
  1513. fStatus = FALSE;
  1514. }
  1515. }
  1516. NetApiBufferFree(SortedBuffer);
  1517. }
  1518. return 0;
  1519. }
  1520. void GetGuestUserName(LPTSTR lpOutGuestUsrName)
  1521. {
  1522. // try to retrieve the guest username the fast way
  1523. // meaning = lookup the domain sid, and the well known guest rid, to get the guest sid.
  1524. // then look it up. The reason for this function is that on large domains with mega users
  1525. // the account can be quickly looked up.
  1526. TCHAR szGuestUsrName[UNLEN+1];
  1527. LPTSTR pszComputerName = NULL;
  1528. if (!GetGuestUserNameForDomain_FastWay(pszComputerName,szGuestUsrName))
  1529. {
  1530. // if the fast way failed for some reason, then let's do it
  1531. // the slow way, since this way always used to work, only on large domains (1 mil users)
  1532. // it could take 24hrs (since this function actually enumerates thru the domain)
  1533. GetGuestUserName_SlowWay(szGuestUsrName);
  1534. }
  1535. // Return back the username
  1536. wcscpy(lpOutGuestUsrName,szGuestUsrName);
  1537. return;
  1538. }
  1539. int GetGuestGrpName(LPTSTR lpGuestGrpName)
  1540. {
  1541. LPCTSTR ServerName = NULL; // local machine
  1542. // use UNLEN because DNLEN is too small
  1543. DWORD cbName = UNLEN+1;
  1544. TCHAR ReferencedDomainName[UNLEN+1];
  1545. DWORD cbReferencedDomainName = sizeof(ReferencedDomainName);
  1546. SID_NAME_USE sidNameUse = SidTypeUser;
  1547. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  1548. PSID GuestsSid = NULL;
  1549. AllocateAndInitializeSid(&NtAuthority,
  1550. 2,
  1551. SECURITY_BUILTIN_DOMAIN_RID,
  1552. DOMAIN_ALIAS_RID_GUESTS,
  1553. 0,
  1554. 0,
  1555. 0,
  1556. 0,
  1557. 0,
  1558. 0,
  1559. &GuestsSid);
  1560. LookupAccountSid(ServerName,
  1561. GuestsSid,
  1562. lpGuestGrpName,
  1563. &cbName,
  1564. ReferencedDomainName,
  1565. &cbReferencedDomainName,
  1566. &sidNameUse);
  1567. if (GuestsSid)
  1568. FreeSid(GuestsSid);
  1569. return 0;
  1570. }
  1571. INT RegisterAccountToLocalGroup(LPCTSTR szAccountName,
  1572. LPCTSTR szLocalGroupName,
  1573. BOOL fAction)
  1574. {
  1575. int err;
  1576. // get the sid of szAccountName
  1577. PSID pSID = NULL;
  1578. BOOL bWellKnownSID = FALSE;
  1579. err = GetPrincipalSID ((LPTSTR)szAccountName, &pSID, &bWellKnownSID);
  1580. if (err != ERROR_SUCCESS)
  1581. {
  1582. return (err);
  1583. }
  1584. // Get the localized LocalGroupName
  1585. TCHAR szLocalizedLocalGroupName[GNLEN + 1];
  1586. if (_wcsicmp(szLocalGroupName, TEXT("Guests")) == 0)
  1587. {
  1588. GetGuestGrpName(szLocalizedLocalGroupName);
  1589. }
  1590. else
  1591. {
  1592. wcscpy(szLocalizedLocalGroupName, szLocalGroupName);
  1593. }
  1594. // transfer szLocalGroupName to WCHAR
  1595. WCHAR wszLocalGroupName[_MAX_PATH];
  1596. wcscpy(wszLocalGroupName, szLocalizedLocalGroupName);
  1597. LOCALGROUP_MEMBERS_INFO_0 buf;
  1598. buf.lgrmi0_sid = pSID;
  1599. if (fAction)
  1600. {
  1601. err = NetLocalGroupAddMembers(NULL, wszLocalGroupName, 0, (LPBYTE)&buf, 1);
  1602. }
  1603. else
  1604. {
  1605. err = NetLocalGroupDelMembers(NULL, wszLocalGroupName, 0, (LPBYTE)&buf, 1);
  1606. }
  1607. if (pSID)
  1608. {
  1609. if (bWellKnownSID)
  1610. FreeSid (pSID);
  1611. else
  1612. free (pSID);
  1613. }
  1614. return (err);
  1615. }
  1616. void InitLsaString(PLSA_UNICODE_STRING LsaString,LPWSTR String)
  1617. {
  1618. DWORD StringLength;
  1619. if (String == NULL)
  1620. {
  1621. LsaString->Buffer = NULL;
  1622. LsaString->Length = 0;
  1623. LsaString->MaximumLength = 0;
  1624. return;
  1625. }
  1626. StringLength = wcslen(String);
  1627. LsaString->Buffer = String;
  1628. LsaString->Length = (USHORT) StringLength * sizeof(WCHAR);
  1629. LsaString->MaximumLength=(USHORT)(StringLength+1) * sizeof(WCHAR);
  1630. }
  1631. DWORD OpenPolicy(LPTSTR ServerName,DWORD DesiredAccess,PLSA_HANDLE PolicyHandle)
  1632. {
  1633. DWORD Error;
  1634. LSA_OBJECT_ATTRIBUTES ObjectAttributes;
  1635. LSA_UNICODE_STRING ServerString;
  1636. PLSA_UNICODE_STRING Server = NULL;
  1637. SECURITY_QUALITY_OF_SERVICE QualityOfService;
  1638. QualityOfService.Length = sizeof(SECURITY_QUALITY_OF_SERVICE);
  1639. QualityOfService.ImpersonationLevel = SecurityImpersonation;
  1640. QualityOfService.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
  1641. QualityOfService.EffectiveOnly = FALSE;
  1642. //
  1643. // The two fields that must be set are length and the quality of service.
  1644. //
  1645. ObjectAttributes.Length = sizeof(LSA_OBJECT_ATTRIBUTES);
  1646. ObjectAttributes.RootDirectory = NULL;
  1647. ObjectAttributes.ObjectName = NULL;
  1648. ObjectAttributes.Attributes = 0;
  1649. ObjectAttributes.SecurityDescriptor = NULL;
  1650. ObjectAttributes.SecurityQualityOfService = &QualityOfService;
  1651. if (ServerName != NULL)
  1652. {
  1653. //
  1654. // Make a LSA_UNICODE_STRING out of the LPWSTR passed in
  1655. //
  1656. InitLsaString(&ServerString,ServerName);
  1657. Server = &ServerString;
  1658. }
  1659. //
  1660. // Attempt to open the policy for all access
  1661. //
  1662. Error = LsaOpenPolicy(Server,&ObjectAttributes,DesiredAccess,PolicyHandle);
  1663. return(Error);
  1664. }
  1665. INT RegisterAccountUserRights(LPCTSTR szAccountName, BOOL fAction, BOOL fSpecicaliWamAccount)
  1666. {
  1667. int err;
  1668. // get the sid of szAccountName
  1669. PSID pSID = NULL;
  1670. BOOL bWellKnownSID = FALSE;
  1671. err = GetPrincipalSID ((LPTSTR)szAccountName, &pSID, &bWellKnownSID);
  1672. if (err != ERROR_SUCCESS)
  1673. {
  1674. return (err);
  1675. }
  1676. LSA_UNICODE_STRING UserRightString;
  1677. LSA_HANDLE PolicyHandle = NULL;
  1678. err = OpenPolicy(NULL, POLICY_ALL_ACCESS,&PolicyHandle);
  1679. if ( err == NERR_Success )
  1680. {
  1681. if (fAction)
  1682. {
  1683. // defined in ntsecapi.h and ntlsa.h
  1684. //#define SE_INTERACTIVE_LOGON_NAME TEXT("SeInteractiveLogonRight")
  1685. //#define SE_NETWORK_LOGON_NAME TEXT("SeNetworkLogonRight")
  1686. //#define SE_BATCH_LOGON_NAME TEXT("SeBatchLogonRight")
  1687. //#define SE_SERVICE_LOGON_NAME TEXT("SeServiceLogonRight")
  1688. // Defined in winnt.h
  1689. //#define SE_CREATE_TOKEN_NAME TEXT("SeCreateTokenPrivilege")
  1690. //#define SE_ASSIGNPRIMARYTOKEN_NAME TEXT("SeAssignPrimaryTokenPrivilege")
  1691. //#define SE_LOCK_MEMORY_NAME TEXT("SeLockMemoryPrivilege")
  1692. //#define SE_INCREASE_QUOTA_NAME TEXT("SeIncreaseQuotaPrivilege")
  1693. //#define SE_UNSOLICITED_INPUT_NAME TEXT("SeUnsolicitedInputPrivilege")
  1694. //#define SE_MACHINE_ACCOUNT_NAME TEXT("SeMachineAccountPrivilege")
  1695. //#define SE_TCB_NAME TEXT("SeTcbPrivilege")
  1696. //#define SE_SECURITY_NAME TEXT("SeSecurityPrivilege")
  1697. //#define SE_TAKE_OWNERSHIP_NAME TEXT("SeTakeOwnershipPrivilege")
  1698. //#define SE_LOAD_DRIVER_NAME TEXT("SeLoadDriverPrivilege")
  1699. //#define SE_SYSTEM_PROFILE_NAME TEXT("SeSystemProfilePrivilege")
  1700. //#define SE_SYSTEMTIME_NAME TEXT("SeSystemtimePrivilege")
  1701. //#define SE_PROF_SINGLE_PROCESS_NAME TEXT("SeProfileSingleProcessPrivilege")
  1702. //#define SE_INC_BASE_PRIORITY_NAME TEXT("SeIncreaseBasePriorityPrivilege")
  1703. //#define SE_CREATE_PAGEFILE_NAME TEXT("SeCreatePagefilePrivilege")
  1704. //#define SE_CREATE_PERMANENT_NAME TEXT("SeCreatePermanentPrivilege")
  1705. //#define SE_BACKUP_NAME TEXT("SeBackupPrivilege")
  1706. //#define SE_RESTORE_NAME TEXT("SeRestorePrivilege")
  1707. //#define SE_SHUTDOWN_NAME TEXT("SeShutdownPrivilege")
  1708. //#define SE_DEBUG_NAME TEXT("SeDebugPrivilege")
  1709. //#define SE_AUDIT_NAME TEXT("SeAuditPrivilege")
  1710. //#define SE_SYSTEM_ENVIRONMENT_NAME TEXT("SeSystemEnvironmentPrivilege")
  1711. //#define SE_CHANGE_NOTIFY_NAME TEXT("SeChangeNotifyPrivilege")
  1712. //#define SE_REMOTE_SHUTDOWN_NAME TEXT("SeRemoteShutdownPrivilege")
  1713. //#define SE_UNDOCK_NAME TEXT("SeUndockPrivilege")
  1714. //#define SE_SYNC_AGENT_NAME TEXT("SeSyncAgentPrivilege")
  1715. //#define SE_ENABLE_DELEGATION_NAME TEXT("SeEnableDelegationPrivilege")
  1716. if (fSpecicaliWamAccount)
  1717. {
  1718. // no interactive logon for iwam!
  1719. InitLsaString(&UserRightString, SE_NETWORK_LOGON_NAME);
  1720. err = LsaAddAccountRights(PolicyHandle, pSID, &UserRightString, 1);
  1721. InitLsaString(&UserRightString, SE_BATCH_LOGON_NAME);
  1722. err = LsaAddAccountRights(PolicyHandle, pSID, &UserRightString, 1);
  1723. }
  1724. else
  1725. {
  1726. InitLsaString(&UserRightString, SE_INTERACTIVE_LOGON_NAME);
  1727. err = LsaAddAccountRights(PolicyHandle, pSID, &UserRightString, 1);
  1728. InitLsaString(&UserRightString, SE_NETWORK_LOGON_NAME);
  1729. err = LsaAddAccountRights(PolicyHandle, pSID, &UserRightString, 1);
  1730. InitLsaString(&UserRightString, SE_BATCH_LOGON_NAME);
  1731. err = LsaAddAccountRights(PolicyHandle, pSID, &UserRightString, 1);
  1732. }
  1733. }
  1734. else
  1735. {
  1736. InitLsaString(&UserRightString, SE_INTERACTIVE_LOGON_NAME);
  1737. err = LsaRemoveAccountRights(PolicyHandle, pSID, FALSE, &UserRightString,1);
  1738. InitLsaString(&UserRightString, SE_NETWORK_LOGON_NAME);
  1739. err = LsaRemoveAccountRights(PolicyHandle, pSID, FALSE, &UserRightString,1);
  1740. InitLsaString(&UserRightString, SE_BATCH_LOGON_NAME);
  1741. err = LsaRemoveAccountRights(PolicyHandle, pSID, FALSE, &UserRightString,1);
  1742. }
  1743. LsaClose(PolicyHandle);
  1744. }
  1745. if (pSID)
  1746. {
  1747. if (bWellKnownSID)
  1748. FreeSid (pSID);
  1749. else
  1750. free (pSID);
  1751. }
  1752. return (err);
  1753. }
  1754. int ChangeUserPassword(IN LPTSTR szUserName, IN LPTSTR szNewPassword)
  1755. {
  1756. int iReturn = TRUE;
  1757. USER_INFO_1003 pi1003;
  1758. NET_API_STATUS nas;
  1759. TCHAR szRawComputerName[CNLEN + 10];
  1760. DWORD dwLen = CNLEN + 10;
  1761. TCHAR szComputerName[CNLEN + 10];
  1762. TCHAR szCopyOfUserName[UNLEN+10];
  1763. TCHAR szTempFullUserName[(CNLEN + 10) + (UNLEN+1)];
  1764. LPTSTR pch = NULL;
  1765. wcscpy(szCopyOfUserName, szUserName);
  1766. //iisDebugOut((LOG_TYPE_TRACE_WIN32_API, _T("ChangeUserPassword().Start.name=%s,pass=%s"),szCopyOfUserName,szNewPassword));
  1767. if ( !GetComputerName( szRawComputerName, &dwLen ))
  1768. {goto ChangeUserPassword_Exit;}
  1769. // Make a copy to be sure not to move the pointer around.
  1770. wcscpy(szTempFullUserName, szCopyOfUserName);
  1771. // Check if there is a "\" in there.
  1772. pch = wcschr(szTempFullUserName, '\\');
  1773. if (pch)
  1774. {
  1775. // szCopyOfUserName should now go from something like this:
  1776. // mycomputer\myuser
  1777. // to this myuser
  1778. wcscpy(szCopyOfUserName,pch+1);
  1779. // trim off the '\' character to leave just the domain\computername so we can check against it.
  1780. *pch = '\0';
  1781. // compare the szTempFullUserName with the local computername.
  1782. if (0 == _wcsicmp(szRawComputerName, szTempFullUserName))
  1783. {
  1784. // the computername\username has a hardcoded computername in it.
  1785. // lets try to get only the username
  1786. // look szCopyOfusername is already set
  1787. }
  1788. else
  1789. {
  1790. // the local computer machine name
  1791. // and the specified username are different, so get out
  1792. // and don't even try to change this user\password since
  1793. // it's probably a domain\username
  1794. // return true -- saying that we did in fact change the passoword.
  1795. // we really didn't but we can't
  1796. iReturn = TRUE;
  1797. goto ChangeUserPassword_Exit;
  1798. }
  1799. }
  1800. // Make sure the computername has a \\ in front of it
  1801. if ( szRawComputerName[0] != '\\' )
  1802. {wcscpy(szComputerName,L"\\\\");}
  1803. wcscat(szComputerName,szRawComputerName);
  1804. //
  1805. // administrative over-ride of existing password
  1806. //
  1807. // by this time szCopyOfUserName
  1808. // should not look like mycomputername\username but it should look like username.
  1809. pi1003.usri1003_password = szNewPassword;
  1810. nas = NetUserSetInfo(
  1811. szComputerName, // computer name
  1812. szCopyOfUserName, // username
  1813. 1003, // info level
  1814. (LPBYTE)&pi1003, // new info
  1815. NULL
  1816. );
  1817. if(nas != NERR_Success)
  1818. {
  1819. iReturn = FALSE;
  1820. goto ChangeUserPassword_Exit;
  1821. }
  1822. ChangeUserPassword_Exit:
  1823. //iisDebugOut((LOG_TYPE_TRACE_WIN32_API, _T("ChangeUserPassword().End.Ret=%d"),iReturn));
  1824. return iReturn;
  1825. }
  1826. //
  1827. // Create InternetGuest Account
  1828. //
  1829. BOOL CreateUser( LPCTSTR szUsername,
  1830. LPCTSTR szPassword,
  1831. LPCTSTR szComment,
  1832. LPCTSTR szFullName,
  1833. BOOL fSpecialiWamAccount
  1834. )
  1835. {
  1836. INT err = NERR_Success;
  1837. BYTE *pBuffer;
  1838. WCHAR defGuest[UNLEN+1];
  1839. TCHAR defGuestGroup[GNLEN+1];
  1840. WCHAR wchGuestGroup[GNLEN+1];
  1841. WCHAR wchUsername[UNLEN+1];
  1842. WCHAR wchPassword[LM20_PWLEN+1];
  1843. GetGuestUserName(defGuest);
  1844. GetGuestGrpName(defGuestGroup);
  1845. memset((PVOID)wchUsername, 0, sizeof(wchUsername));
  1846. memset((PVOID)wchPassword, 0, sizeof(wchPassword));
  1847. wcsncpy(wchGuestGroup, defGuestGroup, GNLEN);
  1848. wcsncpy(wchUsername, szUsername, UNLEN);
  1849. wcsncpy(wchPassword, szPassword, LM20_PWLEN);
  1850. err = NetUserGetInfo( NULL, defGuest, 3, &pBuffer );
  1851. if ( err == NERR_Success )
  1852. {
  1853. do
  1854. {
  1855. WCHAR wchComment[MAXCOMMENTSZ+1];
  1856. WCHAR wchFullName[UNLEN+1];
  1857. memset((PVOID)wchComment, 0, sizeof(wchComment));
  1858. memset((PVOID)wchFullName, 0, sizeof(wchFullName));
  1859. wcsncpy(wchComment, szComment, MAXCOMMENTSZ);
  1860. wcsncpy(wchFullName, szFullName, UNLEN);
  1861. USER_INFO_3 *lpui3 = (USER_INFO_3 *)pBuffer;
  1862. lpui3->usri3_name = wchUsername;
  1863. lpui3->usri3_password = wchPassword;
  1864. lpui3->usri3_flags &= ~ UF_ACCOUNTDISABLE;
  1865. lpui3->usri3_flags |= UF_DONT_EXPIRE_PASSWD;
  1866. lpui3->usri3_acct_expires = TIMEQ_FOREVER;
  1867. lpui3->usri3_comment = wchComment;
  1868. lpui3->usri3_usr_comment = wchComment;
  1869. lpui3->usri3_full_name = wchFullName;
  1870. lpui3->usri3_primary_group_id = DOMAIN_GROUP_RID_USERS;
  1871. DWORD parm_err;
  1872. err = NetUserAdd( NULL, 3, pBuffer, &parm_err );
  1873. if ( err != NERR_Success )
  1874. {
  1875. if ( err == NERR_UserExists )
  1876. {
  1877. // see if we can just change the password.
  1878. if (TRUE == ChangeUserPassword((LPTSTR) szUsername, (LPTSTR) szPassword))
  1879. {err = NERR_Success;}
  1880. }
  1881. else
  1882. {
  1883. break;
  1884. }
  1885. }
  1886. } while (FALSE);
  1887. NetApiBufferFree( pBuffer );
  1888. }
  1889. if ( err == NERR_Success )
  1890. {
  1891. // add it to the guests or IIS_WPG group
  1892. if (fSpecialiWamAccount)
  1893. {
  1894. RegisterAccountToLocalGroup(szUsername, IIS_WP_GROUP, TRUE);
  1895. }
  1896. else
  1897. {
  1898. RegisterAccountToLocalGroup(szUsername, TEXT("Guests"), TRUE);
  1899. }
  1900. // add certain user rights to this account
  1901. RegisterAccountUserRights(szUsername, TRUE, fSpecialiWamAccount);
  1902. }
  1903. return err == NERR_Success;
  1904. }
  1905. INT DeleteGuestUser( LPCTSTR szUsername )
  1906. {
  1907. INT err = NERR_Success;
  1908. BYTE *pBuffer;
  1909. BOOL fDisabled;
  1910. WCHAR wchUsername[UNLEN+1];
  1911. wcsncpy(wchUsername, szUsername, UNLEN);
  1912. if (FALSE == DoesUserExist(wchUsername,&fDisabled))
  1913. {
  1914. return err;
  1915. }
  1916. // remove it from the guests group
  1917. RegisterAccountToLocalGroup(szUsername, TEXT("Guests"), FALSE);
  1918. // remove certain user rights of this account
  1919. RegisterAccountUserRights(szUsername, FALSE, TRUE);
  1920. err = ::NetUserDel( TEXT(""), wchUsername );
  1921. return err;
  1922. }
  1923. #define MAX_REALISTIC_RESOURCE_LEN MAX_PATH
  1924. BOOL CreateUserAccount(LPTSTR pszAnonyName,
  1925. LPTSTR pszAnonyPass,
  1926. DWORD dwUserCommentResourceId,
  1927. DWORD dwUserFullNameResourceId,
  1928. BOOL fSpecicaliWamAccount
  1929. )
  1930. {
  1931. BOOL fReturn = FALSE;
  1932. WCHAR pszComment[MAX_REALISTIC_RESOURCE_LEN];
  1933. WCHAR pszFullName[MAX_REALISTIC_RESOURCE_LEN];
  1934. //
  1935. // First Load the Resources
  1936. //
  1937. HMODULE hBinary;
  1938. hBinary = GetModuleHandle(TEXT("svcext"));
  1939. if (hBinary != NULL) {
  1940. fReturn = LoadString(hBinary,
  1941. dwUserCommentResourceId,
  1942. pszComment,
  1943. MAX_REALISTIC_RESOURCE_LEN);
  1944. if (fReturn) {
  1945. fReturn = LoadString(hBinary,
  1946. dwUserFullNameResourceId,
  1947. pszFullName,
  1948. MAX_REALISTIC_RESOURCE_LEN);
  1949. }
  1950. }
  1951. if (fReturn) {
  1952. fReturn = CreateUser(pszAnonyName,
  1953. pszAnonyPass,
  1954. pszComment,
  1955. pszFullName,
  1956. fSpecicaliWamAccount
  1957. );
  1958. }
  1959. if (fReturn) {
  1960. ChangeDCOMLaunchACL(pszAnonyName,
  1961. TRUE,
  1962. TRUE);
  1963. /* removed when fixing bug 355249
  1964. ChangeDCOMAccessACL(pszAnonyName,
  1965. TRUE,
  1966. TRUE);
  1967. */
  1968. }
  1969. return fReturn;
  1970. }
  1971. typedef void (*P_SslGenerateRandomBits)( PUCHAR pRandomData, LONG size );
  1972. P_SslGenerateRandomBits ProcSslGenerateRandomBits = NULL;
  1973. int GetRandomNum(void)
  1974. {
  1975. int RandomNum;
  1976. UCHAR cRandomByte;
  1977. if ( ProcSslGenerateRandomBits != NULL )
  1978. {
  1979. (*ProcSslGenerateRandomBits)( &cRandomByte, 1 );
  1980. RandomNum = cRandomByte;
  1981. } else
  1982. {
  1983. RandomNum = rand();
  1984. }
  1985. return(RandomNum);
  1986. }
  1987. void ShuffleCharArray(int iSizeOfTheArray, TCHAR * lptsTheArray)
  1988. {
  1989. int i;
  1990. int iTotal;
  1991. int RandomNum;
  1992. iTotal = iSizeOfTheArray / sizeof(TCHAR);
  1993. for (i=0; i<iTotal;i++ )
  1994. {
  1995. // shuffle the array
  1996. RandomNum=GetRandomNum();
  1997. TCHAR c = lptsTheArray[i];
  1998. lptsTheArray[i]=lptsTheArray[RandomNum%iTotal];
  1999. lptsTheArray[RandomNum%iTotal]=c;
  2000. }
  2001. return;
  2002. }
  2003. //
  2004. // Create a random password
  2005. //
  2006. void CreatePassword( TCHAR *pszPassword )
  2007. {
  2008. //
  2009. // Use Maximum available password length, as
  2010. // setting any other length might run afoul
  2011. // of the minimum password length setting
  2012. //
  2013. int nLength = LM20_PWLEN;
  2014. int iTotal = 0;
  2015. int RandomNum = 0;
  2016. int i;
  2017. TCHAR six2pr[64] =
  2018. {
  2019. TEXT('A'), TEXT('B'), TEXT('C'), TEXT('D'), TEXT('E'), TEXT('F'), TEXT('G'), TEXT('H'),
  2020. TEXT('I'), TEXT('J'), TEXT('K'), TEXT('L'), TEXT('M'),
  2021. TEXT('N'), TEXT('O'), TEXT('P'), TEXT('Q'), TEXT('R'), TEXT('S'), TEXT('T'), TEXT('U'),
  2022. TEXT('V'), TEXT('W'), TEXT('X'), TEXT('Y'), TEXT('Z'),
  2023. TEXT('a'), TEXT('b'), TEXT('c'), TEXT('d'), TEXT('e'), TEXT('f'), TEXT('g'), TEXT('h'),
  2024. TEXT('i'), TEXT('j'), TEXT('k'), TEXT('l'), TEXT('m'),
  2025. TEXT('n'), TEXT('o'), TEXT('p'), TEXT('q'), TEXT('r'), TEXT('s'), TEXT('t'), TEXT('u'),
  2026. TEXT('v'), TEXT('w'), TEXT('x'), TEXT('y'), TEXT('z'),
  2027. TEXT('0'), TEXT('1'), TEXT('2'), TEXT('3'), TEXT('4'), TEXT('5'), TEXT('6'), TEXT('7'),
  2028. TEXT('8'), TEXT('9'), TEXT('*'), TEXT('_')
  2029. };
  2030. // create a random password
  2031. ProcSslGenerateRandomBits = NULL;
  2032. HINSTANCE hSslDll = LoadLibraryEx(TEXT("schannel.dll"), NULL, 0 );
  2033. if ( hSslDll )
  2034. {
  2035. ProcSslGenerateRandomBits = (P_SslGenerateRandomBits)GetProcAddress( hSslDll,
  2036. "SslGenerateRandomBits");
  2037. }
  2038. // See the random number generation for rand() call in GetRandomNum()
  2039. time_t timer;
  2040. time( &timer );
  2041. srand( (unsigned int) timer );
  2042. // shuffle around the global six2pr[] array
  2043. ShuffleCharArray(sizeof(six2pr), (TCHAR*) &six2pr);
  2044. // assign each character of the password array
  2045. iTotal = sizeof(six2pr) / sizeof(TCHAR);
  2046. for ( i=0;i<nLength;i++ )
  2047. {
  2048. RandomNum=GetRandomNum();
  2049. pszPassword[i]=six2pr[RandomNum%iTotal];
  2050. }
  2051. //
  2052. // in order to meet a possible
  2053. // policy set upon passwords..
  2054. //
  2055. // replace the last 4 chars with these:
  2056. //
  2057. // 1) something from !@#$%^&*()-+=
  2058. // 2) something from 1234567890
  2059. // 3) an uppercase letter
  2060. // 4) a lowercase letter
  2061. //
  2062. TCHAR something1[12] = {TEXT('!'), TEXT('@'), TEXT('#'), TEXT('$'), TEXT('^'), TEXT('&'),
  2063. TEXT('*'), TEXT('('), TEXT(')'), TEXT('-'), TEXT('+'), TEXT('=')};
  2064. ShuffleCharArray(sizeof(something1), (TCHAR*) &something1);
  2065. TCHAR something2[10] = {TEXT('0'), TEXT('1'), TEXT('2'), TEXT('3'), TEXT('4'), TEXT('5'),
  2066. TEXT('6'), TEXT('7'), TEXT('8'), TEXT('9')};
  2067. ShuffleCharArray(sizeof(something2),(TCHAR*) &something2);
  2068. TCHAR something3[26] = {TEXT('A'), TEXT('B'), TEXT('C'), TEXT('D'), TEXT('E'), TEXT('F'),
  2069. TEXT('G'), TEXT('H'), TEXT('I'), TEXT('J'), TEXT('K'), TEXT('L'),
  2070. TEXT('M'), TEXT('N'), TEXT('O'), TEXT('P'), TEXT('Q'), TEXT('R'),
  2071. TEXT('S'), TEXT('T'), TEXT('U'), TEXT('V'), TEXT('W'), TEXT('X'),
  2072. TEXT('Y'), TEXT('Z')};
  2073. ShuffleCharArray(sizeof(something3),(TCHAR*) &something3);
  2074. TCHAR something4[26] = {TEXT('a'), TEXT('b'), TEXT('c'), TEXT('d'), TEXT('e'), TEXT('f'),
  2075. TEXT('g'), TEXT('h'), TEXT('i'), TEXT('j'), TEXT('k'), TEXT('l'),
  2076. TEXT('m'), TEXT('n'), TEXT('o'), TEXT('p'), TEXT('q'), TEXT('r'),
  2077. TEXT('s'), TEXT('t'), TEXT('u'), TEXT('v'), TEXT('w'), TEXT('x'),
  2078. TEXT('y'), TEXT('z')};
  2079. ShuffleCharArray(sizeof(something4),(TCHAR*)&something4);
  2080. RandomNum=GetRandomNum();
  2081. iTotal = sizeof(something1) / sizeof(TCHAR);
  2082. pszPassword[nLength-4]=something1[RandomNum%iTotal];
  2083. RandomNum=GetRandomNum();
  2084. iTotal = sizeof(something2) / sizeof(TCHAR);
  2085. pszPassword[nLength-3]=something2[RandomNum%iTotal];
  2086. RandomNum=GetRandomNum();
  2087. iTotal = sizeof(something3) / sizeof(TCHAR);
  2088. pszPassword[nLength-2]=something3[RandomNum%iTotal];
  2089. RandomNum=GetRandomNum();
  2090. iTotal = sizeof(something4) / sizeof(TCHAR);
  2091. pszPassword[nLength-1]=something4[RandomNum%iTotal];
  2092. pszPassword[nLength]=TEXT('\0');
  2093. if (hSslDll)
  2094. {FreeLibrary( hSslDll );}
  2095. }
  2096. BOOL ValidatePassword(IN LPCTSTR UserName,IN LPCTSTR Domain,IN LPCTSTR Password)
  2097. /*++
  2098. Routine Description:
  2099. Uses SSPI to validate the specified password
  2100. Arguments:
  2101. UserName - Supplies the user name
  2102. Domain - Supplies the user's domain
  2103. Password - Supplies the password
  2104. Return Value:
  2105. TRUE if the password is valid.
  2106. FALSE otherwise.
  2107. --*/
  2108. {
  2109. SECURITY_STATUS SecStatus;
  2110. SECURITY_STATUS AcceptStatus;
  2111. SECURITY_STATUS InitStatus;
  2112. CredHandle ClientCredHandle;
  2113. CredHandle ServerCredHandle;
  2114. BOOL ClientCredAllocated = FALSE;
  2115. BOOL ServerCredAllocated = FALSE;
  2116. CtxtHandle ClientContextHandle;
  2117. CtxtHandle ServerContextHandle;
  2118. TimeStamp Lifetime;
  2119. ULONG ContextAttributes;
  2120. PSecPkgInfo PackageInfo = NULL;
  2121. ULONG ClientFlags;
  2122. ULONG ServerFlags;
  2123. TCHAR TargetName[100];
  2124. SEC_WINNT_AUTH_IDENTITY_W AuthIdentity;
  2125. BOOL Validated = FALSE;
  2126. SecBufferDesc NegotiateDesc;
  2127. SecBuffer NegotiateBuffer;
  2128. SecBufferDesc ChallengeDesc;
  2129. SecBuffer ChallengeBuffer;
  2130. SecBufferDesc AuthenticateDesc;
  2131. SecBuffer AuthenticateBuffer;
  2132. AuthIdentity.User = (LPWSTR)UserName;
  2133. AuthIdentity.UserLength = lstrlenW(UserName);
  2134. AuthIdentity.Domain = (LPWSTR)Domain;
  2135. AuthIdentity.DomainLength = lstrlenW(Domain);
  2136. AuthIdentity.Password = (LPWSTR)Password;
  2137. AuthIdentity.PasswordLength = lstrlenW(Password);
  2138. AuthIdentity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
  2139. NegotiateBuffer.pvBuffer = NULL;
  2140. ChallengeBuffer.pvBuffer = NULL;
  2141. AuthenticateBuffer.pvBuffer = NULL;
  2142. //
  2143. // Get info about the security packages.
  2144. //
  2145. SecStatus = QuerySecurityPackageInfo( TEXT("NTLM"), &PackageInfo );
  2146. if ( SecStatus != STATUS_SUCCESS ) {
  2147. goto error_exit;
  2148. }
  2149. //
  2150. // Acquire a credential handle for the server side
  2151. //
  2152. SecStatus = AcquireCredentialsHandle(
  2153. NULL,
  2154. TEXT("NTLM"),
  2155. SECPKG_CRED_INBOUND,
  2156. NULL,
  2157. &AuthIdentity,
  2158. NULL,
  2159. NULL,
  2160. &ServerCredHandle,
  2161. &Lifetime );
  2162. if ( SecStatus != STATUS_SUCCESS ) {
  2163. goto error_exit;
  2164. }
  2165. ServerCredAllocated = TRUE;
  2166. //
  2167. // Acquire a credential handle for the client side
  2168. //
  2169. SecStatus = AcquireCredentialsHandle(
  2170. NULL, // New principal
  2171. TEXT("NTLM"),
  2172. SECPKG_CRED_OUTBOUND,
  2173. NULL,
  2174. &AuthIdentity,
  2175. NULL,
  2176. NULL,
  2177. &ClientCredHandle,
  2178. &Lifetime );
  2179. if ( SecStatus != STATUS_SUCCESS ) {
  2180. goto error_exit;
  2181. }
  2182. ClientCredAllocated = TRUE;
  2183. //
  2184. // Get the NegotiateMessage (ClientSide)
  2185. //
  2186. NegotiateDesc.ulVersion = 0;
  2187. NegotiateDesc.cBuffers = 1;
  2188. NegotiateDesc.pBuffers = &NegotiateBuffer;
  2189. NegotiateBuffer.cbBuffer = PackageInfo->cbMaxToken;
  2190. NegotiateBuffer.BufferType = SECBUFFER_TOKEN;
  2191. NegotiateBuffer.pvBuffer = LocalAlloc( 0, NegotiateBuffer.cbBuffer );
  2192. if ( NegotiateBuffer.pvBuffer == NULL ) {
  2193. goto error_exit;
  2194. }
  2195. ClientFlags = ISC_REQ_MUTUAL_AUTH | ISC_REQ_REPLAY_DETECT;
  2196. InitStatus = InitializeSecurityContext(
  2197. &ClientCredHandle,
  2198. NULL, // No Client context yet
  2199. NULL,
  2200. ClientFlags,
  2201. 0, // Reserved 1
  2202. SECURITY_NATIVE_DREP,
  2203. NULL, // No initial input token
  2204. 0, // Reserved 2
  2205. &ClientContextHandle,
  2206. &NegotiateDesc,
  2207. &ContextAttributes,
  2208. &Lifetime );
  2209. if ( !NT_SUCCESS(InitStatus) ) {
  2210. goto error_exit;
  2211. }
  2212. //
  2213. // Get the ChallengeMessage (ServerSide)
  2214. //
  2215. NegotiateBuffer.BufferType |= SECBUFFER_READONLY;
  2216. ChallengeDesc.ulVersion = 0;
  2217. ChallengeDesc.cBuffers = 1;
  2218. ChallengeDesc.pBuffers = &ChallengeBuffer;
  2219. ChallengeBuffer.cbBuffer = PackageInfo->cbMaxToken;
  2220. ChallengeBuffer.BufferType = SECBUFFER_TOKEN;
  2221. ChallengeBuffer.pvBuffer = LocalAlloc( 0, ChallengeBuffer.cbBuffer );
  2222. if ( ChallengeBuffer.pvBuffer == NULL ) {
  2223. goto error_exit;
  2224. }
  2225. ServerFlags = ASC_REQ_EXTENDED_ERROR;
  2226. AcceptStatus = AcceptSecurityContext(
  2227. &ServerCredHandle,
  2228. NULL, // No Server context yet
  2229. &NegotiateDesc,
  2230. ServerFlags,
  2231. SECURITY_NATIVE_DREP,
  2232. &ServerContextHandle,
  2233. &ChallengeDesc,
  2234. &ContextAttributes,
  2235. &Lifetime );
  2236. if ( !NT_SUCCESS(AcceptStatus) ) {
  2237. goto error_exit;
  2238. }
  2239. if (InitStatus != STATUS_SUCCESS)
  2240. {
  2241. //
  2242. // Get the AuthenticateMessage (ClientSide)
  2243. //
  2244. ChallengeBuffer.BufferType |= SECBUFFER_READONLY;
  2245. AuthenticateDesc.ulVersion = 0;
  2246. AuthenticateDesc.cBuffers = 1;
  2247. AuthenticateDesc.pBuffers = &AuthenticateBuffer;
  2248. AuthenticateBuffer.cbBuffer = PackageInfo->cbMaxToken;
  2249. AuthenticateBuffer.BufferType = SECBUFFER_TOKEN;
  2250. AuthenticateBuffer.pvBuffer = LocalAlloc( 0, AuthenticateBuffer.cbBuffer );
  2251. if ( AuthenticateBuffer.pvBuffer == NULL ) {
  2252. goto error_exit;
  2253. }
  2254. SecStatus = InitializeSecurityContext(
  2255. NULL,
  2256. &ClientContextHandle,
  2257. TargetName,
  2258. 0,
  2259. 0, // Reserved 1
  2260. SECURITY_NATIVE_DREP,
  2261. &ChallengeDesc,
  2262. 0, // Reserved 2
  2263. &ClientContextHandle,
  2264. &AuthenticateDesc,
  2265. &ContextAttributes,
  2266. &Lifetime );
  2267. if ( !NT_SUCCESS(SecStatus) ) {
  2268. goto error_exit;
  2269. }
  2270. if (AcceptStatus != STATUS_SUCCESS) {
  2271. //
  2272. // Finally authenticate the user (ServerSide)
  2273. //
  2274. AuthenticateBuffer.BufferType |= SECBUFFER_READONLY;
  2275. SecStatus = AcceptSecurityContext(
  2276. NULL,
  2277. &ServerContextHandle,
  2278. &AuthenticateDesc,
  2279. ServerFlags,
  2280. SECURITY_NATIVE_DREP,
  2281. &ServerContextHandle,
  2282. NULL,
  2283. &ContextAttributes,
  2284. &Lifetime );
  2285. if ( !NT_SUCCESS(SecStatus) ) {
  2286. goto error_exit;
  2287. }
  2288. Validated = TRUE;
  2289. }
  2290. }
  2291. error_exit:
  2292. if (ServerCredAllocated) {
  2293. FreeCredentialsHandle( &ServerCredHandle );
  2294. }
  2295. if (ClientCredAllocated) {
  2296. FreeCredentialsHandle( &ClientCredHandle );
  2297. }
  2298. //
  2299. // Final Cleanup
  2300. //
  2301. if ( NegotiateBuffer.pvBuffer != NULL ) {
  2302. (VOID) LocalFree( NegotiateBuffer.pvBuffer );
  2303. }
  2304. if ( ChallengeBuffer.pvBuffer != NULL ) {
  2305. (VOID) LocalFree( ChallengeBuffer.pvBuffer );
  2306. }
  2307. if ( AuthenticateBuffer.pvBuffer != NULL ) {
  2308. (VOID) LocalFree( AuthenticateBuffer.pvBuffer );
  2309. }
  2310. return(Validated);
  2311. }
  2312. DWORD
  2313. ChangeAppIDAccessACL (
  2314. LPTSTR AppID,
  2315. LPTSTR Principal,
  2316. BOOL SetPrincipal,
  2317. BOOL Permit
  2318. )
  2319. {
  2320. TCHAR keyName [256];
  2321. DWORD err;
  2322. wcscpy(keyName, TEXT("APPID\\"));
  2323. wcscat(keyName, AppID);
  2324. if (SetPrincipal)
  2325. {
  2326. err = RemovePrincipalFromNamedValueSD (HKEY_CLASSES_ROOT,
  2327. keyName,
  2328. TEXT("AccessPermission"),
  2329. Principal);
  2330. err = AddPrincipalToNamedValueSD (HKEY_CLASSES_ROOT,
  2331. keyName,
  2332. TEXT("AccessPermission"),
  2333. Principal,
  2334. Permit);
  2335. }
  2336. else
  2337. {
  2338. err = RemovePrincipalFromNamedValueSD (HKEY_CLASSES_ROOT,
  2339. keyName,
  2340. TEXT("AccessPermission"),
  2341. Principal);
  2342. }
  2343. return err;
  2344. }
  2345. DWORD
  2346. ChangeAppIDLaunchACL (
  2347. LPTSTR AppID,
  2348. LPTSTR Principal,
  2349. BOOL SetPrincipal,
  2350. BOOL Permit
  2351. )
  2352. {
  2353. TCHAR keyName [256];
  2354. DWORD err;
  2355. wcscpy(keyName, TEXT("APPID\\"));
  2356. wcscat(keyName, AppID);
  2357. if (SetPrincipal)
  2358. {
  2359. err = RemovePrincipalFromNamedValueSD (HKEY_CLASSES_ROOT,
  2360. keyName,
  2361. TEXT("LaunchPermission"),
  2362. Principal);
  2363. err = AddPrincipalToNamedValueSD (HKEY_CLASSES_ROOT,
  2364. keyName,
  2365. TEXT("LaunchPermission"),
  2366. Principal,
  2367. Permit);
  2368. }
  2369. else
  2370. {
  2371. err = RemovePrincipalFromNamedValueSD (HKEY_CLASSES_ROOT,
  2372. keyName,
  2373. TEXT("LaunchPermission"),
  2374. Principal);
  2375. }
  2376. return err;
  2377. }
  2378. //
  2379. // Function will open the metabase and check the iusr_ and iwam_ usernames.
  2380. // it will check if the names are still valid and if the passwords are still valid.
  2381. //
  2382. VOID UpdateAnonymousUser(IMDCOM *pcCom,
  2383. LPTSTR pszPath,
  2384. DWORD dwUserMetaId,
  2385. DWORD dwPasswordMetaId,
  2386. DWORD dwUserCommentResourceId,
  2387. DWORD dwUserFullNameResourceId,
  2388. LPTSTR pszDefaultUserNamePrefix,
  2389. USERNAME_STRING_TYPE ustSyncName,
  2390. PASSWORD_STRING_TYPE pstSyncPass,
  2391. BOOL fPerformPasswordValidate)
  2392. {
  2393. int iReturn = FALSE;
  2394. USERNAME_STRING_TYPE ustAnonyName = L"\0";
  2395. PASSWORD_STRING_TYPE pstAnonyPass = L"\0";
  2396. GUFM_RETURN gufmTemp;
  2397. BOOL fRet;
  2398. BOOL fExistence;
  2399. BOOL fDisabled;
  2400. BOOL fUpdateComApplications = FALSE;
  2401. LPTSTR pstrRightsFor_IUSR[] =
  2402. {
  2403. L"SeInteractiveLogonRight",
  2404. L"SeNetworkLogonRight",
  2405. L"SeBatchLogonRight"
  2406. };
  2407. LPTSTR pstrRightsFor_IWAM[] =
  2408. {
  2409. L"SeNetworkLogonRight",
  2410. L"SeBatchLogonRight",
  2411. L"SeAssignPrimaryTokenPrivilege",
  2412. L"SeIncreaseQuotaPrivilege"
  2413. };
  2414. /*
  2415. TCHAR szEntry[_MAX_PATH];
  2416. TCHAR szPassword[LM20_PWLEN+1];
  2417. CreatePassword(szPassword);
  2418. */
  2419. //
  2420. // Get the WAM username and password
  2421. //
  2422. gufmTemp = GetUserFromMetabase(pcCom,
  2423. pszPath,
  2424. dwUserMetaId,
  2425. dwPasswordMetaId,
  2426. ustAnonyName,
  2427. pstAnonyPass);
  2428. //
  2429. // If the metabase path doesn't exist, then
  2430. // service doesn't exist, punt
  2431. // If ID doesn't exist in the metabase, then punt, assume they
  2432. // don't want an anonymous User. We may want to revisit this.
  2433. //
  2434. //
  2435. if ((gufmTemp != GUFM_NO_PATH) && (gufmTemp != GUFM_NO_USER_ID)) {
  2436. BOOL fCreateAccount = FALSE;
  2437. //
  2438. // See if this is our default account. Otherwise do nothing.
  2439. //
  2440. if (_wcsnicmp(pszDefaultUserNamePrefix,
  2441. ustAnonyName,
  2442. wcslen(pszDefaultUserNamePrefix)) == 0) {
  2443. // Check if this user actually exists...
  2444. fExistence = DoesUserExist(ustAnonyName,&fDisabled);
  2445. if (fExistence)
  2446. {
  2447. if (fDisabled)
  2448. {
  2449. fCreateAccount = FALSE;
  2450. if (!g_eventLogForAccountRecreation)
  2451. {
  2452. CreateEventLogObject ();
  2453. }
  2454. if (g_eventLogForAccountRecreation)
  2455. {
  2456. CHAR szAnsiUserName[MAX_PATH];
  2457. const CHAR *pszUserNames[1];
  2458. if (! WideCharToMultiByte(CP_ACP,
  2459. 0,
  2460. ustAnonyName,
  2461. -1,
  2462. szAnsiUserName,
  2463. MAX_PATH-1,
  2464. NULL,
  2465. NULL))
  2466. {
  2467. memset (szAnsiUserName,0,sizeof(szAnsiUserName));
  2468. }
  2469. pszUserNames[0] = szAnsiUserName;
  2470. g_eventLogForAccountRecreation->LogEvent(
  2471. INET_SVC_ACCOUNT_DISABLED,
  2472. 1,
  2473. pszUserNames,
  2474. 0 );
  2475. }
  2476. }
  2477. else
  2478. {
  2479. if (gufmTemp != GUFM_NO_PASSWORD) {
  2480. DBG_ASSERT(gufmTemp == GUFM_SUCCESS);
  2481. if (fPerformPasswordValidate)
  2482. {
  2483. BOOL fCheckPassword = TRUE;
  2484. //
  2485. // Make sure this is the same password as other
  2486. // instances of this account. If not, set it.
  2487. //
  2488. if ((pstSyncPass[0] != (TCHAR)'\0') &&
  2489. (_wcsicmp(ustSyncName, ustAnonyName) == 0)) {
  2490. if (wcscmp(pstSyncPass,
  2491. pstAnonyPass) != 0) {
  2492. //
  2493. // Passwords are different.
  2494. //
  2495. if (WritePasswordToMetabase(pcCom,
  2496. pszPath,
  2497. dwPasswordMetaId,
  2498. pstSyncPass)) {
  2499. wcscpy(pstAnonyPass,
  2500. pstSyncPass);
  2501. }
  2502. else {
  2503. fCheckPassword = FALSE;
  2504. }
  2505. }
  2506. }
  2507. if (fCheckPassword) {
  2508. if (ValidatePassword(ustAnonyName,
  2509. TEXT(""),
  2510. pstAnonyPass))
  2511. {
  2512. // thats a good case account is ok, do nothing there
  2513. }
  2514. else
  2515. {
  2516. // we comment out DeleteGuestUser because we try to change pswd on that user
  2517. // DeleteGuestUser(ustAnonyName);
  2518. fCreateAccount = TRUE;
  2519. }
  2520. }
  2521. }
  2522. //
  2523. // Set the sync password here
  2524. //
  2525. wcscpy(pstSyncPass,
  2526. pstAnonyPass);
  2527. wcscpy(ustSyncName,
  2528. ustAnonyName);
  2529. }
  2530. }
  2531. }
  2532. else {
  2533. fCreateAccount = TRUE;
  2534. }
  2535. if (fCreateAccount) {
  2536. //
  2537. // The user does not exist, so let's create it.
  2538. // Make sure there's a password first.
  2539. //
  2540. if (gufmTemp == GUFM_NO_PASSWORD) {
  2541. #if 0
  2542. //
  2543. // If it's not there then subauth should be set
  2544. // and the password should not be in the metabase.
  2545. // Also, if we add it in, then it could cause
  2546. // a synchronization problem in the IUSR password
  2547. // between W3 and FTP.
  2548. //
  2549. CreatePassword(pstAnonyPass);
  2550. fCreateAccount = WritePasswordToMetabase(pcCom,
  2551. pszPath,
  2552. dwPasswordMetaId,
  2553. pstAnonyPass);
  2554. #endif
  2555. }
  2556. if (fCreateAccount) {
  2557. if (MD_WAM_USER_NAME == dwUserMetaId)
  2558. {
  2559. fRet = CreateUserAccount(ustAnonyName,pstAnonyPass,dwUserCommentResourceId,dwUserFullNameResourceId,TRUE);
  2560. if( fRet )
  2561. {
  2562. fUpdateComApplications = TRUE;
  2563. }
  2564. // if this is a domain controller.
  2565. // we have to wait for the domain controller
  2566. // replication to be finished, otherwise The CreateUserAccount
  2567. // call will fail
  2568. //
  2569. // if we failed to create the user
  2570. // it could be because this is a DC and we need
  2571. // to wait for the sysvol to be ready.
  2572. if (!fRet)
  2573. {
  2574. if (TRUE == WaitForDCAvailability())
  2575. {
  2576. // try again...
  2577. fRet = CreateUserAccount(ustAnonyName,pstAnonyPass,dwUserCommentResourceId,dwUserFullNameResourceId,TRUE);
  2578. if( fRet )
  2579. {
  2580. fUpdateComApplications = TRUE;
  2581. }
  2582. }
  2583. }
  2584. }
  2585. else
  2586. {
  2587. fRet = CreateUserAccount(ustAnonyName,pstAnonyPass,dwUserCommentResourceId,dwUserFullNameResourceId,FALSE);
  2588. if (!fRet)
  2589. {
  2590. if (TRUE == WaitForDCAvailability())
  2591. {
  2592. // try again...
  2593. fRet = CreateUserAccount(ustAnonyName,pstAnonyPass,dwUserCommentResourceId,dwUserFullNameResourceId,FALSE);
  2594. }
  2595. }
  2596. }
  2597. if (!g_eventLogForAccountRecreation)
  2598. {
  2599. CreateEventLogObject ();
  2600. }
  2601. if (g_eventLogForAccountRecreation)
  2602. {
  2603. CHAR szAnsiUserName[MAX_PATH];
  2604. const CHAR *pszUserNames[1];
  2605. if (! WideCharToMultiByte(CP_ACP,
  2606. 0,
  2607. ustAnonyName,
  2608. -1,
  2609. szAnsiUserName,
  2610. MAX_PATH-1,
  2611. NULL,
  2612. NULL))
  2613. {
  2614. memset (szAnsiUserName,0,sizeof(szAnsiUserName));
  2615. }
  2616. pszUserNames[0] = szAnsiUserName;
  2617. // if succeded to recreate an account then log an event
  2618. if (fRet)
  2619. {
  2620. g_eventLogForAccountRecreation->LogEvent(
  2621. INET_SVC_ACCOUNT_RECREATED,
  2622. 1,
  2623. pszUserNames,
  2624. 0 );
  2625. }
  2626. else
  2627. {
  2628. // if the creation of the account failed, then log that too.
  2629. g_eventLogForAccountRecreation->LogEvent(
  2630. INET_SVC_ACCOUNT_CREATE_FAILED,
  2631. 1,
  2632. pszUserNames,
  2633. 0 );
  2634. }
  2635. }
  2636. if (dwUserMetaId == MD_WAM_USER_NAME) {
  2637. ChangeAppIDLaunchACL(TEXT("{9209B1A6-964A-11D0-9372-00A0C9034910}"),
  2638. ustAnonyName,
  2639. TRUE,
  2640. TRUE);
  2641. ChangeAppIDAccessACL(TEXT("{9209B1A6-964A-11D0-9372-00A0C9034910}"),
  2642. ustAnonyName,
  2643. TRUE,
  2644. TRUE);
  2645. }
  2646. } // fCreateAccount == TRUE
  2647. } // fCreateAccount == TRUE
  2648. //
  2649. // check if user has enough rights otherwise add some (bug 361833)
  2650. //
  2651. if (wcscmp(pszDefaultUserNamePrefix,TEXT("IUSR_")) == 0)
  2652. {
  2653. UpdateUserRights (ustAnonyName,pstrRightsFor_IUSR,sizeof(pstrRightsFor_IUSR)/sizeof(LPTSTR));
  2654. }
  2655. else
  2656. if (wcscmp(pszDefaultUserNamePrefix,TEXT("IWAM_")) == 0)
  2657. {
  2658. UpdateUserRights (ustAnonyName,pstrRightsFor_IWAM,sizeof(pstrRightsFor_IWAM)/sizeof(LPTSTR));
  2659. }
  2660. // Update the com applications with the new wam user information
  2661. if( fUpdateComApplications )
  2662. {
  2663. HRESULT hr =
  2664. UpdateComApplications( pcCom, ustAnonyName, pstAnonyPass );
  2665. if( hr != S_OK )
  2666. {
  2667. if( !g_eventLogForAccountRecreation )
  2668. {
  2669. CreateEventLogObject();
  2670. }
  2671. if ( g_eventLogForAccountRecreation )
  2672. {
  2673. g_eventLogForAccountRecreation->LogEvent(
  2674. INET_SVC_ACCOUNT_COMUPDATE_FAILED,
  2675. 0,
  2676. NULL,
  2677. hr
  2678. );
  2679. }
  2680. }
  2681. }
  2682. }
  2683. else
  2684. {
  2685. // This is not one of our accouts.
  2686. // in other words -- it doesn't start with
  2687. // iusr_ or iwam_
  2688. //
  2689. // however there is a problem here.
  2690. //
  2691. // on machines that are made to be replica domain controllers or
  2692. // backup domain controllers, when dcpromo is run to create those types
  2693. // of machines, all the local accounts are wiped out.
  2694. //
  2695. // this is fine if the usernames are iusr_ or iwam_, since they are just
  2696. // re-created in the above code (or the user is warned that they were unable
  2697. // to be crated). however in the case where these are
  2698. // user created accounts, the user has no way of knowing that
  2699. // they're iusr/iwam accounts have been hosed.
  2700. //
  2701. // the code here is just to warn the user of that fact.
  2702. if (TRUE == IsDomainController())
  2703. {
  2704. // check if they are valid.
  2705. // Check if this user actually exists...
  2706. fExistence = DoesUserExist(ustAnonyName,&fDisabled);
  2707. if (!fExistence)
  2708. {
  2709. if (!fDisabled)
  2710. {
  2711. // the user doesn't exist
  2712. // log SOMETHING at least
  2713. if (!g_eventLogForAccountRecreation)
  2714. {
  2715. CreateEventLogObject ();
  2716. }
  2717. if (g_eventLogForAccountRecreation)
  2718. {
  2719. CHAR szAnsiUserName[MAX_PATH];
  2720. const CHAR *pszUserNames[1];
  2721. if (! WideCharToMultiByte(CP_ACP,
  2722. 0,
  2723. ustAnonyName,
  2724. -1,
  2725. szAnsiUserName,
  2726. MAX_PATH-1,
  2727. NULL,
  2728. NULL))
  2729. {
  2730. memset (szAnsiUserName,0,sizeof(szAnsiUserName));
  2731. }
  2732. pszUserNames[0] = szAnsiUserName;
  2733. g_eventLogForAccountRecreation->LogEvent(
  2734. INET_SVC_ACCOUNT_NOT_EXIST,
  2735. 1,
  2736. pszUserNames,
  2737. 0 );
  2738. }
  2739. }
  2740. }
  2741. }
  2742. }
  2743. }
  2744. }
  2745. HRESULT CreateGroup(LPWSTR szGroupName, LPWSTR szGroupComment)
  2746. {
  2747. HRESULT hr = S_OK;
  2748. NET_API_STATUS dwRes;
  2749. LOCALGROUP_INFO_1 MyLocalGroup;
  2750. MyLocalGroup.lgrpi1_name = szGroupName;
  2751. MyLocalGroup.lgrpi1_comment = szGroupComment;
  2752. dwRes = NetLocalGroupAdd(NULL, 1, (LPBYTE)&MyLocalGroup, NULL);
  2753. if(dwRes != NERR_Success &&
  2754. dwRes != NERR_GroupExists &&
  2755. dwRes != ERROR_ALIAS_EXISTS)
  2756. {
  2757. hr = HRESULT_FROM_WIN32(dwRes);
  2758. }
  2759. return hr;
  2760. }
  2761. VOID
  2762. UpdateUsers(
  2763. BOOL fRestore /* = FALSE */
  2764. )
  2765. {
  2766. HRESULT hresTemp;
  2767. IMDCOM *pcCom;
  2768. BOOL fPerformUpdate = TRUE;
  2769. BOOL fPerformPasswordValidate = FALSE;
  2770. HKEY hkRegistryKey = NULL;
  2771. DWORD dwRegReturn,dwBuffer, dwSize, dwType;
  2772. HRESULT hr;
  2773. //
  2774. // First get the metabase interface
  2775. //
  2776. dwRegReturn = RegOpenKey(HKEY_LOCAL_MACHINE,
  2777. L"SOFTWARE\\Microsoft\\InetStp",
  2778. &hkRegistryKey);
  2779. if (dwRegReturn == ERROR_SUCCESS)
  2780. {
  2781. dwSize = sizeof(dwBuffer);
  2782. dwRegReturn = RegQueryValueEx(hkRegistryKey,
  2783. L"DisableUserAccountRestore",
  2784. NULL,
  2785. &dwType,
  2786. (BYTE *)&dwBuffer,
  2787. &dwSize);
  2788. if ((dwRegReturn == ERROR_SUCCESS) && dwType == (REG_DWORD))
  2789. {
  2790. fPerformUpdate = FALSE;
  2791. }
  2792. if (fPerformUpdate)
  2793. {
  2794. // we are doing the check to see if the user exists...
  2795. // see if we need to verify that the password is ssynced as well...
  2796. dwRegReturn = RegQueryValueEx(hkRegistryKey,
  2797. L"EnableUserAccountRestorePassSync",
  2798. NULL,
  2799. &dwType,
  2800. (BYTE *)&dwBuffer,
  2801. &dwSize);
  2802. if ((dwRegReturn == ERROR_SUCCESS) && dwType == (REG_DWORD))
  2803. {
  2804. fPerformPasswordValidate = TRUE;
  2805. }
  2806. }
  2807. RegCloseKey( hkRegistryKey );
  2808. }
  2809. if( fRestore )
  2810. {
  2811. fPerformPasswordValidate = TRUE;
  2812. }
  2813. if (fPerformUpdate)
  2814. {
  2815. hresTemp = CoCreateInstance(CLSID_MDCOM,
  2816. NULL,
  2817. CLSCTX_SERVER,
  2818. IID_IMDCOM,
  2819. (void**) &pcCom);
  2820. if (SUCCEEDED(hresTemp)) {
  2821. //
  2822. // Make sure the IIS_WPG group exists
  2823. //
  2824. hr = CreateGroup(IIS_WP_GROUP, L"IIS Worker Process Group");
  2825. DBGPRINTF((DBG_CONTEXT, "Called into CreateGroup, hr %x\n", hr));
  2826. RegisterAccountToLocalGroup(L"NT Authority\\Local Service",
  2827. IIS_WP_GROUP,
  2828. TRUE);
  2829. RegisterAccountToLocalGroup(L"NT Authority\\Network Service",
  2830. IIS_WP_GROUP,
  2831. TRUE);
  2832. PASSWORD_STRING_TYPE pstAnonyPass;
  2833. USERNAME_STRING_TYPE ustAnonyName;
  2834. pstAnonyPass[0] = (TCHAR)'\0';
  2835. ustAnonyName[0] = (TCHAR)'\0';
  2836. UpdateAnonymousUser(pcCom,
  2837. TEXT("LM/W3SVC"),
  2838. MD_WAM_USER_NAME,
  2839. MD_WAM_PWD,
  2840. IDS_WAMUSER_COMMENT,
  2841. IDS_WAMUSER_FULLNAME,
  2842. TEXT("IWAM_"),
  2843. ustAnonyName,
  2844. pstAnonyPass,
  2845. fPerformPasswordValidate);
  2846. DBGPRINTF((DBG_CONTEXT, "Called into Updating IWAM user, hr %x\n", hr));
  2847. pstAnonyPass[0] = (TCHAR)'\0';
  2848. ustAnonyName[0] = (TCHAR)'\0';
  2849. UpdateAnonymousUser(pcCom,
  2850. TEXT("LM/W3SVC"),
  2851. MD_ANONYMOUS_USER_NAME,
  2852. MD_ANONYMOUS_PWD,
  2853. IDS_USER_COMMENT,
  2854. IDS_USER_FULLNAME,
  2855. TEXT("IUSR_"),
  2856. ustAnonyName,
  2857. pstAnonyPass,
  2858. fPerformPasswordValidate);
  2859. DBGPRINTF((DBG_CONTEXT, "Called into Updating IUSR user, hr %x\n", hr));
  2860. //
  2861. // At this point pstAnonyPass should contain the web server password.
  2862. //
  2863. UpdateAnonymousUser(pcCom,
  2864. TEXT("LM/MSFTPSVC"),
  2865. MD_ANONYMOUS_USER_NAME,
  2866. MD_ANONYMOUS_PWD,
  2867. IDS_USER_COMMENT,
  2868. IDS_USER_FULLNAME,
  2869. TEXT("IUSR_"),
  2870. ustAnonyName,
  2871. pstAnonyPass,
  2872. fPerformPasswordValidate);
  2873. DBGPRINTF((DBG_CONTEXT, "Called into Updating IUSR user, hr %x\n", hr));
  2874. hr = UpdateAdminAcl(pcCom,
  2875. L"/LM/W3SVC",
  2876. IIS_WP_GROUP);
  2877. DBGPRINTF((DBG_CONTEXT, "Called into UpdateAdminAcl, hr %x\n", hr));
  2878. pcCom->Release();
  2879. }
  2880. }
  2881. if (g_eventLogForAccountRecreation)
  2882. {
  2883. delete g_eventLogForAccountRecreation;
  2884. g_eventLogForAccountRecreation = NULL;
  2885. }
  2886. }
  2887. void DumpAdminACL(PSECURITY_DESCRIPTOR pSD)
  2888. {
  2889. BOOL b= FALSE, bDaclPresent = FALSE, bDaclDefaulted = FALSE;;
  2890. PACL pDacl = NULL;
  2891. ACCESS_ALLOWED_ACE* pAce;
  2892. ACCESS_MASK dwOldMask, dwNewMask, dwExtraMask, dwMask;
  2893. DBGPRINTF((DBG_CONTEXT, "Dumping AdminAcl %p\n", pSD));
  2894. b = GetSecurityDescriptorDacl(pSD, &bDaclPresent, &pDacl, &bDaclDefaulted);
  2895. if (b)
  2896. {
  2897. DBGPRINTF((DBG_CONTEXT, "DumpAdminACL:ACE count: %d\n", (int)pDacl->AceCount));
  2898. // get dacl length
  2899. DWORD cbDacl = pDacl->AclSize;
  2900. // now check if SID's ACE is there
  2901. for (int i = 0; i < pDacl->AceCount; i++)
  2902. {
  2903. if (!GetAce(pDacl, i, (LPVOID *) &pAce))
  2904. {
  2905. DBGPRINTF((DBG_CONTEXT, "DumpAdminACL:GetAce failed with 0x%x\n", GetLastError()));
  2906. continue;
  2907. }
  2908. if (IsValidSid( (PSID) &(pAce->SidStart) ) )
  2909. {
  2910. LPTSTR pszSid;
  2911. LPCTSTR ServerName = NULL; // local machine
  2912. DWORD cbName = UNLEN+1;
  2913. TCHAR ReferencedDomainName[200];
  2914. DWORD cbReferencedDomainName = sizeof(ReferencedDomainName);
  2915. SID_NAME_USE sidNameUse = SidTypeUser;
  2916. TCHAR szUserName[UNLEN + 1];
  2917. // dump out the sid in string format
  2918. if (ConvertSidToStringSid( (PSID) &(pAce->SidStart) , &pszSid))
  2919. {
  2920. wcscpy(szUserName, L"(unknown...)");
  2921. if (LookupAccountSid(ServerName, (PSID) &(pAce->SidStart), szUserName, &cbName, ReferencedDomainName, &cbReferencedDomainName, &sidNameUse))
  2922. {
  2923. // echo to logfile
  2924. DBGPRINTF((DBG_CONTEXT, "DumpAdminACL:Sid[%i]=%S,%S,0x%x,0x%x,0x%x,0x%x\n",i,
  2925. pszSid,
  2926. szUserName,
  2927. pAce->Header.AceType,
  2928. pAce->Header.AceFlags,
  2929. pAce->Header.AceSize,
  2930. pAce->Mask
  2931. ));
  2932. }
  2933. else
  2934. {
  2935. DBGPRINTF((DBG_CONTEXT, "DumpAdminACL:Sid[%i]=%S='%S'\n",i,pszSid,szUserName));
  2936. }
  2937. LocalFree(LocalHandle(pszSid));
  2938. }
  2939. }
  2940. else
  2941. {
  2942. DBGPRINTF((DBG_CONTEXT, "DumpAdminACL:IsValidSid failed with 0x%x\n", GetLastError()));
  2943. }
  2944. }
  2945. }
  2946. return;
  2947. }
  2948. BOOL
  2949. MakeAbsoluteCopyFromRelative(
  2950. PSECURITY_DESCRIPTOR psdOriginal,
  2951. PSECURITY_DESCRIPTOR* ppsdNew
  2952. )
  2953. {
  2954. // we have to find out whether the original is already self-relative
  2955. SECURITY_DESCRIPTOR_CONTROL sdc = 0;
  2956. PSECURITY_DESCRIPTOR psdAbsoluteCopy = NULL;
  2957. DWORD dwRevision = 0;
  2958. DWORD cb = 0;
  2959. PACL Dacl = NULL, Sacl = NULL;
  2960. PSID Owner = NULL, Group = NULL;
  2961. DWORD dwDaclSize = 0;
  2962. DWORD dwSaclSize = 0;
  2963. DWORD dwOwnerSize = 0;
  2964. DWORD dwPrimaryGroupSize = 0;
  2965. if( !IsValidSecurityDescriptor( psdOriginal ) ) {
  2966. goto cleanup;
  2967. }
  2968. if( !GetSecurityDescriptorControl( psdOriginal, &sdc, &dwRevision ) ) {
  2969. DWORD err = GetLastError();
  2970. goto cleanup;
  2971. }
  2972. if( sdc & SE_SELF_RELATIVE ) {
  2973. // the original is in self-relative format, build an absolute copy
  2974. // get required buffer size
  2975. cb = 0;
  2976. MakeAbsoluteSD(
  2977. psdOriginal, // address of self-relative SD
  2978. psdAbsoluteCopy, // address of absolute SD
  2979. &cb, // address of size of absolute SD
  2980. NULL, // address of discretionary ACL
  2981. &dwDaclSize, // address of size of discretionary ACL
  2982. NULL, // address of system ACL
  2983. &dwSaclSize, // address of size of system ACL
  2984. NULL, // address of owner SID
  2985. &dwOwnerSize, // address of size of owner SID
  2986. NULL, // address of primary-group SID
  2987. &dwPrimaryGroupSize // address of size of group SID
  2988. );
  2989. // alloc the memory
  2990. psdAbsoluteCopy = (PSECURITY_DESCRIPTOR) malloc( cb );
  2991. Dacl = (PACL) malloc( dwDaclSize );
  2992. Sacl = (PACL) malloc( dwSaclSize );
  2993. Owner = (PSID) malloc( dwOwnerSize );
  2994. Group = (PSID) malloc( dwPrimaryGroupSize );
  2995. if(NULL == psdAbsoluteCopy ||
  2996. NULL == Dacl ||
  2997. NULL == Sacl ||
  2998. NULL == Owner ||
  2999. NULL == Group
  3000. ) {
  3001. goto cleanup;
  3002. }
  3003. // make the copy
  3004. if( !MakeAbsoluteSD(
  3005. psdOriginal, // address of self-relative SD
  3006. psdAbsoluteCopy, // address of absolute SD
  3007. &cb, // address of size of absolute SD
  3008. Dacl, // address of discretionary ACL
  3009. &dwDaclSize, // address of size of discretionary ACL
  3010. Sacl, // address of system ACL
  3011. &dwSaclSize, // address of size of system ACL
  3012. Owner, // address of owner SID
  3013. &dwOwnerSize, // address of size of owner SID
  3014. Group, // address of primary-group SID
  3015. &dwPrimaryGroupSize // address of size of group SID
  3016. )
  3017. ) {
  3018. goto cleanup;
  3019. }
  3020. } else {
  3021. // the original is in absolute format, fail
  3022. goto cleanup;
  3023. }
  3024. // paranoia check
  3025. if( !IsValidSecurityDescriptor( psdAbsoluteCopy ) ) {
  3026. goto cleanup;
  3027. }
  3028. if( !IsValidSecurityDescriptor( psdOriginal ) ) {
  3029. goto cleanup;
  3030. }
  3031. *ppsdNew = psdAbsoluteCopy;
  3032. return(TRUE);
  3033. cleanup:
  3034. if( Dacl != NULL ) {
  3035. free((PVOID) Dacl );
  3036. Dacl = NULL;
  3037. }
  3038. if( Sacl != NULL ) {
  3039. free((PVOID) Sacl );
  3040. Sacl = NULL;
  3041. }
  3042. if( Owner != NULL ) {
  3043. free((PVOID) Owner );
  3044. Owner = NULL;
  3045. }
  3046. if( Group != NULL ) {
  3047. free((PVOID) Group );
  3048. Group = NULL;
  3049. }
  3050. if( psdAbsoluteCopy != NULL ) {
  3051. free((PVOID) psdAbsoluteCopy );
  3052. psdAbsoluteCopy = NULL;
  3053. }
  3054. *ppsdNew = NULL;
  3055. return (FALSE);
  3056. }
  3057. BOOL
  3058. AddUserAccessToSD(
  3059. IN PSECURITY_DESCRIPTOR pSd,
  3060. IN PSID pSid,
  3061. IN DWORD NewAccess,
  3062. IN UCHAR TheAceType,
  3063. OUT PSECURITY_DESCRIPTOR *ppSdNew
  3064. )
  3065. {
  3066. ULONG i;
  3067. BOOL bReturn = FALSE;
  3068. BOOL Result;
  3069. BOOL DaclPresent;
  3070. BOOL DaclDefaulted;
  3071. DWORD Length;
  3072. DWORD NewAclLength;
  3073. ACCESS_ALLOWED_ACE* OldAce;
  3074. PACE_HEADER NewAce;
  3075. ACL_SIZE_INFORMATION AclInfo;
  3076. PACL Dacl = NULL;
  3077. PACL NewDacl = NULL;
  3078. PACL NewAceDacl = NULL;
  3079. PSECURITY_DESCRIPTOR NewSD = NULL;
  3080. PSECURITY_DESCRIPTOR OldSD = NULL;
  3081. PSECURITY_DESCRIPTOR outpSD = NULL;
  3082. DWORD cboutpSD = 0;
  3083. BOOL fAceForGroupPresent = FALSE;
  3084. DWORD dwMask;
  3085. OldSD = pSd;
  3086. // only do if the ace is allowed/denied
  3087. if (ACCESS_ALLOWED_ACE_TYPE != TheAceType && ACCESS_DENIED_ACE_TYPE != TheAceType)
  3088. {
  3089. goto Exit;
  3090. }
  3091. // Convert SecurityDescriptor to absolute format. It generates
  3092. // a new SecurityDescriptor for its output which we must free.
  3093. if ( !MakeAbsoluteCopyFromRelative(OldSD, &NewSD) )
  3094. {
  3095. goto Exit;
  3096. }
  3097. // Must get DACL pointer from new (absolute) SD
  3098. if(!GetSecurityDescriptorDacl(NewSD,&DaclPresent,&Dacl,&DaclDefaulted))
  3099. {
  3100. goto Exit;
  3101. }
  3102. // If no DACL, no need to add the user since no DACL
  3103. // means all accesss
  3104. if( !DaclPresent )
  3105. {
  3106. bReturn = TRUE;
  3107. goto Exit;
  3108. }
  3109. // Code can return DaclPresent, but a NULL which means
  3110. // a NULL Dacl is present. This allows all access to the object.
  3111. if( Dacl == NULL )
  3112. {
  3113. bReturn = TRUE;
  3114. goto Exit;
  3115. }
  3116. // Get the current ACL's size
  3117. if( !GetAclInformation(Dacl,&AclInfo,sizeof(AclInfo),AclSizeInformation) )
  3118. {
  3119. goto Exit;
  3120. }
  3121. // Check if access is already there
  3122. // --------------------------------
  3123. // Check to see if this SID already exists in there
  3124. // if it does (and it has the right access we want) then forget it, we don't have to do anything more.
  3125. for (i = 0; i < AclInfo.AceCount; i++)
  3126. {
  3127. ACE_HEADER *pAceHeader;
  3128. ACCESS_ALLOWED_ACE* pAce = NULL;
  3129. if (!GetAce(Dacl, i, (LPVOID *) &pAce))
  3130. {
  3131. goto Exit;
  3132. }
  3133. pAceHeader = (ACE_HEADER *)pAce;
  3134. // check if group sid is already there
  3135. if (EqualSid((PSID) &(pAce->SidStart), pSid))
  3136. {
  3137. if (pAceHeader->AceType == ACCESS_ALLOWED_ACE_TYPE)
  3138. {
  3139. // If the correct access is present, return success
  3140. if ((pAce->Mask & NewAccess) == NewAccess)
  3141. {
  3142. bReturn = TRUE;
  3143. goto Exit;
  3144. }
  3145. else
  3146. {
  3147. // the ace that exist doesn't have the permissions that we want.
  3148. // If an ACE for our SID exists, we just need to bump
  3149. // up the access level instead of creating a new ACE
  3150. fAceForGroupPresent = TRUE;
  3151. }
  3152. }
  3153. break;
  3154. }
  3155. }
  3156. // If we have to create a new ACE
  3157. // (because our user isn't listed in the existing ACL)
  3158. // then let's Create a new ACL to put the new access allowed ACE on
  3159. // --------------------------------
  3160. if (!fAceForGroupPresent)
  3161. {
  3162. NewAclLength = sizeof(ACL) +
  3163. sizeof(ACCESS_ALLOWED_ACE) - sizeof(ULONG) +
  3164. GetLengthSid( pSid );
  3165. NewAceDacl = (PACL) LocalAlloc( LMEM_FIXED, NewAclLength );
  3166. if ( NewAceDacl == NULL )
  3167. {
  3168. goto Exit;
  3169. }
  3170. if(!InitializeAcl( NewAceDacl, NewAclLength, ACL_REVISION ))
  3171. {
  3172. goto Exit;
  3173. }
  3174. if (ACCESS_DENIED_ACE_TYPE == TheAceType)
  3175. {
  3176. Result = AddAccessDeniedAce(NewAceDacl,ACL_REVISION,NewAccess,pSid);
  3177. }
  3178. else
  3179. {
  3180. Result = AddAccessAllowedAce(NewAceDacl,ACL_REVISION,NewAccess,pSid);
  3181. }
  3182. if( !Result )
  3183. {
  3184. goto Exit;
  3185. }
  3186. // Grab the 1st ace from the Newly created Dacl
  3187. if(!GetAce( NewAceDacl, 0, (void **)&NewAce ))
  3188. {
  3189. goto Exit;
  3190. }
  3191. // add CONTAINER_INHERIT_ACE TO AceFlags
  3192. //NewAce->AceFlags |= CONTAINER_INHERIT_ACE;
  3193. Length = AclInfo.AclBytesInUse + NewAce->AceSize;
  3194. }
  3195. else
  3196. {
  3197. Length = AclInfo.AclBytesInUse;
  3198. }
  3199. // Allocate new DACL
  3200. NewDacl = (PACL) LocalAlloc( LMEM_FIXED, Length );
  3201. if(NewDacl == NULL)
  3202. {
  3203. goto Exit;
  3204. }
  3205. if(!InitializeAcl( NewDacl, Length, ACL_REVISION ))
  3206. {
  3207. goto Exit;
  3208. }
  3209. // Insert new ACE at the front of the new DACL
  3210. if (!fAceForGroupPresent)
  3211. {
  3212. if(!AddAce( NewDacl, ACL_REVISION, 0, NewAce, NewAce->AceSize ))
  3213. {
  3214. goto Exit;
  3215. }
  3216. }
  3217. // ----------------------------------------
  3218. // Read thru the old Dacl and get the ACE's
  3219. // add it to the new Dacl
  3220. // ----------------------------------------
  3221. for ( i = 0; i < AclInfo.AceCount; i++ )
  3222. {
  3223. ACE_HEADER *pAceHeader;
  3224. Result = GetAce( Dacl, i, (LPVOID*) &OldAce );
  3225. if( !Result )
  3226. {
  3227. goto Exit;
  3228. }
  3229. pAceHeader = (ACE_HEADER *)OldAce;
  3230. // If an ACE for our SID exists, we just need to bump
  3231. // up the access level instead of creating a new ACE
  3232. //
  3233. if (pAceHeader->AceType == ACCESS_ALLOWED_ACE_TYPE)
  3234. {
  3235. dwMask = OldAce->Mask;
  3236. if (fAceForGroupPresent)
  3237. {
  3238. if (EqualSid((PSID) &(OldAce->SidStart), pSid))
  3239. {
  3240. dwMask = NewAccess | OldAce->Mask;
  3241. }
  3242. }
  3243. // now add ace to new dacl
  3244. Result = AddAccessAllowedAceEx(NewDacl, ACL_REVISION, OldAce->Header.AceFlags,dwMask,(PSID) &(OldAce->SidStart));
  3245. if( !Result )
  3246. {
  3247. goto Exit;
  3248. }
  3249. }
  3250. else
  3251. {
  3252. // copy denied or audit ace.
  3253. if (!AddAce(NewDacl, ACL_REVISION, 0xFFFFFFFF,OldAce, pAceHeader->AceSize ))
  3254. {
  3255. goto Exit;
  3256. }
  3257. }
  3258. }
  3259. // Set new DACL for Security Descriptor
  3260. if(!SetSecurityDescriptorDacl(NewSD,TRUE,NewDacl,FALSE))
  3261. {
  3262. goto Exit;
  3263. }
  3264. // The new SD is in absolute format. change it to Relative before we pass it back
  3265. cboutpSD = 0;
  3266. MakeSelfRelativeSD(NewSD, outpSD, &cboutpSD);
  3267. outpSD = (PSECURITY_DESCRIPTOR)GlobalAlloc(GPTR, cboutpSD);
  3268. if ( !outpSD )
  3269. {
  3270. goto Exit;
  3271. }
  3272. if (!MakeSelfRelativeSD(NewSD, outpSD, &cboutpSD))
  3273. {
  3274. goto Exit;
  3275. }
  3276. // The new SD is passed back in relative format,
  3277. *ppSdNew = outpSD;
  3278. bReturn = TRUE;
  3279. Exit:
  3280. if (NewSD){free( NewSD );NewSD = NULL;}
  3281. if (NewDacl){LocalFree( NewDacl );NewDacl = NULL;}
  3282. if (NewAceDacl){LocalFree( NewAceDacl );NewAceDacl = NULL;}
  3283. return bReturn;
  3284. }
  3285. HRESULT
  3286. UpdateAdminAcl(
  3287. IMDCOM * pcCom,
  3288. LPCWSTR szPath,
  3289. LPCWSTR szAccountName
  3290. )
  3291. /*++
  3292. Routine Description:
  3293. After DCPromo, need to update the AdminAcl in the metabase
  3294. --*/
  3295. {
  3296. METADATA_HANDLE hMetabase = NULL;
  3297. METADATA_RECORD mdrAdminAcl;
  3298. BUFFER buffSid;
  3299. BUFFER buffDomainName;
  3300. PSECURITY_DESCRIPTOR pOldSd = NULL;
  3301. PSECURITY_DESCRIPTOR pNewSd = NULL;
  3302. HRESULT hr;
  3303. DWORD dwMDGetDataLen;
  3304. DWORD cbSid;
  3305. DWORD cchDomainName;
  3306. SID_NAME_USE peUse;
  3307. hr = pcCom->ComMDOpenMetaObject( METADATA_MASTER_ROOT_HANDLE,
  3308. szPath,
  3309. METADATA_PERMISSION_READ | METADATA_PERMISSION_WRITE,
  3310. OPEN_TIMEOUT_VALUE,
  3311. &hMetabase
  3312. );
  3313. if( FAILED(hr) )
  3314. {
  3315. DBGPRINTF((DBG_CONTEXT, "Error opening metabase path %S, hr %x\n", szPath, hr));
  3316. goto cleanup;
  3317. }
  3318. MD_SET_DATA_RECORD_EXT( &mdrAdminAcl,
  3319. MD_ADMIN_ACL,
  3320. METADATA_INHERIT | METADATA_SECURE | METADATA_REFERENCE,
  3321. IIS_MD_UT_SERVER,
  3322. BINARY_METADATA,
  3323. 0,
  3324. NULL
  3325. );
  3326. hr = pcCom->ComMDGetMetaData(hMetabase,
  3327. NULL,
  3328. &mdrAdminAcl,
  3329. &dwMDGetDataLen);
  3330. if (FAILED(hr))
  3331. {
  3332. DBGPRINTF((DBG_CONTEXT, "Error retrieving data, hr %x\n", hr));
  3333. goto cleanup;
  3334. }
  3335. pOldSd = (PSECURITY_DESCRIPTOR)mdrAdminAcl.pbMDData;
  3336. //
  3337. // obtain the logon sid of the user/group
  3338. //
  3339. cbSid = buffSid.QuerySize();
  3340. cchDomainName = buffDomainName.QuerySize() / sizeof(WCHAR);
  3341. while(!LookupAccountName(NULL,
  3342. szAccountName,
  3343. buffSid.QueryPtr(),
  3344. &cbSid,
  3345. (LPWSTR)buffDomainName.QueryPtr(),
  3346. &cchDomainName,
  3347. &peUse))
  3348. {
  3349. if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  3350. {
  3351. DBGPRINTF((DBG_CONTEXT, "Error retrieving account %S, hr %x\n", szAccountName, hr));
  3352. hr = HRESULT_FROM_WIN32(GetLastError());
  3353. goto cleanup;
  3354. }
  3355. if (!buffSid.Resize(cbSid) ||
  3356. !buffDomainName.Resize(cchDomainName * sizeof(WCHAR)))
  3357. {
  3358. hr = HRESULT_FROM_WIN32(GetLastError());
  3359. goto cleanup;
  3360. }
  3361. }
  3362. DWORD AccessMask = (MD_ACR_READ | MD_ACR_WRITE | MD_ACR_RESTRICTED_WRITE | MD_ACR_UNSECURE_PROPS_READ | MD_ACR_ENUM_KEYS | MD_ACR_WRITE_DAC);
  3363. DumpAdminACL(pOldSd);
  3364. if (!AddUserAccessToSD(pOldSd,
  3365. buffSid.QueryPtr(),
  3366. AccessMask,
  3367. ACCESS_ALLOWED_ACE_TYPE,
  3368. &pNewSd))
  3369. {
  3370. hr = HRESULT_FROM_WIN32(GetLastError());
  3371. DBGPRINTF((DBG_CONTEXT, "Error adding user to AdminAcl, hr %x\n", hr));
  3372. goto cleanup;
  3373. }
  3374. if (pNewSd)
  3375. {
  3376. DumpAdminACL(pNewSd);
  3377. DWORD dwNewSd = GetSecurityDescriptorLength(pNewSd);
  3378. MD_SET_DATA_RECORD_EXT( &mdrAdminAcl,
  3379. MD_ADMIN_ACL,
  3380. METADATA_INHERIT | METADATA_SECURE | METADATA_REFERENCE,
  3381. IIS_MD_UT_SERVER,
  3382. BINARY_METADATA,
  3383. dwNewSd,
  3384. pNewSd
  3385. );
  3386. hr = pcCom->ComMDSetMetaData(hMetabase,
  3387. NULL,
  3388. &mdrAdminAcl);
  3389. if (FAILED(hr))
  3390. {
  3391. DBGPRINTF((DBG_CONTEXT, "Error setting adminacl, hr %x\n", hr));
  3392. goto cleanup;
  3393. }
  3394. }
  3395. cleanup:
  3396. if (pNewSd)
  3397. {
  3398. GlobalFree(pNewSd);
  3399. pNewSd = NULL;
  3400. }
  3401. if (hMetabase)
  3402. {
  3403. // Done with the metabase
  3404. pcCom->ComMDCloseMetaObject(hMetabase);
  3405. hMetabase = NULL;
  3406. }
  3407. return hr;
  3408. }
  3409. HRESULT
  3410. UpdateComApplications(
  3411. IMDCOM * pcCom,
  3412. LPCTSTR szWamUserName,
  3413. LPCTSTR szWamUserPass
  3414. )
  3415. /*++
  3416. Routine Description:
  3417. If the IWAM account has been modified, it is necessary to update the
  3418. the out of process com+ applications with the correct account
  3419. information. This routine will collect all the com+ applications and
  3420. reset the activation information.
  3421. Arguments:
  3422. pcCom - metabase object
  3423. szWamUserName - the new user name
  3424. szWamUserPass - the new user password
  3425. Note:
  3426. This routine is a royal pain in the butt. I take back
  3427. all the good things I may have said about com automation.
  3428. Return Value:
  3429. HRESULT - Return value from failed API call
  3430. - E_OUTOFMEMORY
  3431. - S_OK - everything worked
  3432. - S_FALSE - encountered a non-fatal error, unable
  3433. to reset at least one application.
  3434. --*/
  3435. {
  3436. HRESULT hr = NOERROR;
  3437. BOOL fNoErrors = TRUE;
  3438. METADATA_HANDLE hMetabase = NULL;
  3439. WCHAR * pwszDataPaths = NULL;
  3440. DWORD cchDataPaths = 0;
  3441. BOOL fTryAgain;
  3442. DWORD cMaxApplications;
  3443. WCHAR * pwszCurrentPath;
  3444. STACK_BUFFER( bufMDPath, 64 );
  3445. WCHAR * pwszMDPath = NULL;
  3446. DWORD dwMDPathLen;
  3447. SAFEARRAY * psaApplications = NULL;
  3448. SAFEARRAYBOUND rgsaBound[1];
  3449. DWORD cApplications;
  3450. VARIANT varAppKey;
  3451. LONG rgIndices[1];
  3452. METADATA_RECORD mdrAppIsolated;
  3453. METADATA_RECORD mdrAppPackageId;
  3454. METADATA_RECORD mdrWamClsid;
  3455. DWORD dwAppIsolated;
  3456. WCHAR wszAppPackageId[ 40 ];
  3457. WCHAR wszWamClsid[ 40 ];
  3458. DWORD dwMDGetDataLen = 0;
  3459. ICOMAdminCatalog * pComCatalog = NULL;
  3460. ICatalogCollection * pComAppCollection = NULL;
  3461. ICatalogObject * pComApp = NULL;
  3462. BSTR bstrAppCollectionName = NULL;
  3463. LONG nAppsInCollection;
  3464. LONG iCurrentApp;
  3465. LONG nChanges;
  3466. BOOL fAppCreated = FALSE;
  3467. VARIANT varOldAppIdentity;
  3468. VARIANT varNewAppIdentity;
  3469. VARIANT varNewAppPassword;
  3470. // This is built unicode right now. Since all the com apis I need
  3471. // are unicode only I'm using wide characters here. I should get
  3472. // plenty of compiler errors if _UNICODE isn't defined, but just
  3473. // in case....
  3474. DBG_ASSERT( sizeof(TCHAR) == sizeof(WCHAR) );
  3475. DBGPRINTF(( DBG_CONTEXT,
  3476. "Updating activation identity for out of process apps.\n"
  3477. ));
  3478. // Init variants
  3479. VariantInit( &varAppKey );
  3480. VariantInit( &varOldAppIdentity );
  3481. VariantInit( &varNewAppIdentity );
  3482. VariantInit( &varNewAppPassword );
  3483. //
  3484. // Get the applications to be reset by querying the metabase paths
  3485. //
  3486. hr = pcCom->ComMDOpenMetaObject( METADATA_MASTER_ROOT_HANDLE,
  3487. ROOTMDPath,
  3488. METADATA_PERMISSION_READ,
  3489. OPEN_TIMEOUT_VALUE,
  3490. &hMetabase
  3491. );
  3492. if( FAILED(hr) )
  3493. {
  3494. DBGERROR(( DBG_CONTEXT,
  3495. "Failed to open metabase (%08x)\n",
  3496. hr
  3497. ));
  3498. goto cleanup;
  3499. }
  3500. // Get the data paths string
  3501. fTryAgain = TRUE;
  3502. do
  3503. {
  3504. hr = pcCom->ComMDGetMetaDataPaths( hMetabase,
  3505. NULL,
  3506. MD_APP_PACKAGE_ID,
  3507. STRING_METADATA,
  3508. cchDataPaths,
  3509. pwszDataPaths,
  3510. &cchDataPaths
  3511. );
  3512. if( HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) == hr )
  3513. {
  3514. delete[] pwszDataPaths;
  3515. pwszDataPaths = NULL;
  3516. pwszDataPaths = new WCHAR[cchDataPaths];
  3517. if( !pwszDataPaths )
  3518. {
  3519. hr = E_OUTOFMEMORY;
  3520. goto cleanup;
  3521. }
  3522. }
  3523. else
  3524. {
  3525. fTryAgain = FALSE;
  3526. }
  3527. }
  3528. while( fTryAgain );
  3529. //
  3530. // Done with the metabase for now
  3531. //
  3532. pcCom->ComMDCloseMetaObject(hMetabase);
  3533. hMetabase = NULL;
  3534. if( FAILED(hr) )
  3535. {
  3536. DBGERROR(( DBG_CONTEXT,
  3537. "Failed to find metadata (%08x) Data(%d)\n",
  3538. hr,
  3539. MD_APP_PACKAGE_ID
  3540. ));
  3541. goto cleanup;
  3542. }
  3543. else if (pwszDataPaths == NULL)
  3544. {
  3545. //
  3546. // If we found no isolated apps, make the path list an empty multisz
  3547. //
  3548. cchDataPaths = 1;
  3549. pwszDataPaths = new WCHAR[cchDataPaths];
  3550. if( !pwszDataPaths )
  3551. {
  3552. hr = E_OUTOFMEMORY;
  3553. goto cleanup;
  3554. }
  3555. pwszDataPaths[0] = L'\0';
  3556. }
  3557. // Determine the maximum number of applications
  3558. cMaxApplications = 1; // The pooled application
  3559. for( pwszCurrentPath = pwszDataPaths;
  3560. *pwszCurrentPath != L'\0';
  3561. pwszCurrentPath += wcslen(pwszCurrentPath) + 1
  3562. )
  3563. {
  3564. cMaxApplications++;
  3565. }
  3566. //
  3567. // Build a key array and load the com applications.
  3568. //
  3569. // Create an array to hold the keys
  3570. rgsaBound[0].cElements = cMaxApplications;
  3571. rgsaBound[0].lLbound = 0;
  3572. psaApplications = SafeArrayCreate( VT_VARIANT, 1, rgsaBound );
  3573. if( psaApplications == NULL )
  3574. {
  3575. hr = E_OUTOFMEMORY;
  3576. goto cleanup;
  3577. }
  3578. // Set the out of process pool application key
  3579. varAppKey.vt = VT_BSTR;
  3580. varAppKey.bstrVal = SysAllocString( W3_OOP_POOL_PACKAGE_ID );
  3581. if( !varAppKey.bstrVal )
  3582. {
  3583. hr = E_OUTOFMEMORY;
  3584. goto cleanup;
  3585. }
  3586. rgIndices[0] = 0;
  3587. hr = SafeArrayPutElement( psaApplications, rgIndices, &varAppKey );
  3588. if( FAILED(hr) )
  3589. {
  3590. DBGERROR(( DBG_CONTEXT,
  3591. "Failed setting an element in a safe array (%08x)\n",
  3592. hr
  3593. ));
  3594. goto cleanup;
  3595. }
  3596. // For each of the application paths determine if an out of process
  3597. // application is defined there and set the appropriate key into
  3598. // our array
  3599. MD_SET_DATA_RECORD_EXT( &mdrAppIsolated,
  3600. MD_APP_ISOLATED,
  3601. METADATA_NO_ATTRIBUTES,
  3602. ALL_METADATA,
  3603. DWORD_METADATA,
  3604. sizeof(DWORD),
  3605. (PBYTE)&dwAppIsolated
  3606. );
  3607. MD_SET_DATA_RECORD_EXT( &mdrAppPackageId,
  3608. MD_APP_PACKAGE_ID,
  3609. METADATA_NO_ATTRIBUTES,
  3610. ALL_METADATA,
  3611. STRING_METADATA,
  3612. sizeof(wszAppPackageId),
  3613. (PBYTE)wszAppPackageId
  3614. );
  3615. wszAppPackageId[0] = L'\0';
  3616. MD_SET_DATA_RECORD_EXT( &mdrWamClsid,
  3617. MD_APP_WAM_CLSID,
  3618. METADATA_NO_ATTRIBUTES,
  3619. ALL_METADATA,
  3620. STRING_METADATA,
  3621. sizeof(wszWamClsid),
  3622. (PBYTE)wszWamClsid
  3623. );
  3624. wszWamClsid[0] = L'\0';
  3625. // Go through each data path and set it into our array if
  3626. // it is an isolated application
  3627. cApplications = 1; // Actual # of applications - 1 for pool
  3628. for( pwszCurrentPath = pwszDataPaths;
  3629. *pwszCurrentPath != L'\0';
  3630. pwszCurrentPath += wcslen(pwszCurrentPath) + 1
  3631. )
  3632. {
  3633. if( hMetabase != NULL )
  3634. {
  3635. pcCom->ComMDCloseMetaObject(hMetabase);
  3636. hMetabase = NULL;
  3637. }
  3638. //
  3639. // 20 is the size of L"/LM/W3SVC"
  3640. //
  3641. if( !bufMDPath.Resize( wcslen( pwszCurrentPath ) *
  3642. sizeof( WCHAR ) + 20 ) )
  3643. {
  3644. hr = E_OUTOFMEMORY;
  3645. goto cleanup;
  3646. }
  3647. pwszMDPath = ( WCHAR* )bufMDPath.QueryPtr();
  3648. wcscpy( pwszMDPath, ROOTMDPath );
  3649. wcscat( pwszMDPath, pwszCurrentPath );
  3650. //
  3651. // Get rid of the trailing '/' or '\\'
  3652. //
  3653. dwMDPathLen = wcslen( pwszMDPath );
  3654. if( pwszMDPath[ dwMDPathLen - 1 ] == L'\\' ||
  3655. pwszMDPath[ dwMDPathLen - 1 ] == L'/' )
  3656. {
  3657. pwszMDPath[ dwMDPathLen - 1 ] = L'\0';
  3658. }
  3659. hr = pcCom->ComMDOpenMetaObject( METADATA_MASTER_ROOT_HANDLE,
  3660. pwszMDPath,
  3661. METADATA_PERMISSION_READ,
  3662. OPEN_TIMEOUT_VALUE,
  3663. &hMetabase
  3664. );
  3665. if( FAILED(hr) )
  3666. {
  3667. DBGERROR(( DBG_CONTEXT,
  3668. "Failed to open metabase (%08x)\n",
  3669. hr
  3670. ));
  3671. goto cleanup;
  3672. }
  3673. hr = pcCom->ComMDGetMetaData( hMetabase,
  3674. NULL,
  3675. &mdrAppIsolated,
  3676. &dwMDGetDataLen
  3677. );
  3678. if( FAILED(hr) )
  3679. {
  3680. DBGERROR(( DBG_CONTEXT,
  3681. "Failed to get data from the metabase (%08x)"
  3682. " Path(%S) Data(%d)\n",
  3683. hr,
  3684. pwszMDPath,
  3685. mdrAppIsolated.dwMDIdentifier
  3686. ));
  3687. fNoErrors = FALSE;
  3688. continue;
  3689. }
  3690. // Is the application out of process
  3691. if( dwAppIsolated == 1 )
  3692. {
  3693. // Get the application id
  3694. hr = pcCom->ComMDGetMetaData( hMetabase,
  3695. NULL,
  3696. &mdrAppPackageId,
  3697. &dwMDGetDataLen
  3698. );
  3699. if( FAILED(hr) )
  3700. {
  3701. DBGERROR(( DBG_CONTEXT,
  3702. "Failed to get data from the metabase (%08x)"
  3703. " Path(%S) Data(%d)\n",
  3704. hr,
  3705. pwszMDPath,
  3706. mdrAppPackageId.dwMDIdentifier
  3707. ));
  3708. fNoErrors = FALSE;
  3709. continue;
  3710. }
  3711. // Get the wam class id
  3712. hr = pcCom->ComMDGetMetaData( hMetabase,
  3713. NULL,
  3714. &mdrWamClsid,
  3715. &dwMDGetDataLen
  3716. );
  3717. if( FAILED(hr) )
  3718. {
  3719. DBGERROR(( DBG_CONTEXT,
  3720. "Failed to get data from the metabase (%08x)"
  3721. " Path(%S) Data(%d)\n",
  3722. hr,
  3723. pwszMDPath,
  3724. mdrWamClsid.dwMDIdentifier
  3725. ));
  3726. fNoErrors = FALSE;
  3727. continue;
  3728. }
  3729. //
  3730. // Close meta object since we may need to write to it after we
  3731. // create a new COM+ application.
  3732. //
  3733. pcCom->ComMDCloseMetaObject(hMetabase);
  3734. hMetabase = NULL;
  3735. hr = CreateCOMPlusApplication( pwszMDPath,
  3736. wszAppPackageId,
  3737. wszWamClsid,
  3738. &fAppCreated );
  3739. if( FAILED( hr ) )
  3740. {
  3741. fNoErrors = FALSE;
  3742. continue;
  3743. }
  3744. if( fAppCreated )
  3745. {
  3746. //
  3747. // We don't need to fix the password for this
  3748. // COM+ application
  3749. //
  3750. continue;
  3751. }
  3752. // Add the application id to the array
  3753. VariantClear( &varAppKey );
  3754. varAppKey.vt = VT_BSTR;
  3755. varAppKey.bstrVal = SysAllocString( wszAppPackageId );
  3756. if( !varAppKey.bstrVal )
  3757. {
  3758. hr = E_OUTOFMEMORY;
  3759. goto cleanup;
  3760. }
  3761. rgIndices[0]++;
  3762. hr = SafeArrayPutElement( psaApplications,
  3763. rgIndices,
  3764. &varAppKey
  3765. );
  3766. if( FAILED(hr) )
  3767. {
  3768. DBGERROR(( DBG_CONTEXT,
  3769. "Failed to set safe array element (%08x)\n",
  3770. hr
  3771. ));
  3772. VariantClear( &varAppKey );
  3773. rgIndices[0]--;
  3774. fNoErrors = FALSE;
  3775. continue;
  3776. }
  3777. cApplications++;
  3778. }
  3779. }
  3780. // Shrink the size of the safe-array if necessary
  3781. if( cApplications < cMaxApplications )
  3782. {
  3783. rgsaBound[0].cElements = cApplications;
  3784. hr = SafeArrayRedim( psaApplications, rgsaBound );
  3785. if( FAILED(hr) )
  3786. {
  3787. DBGERROR(( DBG_CONTEXT,
  3788. "Failed to redim safe array (%08x)\n",
  3789. hr
  3790. ));
  3791. goto cleanup;
  3792. }
  3793. }
  3794. //
  3795. // For each application reset the activation identity
  3796. //
  3797. // Use our key array to get the application collection
  3798. hr = CoCreateInstance( CLSID_COMAdminCatalog,
  3799. NULL,
  3800. CLSCTX_SERVER,
  3801. IID_ICOMAdminCatalog,
  3802. (void**)&pComCatalog
  3803. );
  3804. if( FAILED(hr) )
  3805. {
  3806. DBGERROR(( DBG_CONTEXT,
  3807. "Failed to create COM catalog (%08x)\n",
  3808. hr
  3809. ));
  3810. goto cleanup;
  3811. }
  3812. hr = pComCatalog->GetCollection( L"Applications",
  3813. (IDispatch **)&pComAppCollection
  3814. );
  3815. if( FAILED(hr) )
  3816. {
  3817. DBGERROR(( DBG_CONTEXT,
  3818. "Failed to get Applications collection (%08x)\n",
  3819. hr
  3820. ));
  3821. goto cleanup;
  3822. }
  3823. hr = pComAppCollection->PopulateByKey( psaApplications );
  3824. if( FAILED(hr) )
  3825. {
  3826. DBGERROR(( DBG_CONTEXT,
  3827. "Failed to populate Applications collection (%08x)\n",
  3828. hr
  3829. ));
  3830. goto cleanup;
  3831. }
  3832. // Iterate over the application collection and update all the
  3833. // applications that use IWAM.
  3834. hr = pComAppCollection->get_Count( &nAppsInCollection );
  3835. if( FAILED(hr) )
  3836. {
  3837. DBGERROR(( DBG_CONTEXT,
  3838. "Failed to get Applications count (%08x)\n",
  3839. hr
  3840. ));
  3841. goto cleanup;
  3842. }
  3843. // Init our new app identity and password.
  3844. varNewAppIdentity.vt = VT_BSTR;
  3845. varNewAppIdentity.bstrVal = SysAllocString( szWamUserName );
  3846. if( !varNewAppIdentity.bstrVal )
  3847. {
  3848. hr = E_OUTOFMEMORY;
  3849. goto cleanup;
  3850. }
  3851. varNewAppPassword.vt = VT_BSTR;
  3852. varNewAppPassword.bstrVal = SysAllocString( szWamUserPass );
  3853. if( !varNewAppPassword.bstrVal )
  3854. {
  3855. hr = E_OUTOFMEMORY;
  3856. goto cleanup;
  3857. }
  3858. for( iCurrentApp = 0; iCurrentApp < nAppsInCollection; ++iCurrentApp )
  3859. {
  3860. if( pComApp )
  3861. {
  3862. pComApp->Release();
  3863. pComApp = NULL;
  3864. }
  3865. if( varOldAppIdentity.vt != VT_EMPTY )
  3866. {
  3867. VariantClear( &varOldAppIdentity );
  3868. }
  3869. hr = pComAppCollection->get_Item( iCurrentApp,
  3870. (IDispatch **)&pComApp );
  3871. if( FAILED(hr) )
  3872. {
  3873. DBGERROR(( DBG_CONTEXT,
  3874. "Failed to get item from Applications collection (%08x)\n",
  3875. hr
  3876. ));
  3877. fNoErrors = FALSE;
  3878. continue;
  3879. }
  3880. // If the user has set this to something other than the IWAM_
  3881. // user, then we will respect that and not reset the identiy.
  3882. hr = pComApp->get_Value( L"Identity", &varOldAppIdentity );
  3883. if( FAILED(hr) )
  3884. {
  3885. DBGERROR(( DBG_CONTEXT,
  3886. "Failed to get Identify from Application (%08x)\n",
  3887. hr
  3888. ));
  3889. fNoErrors = FALSE;
  3890. continue;
  3891. }
  3892. DBG_ASSERT( varOldAppIdentity.vt == VT_BSTR );
  3893. if( varOldAppIdentity.vt == VT_BSTR )
  3894. {
  3895. if( memcmp( L"IWAM_", varOldAppIdentity.bstrVal, 10 ) == 0 )
  3896. {
  3897. hr = pComApp->put_Value( L"Identity", varNewAppIdentity );
  3898. if( FAILED(hr) )
  3899. {
  3900. DBGERROR(( DBG_CONTEXT,
  3901. "Failed to set new Identify (%08x)\n",
  3902. hr
  3903. ));
  3904. fNoErrors = FALSE;
  3905. continue;
  3906. }
  3907. hr = pComApp->put_Value( L"Password", varNewAppPassword );
  3908. if( FAILED(hr) )
  3909. {
  3910. DBGERROR(( DBG_CONTEXT,
  3911. "Failed to set new Password (%08x)\n",
  3912. hr
  3913. ));
  3914. fNoErrors = FALSE;
  3915. continue;
  3916. }
  3917. }
  3918. else
  3919. {
  3920. DBGINFO(( DBG_CONTEXT,
  3921. "Unrecognized application identity (%S)\n",
  3922. varOldAppIdentity.bstrVal
  3923. ));
  3924. }
  3925. }
  3926. }
  3927. hr = pComAppCollection->SaveChanges( &nChanges );
  3928. if( FAILED(hr) )
  3929. {
  3930. DBGERROR(( DBG_CONTEXT,
  3931. "Failed to save changes (%08x)\n",
  3932. hr
  3933. ));
  3934. goto cleanup;
  3935. }
  3936. cleanup:
  3937. if( hMetabase != NULL )
  3938. {
  3939. pcCom->ComMDCloseMetaObject(hMetabase);
  3940. hMetabase = NULL;
  3941. }
  3942. if( psaApplications != NULL )
  3943. {
  3944. SafeArrayDestroy( psaApplications );
  3945. }
  3946. if( pComCatalog != NULL )
  3947. {
  3948. pComCatalog->Release();
  3949. }
  3950. if( pComAppCollection != NULL )
  3951. {
  3952. pComAppCollection->Release();
  3953. }
  3954. if( pComApp != NULL )
  3955. {
  3956. pComApp->Release();
  3957. }
  3958. if( varAppKey.vt != VT_EMPTY )
  3959. {
  3960. VariantClear( &varAppKey );
  3961. }
  3962. if( varOldAppIdentity.vt != VT_EMPTY )
  3963. {
  3964. VariantClear( &varOldAppIdentity );
  3965. }
  3966. if( varNewAppIdentity.vt != VT_EMPTY )
  3967. {
  3968. VariantClear( &varNewAppIdentity );
  3969. }
  3970. if( varNewAppPassword.vt != VT_EMPTY )
  3971. {
  3972. VariantClear( &varNewAppPassword );
  3973. }
  3974. delete [] pwszDataPaths;
  3975. // return
  3976. if( FAILED(hr) )
  3977. {
  3978. return hr;
  3979. }
  3980. else if( fNoErrors == FALSE )
  3981. {
  3982. return S_FALSE;
  3983. }
  3984. else
  3985. {
  3986. return S_OK;
  3987. }
  3988. }
  3989. int
  3990. IsDomainController(void)
  3991. {
  3992. int iReturn = FALSE;
  3993. OSVERSIONINFOEX VerInfo;
  3994. ZeroMemory(&VerInfo, sizeof VerInfo);
  3995. VerInfo.dwOSVersionInfoSize = sizeof VerInfo;
  3996. if (GetVersionEx(reinterpret_cast<OSVERSIONINFO *>(&VerInfo)))
  3997. {
  3998. if (VER_NT_DOMAIN_CONTROLLER == VerInfo.wProductType)
  3999. {
  4000. iReturn = TRUE;
  4001. }
  4002. }
  4003. return iReturn;
  4004. }
  4005. BOOL
  4006. WaitForDCAvailability(void)
  4007. {
  4008. BOOL bRetVal = FALSE;
  4009. HANDLE hEvent;
  4010. DWORD dwResult;
  4011. DWORD dwCount = 0, dwMax;
  4012. DWORD dwMaxWait = 20000; // 20 second max wait
  4013. HKEY hKeyNetLogonParams;
  4014. if (FALSE == IsDomainController())
  4015. {
  4016. // not a domain controller
  4017. // so we don't have to worry about replication delays...
  4018. return TRUE;
  4019. }
  4020. dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("SYSTEM\\CurrentControlSet\\Services\\netlogon\\parameters"),0,KEY_READ,&hKeyNetLogonParams );
  4021. if ( dwResult == ERROR_SUCCESS )
  4022. {
  4023. //
  4024. // value exists
  4025. //
  4026. DWORD dwSysVolReady = 0,
  4027. dwSize = sizeof( DWORD ),
  4028. dwType = REG_DWORD;
  4029. dwResult = RegQueryValueEx( hKeyNetLogonParams,TEXT("SysVolReady"),0,&dwType,(LPBYTE) &dwSysVolReady,&dwSize );
  4030. if ( dwResult == ERROR_SUCCESS )
  4031. {
  4032. //
  4033. // SysVolReady?
  4034. //
  4035. if ( dwSysVolReady == 0 )
  4036. {
  4037. HANDLE hEvent;
  4038. //
  4039. // wait for SysVol to become ready
  4040. //
  4041. hEvent = CreateEvent( 0, TRUE, FALSE, TEXT("IISSysVolReadyEvent") );
  4042. if ( hEvent )
  4043. {
  4044. dwResult = RegNotifyChangeKeyValue( hKeyNetLogonParams, FALSE, REG_NOTIFY_CHANGE_LAST_SET, hEvent, TRUE );
  4045. if ( dwResult == ERROR_SUCCESS )
  4046. {
  4047. const DWORD dwMaxCount = 3;
  4048. do {
  4049. //
  4050. // wait for SysVolReady to change
  4051. // hEvent is signaled for any changes in hKeyNetLogonParams
  4052. // not just the SysVolReady value.
  4053. //
  4054. WaitForSingleObject (hEvent, dwMaxWait / dwMaxCount );
  4055. dwResult = RegQueryValueEx( hKeyNetLogonParams,TEXT("SysVolReady"),0,&dwType,(LPBYTE) &dwSysVolReady,&dwSize );
  4056. } while ( dwSysVolReady == 0 && ++dwCount < dwMaxCount );
  4057. if ( dwSysVolReady )
  4058. {
  4059. bRetVal = TRUE;
  4060. }
  4061. }
  4062. CloseHandle( hEvent );
  4063. }
  4064. }
  4065. else
  4066. {
  4067. // sysvol is ready
  4068. bRetVal = TRUE;
  4069. }
  4070. }
  4071. else
  4072. {
  4073. //
  4074. // value is non-existent, SysVol is assumed to be ready
  4075. //
  4076. if ( dwResult == ERROR_FILE_NOT_FOUND )
  4077. {
  4078. bRetVal = TRUE;
  4079. }
  4080. }
  4081. RegCloseKey( hKeyNetLogonParams );
  4082. }
  4083. else
  4084. {
  4085. // error opening regkey, maybe it's not even there
  4086. bRetVal = TRUE;
  4087. }
  4088. return bRetVal;
  4089. }