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.

388 lines
18 KiB

  1. //#pragma title ("SidCache.hpp -- Cache, Tree of SIDs")
  2. /*
  3. Copyright (c) 1995-1998, Mission Critical Software, Inc. All rights reserved.
  4. ===============================================================================
  5. Module - sidcache.hpp
  6. System - SDResolve
  7. Author - Christy Boles
  8. Created - 97/06/27
  9. Description - Cache of SIDs. Implemented using TNode derived classes, Cache is
  10. organized as a tree, sorted by Domain B RID. Each node contains
  11. Domain A RID, Domain B RID, Account Name, and counters for stats.
  12. Updates -
  13. ===============================================================================
  14. */
  15. #ifndef TSIDCACHE_HEADER
  16. #define TSIDCACHE_HEADER
  17. #ifndef TNODEINCLUDED
  18. #include "Tnode.hpp"
  19. #define TNODEINCLUDED
  20. #endif
  21. //#import "\bin\McsVarSetMin.tlb" no_namespace
  22. //#import "VarSet.tlb" no_namespace rename("property", "aproperty")//#imported below via sdstat.hpp
  23. #include "DCTStat.h"
  24. #include "WorkObj.h"
  25. #include "sdstat.hpp"
  26. #ifndef IStatusObjPtr
  27. _COM_SMARTPTR_TYPEDEF(IStatusObj, __uuidof(IStatusObj));
  28. #endif
  29. //#define ACCOUNT_NAME_LENGTH 256
  30. #define NUM_IN_BUF 5000 /* the number of accounts to get at one time NetQueryDisplayInfo()*/
  31. #define BUFSIZE 100000 /* preferred max size of buffer for receiving accounts NetQueryDisplayInfo()*/
  32. #define DEFAULT_SID_SIZE 500
  33. #define EALB_OCX_LOCAL_USER 13
  34. #define EALB_OCX_GLOBAL_USER 14
  35. #define EALB_OCX_LOCAL_GROUP 16
  36. #define EALB_OCX_GLOBAL_GROUP 17
  37. #define FST_CACHE_SOME_SOURCE 1
  38. #define FST_CACHE_NO_TARGET 2
  39. #define FST_CACHE_SOME_TARGET 4
  40. #define FST_CACHE_NO_DOMAIN 8
  41. class TAcctNode:public TNode
  42. {
  43. protected:
  44. DWORD owner_changes; // Stats for each node
  45. DWORD group_changes;
  46. DWORD ace_changes;
  47. DWORD sace_changes;
  48. public:
  49. TAcctNode();
  50. virtual WCHAR * GetAcctName() = 0;
  51. virtual bool IsValidOnTgt() const = 0;
  52. virtual void AddOwnerChange(objectType type) { owner_changes++; } // Stats functions
  53. virtual void AddGroupChange(objectType type) { group_changes++; }
  54. virtual void AddAceChange(objectType type) { ace_changes++; }
  55. virtual void AddSaceChange(objectType type) { sace_changes++; }
  56. virtual void DisplayStats() const;
  57. DWORD OwnerChanges() { return owner_changes; }
  58. DWORD GroupChanges() { return group_changes; }
  59. DWORD DACEChanges() { return ace_changes; }
  60. DWORD SACEChanges() { return sace_changes; }
  61. BOOL ReportToVarSet(IVarSet * pVarSet, DWORD n);
  62. };
  63. class TRidNode:public TAcctNode
  64. {
  65. protected:
  66. DWORD srcRid; // RID for domain A
  67. DWORD tgtRid; // RID for domain B
  68. bool tgtRidIsValid;
  69. short acct_type;
  70. int acct_len; // length of account name
  71. WCHAR srcDomSid[MAX_PATH]; // source domain sid
  72. WCHAR tgtDomSid[MAX_PATH]; // target domain sid
  73. WCHAR srcDomName[MAX_PATH]; // source domain name
  74. WCHAR tgtDomName[MAX_PATH]; // target domain name
  75. WCHAR acct_name[1]; // source account name \0 target account name
  76. public:
  77. void * operator new(size_t sz,const LPWSTR name1,const LPWSTR name2);
  78. TRidNode(const LPWSTR oldacctname, const LPWSTR newacctname);
  79. ~TRidNode();
  80. WCHAR * GetAcctName() { return acct_name; } // member "Get" functions
  81. WCHAR * GetTargetAcctName() { return acct_name + acct_len + 1; }
  82. DWORD SrcRid() const { return srcRid; }
  83. DWORD TgtRid() const { return tgtRid; }
  84. bool IsValidOnTgt() const { return tgtRidIsValid; }
  85. short Type() { return acct_type; }
  86. void Type(short newtype) { acct_type = newtype; }
  87. void MarkInvalidOnTgt() { tgtRidIsValid = false; }
  88. void SrcRid(DWORD const val) { srcRid=val; } // member "Set" functions
  89. void TgtRid(DWORD const val) { tgtRid=val; tgtRidIsValid=true;}
  90. void DisplayStats() const;
  91. void DisplaySidInfo() const;
  92. WCHAR * GetSrcDomSid() { return srcDomSid; } // member "Get" function
  93. WCHAR * GetTgtDomSid() { return tgtDomSid; } // member "Get" function
  94. void SrcDomSid(const LPWSTR sSid) { wcscpy(srcDomSid,sSid); } // member "Set" function
  95. void TgtDomSid(const LPWSTR sSid) { wcscpy(tgtDomSid,sSid); } // member "Set" function
  96. WCHAR * GetSrcDomName() { return srcDomName; } // member "Get" function
  97. WCHAR * GetTgtDomName() { return tgtDomName; } // member "Get" function
  98. void SrcDomName(const LPWSTR sName) { wcscpy(srcDomName,sName); } // member "Set" function
  99. void TgtDomName(const LPWSTR sName) { wcscpy(tgtDomName,sName); } // member "Set" function
  100. protected:
  101. };
  102. class TGeneralSidNode:public TAcctNode
  103. {
  104. protected:
  105. LPWSTR src_acct_name;
  106. LPWSTR tgt_acct_name;
  107. PSID src_sid;
  108. PSID tgt_sid;
  109. UCHAR src_nsubs;
  110. UCHAR tgt_nsubs;
  111. WCHAR * src_domain;
  112. WCHAR * tgt_domain;
  113. DWORD sizediff;
  114. TSDFileDirCell ownerStats;
  115. TSDFileDirCell groupStats;
  116. TSDFileDirCell daclStats;
  117. TSDFileDirCell saclStats;
  118. public:
  119. TGeneralSidNode(const LPWSTR name1, const LPWSTR name2);
  120. TGeneralSidNode(const PSID pSid1, const PSID pSid2);
  121. ~TGeneralSidNode();
  122. LPWSTR GetAcctName() { return src_acct_name; }
  123. PSID SrcSid() { return src_sid; }
  124. PSID TgtSid() { return src_sid; /* this is a hack to allow for counting all references to accounts */ }
  125. bool IsValidOnTgt() const { return TRUE;/*tgt_sid != NULL;*/ }
  126. void DisplaySidInfo() const;
  127. DWORD SizeDiff() const { return 0; }
  128. TSDFileDirCell * GetOwnerStats() { return &ownerStats; }
  129. TSDFileDirCell * GetGroupStats() { return &groupStats; }
  130. TSDFileDirCell * GetDaclStats() { return &daclStats; }
  131. TSDFileDirCell * GetSaclStats() { return &saclStats; }
  132. virtual void AddOwnerChange(objectType type)
  133. {
  134. switch (type)
  135. {
  136. case file: ownerStats.file++; break;
  137. case directory: ownerStats.dir++; break;
  138. case mailbox: ownerStats.mailbox++; break;
  139. case container: ownerStats.container++; break;
  140. case share: ownerStats.share++; break;
  141. case groupmember: ownerStats.member++; break;
  142. case userright: ownerStats.userright++; break;
  143. case regkey: ownerStats.regkey++; break;
  144. case printer: ownerStats.printer++; break;
  145. default:
  146. break;
  147. };
  148. }
  149. virtual void AddGroupChange(objectType type)
  150. {
  151. switch (type)
  152. {
  153. case file: groupStats.file++; break;
  154. case directory: groupStats.dir++; break;
  155. case mailbox: groupStats.mailbox++; break;
  156. case container: groupStats.container++; break;
  157. case share: groupStats.share++; break;
  158. case groupmember: groupStats.member++; break;
  159. case userright: groupStats.userright++; break;
  160. case regkey: groupStats.regkey++; break;
  161. case printer: groupStats.printer++; break;
  162. default:
  163. break;
  164. };
  165. }
  166. virtual void AddAceChange(objectType type)
  167. {
  168. switch (type)
  169. {
  170. case file: daclStats.file++; break;
  171. case directory: daclStats.dir++; break;
  172. case mailbox: daclStats.mailbox++; break;
  173. case container: daclStats.container++; break;
  174. case share: daclStats.share++; break;
  175. case groupmember: daclStats.member++; break;
  176. case userright: daclStats.userright++; break;
  177. case regkey: daclStats.regkey++; break;
  178. case printer: daclStats.printer++; break;
  179. default:
  180. break;
  181. };
  182. }
  183. virtual void AddSaceChange(objectType type)
  184. {
  185. switch (type)
  186. {
  187. case file: saclStats.file++; break;
  188. case directory: saclStats.dir++; break;
  189. case mailbox: saclStats.mailbox++; break;
  190. case container: saclStats.container++; break;
  191. case share: saclStats.share++; break;
  192. case groupmember: saclStats.member++; break;
  193. case userright: saclStats.userright++; break;
  194. case regkey: saclStats.regkey++; break;
  195. case printer: saclStats.printer++; break;
  196. default:
  197. break;
  198. };
  199. }
  200. };
  201. /**************************************************************************************************/
  202. /* TSidCache: Cache for SIDs.
  203. The cache is filled by calling FillCache(name_of_domain_A, name_of_domain_B)
  204. Lookup, and GetName search the tree for a domain B SID value.
  205. Lookup returns a pointer to the node, while GetName returns the account
  206. name for the node.
  207. GetSidB( tsidnode *) builds and returns the domain B SID for the node (the node contains only the RID)
  208. SizeDiff() returns the answer to "How much bigger are domain B sids than domain A sids?"
  209. this information is needed when allocating space for ACES.
  210. /**************************************************************************************************/
  211. class TAccountCache: public TNodeListSortable
  212. {
  213. IStatusObjPtr m_pStatus;
  214. public:
  215. TAccountCache() { m_cancelled = false; m_bAddIfNotFound = FALSE; }
  216. ~TAccountCache() {}
  217. virtual TAcctNode * Lookup(const PSID psid) = 0; // sid lookup functions
  218. virtual LPWSTR GetName(const PSID psid) = 0;
  219. //virtual BOOL Insert(const LPWSTR acctname,DWORD srcSid, DWORD tgtSid) = 0;
  220. virtual PSID GetTgtSid(const TAcctNode* tnode) = 0;
  221. virtual DWORD SizeDiff(const TAcctNode *tnode) const = 0; // returns max( 0 , (length(to_sid) - length(from_sid)) )
  222. bool IsCancelled()
  223. {
  224. if ( m_pStatus )
  225. {
  226. LONG status = 0;
  227. // HRESULT hr = m_pStatus->get_Status(&status);
  228. m_pStatus->get_Status(&status);
  229. return (status == DCT_STATUS_ABORTING);
  230. }
  231. else
  232. {
  233. return m_cancelled;
  234. }
  235. }
  236. void Cancel() { m_cancelled = true; if ( m_pStatus ) m_pStatus->put_Status(DCT_STATUS_ABORTING); }
  237. void UnCancel() { m_cancelled = false; }
  238. void AddIfNotFound(BOOL val) { m_bAddIfNotFound = val; }
  239. BOOL AddIfNotFound() { return m_bAddIfNotFound; }
  240. void SetStatusObject(IStatusObj * pS) { m_pStatus = pS; }
  241. protected:
  242. bool m_cancelled;
  243. BOOL m_bAddIfNotFound;
  244. };
  245. class TGeneralCache;
  246. class TSDRidCache: public TAccountCache
  247. {
  248. protected:
  249. WCHAR from_domain[MAX_PATH + 1]; // domain names
  250. WCHAR to_domain[MAX_PATH + 1];
  251. WCHAR from_dc[MAX_PATH + 1]; // domain controller (machine) names
  252. WCHAR to_dc[MAX_PATH + 1];
  253. PSID from_sid; // domain sids (dynamically allocated)
  254. PSID to_sid;
  255. UCHAR from_nsubs; // # subauthorities in domain sids
  256. UCHAR to_nsubs;
  257. DWORD accts; // statistical stuff
  258. DWORD accts_resolved;
  259. TGeneralCache * m_otherAccounts;
  260. public:
  261. TSDRidCache();
  262. ~TSDRidCache();
  263. // filling methods
  264. WCHAR const * GetSourceDomainName() { return from_domain; }
  265. WCHAR const * GetTargetDomainName() { return to_domain; }
  266. WCHAR const * GetSourceDCName() { return from_dc; }
  267. WCHAR const * GetTargetDCName() { return to_dc; }
  268. void InsertLast(const LPWSTR acctname,DWORD rida, const LPWSTR newname, DWORD ridb, short type = 0)
  269. { TRidNode * tn = new (acctname,newname) TRidNode(acctname,newname); if (tn){tn->SrcRid(rida); tn->TgtRid(ridb);
  270. tn->Type(type); if ( ridb == 0 ) tn->MarkInvalidOnTgt(); else accts_resolved++; accts++; TNodeListSortable::InsertBottom((TNode *)tn); }}
  271. void InsertLastWithSid(const LPWSTR acctname, const LPWSTR srcdomainsid, const LPWSTR srcdomainname, DWORD rida, const LPWSTR newname,
  272. const LPWSTR tgtdomainsid, const LPWSTR tgtdomainname, DWORD ridb, short type = 0)
  273. { TRidNode * tn = new (acctname,newname) TRidNode(acctname,newname); if (tn){tn->SrcRid(rida); tn->TgtRid(ridb);
  274. tn->SrcDomSid(srcdomainsid); tn->TgtDomSid(tgtdomainsid); tn->SrcDomName(srcdomainname); tn->TgtDomName(tgtdomainname);
  275. tn->Type(type); if ( ridb == 0 ) tn->MarkInvalidOnTgt(); else accts_resolved++; accts++; TNodeListSortable::InsertBottom((TNode *)tn); }}
  276. TAcctNode * Lookup(const PSID psid); // sid lookup functions
  277. TAcctNode * LookupWODomain(const PSID psid); // sid lookup functions
  278. LPWSTR GetName(const PSID psid);
  279. // helper methods
  280. PSID GetTgtSid(TAcctNode const * tnode) ; // "Get" functions
  281. DWORD SizeDiff(const TAcctNode *tnode) const ; // returns max( 0 , (length(to_sid) - length(from_sid)) )
  282. void Display(bool summary, bool detail);
  283. void ReportToVarSet(IVarSet * pVarSet,bool summary, bool detail);
  284. PSID GetTgtSid(const PSID psid) { return GetTgtSid(Lookup(psid)); }
  285. void CopyDomainInfo( TSDRidCache const * other);
  286. PSID GetTgtSidWODomain(TAcctNode const * tnode); // "Get" functions
  287. PSID GetTgtSidWODomain(const PSID psid) { return GetTgtSid(LookupWODomain(psid)); } // "Get" functions
  288. DWORD GetNumAccts() const {return accts; }
  289. DWORD GetNumResolvedAccts() const { return accts_resolved; }
  290. void Clear();
  291. void SetSourceAndTargetDomains(WCHAR const * src, WCHAR const * tgt) { SetDomainInfo(src,true); SetDomainInfo(tgt,false); }
  292. void SetSourceAndTargetDomainsWithSids(WCHAR const * src, WCHAR const * srcSid, WCHAR const * tgt,WCHAR const * tgtSid)
  293. { SetDomainInfoWithSid(src,srcSid,true); SetDomainInfoWithSid(tgt,tgtSid,false); }
  294. void ReportAccountReferences(WCHAR const * filename);
  295. BOOL IsInitialized() { return from_sid!=NULL && to_sid!=NULL; }
  296. protected:
  297. int SetDomainInfo(WCHAR const * domname, bool firstdom);
  298. int SetDomainInfoWithSid(WCHAR const * domainName, WCHAR const * domainSid, bool firstdom);
  299. };
  300. class TGeneralCache : public TAccountCache
  301. {
  302. protected:
  303. DWORD accts; // statistical stuff
  304. DWORD accts_resolved;
  305. public:
  306. TGeneralCache();
  307. ~TGeneralCache();
  308. TAcctNode * Lookup(const PSID psid) ; // sid lookup functions
  309. LPWSTR GetName(const PSID psid) ;
  310. BOOL Insert(const LPWSTR acctname1,const LPWSTR acctname2,PSID sid1, PSID sid2);
  311. PSID GetTgtSid(const TAcctNode* tnode) { return ((TGeneralSidNode *)tnode)->TgtSid(); }
  312. DWORD SizeDiff(const TAcctNode *tnode) const { return ((TGeneralSidNode *)tnode)->SizeDiff(); } // returns max( 0 , (length(to_sid) - length(from_sid)) )
  313. };
  314. // Global Functions
  315. struct SDRDomainInfo
  316. {
  317. bool valid;
  318. PSID domain_sid;
  319. WCHAR domain_name[80];
  320. WCHAR dc_name[80];
  321. UCHAR nsubs;
  322. };
  323. int vRidComp(const TNode * tn, const void * v1);
  324. int vNameComp(const TNode * tn, const void * v1);
  325. int vTargetNameComp(const TNode * tn, const void * v1);
  326. int RidComp(const TNode * n1, const TNode * n2);
  327. int CompN(const TNode * n1, const TNode * n2);
  328. int CompTargetN(const TNode * n1, const TNode * n2);
  329. void DisplaySid(const PSID); // displays the contents of a SID
  330. void DisplaySid(const PSID,TAccountCache *); // displays the acct name if in cache, or
  331. void
  332. SetDomainInfoStruct(
  333. WCHAR const * domname, // in -name of domain
  334. SDRDomainInfo * info // in -struct to put info into
  335. );
  336. void
  337. SetDomainInfoStructFromSid(
  338. PSID pSid, // in -sid for domain
  339. SDRDomainInfo * info // in -struct to put info into
  340. );
  341. PSID DomainizeSid(PSID psid,BOOL freeOldSid);
  342. #endif