Exemple #1
0
        static public bool DeletePolicy(LoadedPolicyObject obj)
        {
            LoadedPolicyObjects.Remove(obj);
            try
            {
                File.Delete(obj.Filename);
            }
            catch
            {
            }
            try
            {
                File.Delete(obj.SignFilename);
            }
            catch
            {
            }
            Debug.WriteLine(obj.Filename + " deleted");
            Debug.WriteLine(obj.SignFilename + " deleted");

            return(true);
        }
Exemple #2
0
        static public bool InstallPolicy(PolicyObject data, Int64 Order)
        {
            if (data == null)
            {
                return(false);
            }
            data.Order = Order;
            string PoliciesFolder = SystemInfos.ProgramData + "Policies\\";
            string Filename       = data.ID.ToString("X8") + "-" + Guid.NewGuid().ToString();

            byte[] pol  = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data));
            byte[] sign = ApplicationCertificate.Sign(pol);
            if (sign == null)
            {
                FoxEventLog.WriteEventLog("Cannot sign policy for saving", System.Diagnostics.EventLogEntryType.Error);
                return(false);
            }

            //do we have that policy loaded?... - overwrite that (and store it to an Update Dict)
            if (LoadedPolicyObjects == null)
            {
                LoadedPolicyObjects = new List <LoadedPolicyObject>();
            }

            foreach (LoadedPolicyObject loadedpol in LoadedPolicyObjects)
            {
                if (loadedpol.PolicyObject.ID != data.ID)
                {
                    continue;
                }

                if (UpdatePolicies == null)
                {
                    UpdatePolicies = new Dictionary <LoadedPolicyObject, LoadedPolicyObject>();
                }
                UpdatePolicies.Add(
                    new LoadedPolicyObject()
                {
                    Filename     = loadedpol.Filename,
                    SignFilename = loadedpol.SignFilename,
                    PolicyObject = new PolicyObject()
                    {
                        Condition      = loadedpol.PolicyObject.Condition,
                        DataAddtions1  = loadedpol.PolicyObject.DataAddtions1,
                        DataAddtions2  = loadedpol.PolicyObject.DataAddtions2,
                        DataAddtions3  = loadedpol.PolicyObject.DataAddtions3,
                        DataAddtions4  = loadedpol.PolicyObject.DataAddtions4,
                        DataAddtions5  = loadedpol.PolicyObject.DataAddtions5,
                        Data           = loadedpol.PolicyObject.Data,
                        DT             = loadedpol.PolicyObject.DT,
                        Enabled        = loadedpol.PolicyObject.Enabled,
                        Grouping       = loadedpol.PolicyObject.Grouping,
                        ID             = loadedpol.PolicyObject.ID,
                        MachineID      = loadedpol.PolicyObject.MachineID,
                        Name           = loadedpol.PolicyObject.Name,
                        Order          = loadedpol.PolicyObject.Order,
                        TimeStampCheck = loadedpol.PolicyObject.TimeStampCheck,
                        Type           = loadedpol.PolicyObject.Type,
                        Version        = loadedpol.PolicyObject.Version
                    }
                }
                    ,
                    new LoadedPolicyObject()
                {
                    PolicyObject = data,
                    Filename     = loadedpol.Filename,
                    SignFilename = loadedpol.SignFilename
                });

                loadedpol.PolicyObject = data;

                try
                {
                    File.WriteAllBytes(loadedpol.Filename, pol);
                }
                catch
                {
                    FoxEventLog.WriteEventLog("Cannot save policy", System.Diagnostics.EventLogEntryType.Error);
                    return(false);
                }

                try
                {
                    File.WriteAllBytes(loadedpol.SignFilename, sign);
                }
                catch
                {
                    try
                    {
                        File.Delete(loadedpol.Filename);
                    }
                    catch
                    {
                    }
                    FoxEventLog.WriteEventLog("Cannot save policy signature", System.Diagnostics.EventLogEntryType.Error);
                    return(false);
                }

                return(true);
            }

            //no? create the file

            LoadedPolicyObject lobj = new LoadedPolicyObject();

            lobj.PolicyObject = data;
            lobj.Filename     = PoliciesFolder + Filename + ".pol";
            lobj.SignFilename = PoliciesFolder + Filename + ".sign";

            try
            {
                File.WriteAllBytes(lobj.Filename, pol);
            }
            catch
            {
                FoxEventLog.WriteEventLog("Cannot save policy", System.Diagnostics.EventLogEntryType.Error);
                return(false);
            }

            try
            {
                File.WriteAllBytes(lobj.SignFilename, sign);
            }
            catch
            {
                try
                {
                    File.Delete(lobj.Filename);
                }
                catch
                {
                }
                FoxEventLog.WriteEventLog("Cannot save policy signature", System.Diagnostics.EventLogEntryType.Error);
                return(false);
            }

            LoadedPolicyObjects.Add(lobj);

            return(true);
        }
