static int lccFModifyGroup(String lccParamSServer, String lccParamSStartOU, lccRequestClass lccParamRequest) { // lccIReturnVal // 0 - aborted // 1 - success // 2 - search OU issue, do not retry // 3 - user object issue, do not retry int lccIReturnVal = 0; bool lccBAbortFunction = false; bool lccBGroupMemberFound = false; int lccMaxRetries = lccSCSettings.lccILDAPMaxRetries; int lccRetries = 0; int lccIGroupsLoop = 0; int lccIGroupMembersLoop = 0; int lccIUsersLoop = 0; int lccIMaintainUsersLoop = 0; string lccSStartOU = ""; lccLDAPObjectClass lccLDAPGroupObject = new lccLDAPObjectClass(); lccLDAPObjectClass lccLDAPUserObject = new lccLDAPObjectClass(); List <string> lccALGroupMembersDN = new List <string>(); List <string> lccALUsersDN = new List <string>(); DirectoryEntry lccDEAddToGroup = null; try { for (lccIGroupsLoop = 0; lccIGroupsLoop < lccParamRequest.lccALGroups.Count; lccIGroupsLoop++) { lccFLogInfo(0, "[lccFModifyGroup] Started Group [" + lccParamRequest.lccALGroups[lccIGroupsLoop] + "]"); lccRetries = 0; lccALUsersDN.Clear(); do { lccRetries++; try { lccFLogInfo(0, "[lccFModifyGroup] LDAP Server [" + lccParamSServer + "]"); lccSStartOU = "LDAP://" + lccParamSServer + "/" + lccParamSStartOU; lccFLogInfo(0, "[lccFModifyGroup] LDAP Root [" + lccSStartOU + "]"); if (lccBAbortFunction == false) { try { if (DirectoryEntry.Exists(lccSStartOU) == false) { lccBAbortFunction = true; lccIReturnVal = 2; lccFLogInfo(0, "[lccFModifyGroup] lcc:startOU does not exist: " + lccSStartOU); } } catch (Exception lccExceptionDirectoryEntryExists) { lccBAbortFunction = true; lccIReturnVal = 2; lccFLogInfo(0, "[lccFModifyGroup] OU Check Exists Error. Incorrect lcc:startOU value? ERROR: " + lccExceptionDirectoryEntryExists.Message); } } if (lccBAbortFunction == false) { lccLDAPGroupObject = lccFFindLDAPobject(lccSStartOU, lccParamRequest.lccALGroups[lccIGroupsLoop], "Group"); if (lccLDAPGroupObject.lccSDN.Length == 0) { lccBAbortFunction = true; lccIReturnVal = 2; } } if (lccBAbortFunction == false) { lccFLogInfo(0, "[lccFModifyGroup] lcc:startOU exist: " + lccSStartOU); for (lccIUsersLoop = 0; lccIUsersLoop < lccParamRequest.lccALUsers.Count; lccIUsersLoop++) { lccLDAPUserObject.lccFClearValues(); lccFLogInfo(0, "[lccFModifyGroup] Search for user [" + lccParamRequest.lccALUsers[lccIUsersLoop] + "]"); try { lccLDAPUserObject = lccFFindLDAPobject(lccSStartOU, lccParamRequest.lccALUsers[lccIUsersLoop], "User"); if (lccLDAPUserObject != null) { if (lccLDAPUserObject.lccSDN.Length > 0) { lccALUsersDN.Add(lccLDAPUserObject.lccSDN); } } } catch (Exception lccExceptionSearcher) { lccIReturnVal = 3; lccFLogInfo(0, "[lccFModifyGroup] Searcher ERROR: " + lccExceptionSearcher.Message); } } } if (lccBAbortFunction == false) { if (lccALUsersDN.Count == 0) { lccBAbortFunction = true; lccIReturnVal = 3; lccFLogInfo(0, "[lccFModifyGroup] No User Object DNs available. Cannot process."); } } if (lccBAbortFunction == false) { lccFLogInfo(0, "[lccFModifyGroup] " + lccParamRequest.lccSType + " User to Group [Attempt: " + lccRetries.ToString() + " out of " + lccMaxRetries.ToString() + "] [" + lccLDAPGroupObject.lccSDN + "]"); if (DirectoryEntry.Exists("LDAP://" + lccLDAPGroupObject.lccSDN) == false) { lccFLogInfo(0, "[lccADTasksPerform] lcc:searchOU does not exist: " + "LDAP://" + lccLDAPGroupObject.lccSDN); } else { lccDEAddToGroup = new DirectoryEntry("LDAP://" + lccLDAPGroupObject.lccSDN); for (lccIUsersLoop = 0; lccIUsersLoop < lccALUsersDN.Count; lccIUsersLoop++) { if (lccParamRequest.lccSType.Equals("Add") == true) { lccFLogInfo(0, "[lccADTasksPerform] " + lccParamRequest.lccSType + " User [" + lccALUsersDN[lccIUsersLoop] + "]"); lccDEAddToGroup.Properties["member"].Add(lccALUsersDN[lccIUsersLoop]); } else if (lccParamRequest.lccSType.Equals("Remove") == true) { lccFLogInfo(0, "[lccADTasksPerform] " + lccParamRequest.lccSType + " User [" + lccALUsersDN[lccIUsersLoop] + "]"); lccDEAddToGroup.Properties["member"].Remove(lccALUsersDN[lccIUsersLoop]); } else if (lccParamRequest.lccSType.Equals("Maintain") == true) { lccBGroupMemberFound = false; for (lccIGroupMembersLoop = 0; lccIGroupMembersLoop < lccLDAPGroupObject.lccALMembers.Count && lccBGroupMemberFound == false; lccIGroupMembersLoop++) { lccBGroupMemberFound = lccLDAPGroupObject.lccALMembers[lccIGroupMembersLoop].Equals(lccALUsersDN[lccIUsersLoop]); } if (lccBGroupMemberFound == false) { lccFLogInfo(0, "[lccADTasksPerform] (Maintain) Add User [" + lccALUsersDN[lccIUsersLoop] + "]"); lccDEAddToGroup.Properties["member"].Add(lccALUsersDN[lccIUsersLoop]); } else { lccFLogInfo(0, "[lccADTasksPerform] (Maintain) User Already Member [" + lccALUsersDN[lccIUsersLoop] + "]"); } } } for (lccIGroupMembersLoop = 0; lccIGroupMembersLoop < lccLDAPGroupObject.lccALMembers.Count; lccIGroupMembersLoop++) { lccBGroupMemberFound = false; for (lccIMaintainUsersLoop = 0; lccIMaintainUsersLoop < lccALUsersDN.Count && lccBGroupMemberFound == false; lccIMaintainUsersLoop++) { lccBGroupMemberFound = lccALUsersDN[lccIMaintainUsersLoop].Equals(lccLDAPGroupObject.lccALMembers[lccIGroupMembersLoop]); } if (lccBGroupMemberFound == false) { lccFLogInfo(0, "[lccADTasksPerform] (Maintain) Remove User [" + lccLDAPGroupObject.lccALMembers[lccIGroupMembersLoop] + "]"); lccDEAddToGroup.Properties["member"].Remove(lccLDAPGroupObject.lccALMembers[lccIGroupMembersLoop]); } } lccDEAddToGroup.CommitChanges(); lccDEAddToGroup.Close(); lccIReturnVal = 1; } lccFLogInfo(0, "[lccFModifyGroup] Finished"); } } catch (Exception exception1) { lccFLogInfo(0, "[lccFModifyGroup] Failed on retry " + lccRetries.ToString() + " out of " + lccMaxRetries.ToString() + ". ERROR: " + exception1.Message); lccPauseProcess(2); } if (lccIReturnVal == 0) { lccPauseProcess(1); } } while (lccIReturnVal == 0 && lccRetries < lccMaxRetries); if (lccIReturnVal == 0 && lccRetries == lccMaxRetries ) { lccFLogInfo(0, "[lccFModifyGroup] Failed with the maxium retries."); } } if (lccSCSettings.lccBDebugMode == true) { lccFLogInfo(0, "[lccFModifyGroup] Done"); } } catch (Exception lccException) { lccFLogInfo(0, "[lccFModifyGroup] ERROR: " + lccException.Message); } return(lccIReturnVal); }
static lccLDAPObjectClass lccFFindLDAPobject(string lccParamSSearchOU, string lccParamSId, string lccParamSObjectType) { lccLDAPObjectClass lccReturn = new lccLDAPObjectClass(); DirectoryEntry lccDESearchOU = null; DirectorySearcher lccDSSearcher = null; SearchResultCollection lccSRCCollection = null; ResultPropertyCollection lccRPCCollection = null; try { lccDESearchOU = new DirectoryEntry(lccParamSSearchOU); lccDSSearcher = null; lccDSSearcher = new DirectorySearcher(lccDESearchOU); lccDSSearcher.PageSize = 1; lccDSSearcher.SizeLimit = 1; //lccDSSearcher.PropertiesToLoad.Add("sAMAccountName"); lccDSSearcher.PropertiesToLoad.Add("cn"); if (lccParamSObjectType.Equals("Group") == true) { lccDSSearcher.PropertiesToLoad.Add("member"); } lccDSSearcher.PropertiesToLoad.Add("sn"); lccDSSearcher.PropertiesToLoad.Add("distinguishedname"); lccDSSearcher.SearchScope = SearchScope.Subtree; lccDSSearcher.Filter = "(samAccountName=" + lccParamSId + ")"; if (lccDSSearcher.FindOne() != null) { lccSRCCollection = lccDSSearcher.FindAll(); if (lccSRCCollection.Count == 0) { lccReturn.lccIReturnVal = 3; lccFLogInfo(0, "[lccFFindLDAPobject] No objects returned from Active Directory for [" + lccParamSId + "]"); } else { try { foreach (SearchResult aSearchResult in lccSRCCollection) { if (lccSCSettings.lccBDebugMode == true) { lccFLogInfo(0, "[lccFFindLDAPobject] Object Path: " + aSearchResult.Path); } lccRPCCollection = aSearchResult.Properties; if (lccRPCCollection != null) { foreach (String aPropertyName in lccRPCCollection.PropertyNames) { if (lccParamSObjectType.Equals("Group") == true) { if (aPropertyName.CompareTo("member") == 0) { foreach (object aCollection in lccRPCCollection[aPropertyName]) { lccFLogInfo(0, "[lccFModifyGroup] Found Member [" + aCollection.ToString() + "]"); lccReturn.lccALMembers.Add(aCollection.ToString()); } } } /* * if (aPropertyName.CompareTo("cn") == 0) * { * foreach (object aCollection in lccRPCCollection[aPropertyName]) * { * lccFLogInfo(0, "[lccFModifyGroup] Found Object CN [" + aCollection.ToString() + "]"); * } * } */ if (aPropertyName.CompareTo("distinguishedname") == 0) { foreach (object aCollection in lccRPCCollection[aPropertyName]) { if (lccSCSettings.lccBDebugMode == true) { lccFLogInfo(0, "[lccFFindLDAPobject] Found Object DN [" + aCollection.ToString() + "]"); } lccReturn.lccSDN = aCollection.ToString(); } } } } } } catch (Exception lccExceptionCollectionSearchResults) { lccReturn.lccIReturnVal = 3; lccFLogInfo(0, "[lccFFindLDAPobject] Object attributes failed to read. ERROR: " + lccExceptionCollectionSearchResults.Message); } } } else { lccReturn.lccIReturnVal = 3; lccFLogInfo(0, "[lccFFindLDAPobject] No object found for [" + lccParamSId + "]"); } } catch (Exception lccException) { lccFLogInfo(0, "[lccFFindLDAPobject] ERROR: " + lccException.Message); } return(lccReturn); }