void ReportingThread(object StartupListO) { try { using (SQLLib sql = SQLTest.ConnectSQL("Fox SDC Server for Startup Data")) { if (sql == null) { FoxEventLog.WriteEventLog("Cannot connect to SQL Server for Startup Reporting!", System.Diagnostics.EventLogEntryType.Error); return; } StartupLst StartupList = (StartupLst)StartupListO; ComputerData computerdata = Computers.GetComputerDetail(sql, StartupList.MachineID); if (computerdata == null) { FoxEventLog.WriteEventLog("Cannot get any computer data for Startup Reporting!", System.Diagnostics.EventLogEntryType.Error); return; } List <PolicyObject> Pol = Policies.GetPolicyForComputerInternal(sql, StartupList.MachineID); Dictionary <string, Int64> AlreadyReported = new Dictionary <string, long>(); foreach (PolicyObject PolO in Pol) { if (PolO.Type != PolicyIDs.ReportingPolicy) { continue; } ReportingPolicyElement RepElementRoot = JsonConvert.DeserializeObject <ReportingPolicyElement>(Policies.GetPolicy(sql, PolO.ID).Data); if (RepElementRoot.Type != ReportingPolicyType.Startup) { continue; } if (RepElementRoot.ReportToAdmin == null) { RepElementRoot.ReportToAdmin = false; } if (RepElementRoot.ReportToClient == null) { RepElementRoot.ReportToClient = false; } if (RepElementRoot.UrgentForAdmin == null) { RepElementRoot.UrgentForAdmin = false; } if (RepElementRoot.UrgentForClient == null) { RepElementRoot.UrgentForClient = false; } if (RepElementRoot.ReportToAdmin == false && RepElementRoot.ReportToClient == false && RepElementRoot.UrgentForAdmin == false && RepElementRoot.UrgentForClient == false) { continue; } foreach (string Element in RepElementRoot.ReportingElements) { ReportingPolicyElementStartup arprep = JsonConvert.DeserializeObject <ReportingPolicyElementStartup>(Element); if (arprep.NotifyOnAdd == false && arprep.NotifyOnRemove == false && arprep.NotifyOnUpdate == false) { continue; } if (arprep.NotifyOnAdd == true) { foreach (StartupItem ar in GetFilteredData(StartupList.Added, computerdata, arprep)) { ReportThings(sql, StartupList.MachineID, "Add", ar, ref AlreadyReported, RepElementRoot); } } if (arprep.NotifyOnUpdate == true) { foreach (StartupItem ar in GetFilteredData(StartupList.Updated, computerdata, arprep)) { ReportThings(sql, StartupList.MachineID, "Update", ar, ref AlreadyReported, RepElementRoot); } } if (arprep.NotifyOnRemove == true) { foreach (StartupItem ar in GetFilteredData(StartupList.Removed, computerdata, arprep)) { ReportThings(sql, StartupList.MachineID, "Remove", ar, ref AlreadyReported, RepElementRoot); } } } } } } catch (Exception ee) { FoxEventLog.WriteEventLog("SEH in Startup Reporting " + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); } }
public RESTStatus ReportStatups(SQLLib sql, ListStartupItems StartupList, NetworkConnectionInfo ni) { if (ni.HasAcl(ACLFlags.ComputerLogin) == false) { ni.Error = "Access denied"; ni.ErrorID = ErrorFlags.AccessDenied; return(RESTStatus.Denied); } StartupList.MachineID = ni.Username; lock (ni.sqllock) { if (Convert.ToInt32(sql.ExecSQLScalar("SELECT COUNT(*) FROM ComputerAccounts WHERE MachineID=@m", new SQLParam("@m", StartupList.MachineID))) == 0) { ni.Error = "Invalid MachineID"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Denied); } } if (StartupList.Items == null) { ni.Error = "Invalid Items"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } if (StartupList.SIDUsers == null) { StartupList.SIDUsers = new List <string>(); } foreach (StartupItem rep in StartupList.Items) { if (string.IsNullOrWhiteSpace(rep.Location) == true) { ni.Error = "Invalid Items"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } if (string.IsNullOrWhiteSpace(rep.Key) == true) { ni.Error = "Invalid Items"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } if (string.IsNullOrWhiteSpace(rep.HKCUUser) == true) { rep.HKCUUser = ""; } if (string.IsNullOrWhiteSpace(rep.Item) == true) { rep.Item = ""; } } List <StartupItem> Installed = new List <StartupItem>(); List <StartupItem> Reported = StartupList.Items; lock (ni.sqllock) { SqlDataReader dr = sql.ExecSQLReader("SELECT * FROM Startups WHERE MachineID=@id", new SQLParam("@id", StartupList.MachineID)); while (dr.Read()) { StartupItem ar = new StartupItem(); ar.Location = Convert.ToString(dr["Location"]); ar.Key = Convert.ToString(dr["Key"]); ar.Item = Convert.ToString(dr["Item"]); ar.HKCUUser = Convert.ToString(dr["HKCUUser"]); if (string.IsNullOrWhiteSpace(ar.HKCUUser) == true) { ar.HKCUUser = ""; } Installed.Add(ar); } dr.Close(); } List <StartupItem> Updated = new List <StartupItem>(); List <StartupItem> Unchanged = new List <StartupItem>(); List <StartupItem> Removed = new List <StartupItem>(); List <StartupItem> Added = new List <StartupItem>(); foreach (StartupItem inst in Installed) { inst.Location = inst.Location.Trim(); inst.Key = inst.Key.Trim(); inst.Item = inst.Item.Trim(); inst.HKCUUser = inst.HKCUUser.Trim(); bool Found = false; foreach (StartupItem rep in Reported) { rep.Location = rep.Location.Trim(); rep.Key = rep.Key.Trim(); rep.Item = rep.Item.Trim(); rep.HKCUUser = rep.HKCUUser.Trim(); if (rep.Location.ToLower() == inst.Location.ToLower() && rep.Key == inst.Key && rep.HKCUUser == inst.HKCUUser) { if (rep.Item.ToLower() == inst.Item.ToLower()) { Unchanged.Add(rep); Found = true; break; } else { Updated.Add(rep); Found = true; break; } } } if (Found == false) { if (inst.HKCUUser != "" && StartupList.SIDUsers.Contains(inst.HKCUUser, StringComparer.InvariantCultureIgnoreCase) == false) { //likely that this user is not logged on or such Unchanged.Add(inst); } else { Removed.Add(inst); } } } foreach (StartupItem inst in Reported) { bool Found = false; foreach (StartupItem rep in Installed) { if (rep.Key.ToLower() == inst.Key.ToLower() && rep.Location == inst.Location && rep.HKCUUser == inst.HKCUUser) { Found = true; break; } } if (Found == false) { Added.Add(inst); } } lock (ni.sqllock) { try { sql.BeginTransaction(); sql.SEHError = true; foreach (StartupItem ar in Removed) { sql.ExecSQL("DELETE FROM Startups WHERE MachineID=@id AND [Key]=@key AND Location=@location AND HKCUUser=@user", new SQLParam("@id", StartupList.MachineID), new SQLParam("@key", ar.Key), new SQLParam("@user", ar.HKCUUser), new SQLParam("@location", ar.Location)); } foreach (StartupItem ar in Updated) { sql.ExecSQL(@"UPDATE Startups SET Item=@item, DT=@DT WHERE MachineID=@id AND [Key]=@key AND Location=@location AND HKCUUser=@user", new SQLParam("@id", StartupList.MachineID), new SQLParam("@key", ar.Key), new SQLParam("@user", ar.HKCUUser), new SQLParam("@location", ar.Location), new SQLParam("@item", ar.Item), new SQLParam("@DT", DateTime.UtcNow)); } foreach (StartupItem ar in Added) { sql.InsertMultiData("Startups", new SQLData("MachineID", StartupList.MachineID), new SQLData("Item", ar.Item), new SQLData("Location", ar.Location), new SQLData("Key", ar.Key), new SQLData("HKCUUser", ar.HKCUUser), new SQLData("DT", DateTime.UtcNow)); } sql.CommitTransaction(); } catch (Exception ee) { sql.RollBackTransaction(); FoxEventLog.WriteEventLog("DB Error: Cannot update Startups: " + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); return(RESTStatus.ServerError); } finally { sql.SEHError = false; } } StartupLst l = new StartupLst(); l.Added = Added; l.Removed = Removed; l.Unchanged = Unchanged; l.Updated = Updated; l.MachineID = StartupList.MachineID; Thread t = new Thread(new ParameterizedThreadStart(new DReportingThread(ReportingThread))); t.Start(l); return(RESTStatus.Created); }