private void btnConnect_Click(object sender, EventArgs e) { targ.Dispose(); //Dispose of any prior data **BROKEN, FIX user.AdminRightsExist = false; //**Remove once Dispose is fixed. if (PreConnectCheck(txtHostIP.Text.Trim())) { try { adTasksGrant = new AdTasks(targ); //Before we can connect and fetch host info adTasksGrant.CheckLocale(); //we need to check it's Locale (english / french) remoteTasksGrant = new RemoteTasks(targ); remoteTasksGrant.GetHostInfo(); } catch (Exception connectError) { Util.MsgBox.Info(connectError.ToString()); WriteToConsole("::Failed to connect to host::"); eventLogs.LogError(connectError); ClearForm(false); return; } if (string.IsNullOrWhiteSpace(txtUserID.Text)) //populate txtUserID & update UserID { txtUserID.Text = user.CurrentUser; user.UserID = user.CurrentUser; } StatusReport(0); ToggleButtonsAndHostIP(false, false); //Online with host, disable Remove button & txtHostIP } else { ClearForm(false); //Set initial form load state } }
/// <summary> /// CheckExistingRights() - is used to query the host machine for user's in the Administrators group /// Uses WMI if available / fails over to LDAP query /// </summary> /// <param name="addToCombo">Add administrators to comboAdminList on the Main form</param> /// <param name="devOutput">Dev/testing output</param> #region CheckExistingRights() public void CheckExistingRights(bool addToCombo = false, bool devOutput = false) { string adminUsers = string.Empty; string currAdmin = string.Empty; Cursor.Current = Cursors.WaitCursor; try { if (connection.IsWmiActive) { //Get info via WMI Query string queryString = @"GroupUser where GroupComponent = ""Win32_Group.Domain='" + connection.HostName + @"',Name='" + connection.PathAdminLang + @"'"""; ManagementObjectCollection wmiCollection = Util.Tools.WmiQuery(queryString, connection.HostIP); foreach (ManagementObject m in wmiCollection) { const int trimEnd = 3; //Remove 3 char's from end of query results const string strToFind = @",NAME=\"""; //Precursor to UserID //Extract UserID from query currAdmin = Util.Tools.ExtractSubstring(m.ToString().ToUpper(), strToFind, trimEnd); //Compile list of admin's found on host for Dev output to console adminUsers = string.Concat(adminUsers, currAdmin, Environment.NewLine); if (addToCombo) { if (!CheckSuperUser(currAdmin)) //Do not add Super User's for right's removal { targ.mainForm.UpdateCombo(currAdmin); //Add UserID to comboAdminList on Main form. } } if (currAdmin.Contains(user.UserID)) //UserID found in Adminstrators group { user.AdminRightsExist = true; } //Can add a return, but we want to skim the entire admin group } } else { //Get data using DicectoryEntry/LDAP using (DirectoryEntry groupEntry = new DirectoryEntry(connection.HostLocale)) { foreach (object member in (IEnumerable)groupEntry.Invoke("Members")) { using (DirectoryEntry memberEntry = new DirectoryEntry(member)) { const string strToFind = "/"; //last char in string before UserID string adminPath = memberEntry.Path.ToUpper(); //Extract UserID from LDAP Query. currAdmin = Util.Tools.ExtractSubstring(adminPath, strToFind); adminUsers = string.Concat(adminUsers, currAdmin, Environment.NewLine); //Add each result to Main.comboAdminList //Omit Domain Administrators if (addToCombo) { if (!CheckSuperUser(currAdmin)) { targ.mainForm.UpdateCombo(currAdmin); } } //Specified user exists in "Administrator" group //flag rights exist == true. if (currAdmin.Contains(user.UserID)) { user.AdminRightsExist = true; } } } } } //DEV / TEST if (devOutput) { WriteAdminList(adminUsers); } Cursor.Current = Cursors.Default; } catch (Exception e) { Util.MsgBox.Info(e.ToString()); targ.mainForm.WriteToConsole("::Failed to check for existing rights::"); EventLogs.LogError(e); ExceptionThrown = true; } }