Exemple #3
0
        static public bool LoadPolicies()
        {
            LoadedPolicyObjects = new List <LoadedPolicyObject>();
            string PoliciesFolder = SystemInfos.ProgramData + "Policies\\";

            if (Directory.Exists(PoliciesFolder) == false)
            {
                Directory.CreateDirectory(PoliciesFolder);
            }

            foreach (string file in Directory.EnumerateFiles(PoliciesFolder, "*.pol", SearchOption.TopDirectoryOnly))
            {
                string signfile = file.Substring(0, file.Length - 4) + ".sign";

                if (File.Exists(file) == false)
                {
                    if (File.Exists(signfile) == true)
                    {
                        FoxEventLog.WriteEventLog("Found lonely file \"" + file + "\" - deleting the file", System.Diagnostics.EventLogEntryType.Warning);
                        File.Delete(signfile);
                    }
                }
                else
                {
                    if (File.Exists(signfile) == false)
                    {
                        FoxEventLog.WriteEventLog("Found \"" + file + "\" but no signature - deleting the file", System.Diagnostics.EventLogEntryType.Warning);
                        File.Delete(file);
                    }
                    else
                    {
                        FileInfo fileinfo;
                        fileinfo = new FileInfo(file);
                        if (fileinfo.Length > 33554432)
                        {
                            FoxEventLog.WriteEventLog("File \"" + file + "\" too large (>32MB) - deleting the files", System.Diagnostics.EventLogEntryType.Warning);
                            File.Delete(file);
                            File.Delete(signfile);
                            continue;
                        }
                        fileinfo = new FileInfo(signfile);
                        if (fileinfo.Length > 33554432)
                        {
                            FoxEventLog.WriteEventLog("File \"" + signfile + "\" too large (>32MB) - deleting the files", System.Diagnostics.EventLogEntryType.Warning);
                            File.Delete(file);
                            File.Delete(signfile);
                            continue;
                        }

                        byte[] pol  = File.ReadAllBytes(file);
                        byte[] sign = File.ReadAllBytes(signfile);

                        if (ApplicationCertificate.Verify(pol, sign) == false)
                        {
                            FoxEventLog.WriteEventLog("File \"" + file + "\" is not proper signed - deleting the files", System.Diagnostics.EventLogEntryType.Warning);
                            File.Delete(file);
                            File.Delete(signfile);
                            continue;
                        }

                        try
                        {
                            PolicyObject obj = JsonConvert.DeserializeObject <PolicyObject>(Encoding.UTF8.GetString(pol));
                            if (ContainsPolicy(obj, true, true) == true)
                            {
                                FoxEventLog.WriteEventLog("File \"" + file + "\" is already loaded from a different file - deleting the files", System.Diagnostics.EventLogEntryType.Warning);
                                File.Delete(file);
                                File.Delete(signfile);
                                continue;
                            }
                            LoadedPolicyObject lobj = new LoadedPolicyObject();
                            lobj.PolicyObject = obj;
                            lobj.Filename     = file;
                            lobj.SignFilename = signfile;
                            LoadedPolicyObjects.Add(lobj);
                            Debug.WriteLine(file + " loaded");
                        }
                        catch
                        {
                            FoxEventLog.WriteEventLog("File \"" + file + "\" cannot be loaded properly - deleting the files", System.Diagnostics.EventLogEntryType.Warning);
                            File.Delete(file);
                            File.Delete(signfile);
                            continue;
                        }
                    }
                }
            }

            return(true);
        }