private void btnOK_Click(object sender, EventArgs e) { //Need to check with the existing AceMask and then add a //new entry to the Advanced permissions dialog if it varies //Check for the edit mode or add mode. Since in the add mode user should send the null for daclInfo object if (_daclInfo == null) { _daclInfo = new List<LwAccessControlEntry>(); LwAccessControlEntry ace = new LwAccessControlEntry(); ace.AccessMask = "-1"; ace.AceType = 0; _daclInfo.Add(ace); ace = new LwAccessControlEntry(); ace.AccessMask = "-1"; ace.AceType = 1; _daclInfo.Add(ace); } //Need to calculate the access mask for the Allow and deny permission sets. foreach (LwAccessControlEntry ace in _daclInfo) { long iAceMask = Convert.ToInt64(ace.AccessMask); //Validation for the AceType = Allow //Update the the AceType object with modified access modes if (ace.AceType == 0) { foreach (DataGridViewRow dgRow in dgPermissions.Rows) { if (dgRow.Cells[1].Value.ToString().Equals("True")) _securityDescriptor.GetIntAccessMaskFromStringAceMask(dgRow.Cells[0].Value.ToString(), ref iAceMask); } } //Validation for the AceType = Deny if (ace.AceType == 1) { foreach (DataGridViewRow dgRow in dgPermissions.Rows) { if (dgRow.Cells[2].Value.ToString().Equals("True")) _securityDescriptor.GetIntAccessMaskFromStringAceMask(dgRow.Cells[0].Value.ToString(), ref iAceMask); } } //Check for the edit values if (Convert.ToInt32(ace.AccessMask) != Convert.ToInt32(iAceMask)) { ace.AccessMask = iAceMask.ToString(); IsCommit = true; } } this.DialogResult = DialogResult.OK; Close(); }
public static uint ReadSecurityDescriptor( IntPtr pSECURITY_DESCRIPTOR, ref SecurityDescriptor ObjSecurityDescriptor) { Logger.Log(string.Format("SecurityDescriptorWrapper.ReadSecurityDescriptor()"), Logger.SecurityDescriptorLogLevel); Dictionary<string, List<LwAccessControlEntry>> SdDacls = null; IntPtr ptrSid; uint errorReturn = 0; bool bRet = false; ObjSecurityDescriptor = new SecurityDescriptor(); ObjSecurityDescriptor.InitailizeToNull(); SecurityDescriptorApi.SECURITY_DESCRIPTOR sSECURITY_DESCRIPTOR = new SecurityDescriptorApi.SECURITY_DESCRIPTOR(); try { if (pSECURITY_DESCRIPTOR != IntPtr.Zero) { SdDacls = new Dictionary<string, List<LwAccessControlEntry>>(); IntPtr pDaclOffset; bool lpbDaclPresent = false; bool lpbDaclDefaulted = false; bRet = SecurityDescriptorApi.GetSecurityDescriptorDacl(pSECURITY_DESCRIPTOR, out lpbDaclPresent, out pDaclOffset, out lpbDaclDefaulted); Logger.Log("SecurityDescriptorApi.GetSecurityDescriptorDacl iRet value", Logger.SecurityDescriptorLogLevel); SecurityDescriptorApi.ACL_SIZE_INFORMATION AclSize = new SecurityDescriptorApi.ACL_SIZE_INFORMATION(); SecurityDescriptorApi.GetAclInformation(pDaclOffset, AclSize, ((uint)Marshal.SizeOf(typeof(SecurityDescriptorApi.ACL_SIZE_INFORMATION))), SecurityDescriptorApi.ACL_INFORMATION_CLASS.AclSizeInformation); if (pDaclOffset != IntPtr.Zero) { SdDacls = new Dictionary<string, List<LwAccessControlEntry>>(); List<LwAccessControlEntry> daclInfo = new List<LwAccessControlEntry>(); for (int idx = 0; idx < AclSize.AceCount; idx++) { IntPtr pAce; string sUsername, sDomain; int err = SecurityDescriptorApi.GetAce(pDaclOffset, idx, out pAce); SecurityDescriptorApi.ACCESS_ALLOWED_ACE ace = (SecurityDescriptorApi.ACCESS_ALLOWED_ACE)Marshal.PtrToStructure(pAce, typeof(SecurityDescriptorApi.ACCESS_ALLOWED_ACE)); IntPtr iter = (IntPtr)((int)pAce + (int)Marshal.OffsetOf(typeof(SecurityDescriptorApi.ACCESS_ALLOWED_ACE), "SidStart")); string strSID = GetObjectStringSid(iter); //Commented this, to use it in feature //IntPtr pTrustee = IntPtr.Zero; //SecurityDescriptorApi.BuildTrusteeWithSid(out pTrustee, ptrSid); //SecurityDescriptorApi.TRUSTEE trustee = new SecurityDescriptorApi.TRUSTEE(); //Marshal.PtrToStructure(pTrustee, trustee); GetObjectLookUpName(iter, out sUsername, out sDomain); if (String.IsNullOrEmpty(sUsername)) sUsername = strSID; Logger.Log("Trustee = " + sUsername, Logger.SecurityDescriptorLogLevel); Logger.Log(string.Format("SID={0} : AceType={1}/ AceMask={2}/ AceFlags={3}", strSID, ace.Header.AceType.ToString(), ace.Mask.ToString(), ace.Header.AceFlags.ToString()), Logger.SecurityDescriptorLogLevel); LwAccessControlEntry Ace = new LwAccessControlEntry(); Ace.Username = sUsername + "(" + sUsername + "@" + sDomain + ")"; Ace.SID = strSID; Ace.AceType = Convert.ToInt32(ace.Header.AceType); Ace.AccessMask = ace.Mask.ToString(); Ace.AceFlags = Convert.ToInt32(ace.Header.AceFlags.ToString()); Ace.AceSize = Convert.ToInt32(ace.Header.AceSize.ToString()); daclInfo.Add(Ace); } if (daclInfo != null && daclInfo.Count != 0) { List<LwAccessControlEntry> objectDacl = new List<LwAccessControlEntry>(); foreach (LwAccessControlEntry Ace in daclInfo) { if (!SdDacls.ContainsKey(Ace.Username)) { objectDacl = new List<LwAccessControlEntry>(); objectDacl.Add(Ace); SdDacls.Add(Ace.Username, objectDacl); } else { objectDacl = SdDacls[Ace.Username]; objectDacl.Add(Ace); SdDacls[Ace.Username] = objectDacl; } } } ObjSecurityDescriptor.Descretionary_Access_Control_List = SdDacls; } else { ObjSecurityDescriptor.Descretionary_Access_Control_List = null; ObjSecurityDescriptor.IsAccessDenied = true; } sSECURITY_DESCRIPTOR = (SecurityDescriptorApi.SECURITY_DESCRIPTOR)Marshal.PtrToStructure(pSECURITY_DESCRIPTOR, typeof(SecurityDescriptorApi.SECURITY_DESCRIPTOR)); //Get Security Descriptor Control uint dwRevision; SecurityDescriptorApi.SECURITY_DESCRIPTOR_CONTROL pControl; SecurityDescriptorApi.GetSecurityDescriptorControl(pSECURITY_DESCRIPTOR, out pControl, out dwRevision); ObjSecurityDescriptor.Control = (uint)pControl; ObjSecurityDescriptor.Revision = dwRevision; //Get Security Descriptor Owner bool lpbOwnerDefaulted = false; ptrSid = IntPtr.Zero; bRet = SecurityDescriptorApi.GetSecurityDescriptorOwner(pSECURITY_DESCRIPTOR, out ptrSid, out lpbOwnerDefaulted); Logger.Log("SecurityDescriptorApi.GetSecurityDescriptorOwner iRet value: " + Marshal.GetLastWin32Error()); ObjSecurityDescriptor.Owner = GetObjectStringSid(ptrSid); SecurityDescriptorApi.FreeSid(ptrSid); //Get Security Descriptor Group bool lpbGroupDefaulted = false; ptrSid = IntPtr.Zero; bRet = SecurityDescriptorApi.GetSecurityDescriptorGroup(pSECURITY_DESCRIPTOR, out ptrSid, out lpbGroupDefaulted); Logger.Log("SecurityDescriptorApi.GetSecurityDescriptorGroup iRet value: " + Marshal.GetLastWin32Error()); ObjSecurityDescriptor.PrimaryGroupID = GetObjectStringSid(ptrSid); SecurityDescriptorApi.FreeSid(ptrSid); ObjSecurityDescriptor.Size = SecurityDescriptorApi.GetSecurityDescriptorLength(pSECURITY_DESCRIPTOR); ObjSecurityDescriptor.pSecurityDescriptor = pSECURITY_DESCRIPTOR; } } catch (Exception ex) { Logger.LogException("SecurityDescriptorWrapper.ReadSecurityDescriptor()", ex); } return errorReturn; }
private void btnAdd_Click(object sender, EventArgs e) { //AdvancedPermissionsControlDlg advancedPermissionsControlDlg = new AdvancedPermissionsControlDlg(_securityDescriptor, _ObjectPath); //advancedPermissionsControlDlg.ShowDialog(this); //return; string distinguishedName = string.Empty; string domainName = _securityDescriptor.GetDCInfo(null); distinguishedName = System.DirectoryServices.SDSUtils.DomainNameToDN(domainName); // show picker string sLdapPath = string.Format("LDAP://{0}/{1}", domainName, distinguishedName); string sProtocol; string sServer; string sCNs; string sDCs; System.DirectoryServices.SDSUtils.CrackPath(sLdapPath, out sProtocol, out sServer, out sCNs, out sDCs); System.DirectoryServices.Misc.DsPicker dsPickerDlg = new System.DirectoryServices.Misc.DsPicker(); dsPickerDlg.SetData(System.DirectoryServices.Misc.DsPicker.DialogType.SELECT_USERS_OR_GROUPS, sProtocol, sServer, sDCs, true); if (dsPickerDlg.waitForm != null && dsPickerDlg.waitForm.bIsInterrupted) { return; } if (dsPickerDlg.ShowDialog(this) == DialogResult.OK) { if (dsPickerDlg.ADobjectsArray != null && dsPickerDlg.ADobjectsArray.Length != 0) { foreach (System.DirectoryServices.Misc.ADObject ado in dsPickerDlg.ADobjectsArray) { byte[] sObjectSid = ado.de.Properties["objectSid"].Value as byte[]; string sAMAccountName = ado.de.Properties["sAMAccountName"].Value as string; string sSID = _securityDescriptor.ConvetByteSidToStringSid(sObjectSid); //Need to set the permission check list in the permission set LwAccessControlEntry Ace = new LwAccessControlEntry(); Ace.SID = sSID; Ace.Username = sAMAccountName; Ace.AceType = 0; Ace.AceFlags = 0; Ace.AccessMask = Convert.ToInt64(LwAccessMask.ACCESS_MASK.Special_Permissions).ToString(); bool bIsEntryFound = false; List<LwAccessControlEntry> acelist = null; foreach (ListViewItem item in lvGroupOrUserNames.Items) { if (item.Text.Contains(sAMAccountName)) { acelist = item.Tag as List<LwAccessControlEntry>; foreach (LwAccessControlEntry aceEntry in acelist) { if (aceEntry.AceType == 0) { aceEntry.AccessMask = Ace.AccessMask; item.Tag = acelist; item.Selected = true; } } } } if (!bIsEntryFound) { ListViewItem lvItem = new ListViewItem(sAMAccountName); acelist = new List<LwAccessControlEntry>(); acelist.Add(Ace); lvItem.Tag = acelist; //Need to initialize the DaclInfo for the object lvGroupOrUserNames.Items.Add(lvItem); lvGroupOrUserNames.Items[lvGroupOrUserNames.Items.Count - 1].Selected = true; } _addedObjects.Add(sAMAccountName, acelist); } } } }
public static uint ReadSecurityDescriptor( IntPtr pSECURITY_DESCRIPTOR, ref SecurityDescriptor ObjSecurityDescriptor) { Logger.Log(string.Format("SecurityDescriptorWrapper.ReadSecurityDescriptor()"), Logger.SecurityDescriptorLogLevel); Dictionary <string, List <LwAccessControlEntry> > SdDacls = null; IntPtr ptrSid; uint errorReturn = 0; bool bRet = false; ObjSecurityDescriptor = new SecurityDescriptor(); ObjSecurityDescriptor.InitailizeToNull(); SecurityDescriptorApi.SECURITY_DESCRIPTOR sSECURITY_DESCRIPTOR = new SecurityDescriptorApi.SECURITY_DESCRIPTOR(); try { if (pSECURITY_DESCRIPTOR != IntPtr.Zero) { SdDacls = new Dictionary <string, List <LwAccessControlEntry> >(); IntPtr pDaclOffset; bool lpbDaclPresent = false; bool lpbDaclDefaulted = false; bRet = SecurityDescriptorApi.GetSecurityDescriptorDacl(pSECURITY_DESCRIPTOR, out lpbDaclPresent, out pDaclOffset, out lpbDaclDefaulted); Logger.Log("SecurityDescriptorApi.GetSecurityDescriptorDacl iRet value", Logger.SecurityDescriptorLogLevel); SecurityDescriptorApi.ACL_SIZE_INFORMATION AclSize = new SecurityDescriptorApi.ACL_SIZE_INFORMATION(); SecurityDescriptorApi.GetAclInformation(pDaclOffset, AclSize, ((uint)Marshal.SizeOf(typeof(SecurityDescriptorApi.ACL_SIZE_INFORMATION))), SecurityDescriptorApi.ACL_INFORMATION_CLASS.AclSizeInformation); if (pDaclOffset != IntPtr.Zero) { SdDacls = new Dictionary <string, List <LwAccessControlEntry> >(); List <LwAccessControlEntry> daclInfo = new List <LwAccessControlEntry>(); for (int idx = 0; idx < AclSize.AceCount; idx++) { IntPtr pAce; string sUsername, sDomain; int err = SecurityDescriptorApi.GetAce(pDaclOffset, idx, out pAce); SecurityDescriptorApi.ACCESS_ALLOWED_ACE ace = (SecurityDescriptorApi.ACCESS_ALLOWED_ACE)Marshal.PtrToStructure(pAce, typeof(SecurityDescriptorApi.ACCESS_ALLOWED_ACE)); IntPtr iter = (IntPtr)((int)pAce + (int)Marshal.OffsetOf(typeof(SecurityDescriptorApi.ACCESS_ALLOWED_ACE), "SidStart")); string strSID = GetObjectStringSid(iter); //Commented this, to use it in feature //IntPtr pTrustee = IntPtr.Zero; //SecurityDescriptorApi.BuildTrusteeWithSid(out pTrustee, ptrSid); //SecurityDescriptorApi.TRUSTEE trustee = new SecurityDescriptorApi.TRUSTEE(); //Marshal.PtrToStructure(pTrustee, trustee); GetObjectLookUpName(iter, out sUsername, out sDomain); if (String.IsNullOrEmpty(sUsername)) { sUsername = strSID; } Logger.Log("Trustee = " + sUsername, Logger.SecurityDescriptorLogLevel); Logger.Log(string.Format("SID={0} : AceType={1}/ AceMask={2}/ AceFlags={3}", strSID, ace.Header.AceType.ToString(), ace.Mask.ToString(), ace.Header.AceFlags.ToString()), Logger.SecurityDescriptorLogLevel); LwAccessControlEntry Ace = new LwAccessControlEntry(); Ace.Username = sUsername + "(" + sUsername + "@" + sDomain + ")"; Ace.SID = strSID; Ace.AceType = Convert.ToInt32(ace.Header.AceType); Ace.AccessMask = ace.Mask.ToString(); Ace.AceFlags = Convert.ToInt32(ace.Header.AceFlags.ToString()); Ace.AceSize = Convert.ToInt32(ace.Header.AceSize.ToString()); daclInfo.Add(Ace); } if (daclInfo != null && daclInfo.Count != 0) { List <LwAccessControlEntry> objectDacl = new List <LwAccessControlEntry>(); foreach (LwAccessControlEntry Ace in daclInfo) { if (!SdDacls.ContainsKey(Ace.Username)) { objectDacl = new List <LwAccessControlEntry>(); objectDacl.Add(Ace); SdDacls.Add(Ace.Username, objectDacl); } else { objectDacl = SdDacls[Ace.Username]; objectDacl.Add(Ace); SdDacls[Ace.Username] = objectDacl; } } } ObjSecurityDescriptor.Descretionary_Access_Control_List = SdDacls; } else { ObjSecurityDescriptor.Descretionary_Access_Control_List = null; ObjSecurityDescriptor.IsAccessDenied = true; } sSECURITY_DESCRIPTOR = (SecurityDescriptorApi.SECURITY_DESCRIPTOR)Marshal.PtrToStructure(pSECURITY_DESCRIPTOR, typeof(SecurityDescriptorApi.SECURITY_DESCRIPTOR)); //Get Security Descriptor Control uint dwRevision; SecurityDescriptorApi.SECURITY_DESCRIPTOR_CONTROL pControl; SecurityDescriptorApi.GetSecurityDescriptorControl(pSECURITY_DESCRIPTOR, out pControl, out dwRevision); ObjSecurityDescriptor.Control = (uint)pControl; ObjSecurityDescriptor.Revision = dwRevision; //Get Security Descriptor Owner bool lpbOwnerDefaulted = false; ptrSid = IntPtr.Zero; bRet = SecurityDescriptorApi.GetSecurityDescriptorOwner(pSECURITY_DESCRIPTOR, out ptrSid, out lpbOwnerDefaulted); Logger.Log("SecurityDescriptorApi.GetSecurityDescriptorOwner iRet value: " + Marshal.GetLastWin32Error()); ObjSecurityDescriptor.Owner = GetObjectStringSid(ptrSid); SecurityDescriptorApi.FreeSid(ptrSid); //Get Security Descriptor Group bool lpbGroupDefaulted = false; ptrSid = IntPtr.Zero; bRet = SecurityDescriptorApi.GetSecurityDescriptorGroup(pSECURITY_DESCRIPTOR, out ptrSid, out lpbGroupDefaulted); Logger.Log("SecurityDescriptorApi.GetSecurityDescriptorGroup iRet value: " + Marshal.GetLastWin32Error()); ObjSecurityDescriptor.PrimaryGroupID = GetObjectStringSid(ptrSid); SecurityDescriptorApi.FreeSid(ptrSid); ObjSecurityDescriptor.Size = SecurityDescriptorApi.GetSecurityDescriptorLength(pSECURITY_DESCRIPTOR); ObjSecurityDescriptor.pSecurityDescriptor = pSECURITY_DESCRIPTOR; } } catch (Exception ex) { Logger.LogException("SecurityDescriptorWrapper.ReadSecurityDescriptor()", ex); } return(errorReturn); }