Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

579 lines
12 KiB

  1. #include <tchar.h>
  2. #include <list>
  3. #include <Sddl.h>
  4. #include <aclapi.h>
  5. using namespace std;
  6. class CDefaultSD;
  7. class CNameAndSD;
  8. typedef list<CNameAndSD> CNameAndSDList;
  9. class TSState;
  10. //from privs.cpp
  11. DWORD GrantRemotePrivilegeToEveryone( IN BOOL addPrivilage ); // add or remove
  12. //from securd.cpp
  13. DWORD SetupWorker(IN const TSState &State);
  14. //from users.cpp
  15. DWORD CopyUsersGroupToRDUsersGroup();
  16. DWORD RemoveAllFromRDUsersGroup();
  17. DWORD CopyUsersGroupToRDUsersGroup();
  18. //
  19. DWORD
  20. GetDacl(
  21. IN PSECURITY_DESCRIPTOR pSD,
  22. OUT PACL *ppDacl);
  23. DWORD
  24. GetSacl(
  25. IN PSECURITY_DESCRIPTOR pSD,
  26. OUT PACL *ppSacl);
  27. DWORD
  28. EnumWinStationSecurityDescriptors(
  29. IN HKEY hKeyParent,
  30. OUT CNameAndSDList *pNameSDList);
  31. DWORD
  32. GetWinStationSecurity(
  33. IN HKEY hKeyParent,
  34. IN PWINSTATIONNAME pWSName,
  35. IN LPCTSTR szValueName,
  36. OUT PSECURITY_DESCRIPTOR *ppSecurityDescriptor );
  37. DWORD
  38. SetWinStationSecurity(
  39. IN HKEY hKeyParent,
  40. IN PWINSTATIONNAME pWSName,
  41. IN PSECURITY_DESCRIPTOR pSecurityDescriptor );
  42. DWORD
  43. AddRemoteUsersToWinstationSD(
  44. IN HKEY hKeyParent,
  45. IN CNameAndSD *pNameSD);
  46. DWORD
  47. AddLocalAndNetworkServiceToWinstationSD(
  48. IN HKEY hKeyParent,
  49. IN CNameAndSD *pNameSD);
  50. DWORD
  51. AddUserToDacl(
  52. IN HKEY hKeyParent,
  53. IN PACL pOldACL,
  54. IN PSID pSid,
  55. IN DWORD dwAccessMask,
  56. IN CNameAndSD *pNameSD);
  57. DWORD
  58. RemoveWinstationSecurity(
  59. IN HKEY hKeyParent,
  60. IN PWINSTATIONNAME pWSName);
  61. DWORD
  62. SetNewDefaultSecurity(
  63. IN HKEY hKey);
  64. DWORD
  65. SetNewConsoleSecurity(
  66. IN HKEY hKeyParent,
  67. IN BOOL bServer);
  68. DWORD
  69. SetupWorkerNotStandAlone(
  70. IN BOOL bClean,
  71. IN BOOL bServer,
  72. IN BOOL bAppServer );
  73. DWORD
  74. GrantRemoteUsersAccessToWinstations(
  75. IN HKEY hKey,
  76. IN BOOL bServer,
  77. IN BOOL bAppServer);
  78. BOOL
  79. LookupSid(
  80. IN PSID pSid,
  81. OUT LPWSTR *ppName,
  82. OUT SID_NAME_USE *peUse);
  83. BOOL
  84. IsLocal(
  85. IN LPWSTR wszLocalCompName,
  86. IN OUT LPWSTR wszDomainandname);
  87. DWORD
  88. GetAbsoluteSD(
  89. IN PSECURITY_DESCRIPTOR pSelfRelativeSD,
  90. OUT PSECURITY_DESCRIPTOR *ppAbsoluteSD,
  91. OUT PACL *ppDacl,
  92. OUT PACL *ppSacl,
  93. OUT PSID *ppOwner,
  94. OUT PSID *ppPrimaryGroup);
  95. DWORD
  96. GetSelfRelativeSD(
  97. PSECURITY_DESCRIPTOR pAbsoluteSD,
  98. PSECURITY_DESCRIPTOR *ppSelfRelativeSD);
  99. enum DefaultSDType {
  100. DefaultRDPSD = 0,
  101. DefaultConsoleSD
  102. };
  103. /*++ class CDefaultSD
  104. Class Description:
  105. Represents the the default security descriptor
  106. in binary (self relative) form
  107. Revision History:
  108. 06-June-2000 a-skuzin Created
  109. --*/
  110. class CDefaultSD
  111. {
  112. private:
  113. PSECURITY_DESCRIPTOR m_pSD;
  114. DWORD m_dwSDSize;
  115. public:
  116. CDefaultSD() : m_pSD(NULL), m_dwSDSize(0)
  117. {
  118. }
  119. ~CDefaultSD()
  120. {
  121. if(m_pSD)
  122. {
  123. LocalFree(m_pSD);
  124. }
  125. }
  126. //read default SD from the registry
  127. DWORD Init(HKEY hKey, DefaultSDType Type)
  128. {
  129. DWORD err;
  130. if(Type == DefaultConsoleSD)
  131. {
  132. err = GetWinStationSecurity(hKey,NULL,_T("ConsoleSecurity"),&m_pSD);
  133. if(err == ERROR_FILE_NOT_FOUND)
  134. {
  135. //No "ConsoleSecurity" value means that
  136. //"DefaultSecurity" value is used as a
  137. //default SD for the console.
  138. err = GetWinStationSecurity(hKey,NULL,_T("DefaultSecurity"),&m_pSD);
  139. }
  140. }
  141. else
  142. {
  143. err = GetWinStationSecurity(hKey,NULL,_T("DefaultSecurity"),&m_pSD);
  144. }
  145. if(err == ERROR_SUCCESS)
  146. {
  147. m_dwSDSize = GetSecurityDescriptorLength(m_pSD);
  148. }
  149. return err;
  150. }
  151. // Must be a self-relative type of security descr, since after all, it is comming from
  152. // the registry
  153. BOOL IsEqual(const PSECURITY_DESCRIPTOR pSD) const
  154. {
  155. return ((m_dwSDSize == GetSecurityDescriptorLength(pSD)) &&
  156. !memcmp(pSD,m_pSD,m_dwSDSize));
  157. }
  158. // Must be a self-relative type of security descr, since after all, it is comming from
  159. // the registry
  160. DWORD CopySD(PSECURITY_DESCRIPTOR *ppSD) const
  161. {
  162. *ppSD = ( PSECURITY_DESCRIPTOR )LocalAlloc( LMEM_FIXED , m_dwSDSize );
  163. if( *ppSD )
  164. {
  165. memcpy(*ppSD,m_pSD,m_dwSDSize);
  166. return ERROR_SUCCESS;
  167. }
  168. else
  169. {
  170. return ERROR_NOT_ENOUGH_MEMORY;
  171. }
  172. }
  173. DWORD DoesDefaultSDHaveRemoteUsers(OUT LPBOOL pbHas);
  174. };
  175. /*++ class CNameAndSD
  176. Class Description:
  177. Represents the the name of a winstation along with its
  178. security descriptor
  179. Revision History:
  180. 30-March-2000 a-skuzin Created
  181. --*/
  182. class CNameAndSD
  183. {
  184. public:
  185. PWINSTATIONNAME m_pName;
  186. PSECURITY_DESCRIPTOR m_pSD;
  187. CNameAndSD() :
  188. m_pName(NULL), m_pSD(NULL)
  189. {
  190. }
  191. CNameAndSD(LPCTSTR szName) :
  192. m_pName(NULL), m_pSD(NULL)
  193. {
  194. if(szName)
  195. {
  196. m_pName = (PWINSTATIONNAME)LocalAlloc(LPTR,(_tcslen(szName)+1)*sizeof(TCHAR));
  197. if(m_pName)
  198. {
  199. _tcscpy(m_pName,szName);
  200. }
  201. else
  202. {
  203. throw(DWORD(ERROR_NOT_ENOUGH_MEMORY));
  204. }
  205. }
  206. }
  207. CNameAndSD(const CNameAndSD &ns) :
  208. m_pName(NULL), m_pSD(NULL)
  209. {
  210. *this=ns;
  211. }
  212. ~CNameAndSD()
  213. {
  214. if(m_pSD)
  215. {
  216. LocalFree(m_pSD);
  217. }
  218. if(m_pName)
  219. {
  220. LocalFree(m_pName);
  221. }
  222. }
  223. private:
  224. void operator=(const CNameAndSD &ns)
  225. {
  226. //If it is the same object - do nothing
  227. if(this == &ns)
  228. {
  229. return;
  230. }
  231. if(m_pSD)
  232. {
  233. LocalFree(m_pSD);
  234. m_pSD = NULL;
  235. }
  236. if(m_pName)
  237. {
  238. LocalFree(m_pName);
  239. m_pName = NULL;
  240. }
  241. if(ns.m_pName)
  242. {
  243. m_pName = (PWINSTATIONNAME)LocalAlloc(LPTR,(_tcslen(ns.m_pName)+1)*sizeof(TCHAR));
  244. if(m_pName)
  245. {
  246. _tcscpy(m_pName,ns.m_pName);
  247. }
  248. else
  249. {
  250. throw(DWORD(ERROR_NOT_ENOUGH_MEMORY));
  251. }
  252. }
  253. if(ns.m_pSD)
  254. {
  255. DWORD dwSize = GetSecurityDescriptorLength(ns.m_pSD);
  256. m_pSD = (PWINSTATIONNAME)LocalAlloc(LPTR,GetSecurityDescriptorLength(ns.m_pSD));
  257. if(m_pSD)
  258. {
  259. memcpy(m_pSD,ns.m_pSD,dwSize);
  260. }
  261. else
  262. {
  263. if(m_pName)
  264. {
  265. LocalFree(m_pName);
  266. m_pName = NULL;
  267. }
  268. throw(DWORD(ERROR_NOT_ENOUGH_MEMORY));
  269. }
  270. }
  271. }
  272. public:
  273. BOOL IsDefaultOrEmpty(const CDefaultSD *pds, //Default RDP SD
  274. const CDefaultSD *pcs) const //Default console SD
  275. {
  276. if(!m_pSD)
  277. {
  278. return TRUE;
  279. }
  280. else
  281. {
  282. if(IsConsole())
  283. {
  284. ASSERT(pcs);
  285. return pcs->IsEqual(m_pSD);
  286. }
  287. else
  288. {
  289. ASSERT(pds);
  290. return pds->IsEqual(m_pSD);
  291. }
  292. }
  293. }
  294. // Inilialize the security descriptor of this object to be the one being passed into it.
  295. DWORD SetDefault(const CDefaultSD &ds)
  296. {
  297. if (m_pSD)
  298. {
  299. LocalFree(m_pSD);
  300. m_pSD = NULL;
  301. }
  302. return ds.CopySD(&m_pSD);
  303. }
  304. BOOL IsConsole() const
  305. {
  306. if(m_pName && !(_tcsicmp(m_pName,_T("Console"))))
  307. {
  308. return TRUE;
  309. }
  310. return FALSE;
  311. }
  312. void SetSD(PSECURITY_DESCRIPTOR pSD)
  313. {
  314. if (m_pSD)
  315. {
  316. LocalFree(m_pSD);
  317. m_pSD = NULL;
  318. }
  319. m_pSD = pSD;
  320. }
  321. };
  322. /*++ class CNameSID
  323. Class Description:
  324. Represents the the name of a user or a group
  325. along with it's SID
  326. Revision History:
  327. 09-March-2001 skuzin Created
  328. --*/
  329. class CNameSID
  330. {
  331. private:
  332. LPWSTR m_wszName;
  333. PSID m_pSID;
  334. LPWSTR m_wszSID;
  335. public:
  336. CNameSID() :
  337. m_pSID(NULL), m_wszName(NULL), m_wszSID(NULL)
  338. {
  339. }
  340. CNameSID(LPCWSTR wszName, PSID pSID) :
  341. m_pSID(NULL), m_wszName(NULL), m_wszSID(NULL)
  342. {
  343. if(wszName)
  344. {
  345. m_wszName = (LPWSTR)LocalAlloc(LPTR,(wcslen(wszName)+1)*sizeof(WCHAR));
  346. if(m_wszName)
  347. {
  348. wcscpy(m_wszName,wszName);
  349. }
  350. else
  351. {
  352. throw(DWORD(ERROR_NOT_ENOUGH_MEMORY));
  353. }
  354. }
  355. if(pSID)
  356. {
  357. DWORD dwSidLength = GetLengthSid(pSID);
  358. m_pSID = (PSID)LocalAlloc(LPTR,dwSidLength);
  359. if(m_pSID)
  360. {
  361. CopySid(dwSidLength,m_pSID,pSID);
  362. }
  363. else
  364. {
  365. if(m_wszName)
  366. {
  367. LocalFree(m_wszName);
  368. m_wszName = NULL;
  369. }
  370. throw(DWORD(ERROR_NOT_ENOUGH_MEMORY));
  371. }
  372. }
  373. }
  374. CNameSID(const CNameSID &ns) :
  375. m_pSID(NULL), m_wszName(NULL), m_wszSID(NULL)
  376. {
  377. *this=ns;
  378. }
  379. ~CNameSID()
  380. {
  381. if(m_pSID)
  382. {
  383. LocalFree(m_pSID);
  384. m_pSID = NULL;
  385. }
  386. if(m_wszName)
  387. {
  388. LocalFree(m_wszName);
  389. m_wszName = NULL;
  390. }
  391. if(m_wszSID)
  392. {
  393. LocalFree(m_wszSID);
  394. m_wszSID = NULL;
  395. }
  396. }
  397. private:
  398. void operator=(const CNameSID &ns)
  399. {
  400. //If it is the same object - do nothing
  401. if(this == &ns)
  402. {
  403. return;
  404. }
  405. if(m_pSID)
  406. {
  407. LocalFree(m_pSID);
  408. m_pSID = NULL;
  409. }
  410. if(m_wszName)
  411. {
  412. LocalFree(m_wszName);
  413. m_wszName = NULL;
  414. }
  415. if(m_wszSID)
  416. {
  417. LocalFree(m_wszSID);
  418. m_wszSID = NULL;
  419. }
  420. if(ns.m_wszName)
  421. {
  422. m_wszName = (LPWSTR)LocalAlloc(LPTR,(wcslen(ns.m_wszName)+1)*sizeof(WCHAR));
  423. if(m_wszName)
  424. {
  425. wcscpy(m_wszName,ns.m_wszName);
  426. }
  427. else
  428. {
  429. throw(DWORD(ERROR_NOT_ENOUGH_MEMORY));
  430. }
  431. }
  432. if(ns.m_pSID)
  433. {
  434. DWORD dwSidLength = GetLengthSid(ns.m_pSID);
  435. m_pSID = (PSID)LocalAlloc(LPTR,dwSidLength);
  436. if(m_pSID)
  437. {
  438. CopySid(dwSidLength,m_pSID,ns.m_pSID);
  439. }
  440. else
  441. {
  442. if(m_wszName)
  443. {
  444. LocalFree(m_wszName);
  445. m_wszName = NULL;
  446. }
  447. throw(DWORD(ERROR_NOT_ENOUGH_MEMORY));
  448. }
  449. }
  450. if(ns.m_wszSID)
  451. {
  452. m_wszSID = (LPWSTR)LocalAlloc(LPTR,(wcslen(ns.m_wszSID)+1)*sizeof(WCHAR));
  453. if(m_wszSID)
  454. {
  455. wcscpy(m_wszSID,ns.m_wszSID);
  456. }
  457. else
  458. {
  459. if(m_wszName)
  460. {
  461. LocalFree(m_wszName);
  462. m_wszName = NULL;
  463. }
  464. if(m_pSID)
  465. {
  466. LocalFree(m_pSID);
  467. m_pSID = NULL;
  468. }
  469. throw(DWORD(ERROR_NOT_ENOUGH_MEMORY));
  470. }
  471. }
  472. }
  473. public:
  474. LPCWSTR GetName()
  475. {
  476. return m_wszName;
  477. }
  478. const PSID GetSID()
  479. {
  480. if(!m_pSID && m_wszSID)
  481. {
  482. ConvertStringSidToSidW(m_wszSID,&m_pSID);
  483. }
  484. return m_pSID;
  485. }
  486. LPCWSTR GetTextSID()
  487. {
  488. if(!m_wszSID && m_pSID)
  489. {
  490. ConvertSidToStringSidW(m_pSID,&m_wszSID);
  491. }
  492. return m_wszSID;
  493. }
  494. };