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.

503 lines
10 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. }
  202. }
  203. CNameAndSD(const CNameAndSD &ns) :
  204. m_pName(NULL), m_pSD(NULL)
  205. {
  206. *this=ns;
  207. }
  208. ~CNameAndSD()
  209. {
  210. if(m_pSD)
  211. {
  212. LocalFree(m_pSD);
  213. }
  214. if(m_pName)
  215. {
  216. LocalFree(m_pName);
  217. }
  218. }
  219. void operator=(const CNameAndSD &ns)
  220. {
  221. if(m_pSD)
  222. {
  223. LocalFree(m_pSD);
  224. m_pSD = NULL;
  225. }
  226. if(m_pName)
  227. {
  228. LocalFree(m_pName);
  229. m_pName = NULL;
  230. }
  231. if(ns.m_pName)
  232. {
  233. m_pName = (PWINSTATIONNAME)LocalAlloc(LPTR,(_tcslen(ns.m_pName)+1)*sizeof(TCHAR));
  234. if(m_pName)
  235. {
  236. _tcscpy(m_pName,ns.m_pName);
  237. }
  238. }
  239. if(ns.m_pSD)
  240. {
  241. DWORD dwSize = GetSecurityDescriptorLength(ns.m_pSD);
  242. m_pSD = (PWINSTATIONNAME)LocalAlloc(LPTR,GetSecurityDescriptorLength(ns.m_pSD));
  243. if(m_pSD)
  244. {
  245. memcpy(m_pSD,ns.m_pSD,dwSize);
  246. }
  247. }
  248. }
  249. BOOL IsDefaultOrEmpty(const CDefaultSD *pds, //Default RDP SD
  250. const CDefaultSD *pcs) const //Default console SD
  251. {
  252. if(!m_pSD)
  253. {
  254. return TRUE;
  255. }
  256. else
  257. {
  258. if(IsConsole())
  259. {
  260. ASSERT(pcs);
  261. return pcs->IsEqual(m_pSD);
  262. }
  263. else
  264. {
  265. ASSERT(pds);
  266. return pds->IsEqual(m_pSD);
  267. }
  268. }
  269. }
  270. // Inilialize the security descriptor of this object to be the one being passed into it.
  271. DWORD SetDefault(const CDefaultSD &ds)
  272. {
  273. if (m_pSD)
  274. {
  275. LocalFree(m_pSD);
  276. m_pSD = NULL;
  277. }
  278. return ds.CopySD(&m_pSD);
  279. }
  280. BOOL IsConsole() const
  281. {
  282. if(m_pName && !(_tcsicmp(m_pName,_T("Console"))))
  283. {
  284. return TRUE;
  285. }
  286. return FALSE;
  287. }
  288. void SetSD(PSECURITY_DESCRIPTOR pSD)
  289. {
  290. if (m_pSD)
  291. {
  292. LocalFree(m_pSD);
  293. m_pSD = NULL;
  294. }
  295. m_pSD = pSD;
  296. }
  297. };
  298. /*++ class CNameSID
  299. Class Description:
  300. Represents the the name of a user or a group
  301. along with it's SID
  302. Revision History:
  303. 09-March-2001 skuzin Created
  304. --*/
  305. class CNameSID
  306. {
  307. private:
  308. LPWSTR m_wszName;
  309. PSID m_pSID;
  310. LPWSTR m_wszSID;
  311. public:
  312. CNameSID() :
  313. m_pSID(NULL), m_wszName(NULL), m_wszSID(NULL)
  314. {
  315. }
  316. CNameSID(LPCWSTR wszName, PSID pSID) :
  317. m_pSID(NULL), m_wszName(NULL), m_wszSID(NULL)
  318. {
  319. if(wszName)
  320. {
  321. m_wszName = (LPWSTR)LocalAlloc(LPTR,(wcslen(wszName)+1)*sizeof(WCHAR));
  322. if(m_wszName)
  323. {
  324. wcscpy(m_wszName,wszName);
  325. }
  326. }
  327. if(pSID)
  328. {
  329. DWORD dwSidLength = GetLengthSid(pSID);
  330. m_pSID = (PSID)LocalAlloc(LPTR,dwSidLength);
  331. if(m_pSID)
  332. {
  333. CopySid(dwSidLength,m_pSID,pSID);
  334. }
  335. }
  336. }
  337. CNameSID(const CNameSID &ns) :
  338. m_pSID(NULL), m_wszName(NULL), m_wszSID(NULL)
  339. {
  340. *this=ns;
  341. }
  342. ~CNameSID()
  343. {
  344. if(m_pSID)
  345. {
  346. LocalFree(m_pSID);
  347. m_pSID = NULL;
  348. }
  349. if(m_wszName)
  350. {
  351. LocalFree(m_wszName);
  352. m_wszName = NULL;
  353. }
  354. if(m_wszSID)
  355. {
  356. LocalFree(m_wszSID);
  357. m_wszSID = NULL;
  358. }
  359. }
  360. void operator=(const CNameSID &ns)
  361. {
  362. if(m_pSID)
  363. {
  364. LocalFree(m_pSID);
  365. m_pSID = NULL;
  366. }
  367. if(m_wszName)
  368. {
  369. LocalFree(m_wszName);
  370. m_wszName = NULL;
  371. }
  372. if(m_wszSID)
  373. {
  374. LocalFree(m_wszSID);
  375. m_wszSID = NULL;
  376. }
  377. if(ns.m_wszName)
  378. {
  379. m_wszName = (LPWSTR)LocalAlloc(LPTR,(wcslen(ns.m_wszName)+1)*sizeof(WCHAR));
  380. if(m_wszName)
  381. {
  382. wcscpy(m_wszName,ns.m_wszName);
  383. }
  384. }
  385. if(ns.m_pSID)
  386. {
  387. DWORD dwSidLength = GetLengthSid(ns.m_pSID);
  388. m_pSID = (PSID)LocalAlloc(LPTR,dwSidLength);
  389. if(m_pSID)
  390. {
  391. CopySid(dwSidLength,m_pSID,ns.m_pSID);
  392. }
  393. }
  394. if(ns.m_wszSID)
  395. {
  396. m_wszSID = (LPWSTR)LocalAlloc(LPTR,(wcslen(ns.m_wszSID)+1)*sizeof(WCHAR));
  397. if(m_wszSID)
  398. {
  399. wcscpy(m_wszSID,ns.m_wszSID);
  400. }
  401. }
  402. }
  403. LPCWSTR GetName()
  404. {
  405. return m_wszName;
  406. }
  407. const PSID GetSID()
  408. {
  409. if(!m_pSID && m_wszSID)
  410. {
  411. ConvertStringSidToSidW(m_wszSID,&m_pSID);
  412. }
  413. return m_pSID;
  414. }
  415. LPCWSTR GetTextSID()
  416. {
  417. if(!m_wszSID && m_pSID)
  418. {
  419. ConvertSidToStringSidW(m_pSID,&m_wszSID);
  420. }
  421. return m_wszSID;
  422. }
  423. };