void ReportingThread(object EventDataListO)
        {
            try
            {
                using (SQLLib sql = SQLTest.ConnectSQL("Fox SDC Server for EventLog Data"))
                {
                    if (sql == null)
                    {
                        FoxEventLog.WriteEventLog("Cannot connect to SQL Server for Event Log Data Reporting!", System.Diagnostics.EventLogEntryType.Error);
                        return;
                    }
                    List <EventLogReportFull> EventDataList = (List <EventLogReportFull>)EventDataListO;
                    if (EventDataList.Count == 0)
                    {
                        return;
                    }
                    List <PolicyObject>        Pol             = Policies.GetPolicyForComputerInternal(sql, EventDataList[0].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.EventLog)
                        {
                            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)
                        {
                            ReportingPolicyElementEventLog evrep = JsonConvert.DeserializeObject <ReportingPolicyElementEventLog>(Element);
                            if (evrep.Book == null)
                            {
                                evrep.Book = new List <string>();
                            }
                            if (evrep.CategoryNumbers == null)
                            {
                                evrep.CategoryNumbers = new List <int>();
                            }
                            if (evrep.EventLogTypes == null)
                            {
                                evrep.EventLogTypes = new List <int>();
                            }
                            if (evrep.Sources == null)
                            {
                                evrep.Sources = new List <string>();
                            }
                            if (evrep.Book.Count == 0 && evrep.CategoryNumbers.Count == 0 && evrep.EventLogTypes.Count == 0 && evrep.Sources.Count == 0)
                            {
                                continue;
                            }
                            foreach (EventLogReportFull EV in EventDataList)
                            {
                                if (evrep.Book.Count != 0)
                                {
                                    bool Match = false;
                                    foreach (string Book in evrep.Book)
                                    {
                                        if (Book.ToLower() == EV.EventLog.ToLower())
                                        {
                                            Match = true;
                                            break;
                                        }
                                    }
                                    if (Match == false)
                                    {
                                        continue;
                                    }
                                }
                                if (evrep.Sources.Count != 0)
                                {
                                    bool Match = false;
                                    foreach (string Source in evrep.Sources)
                                    {
                                        if (Source.ToLower() == EV.Source.ToLower())
                                        {
                                            Match = true;
                                            break;
                                        }
                                    }
                                    if (Match == false)
                                    {
                                        continue;
                                    }
                                }
                                if (evrep.EventLogTypes.Count != 0)
                                {
                                    bool Match = false;
                                    foreach (int EVLType in evrep.EventLogTypes)
                                    {
                                        if (EVLType == EV.EventLogType)
                                        {
                                            Match = true;
                                            break;
                                        }
                                    }
                                    if (Match == false)
                                    {
                                        continue;
                                    }
                                }
                                if (evrep.CategoryNumbers.Count != 0)
                                {
                                    bool Match = false;
                                    foreach (int Cat in evrep.CategoryNumbers)
                                    {
                                        if (Cat == (EV.InstanceID & 0x3FFFFFFF))
                                        {
                                            Match = true;
                                            break;
                                        }
                                    }
                                    if (Match == false)
                                    {
                                        continue;
                                    }
                                }

                                if (evrep.IncludeExclude == 1) //include
                                {
                                    if (evrep.IncludeExcludeTexts != null)
                                    {
                                        bool Match = false;
                                        foreach (string s in evrep.IncludeExcludeTexts)
                                        {
                                            if (EV.Message.ToLower().Contains(s.ToLower()) == true)
                                            {
                                                Match = true;
                                                break;
                                            }
                                        }
                                        if (Match == false)
                                        {
                                            continue;
                                        }
                                    }
                                }

                                if (evrep.IncludeExclude == 2) //exclude
                                {
                                    if (evrep.IncludeExcludeTexts != null)
                                    {
                                        bool Match = true;
                                        foreach (string s in evrep.IncludeExcludeTexts)
                                        {
                                            if (EV.Message.ToLower().Contains(s.ToLower()) == true)
                                            {
                                                Match = false;
                                                break;
                                            }
                                        }
                                        if (Match == false)
                                        {
                                            continue;
                                        }
                                    }
                                }

                                bool ReportToAdmin   = RepElementRoot.ReportToAdmin.Value;
                                bool ReportToClient  = RepElementRoot.ReportToClient.Value;
                                bool UrgentForAdmin  = RepElementRoot.UrgentForAdmin.Value;
                                bool UrgentForClient = RepElementRoot.UrgentForClient.Value;

                                if (AlreadyReported.ContainsKey(EV.LogID) == true)
                                {
                                    if ((AlreadyReported[EV.LogID] & (Int64)ReportingFlags.ReportToAdmin) != 0)
                                    {
                                        ReportToAdmin = false;
                                    }
                                    if ((AlreadyReported[EV.LogID] & (Int64)ReportingFlags.ReportToClient) != 0)
                                    {
                                        ReportToClient = false;
                                    }
                                    if ((AlreadyReported[EV.LogID] & (Int64)ReportingFlags.UrgentForAdmin) != 0)
                                    {
                                        UrgentForAdmin = false;
                                    }
                                    if ((AlreadyReported[EV.LogID] & (Int64)ReportingFlags.UrgentForClient) != 0)
                                    {
                                        UrgentForClient = false;
                                    }
                                }

                                if (ReportToAdmin == false && ReportToClient == false && UrgentForAdmin == false && UrgentForClient == false)
                                {
                                    continue;
                                }
                                ReportingFlags Flags = (ReportToAdmin == true ? ReportingFlags.ReportToAdmin : 0) |
                                                       (ReportToClient == true ? ReportingFlags.ReportToClient : 0) |
                                                       (UrgentForAdmin == true ? ReportingFlags.UrgentForAdmin : 0) |
                                                       (UrgentForClient == true ? ReportingFlags.UrgentForClient : 0);
                                switch ((EventLogEntryType)EV.EventLogType)
                                {
                                case 0:
                                case EventLogEntryType.Information:
                                    Flags = (ReportingFlags)((Int64)Flags | ((Int64)ReportingStatusPictureEnum.Info << (int)ReportingFlags.IconFlagsShift));
                                    break;

                                case EventLogEntryType.Warning:
                                    Flags = (ReportingFlags)((Int64)Flags | ((Int64)ReportingStatusPictureEnum.Exclamation << (int)ReportingFlags.IconFlagsShift));
                                    break;

                                case EventLogEntryType.Error:
                                    Flags = (ReportingFlags)((Int64)Flags | ((Int64)ReportingStatusPictureEnum.Stop << (int)ReportingFlags.IconFlagsShift));
                                    break;

                                case EventLogEntryType.SuccessAudit:
                                    Flags = (ReportingFlags)((Int64)Flags | ((Int64)ReportingStatusPictureEnum.Key << (int)ReportingFlags.IconFlagsShift));
                                    break;

                                case EventLogEntryType.FailureAudit:
                                    Flags = (ReportingFlags)((Int64)Flags | ((Int64)ReportingStatusPictureEnum.NoKey << (int)ReportingFlags.IconFlagsShift));
                                    break;
                                }
                                ReportingProcessor.ReportEventLog(sql, EV.MachineID, EV, Flags);
                                if (AlreadyReported.ContainsKey(EV.LogID) == true)
                                {
                                    AlreadyReported[EV.LogID] |= (Int64)Flags;
                                }
                                else
                                {
                                    AlreadyReported.Add(EV.LogID, (Int64)Flags);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ee)
            {
                FoxEventLog.WriteEventLog("SEH in Event Data Reporting " + ee.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }
        }
Beispiel #2
0
        public static List <PolicyObject> GetPolicyForComputerInternal(SQLLib sql, string MachineID)
        {
            PolicyObjectList PolicyListSigned = new PolicyObjectList();

            PolicyListSigned.Items = new List <PolicyObject>();

            SqlDataReader dr;

            dr = sql.ExecSQLReader("SELECT * FROM Policies WHERE MachineID=@m AND Enabled=1",
                                   new SQLParam("@m", MachineID));
            while (dr.Read())
            {
                PolicyObject obj = LoadPolicyDB(dr, false, false);
                PolicyListSigned.Items.Add(obj);
            }
            dr.Close();

            Int64? GroupID = null;
            object sqlo    = sql.ExecSQLScalar("select Grouping from ComputerAccounts where MachineID=@m", new SQLParam("@m", MachineID));

            if (sqlo is DBNull || sqlo == null)
            {
                GroupID = null;
            }
            else
            {
                GroupID = Convert.ToInt64(sqlo);
            }

            do
            {
                dr = sql.ExecSQLReader("SELECT * FROM Policies WHERE " + (GroupID == null ? "Grouping is NULL" : "Grouping=@g") + " AND Enabled=1 AND MachineID is NULL",
                                       new SQLParam("@g", GroupID));
                while (dr.Read())
                {
                    PolicyObject obj = LoadPolicyDB(dr, false, false);
                    PolicyListSigned.Items.Add(obj);
                }
                dr.Close();

                if (GroupID != null)
                {
                    sqlo = sql.ExecSQLScalar("select ParentID FROM Grouping WHERE ID=@g", new SQLParam("@g", GroupID));
                    if (sqlo is DBNull || sqlo == null)
                    {
                        GroupID = null;
                    }
                    else
                    {
                        GroupID = Convert.ToInt64(sqlo);
                    }
                }
                else
                {
                    break;
                }
            } while (true);

            PolicyListSigned.Items.Reverse();
            Int64 Count = 1;

            foreach (PolicyObject p in PolicyListSigned.Items)
            {
                p.Order = Count;
                Count++;
            }

            #region Resolve Linked Policies for client

            for (int i = 0; i < PolicyListSigned.Items.Count; i++)
            {
                PolicyObject pol = PolicyListSigned.Items[i];
                if (pol.Type == PolicyIDs.LinkedPolicy)
                {
                    List <Int64> RunningIDs = new List <Int64>();

                    PolicyObject po = pol;

                    while (true)
                    {
                        if (RunningIDs.Contains(po.ID) == true)
                        {
                            FoxEventLog.WriteEventLog("Policy ID " + pol.ID.ToString() + " (" + pol.Name + ") creates a loop!", System.Diagnostics.EventLogEntryType.Warning);
                            break;
                        }

                        RunningIDs.Add(po.ID);

                        Int64 PolID;
                        po.Data = Convert.ToString(sql.ExecSQLScalar("SELECT DataBlob FROM Policies WHERE ID=@id",
                                                                     new SQLParam("@id", po.ID)));
                        if (Int64.TryParse(po.Data, out PolID) == false)
                        {
                            FoxEventLog.WriteEventLog("Cannot read data of policy ID " + po.ID.ToString() + " (" + po.Name + ") for linking.", System.Diagnostics.EventLogEntryType.Warning);
                            break;
                        }

                        if (Policies.PolicyExsits(sql, PolID) == false)
                        {
                            FoxEventLog.WriteEventLog("Policy ID " + PolID.ToString() + " referencing from " + po.ID.ToString() + " (" + po.Name + ") does not exist.", System.Diagnostics.EventLogEntryType.Warning);
                            break;
                        }

                        if (Convert.ToInt32(sql.ExecSQLScalar("SELECT Enabled FROM Policies WHERE ID=@id", new SQLParam("@id", PolID))) != 1)
                        {
                            break;
                        }

                        dr = sql.ExecSQLReader("SELECT * FROM Policies WHERE ID=@id",
                                               new SQLParam("@id", PolID));
                        dr.Read();
                        po = LoadPolicyDB(dr, false, false);
                        dr.Close();
                        if (po == null)
                        {
                            FoxEventLog.WriteEventLog("Cannot read policy ID for linking " + PolID.ToString(), System.Diagnostics.EventLogEntryType.Warning);
                            break;
                        }
                        if (po.Type != PolicyIDs.LinkedPolicy)
                        {
                            pol = po;
                            PolicyListSigned.Items[i] = po;
                            break;
                        }
                    }
                }
            }

            #endregion

            return(PolicyListSigned.Items);
        }
Beispiel #3
0
        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);
            }
        }
Beispiel #4
0
        public RESTStatus GetPolicyForComputer(SQLLib sql, object dummy, NetworkConnectionInfo ni)
        {
            if (ni.HasAcl(ACLFlags.ComputerLogin) == false)
            {
                ni.Error   = "Access denied";
                ni.ErrorID = ErrorFlags.AccessDenied;
                return(RESTStatus.Denied);
            }

            PolicyListSigned       = new PolicyObjectListSigned();
            PolicyListSigned.Items = new List <PolicyObjectSigned>();

            string MachineID = ni.Username;

            lock (ni.sqllock)
            {
                SqlDataReader dr = sql.ExecSQLReader("SELECT * FROM Policies WHERE MachineID=@m AND Enabled=1 AND Type not in (" + PolicyIDs.HiddenPoliciesSQLINClause + ")",
                                                     new SQLParam("@m", MachineID));
                while (dr.Read())
                {
                    PolicyObject       obj  = LoadPolicyDB(dr, false, true);
                    PolicyObjectSigned objs = new PolicyObjectSigned();
                    objs.Policy = obj;
                    if (Certificates.Sign(objs, SettingsManager.Settings.UseCertificate) == false)
                    {
                        FoxEventLog.WriteEventLog("Cannot sign policy with Certificate " + SettingsManager.Settings.UseCertificate, System.Diagnostics.EventLogEntryType.Warning);
                        ni.Error   = "Cannot sign policy with Certificate " + SettingsManager.Settings.UseCertificate;
                        ni.ErrorID = ErrorFlags.CannotSign;
                        dr.Close();
                        return(RESTStatus.ServerError);
                    }
                    PolicyListSigned.Items.Add(objs);
                }
                dr.Close();
            }

            Int64? GroupID = null;
            object sqlo    = sql.ExecSQLScalar("select Grouping from ComputerAccounts where MachineID=@m", new SQLParam("@m", MachineID));

            if (sqlo is DBNull || sqlo == null)
            {
                GroupID = null;
            }
            else
            {
                GroupID = Convert.ToInt64(sqlo);
            }

            do
            {
                lock (ni.sqllock)
                {
                    SqlDataReader dr = sql.ExecSQLReader("SELECT * FROM Policies WHERE " + (GroupID == null ? "Grouping is NULL" : "Grouping=@g") + " AND Enabled=1 AND Type NOT IN (" + PolicyIDs.HiddenPoliciesSQLINClause + ") AND MachineID is NULL",
                                                         new SQLParam("@g", GroupID));
                    while (dr.Read())
                    {
                        PolicyObject       obj  = LoadPolicyDB(dr, false, true);
                        PolicyObjectSigned objs = new PolicyObjectSigned();
                        objs.Policy = obj;
                        PolicyListSigned.Items.Add(objs);
                    }
                    dr.Close();
                }

                if (GroupID != null)
                {
                    lock (ni.sqllock)
                    {
                        sqlo = sql.ExecSQLScalar("select ParentID FROM Grouping WHERE ID=@g", new SQLParam("@g", GroupID));
                    }

                    if (sqlo is DBNull || sqlo == null)
                    {
                        GroupID = null;
                    }
                    else
                    {
                        GroupID = Convert.ToInt64(sqlo);
                    }
                }
                else
                {
                    break;
                }
            } while (true);

            PolicyListSigned.Items.Reverse();
            Int64 Count = 1;

            foreach (PolicyObjectSigned p in PolicyListSigned.Items)
            {
                p.Policy.Order = Count;
                Count++;
                if (Certificates.Sign(p, SettingsManager.Settings.UseCertificate) == false)
                {
                    FoxEventLog.WriteEventLog("Cannot sign policy with Certificate " + SettingsManager.Settings.UseCertificate, System.Diagnostics.EventLogEntryType.Warning);
                    ni.Error   = "Cannot sign policy with Certificate " + SettingsManager.Settings.UseCertificate;
                    ni.ErrorID = ErrorFlags.CannotSign;
                    return(RESTStatus.ServerError);
                }
            }

            List <PolicyObjectSigned> RemoveFromList = new List <PolicyObjectSigned>();

            #region Resolve Linked Policies for client

            for (int i = 0; i < PolicyListSigned.Items.Count; i++)
            {
                PolicyObjectSigned pol = PolicyListSigned.Items[i];
                if (pol.Policy.Type == PolicyIDs.LinkedPolicy)
                {
                    List <Int64> RunningIDs = new List <Int64>();

                    PolicyObject po = pol.Policy;

                    while (true)
                    {
                        if (RunningIDs.Contains(po.ID) == true)
                        {
                            FoxEventLog.WriteEventLog("Policy ID " + pol.Policy.ID.ToString() + " (" + pol.Policy.Name + ") creates a loop!", System.Diagnostics.EventLogEntryType.Warning);
                            break;
                        }

                        RunningIDs.Add(po.ID);

                        lock (ni.sqllock)
                        {
                            po.Data = Convert.ToString(sql.ExecSQLScalar("SELECT DataBlob FROM Policies WHERE ID=@id",
                                                                         new SQLParam("@id", po.ID)));
                        }

                        Int64 PolID;
                        if (Int64.TryParse(po.Data, out PolID) == false)
                        {
                            FoxEventLog.WriteEventLog("Cannot read data of policy ID " + po.ID.ToString() + " (" + po.Name + ") for linking.", System.Diagnostics.EventLogEntryType.Warning);
                            break;
                        }

                        lock (ni.sqllock)
                        {
                            if (Policies.PolicyExsits(sql, PolID) == false)
                            {
                                FoxEventLog.WriteEventLog("Policy ID " + PolID.ToString() + " referencing from " + po.ID.ToString() + " (" + po.Name + ") does not exist.", System.Diagnostics.EventLogEntryType.Warning);
                                break;
                            }
                        }

                        lock (ni.sqllock)
                        {
                            if (Convert.ToInt32(sql.ExecSQLScalar("SELECT Enabled FROM Policies WHERE ID=@id", new SQLParam("@id", PolID))) != 1)
                            {
                                break;
                            }
                        }

                        lock (ni.sqllock)
                        {
                            SqlDataReader dr = sql.ExecSQLReader("SELECT * FROM Policies WHERE ID=@id",
                                                                 new SQLParam("@id", PolID));
                            dr.Read();
                            po = LoadPolicyDB(dr, false, true);
                            dr.Close();
                        }

                        if (po == null)
                        {
                            FoxEventLog.WriteEventLog("Cannot read policy ID for linking " + PolID.ToString(), System.Diagnostics.EventLogEntryType.Warning);
                            break;
                        }

                        if (po.Type != PolicyIDs.LinkedPolicy)
                        {
                            if (PolicyIDs.HiddenPolicies.Contains(po.Type) == true)
                            {
                                pol.Policy    = null;
                                pol.Signature = null;
                                break;
                            }
                            else
                            {
                                pol.Policy    = po;
                                pol.Signature = null;
                                break;
                            }
                        }
                    }

                    if (pol.Policy != null)
                    {
                        if (Certificates.Sign(pol, SettingsManager.Settings.UseCertificate) == false)
                        {
                            FoxEventLog.WriteEventLog("Cannot sign policy with Certificate " + SettingsManager.Settings.UseCertificate, System.Diagnostics.EventLogEntryType.Warning);
                            ni.Error   = "Cannot sign policy with Certificate " + SettingsManager.Settings.UseCertificate;
                            ni.ErrorID = ErrorFlags.CannotSign;
                            return(RESTStatus.ServerError);
                        }
                    }
                    else
                    {
                        RemoveFromList.Add(pol);
                    }
                }
            }

            foreach (PolicyObjectSigned pos in RemoveFromList)
            {
                PolicyListSigned.Items.Remove(pos);
            }

            #endregion

            if (Certificates.Sign(PolicyListSigned, SettingsManager.Settings.UseCertificate) == false)
            {
                FoxEventLog.WriteEventLog("Cannot sign policy list with Certificate " + SettingsManager.Settings.UseCertificate, System.Diagnostics.EventLogEntryType.Warning);
                ni.Error   = "Cannot sign policy list with Certificate " + SettingsManager.Settings.UseCertificate;
                ni.ErrorID = ErrorFlags.CannotSign;
                return(RESTStatus.ServerError);
            }

            return(RESTStatus.Success);
        }
Beispiel #5
0
        public RESTStatus EditPolicy(SQLLib sql, EditPolicy request, NetworkConnectionInfo ni, Int64 id)
        {
            if (ni.HasAcl(ACLFlags.ChangeServerSettings) == false)
            {
                ni.Error   = "Access denied";
                ni.ErrorID = ErrorFlags.AccessDenied;
                return(RESTStatus.Denied);
            }

            lock (ni.sqllock)
            {
                if (Policies.PolicyExsits(sql, id) == false)
                {
                    ni.Error   = "Invalid data";
                    ni.ErrorID = ErrorFlags.InvalidData;
                    return(RESTStatus.Fail);
                }
            }

            if (id != request.ID)
            {
                ni.Error   = "Invalid data";
                ni.ErrorID = ErrorFlags.InvalidData;
                return(RESTStatus.Fail);
            }

            if (request.DataOnly == false)
            {
                if (request.Name == null || request.Name.Trim() == "")
                {
                    ni.Error   = "Invalid name";
                    ni.ErrorID = ErrorFlags.InvalidData;
                    return(RESTStatus.Fail);
                }

                request.Name = request.Name.Trim();

                if (request.Grouping != null && request.MachineID != null)
                {
                    ni.Error   = "Either Grouping OR MachineID should be set";
                    ni.ErrorID = ErrorFlags.InvalidData;
                    return(RESTStatus.Fail);
                }

                if (request.Grouping != null)
                {
                    lock (ni.sqllock)
                    {
                        if (Groups.GroupExsits(sql, request.Grouping.Value) == false)
                        {
                            ni.Error   = "Group does not exists";
                            ni.ErrorID = ErrorFlags.InvalidData;
                            return(RESTStatus.Fail);
                        }
                    }
                }

                if (request.MachineID != null)
                {
                    lock (ni.sqllock)
                    {
                        if (Computers.MachineExists(sql, request.MachineID) == false)
                        {
                            ni.Error   = "MachineID does not exists";
                            ni.ErrorID = ErrorFlags.InvalidData;
                            return(RESTStatus.Fail);
                        }
                    }
                }
            }

            try
            {
                JsonConvert.DeserializeObject(request.Data);
            }
            catch
            {
                ni.Error   = "JSON error";
                ni.ErrorID = ErrorFlags.InvalidData;
                return(RESTStatus.Fail);
            }

            try
            {
                if (request.DataOnly == false)
                {
                    lock (ni.sqllock)
                    {
                        sql.ExecSQL("Update Policies SET Name=@n, Grouping=@g, MachineID=@m, DataBlob=@blob, DT=Getutcdate(), Version=Version+1 WHERE ID=@id",
                                    new SQLParam("@id", request.ID),
                                    new SQLParam("@n", request.Name),
                                    new SQLParam("@g", request.Grouping),
                                    new SQLParam("@m", request.MachineID),
                                    new SQLParam("@blob", request.Data));
                    }
                }
                else
                {
                    lock (ni.sqllock)
                    {
                        sql.ExecSQL("Update Policies SET DataBlob=@blob, DT=Getutcdate(), Version=Version+1 WHERE ID=@id",
                                    new SQLParam("@id", request.ID),
                                    new SQLParam("@blob", request.Data));
                    }
                }
            }
            catch
            {
                ni.Error   = "SQL Error";
                ni.ErrorID = ErrorFlags.SQLError;
                return(RESTStatus.ServerError);
            }

            return(RESTStatus.Success);
        }
Beispiel #6
0
        void ReportingThread(object SimpleTaskO)
        {
            try
            {
                using (SQLLib sql = SQLTest.ConnectSQL("Fox SDC Server for SimpleTask Result Data"))
                {
                    if (sql == null)
                    {
                        FoxEventLog.WriteEventLog("Cannot connect to SQL Server for SimpleTask Result Reporting!", System.Diagnostics.EventLogEntryType.Error);
                        return;
                    }
                    SimpleTaskResult SimpleTask = (SimpleTaskResult)SimpleTaskO;

                    List <PolicyObject>        Pol             = Policies.GetPolicyForComputerInternal(sql, SimpleTask.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.SimpleTaskCompleted)
                        {
                            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)
                        {
                            ReportingPolicyElementSimpleTaskCompleted arprep = JsonConvert.DeserializeObject <ReportingPolicyElementSimpleTaskCompleted>(Element);

                            ReportThings(sql, SimpleTask.MachineID, "Completed", SimpleTask, ref AlreadyReported, RepElementRoot);
                        }
                    }
                }
            }
            catch (Exception ee)
            {
                FoxEventLog.WriteEventLog("SEH in SimpleTask Result Reporting " + ee.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }
        }
Beispiel #7
0
        void ReportingThread(object SmartDataListO)
        {
            try
            {
                using (SQLLib sql = SQLTest.ConnectSQL("Fox SDC Server for SMART Device Data"))
                {
                    if (sql == null)
                    {
                        FoxEventLog.WriteEventLog("Cannot connect to SQL Server for SMART Device Reporting!", System.Diagnostics.EventLogEntryType.Error);
                        return;
                    }
                    SmartDataLst SmartDataList = (SmartDataLst)SmartDataListO;
                    ComputerData computerdata  = Computers.GetComputerDetail(sql, SmartDataList.MachineID);
                    if (computerdata == null)
                    {
                        FoxEventLog.WriteEventLog("Cannot get any computer data for SMART Device Reporting!", System.Diagnostics.EventLogEntryType.Error);
                        return;
                    }

                    List <PolicyObject>        Pol             = Policies.GetPolicyForComputerInternal(sql, SmartDataList.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.SMART)
                        {
                            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)
                        {
                            ReportingPolicyElementSMART arprep = JsonConvert.DeserializeObject <ReportingPolicyElementSMART>(Element);
                            if (arprep.NotifyOnAdd == false && arprep.NotifyOnRemove == false && arprep.NotifyOnUpdate == false &&
                                arprep.NotifyOnError == false)
                            {
                                continue;
                            }

                            if (arprep.NotifyOnAdd == true)
                            {
                                foreach (VulpesSMARTInfo ar in SmartDataList.Added)
                                {
                                    ReportThings(sql, SmartDataList.MachineID, "Add", ar, ref AlreadyReported, RepElementRoot);
                                }
                            }

                            if (arprep.NotifyOnUpdate == true)
                            {
                                foreach (VulpesSMARTInfo ar in SmartDataList.Updated)
                                {
                                    foreach (VulpesSMARTInfo indbvsm in SmartDataList.InDB)
                                    {
                                        if (indbvsm.PNPDeviceID == ar.PNPDeviceID)
                                        {
                                            if (SMARTDescription.CompareFull(ar, indbvsm, arprep.SkipAttribUpdateReport) == false)
                                            {
                                                if (indbvsm.Attributes != null)
                                                {
                                                    List <int> UpdatedAttribs = new List <int>();
                                                    if (ar.Attributes == null)
                                                    {
                                                        ar.Attributes = new Dictionary <int, VulpesSMARTAttribute>();
                                                    }
                                                    foreach (KeyValuePair <int, VulpesSMARTAttribute> indb in indbvsm.Attributes)
                                                    {
                                                        if (ar.Attributes.ContainsKey(indb.Key) == true)
                                                        {
                                                            if (ar.Attributes[indb.Key].FailureImminent != indb.Value.FailureImminent ||
                                                                ar.Attributes[indb.Key].Flags != indb.Value.Flags ||
                                                                ar.Attributes[indb.Key].ID != indb.Value.ID ||
                                                                ar.Attributes[indb.Key].Threshold != indb.Value.Threshold ||
                                                                ar.Attributes[indb.Key].Value != indb.Value.Value ||
                                                                ar.Attributes[indb.Key].Vendordata != indb.Value.Vendordata ||
                                                                ar.Attributes[indb.Key].Worst != indb.Value.Worst)
                                                            {
                                                                UpdatedAttribs.Add(indb.Key);
                                                            }
                                                        }
                                                    }
                                                    ReportThings(sql, SmartDataList.MachineID, "Update", ar, ref AlreadyReported, RepElementRoot, UpdatedAttribs);
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            if (arprep.NotifyOnRemove == true)
                            {
                                foreach (VulpesSMARTInfo ar in SmartDataList.Removed)
                                {
                                    ReportThings(sql, SmartDataList.MachineID, "Remove", ar, ref AlreadyReported, RepElementRoot);
                                }
                            }

                            if (arprep.NotifyOnError == true)
                            {
                                foreach (VulpesSMARTInfo ar in SmartDataList.Added)
                                {
                                    if (SMARTDescription.IsInError(ar) == true)
                                    {
                                        ReportThings(sql, SmartDataList.MachineID, "Error", ar, ref AlreadyReported, RepElementRoot);
                                    }
                                }
                                foreach (VulpesSMARTInfo ar in SmartDataList.Updated)
                                {
                                    if (SMARTDescription.IsInError(ar) == true)
                                    {
                                        foreach (VulpesSMARTInfo indbvsm in SmartDataList.InDB)
                                        {
                                            if (indbvsm.PNPDeviceID == ar.PNPDeviceID)
                                            {
                                                if (SMARTDescription.CompareFullCriticalOnly(indbvsm, ar) == false)
                                                {
                                                    ReportThings(sql, SmartDataList.MachineID, "Error", ar, ref AlreadyReported, RepElementRoot);
                                                }
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ee)
            {
                FoxEventLog.WriteEventLog("SEH in SMART Data Reporting " + ee.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }
        }
Beispiel #8
0
        void ReportingThread(object DiskDataListO)
        {
            try
            {
                using (SQLLib sql = SQLTest.ConnectSQL("Fox SDC Server for DiskData"))
                {
                    if (sql == null)
                    {
                        FoxEventLog.WriteEventLog("Cannot connect to SQL Server for Disk Data Reporting!", System.Diagnostics.EventLogEntryType.Error);
                        return;
                    }
                    ListDiskDataReport  DiskDataList = (ListDiskDataReport)DiskDataListO;
                    List <PolicyObject> Pol          = Policies.GetPolicyForComputerInternal(sql, DiskDataList.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.Disk)
                        {
                            continue;
                        }

                        foreach (string Element in RepElementRoot.ReportingElements)
                        {
                            ReportingPolicyElementDisk diskrep = JsonConvert.DeserializeObject <ReportingPolicyElementDisk>(Element);
                            if (diskrep.DriveLetter == null)
                            {
                                continue;
                            }
                            if (diskrep.DriveLetter.Length != 1)
                            {
                                continue;
                            }

                            foreach (DiskDataReport DD in DiskDataList.Items)
                            {
                                string Drive = diskrep.DriveLetter;

                                if (diskrep.DriveLetter == "$")
                                {
                                    ComputerData d = Computers.GetComputerDetail(sql, DiskDataList.MachineID);
                                    if (d != null)
                                    {
                                        if (string.IsNullOrWhiteSpace(d.SystemRoot) == false)
                                        {
                                            Drive = d.SystemRoot.Substring(0, 1);
                                        }
                                    }
                                }

                                if (string.IsNullOrWhiteSpace(DD.DriveLetter) == true)
                                {
                                    continue;
                                }
                                if (DD.DriveLetter.ToLower().Substring(0, 1) != Drive.ToLower())
                                {
                                    continue;
                                }

                                Int64 SZLimit;

                                if (diskrep.Method == 1)
                                {
                                    SZLimit = (Int64)((100m / (decimal)DD.Capacity) * (decimal)diskrep.MinimumSize);
                                }
                                else
                                {
                                    SZLimit = diskrep.MinimumSize;
                                }

                                if (DD.FreeSpace < SZLimit)
                                {
                                    bool ReportToAdmin   = RepElementRoot.ReportToAdmin.Value;
                                    bool ReportToClient  = RepElementRoot.ReportToClient.Value;
                                    bool UrgentForAdmin  = RepElementRoot.UrgentForAdmin.Value;
                                    bool UrgentForClient = RepElementRoot.UrgentForClient.Value;

                                    if (AlreadyReported.ContainsKey(DD.DriveLetter) == true)
                                    {
                                        if ((AlreadyReported[DD.DriveLetter] & (Int64)ReportingFlags.ReportToAdmin) != 0)
                                        {
                                            ReportToAdmin = false;
                                        }
                                        if ((AlreadyReported[DD.DriveLetter] & (Int64)ReportingFlags.ReportToClient) != 0)
                                        {
                                            ReportToClient = false;
                                        }
                                        if ((AlreadyReported[DD.DriveLetter] & (Int64)ReportingFlags.UrgentForAdmin) != 0)
                                        {
                                            UrgentForAdmin = false;
                                        }
                                        if ((AlreadyReported[DD.DriveLetter] & (Int64)ReportingFlags.UrgentForClient) != 0)
                                        {
                                            UrgentForClient = false;
                                        }
                                    }

                                    if (ReportToAdmin == false && ReportToClient == false && UrgentForAdmin == false && UrgentForClient == false)
                                    {
                                        continue;
                                    }
                                    ReportingFlags Flags = (ReportToAdmin == true ? ReportingFlags.ReportToAdmin : 0) |
                                                           (ReportToClient == true ? ReportingFlags.ReportToClient : 0) |
                                                           (UrgentForAdmin == true ? ReportingFlags.UrgentForAdmin : 0) |
                                                           (UrgentForClient == true ? ReportingFlags.UrgentForClient : 0);
                                    ReportingProcessor.ReportDiskData(sql, DiskDataList.MachineID, DD.DriveLetter, SZLimit, DD.FreeSpace, DD.Capacity, Flags);
                                    if (AlreadyReported.ContainsKey(DD.DriveLetter) == true)
                                    {
                                        AlreadyReported[DD.DriveLetter] |= (Int64)Flags;
                                    }
                                    else
                                    {
                                        AlreadyReported.Add(DD.DriveLetter, (Int64)Flags);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ee)
            {
                FoxEventLog.WriteEventLog("SEH in Disk Data Reporting " + ee.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }
        }