public static void LoadEventLogList() { if (SyncedEventLog == null) { SyncedEventLog = new HashSet <string>(); } try { if (File.Exists(SystemInfos.ProgramData + "SyncedEventLog.sign") == true && File.Exists(SystemInfos.ProgramData + "SyncedEventLog.json") == true) { byte[] list = File.ReadAllBytes(SystemInfos.ProgramData + "SyncedEventLog.json"); byte[] sign = File.ReadAllBytes(SystemInfos.ProgramData + "SyncedEventLog.sign"); if (ApplicationCertificate.Verify(list, sign) == false) { File.Delete(SystemInfos.ProgramData + "SyncedEventLog.json"); File.Delete(SystemInfos.ProgramData + "SyncedEventLog.sign"); FoxEventLog.WriteEventLog("Event Log List signature is invalid: deleting the files!", EventLogEntryType.Warning); SyncedEventLog = new HashSet <string>(); } else { SyncedEventLog = JsonConvert.DeserializeObject <HashSet <string> >(Encoding.UTF8.GetString(list)); } } } catch (Exception ee) { Debug.WriteLine(ee.ToString()); FoxEventLog.WriteEventLog("Error decoding Event Log List", EventLogEntryType.Warning); SyncedEventLog = new HashSet <string>(); } }
static public bool InstallCertificate(byte[] data) { if (ContainsLoadedCert(data) == true) { return(true); } string CertFolder = SystemInfos.ProgramData + "Certificates\\"; FilesystemCertificateData cer = new FilesystemCertificateData(); for (int i = 1; i < 100; i++) { if (File.Exists(CertFolder + "Certificate" + i.ToString("00") + ".cer") == false) { File.WriteAllBytes(CertFolder + "Certificate" + i.ToString("00") + ".cer", data); byte[] d = ApplicationCertificate.Sign(data); if (d != null) { File.WriteAllBytes(CertFolder + "Certificate" + i.ToString("00") + ".sign", d); } FoxEventLog.WriteEventLog("Certificate " + Certificates.GetCN(data) + " installed as ID=" + i.ToString("00"), System.Diagnostics.EventLogEntryType.Information); cer.FSFilename = "Certificate" + i.ToString("00") + ".cer"; cer.Certificate = data; break; } } LoadedCertificates.Add(cer); FoxEventLog.WriteEventLog("Certificate " + Certificates.GetCN(data) + " loaded", System.Diagnostics.EventLogEntryType.Information); return(true); }
public static void WritePackageList() { string PackagesFolder = SystemInfos.ProgramData + "Packages\\"; if (Directory.Exists(PackagesFolder) == false) { Directory.CreateDirectory(PackagesFolder); } #if DEBUG Formatting frm = Formatting.Indented; #else Formatting frm = Formatting.None; #endif byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(LocalPackages, frm)); File.WriteAllBytes(PackagesFolder + "List.json", data); byte[] sign = ApplicationCertificate.Sign(data); if (sign == null) { FoxEventLog.WriteEventLog("Cannot sign package list for saving", System.Diagnostics.EventLogEntryType.Error); return; } else { File.WriteAllBytes(PackagesFolder + "List.sign", sign); } }
public static bool DoSyncSimpleTasks() { try { Network net; net = Utilities.ConnectNetwork(0); if (net == null) { return(false); } Status.UpdateMessage(0, "Checking Simple Tasks"); Int64 Aside = -1; SimpleTaskDataSigned st = null; do { st = net.GetSimpleTaskSigned(); if (st == null) { break; } if (ApplicationCertificate.Verify(st) == false) { FoxEventLog.WriteEventLog("One or more Simple Tasks are tampered - no tasks will be processed.", System.Diagnostics.EventLogEntryType.Error); break; } if (Aside != -1) { if (Aside == st.STask.ID) { break; } } Int64 AID; if (ProcessSimpleTask(net, st.STask, out AID) == false) { break; } if (AID != -1) { Aside = AID; } } while (st != null); net.CloseConnection(); } catch (Exception ee) { Debug.WriteLine(ee.ToString()); FoxEventLog.WriteEventLog("Servere error while running Simple Tasks: " + ee.ToString(), EventLogEntryType.Error); } Status.UpdateMessage(0); return(true); }
static public void LoadLocalPackageData() { string PackagesFolder = SystemInfos.ProgramData + "Packages\\"; if (Directory.Exists(PackagesFolder) == false) { Directory.CreateDirectory(PackagesFolder); } if (LocalPackageDataList == null) { LocalPackageDataList = new List <LocalPackageData>(); } try { if (File.Exists(PackagesFolder + "Install.json") == true && File.Exists(PackagesFolder + "Install.sign") == true) { byte[] list = File.ReadAllBytes(PackagesFolder + "Install.json"); byte[] sign = File.ReadAllBytes(PackagesFolder + "Install.sign"); if (ApplicationCertificate.Verify(list, sign) == false) { File.Delete(PackagesFolder + "Install.json"); File.Delete(PackagesFolder + "Install.sign"); FoxEventLog.WriteEventLog("Package data list signature is invalid: deleting the files!", EventLogEntryType.Warning); LocalPackageDataList = new List <LocalPackageData>(); } else { LocalPackageDataList = JsonConvert.DeserializeObject <List <LocalPackageData> >(Encoding.UTF8.GetString(list)); } } } catch (Exception ee) { Debug.WriteLine(ee.ToString()); FoxEventLog.WriteEventLog("Error decoding package data list data", EventLogEntryType.Warning); LocalPackages = new List <PackagesToInstall>(); } }
public static void WriteEventLogList() { if (Directory.Exists(SystemInfos.ProgramData) == false) { Directory.CreateDirectory(SystemInfos.ProgramData); } #if DEBUG Formatting frm = Formatting.Indented; #else Formatting frm = Formatting.None; #endif byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(SyncedEventLog, frm)); File.WriteAllBytes(SystemInfos.ProgramData + "SyncedEventLog.json", data); byte[] sign = ApplicationCertificate.Sign(data); if (sign == null) { FoxEventLog.WriteEventLog("Cannot sign synced Event Log for saving", System.Diagnostics.EventLogEntryType.Error); return; } else { File.WriteAllBytes(SystemInfos.ProgramData + "SyncedEventLog.sign", sign); } }
static void ThreadRunner() { lock (FileLock) { FilesystemData.LoadFileTransferStatus(); } while (StopService == false) { try { Network net = Utilities.ConnectNetwork(7); if (net == null) { Wait(60); continue; } NetInt64ListSigned lst = net.File_Agent_GetFileList(); if (lst == null) { net.CloseConnection(); Wait(2 * 60); continue; } if (ApplicationCertificate.Verify(lst) == false) { FoxEventLog.WriteEventLog("One or more DownloadFS Lists are tampered - no download / uploads will be processed.", System.Diagnostics.EventLogEntryType.Error); net.CloseConnection(); Wait(2 * 60); continue; } if (lst.data.data.Count == 0) { net.CloseConnection(); Wait(2 * 60); continue; } Int64 RunningID = 0; lock (FileLock) { if (FilesystemData.FileTransferStatus.ServerID != null) { bool Found = false; foreach (Int64 l in lst.data.data) { if (l == FilesystemData.FileTransferStatus.ServerID.Value) { Found = true; RunningID = l; break; } } if (Found == false) { RunningID = lst.data.data[0]; } } else { RunningID = lst.data.data[0]; } } Debug.Assert(RunningID != 0); FileUploadDataSigned fud = net.File_Agent_GetFileAnyData(RunningID); if (ApplicationCertificate.Verify(fud) == false) { FoxEventLog.WriteEventLog("One or more DownloadFS Elements are tampered - no download / uploads will be processed.", System.Diagnostics.EventLogEntryType.Error); net.CloseConnection(); Wait(2 * 60); continue; } net.CloseConnection(); string LocalFilename = ""; bool WaitandContinue = false; lock (FileLock) { //may be needed for deleting the file (if canceled by the server) LocalFilename = FilesystemData.FileTransferStatus.RemoteFileLocation; if (FilesystemData.FileTransferStatus.ServerID != null) { if (RunningID != FilesystemData.FileTransferStatus.ServerID) { CancelAndDeleteDL = true; } else { if (FilesystemData.FileTransferStatus.Size != fud.Data.Size || FilesystemData.FileTransferStatus.RemoteFileLocation != fud.Data.RemoteFileLocation || FilesystemData.FileTransferStatus.MD5CheckSum.ToLower() != fud.Data.MD5CheckSum.ToLower() || FilesystemData.FileTransferStatus.RequestOnly != fud.Data.RequestOnly) { CancelAndDeleteDL = true; } else { if (RunningDLThread == null) { RunningDLThread = new Thread(new ThreadStart(DownloadThreadRunner)); RunningDLThread.Start(); } else { if (RunningDLThread.IsAlive == false) { RunningDLThread = new Thread(new ThreadStart(DownloadThreadRunner)); RunningDLThread.Start(); } } } WaitandContinue = true; } } } if (WaitandContinue == true) { Wait(2 * 60); continue; } if (CancelAndDeleteDL == true) { if (RunningDLThread != null) { RunningDLThread.Join(); } if (string.IsNullOrWhiteSpace(LocalFilename) == false) { try { if (FilesystemData.FileTransferStatus.Direction == 0) { if (File.Exists(LocalFilename) == true) { File.Delete(LocalFilename); } } } catch (Exception ee) { FoxEventLog.WriteEventLog("Cannot check / delete " + LocalFilename + "\n" + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); } } } CancelAndDeleteDL = false; lock (FileLock) { FilesystemData.FileTransferStatus.Size = fud.Data.Size; FilesystemData.FileTransferStatus.RemoteFileLocation = fud.Data.RemoteFileLocation; FilesystemData.FileTransferStatus.MD5CheckSum = fud.Data.MD5CheckSum.ToLower(); FilesystemData.FileTransferStatus.OverrideMeteredConnection = fud.Data.OverrideMeteredConnection; FilesystemData.FileTransferStatus.Direction = fud.Data.Direction; FilesystemData.FileTransferStatus.ProgressSize = 0; FilesystemData.FileTransferStatus.ServerID = RunningID; FilesystemData.FileTransferStatus.RequestOnly = fud.Data.RequestOnly; FilesystemData.FileTransferStatus.LastModfied = fud.Data.FileLastModified; FilesystemData.WriteFileTransferStatus(); } RunningDLThread = new Thread(new ThreadStart(DownloadThreadRunner)); RunningDLThread.Start(); Wait(2 * 60); continue; } catch (Exception ee) { FoxEventLog.WriteEventLog("Internal crash in DownloadFS System: " + ee.Message, System.Diagnostics.EventLogEntryType.Error); Wait(10); } Wait(2 * 60); } ; }
public static void SMain() { #if !DEBUG AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; #endif FoxEventLog.RegisterEventLog(); if (UsePipeAction == false && UseScreenAction == false && UseDNSAutoConfig == false && UseLoginRecovery == false) //Pipe Actions can also be run in user-space ... { if (IsSystemUser() != true) { #if !DEBUG || DEBUGSERVICE service.Stop(); #endif return; } } if (LoadDLL() == false) { #if !DEBUG || DEBUGSERVICE service.Stop(); #endif return; } #if !DEBUG if (TestIntegrity(null) == false) { FoxEventLog.WriteEventLog("Integrity check failed!", EventLogEntryType.Error); service.Stop(); return; } #endif if (UseDNSAutoConfig == true) { try { List <List <string> > Query = CPP.DNSQueryTXT("sdc-contract.my-vulpes-config.lu"); if (Query == null) { return; } string ContractID = null; string ContractPassword = null; string UseOnPrem = null; string OnPremURL = null; foreach (List <string> Q in Query) { if (Q == null) { continue; } foreach (string QR in Q) { if (string.IsNullOrWhiteSpace(QR) == true) { continue; } if (QR.ToLower().StartsWith("contractid=") == true) { ContractID = QR.Substring(11).Trim(); } if (QR.ToLower().StartsWith("contractpassword="******"useonprem=") == true) { UseOnPrem = QR.Substring(10).Trim(); } if (QR.ToLower().StartsWith("onpremurl=") == true) { OnPremURL = QR.Substring(10).Trim(); } } } using (RegistryKey k = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Fox\\SDC")) { if (string.IsNullOrWhiteSpace(ContractID) == false && string.IsNullOrWhiteSpace(ContractPassword) == false) { k.SetValue("ContractID", ContractID, RegistryValueKind.String); k.SetValue("ContractPassword", ContractPassword, RegistryValueKind.String); } int UseOnPremInt; if (int.TryParse(UseOnPrem, out UseOnPremInt) == true) { if (UseOnPremInt == 1 && string.IsNullOrWhiteSpace(OnPremURL) == false) { k.SetValue("UseOnPremServer", 1, RegistryValueKind.DWord); k.SetValue("Server", OnPremURL, RegistryValueKind.String); } else { k.SetValue("UseOnPremServer", 0, RegistryValueKind.DWord); } } else { k.SetValue("UseOnPremServer", 0, RegistryValueKind.DWord); } } } catch (Exception ee) { Debug.WriteLine(ee.ToString()); } return; } if (UseLoginRecovery == true) { if (SystemInfos.CollectSystemInfo() != 0) { return; } #if !DEBUG if (SystemInfos.SysInfo.RunningInWindowsPE == false || SystemInfos.SysInfo.RunningInWindowsPE == null) { return; } #endif RecoveryLogon reclogon = new RecoveryLogon(); reclogon.UCID = SystemInfos.SysInfo.UCID; reclogon.ContractID = SystemInfos.ContractID; reclogon.ContractPassword = SystemInfos.ContractPassword; string Check = SystemInfos.SysInfo.CPUName.Trim(); Check += SystemInfos.SysInfo.ComputerModel == "" ? "N/A" : SystemInfos.SysInfo.ComputerModel.Trim(); Check += SystemInfos.SysInfo.BIOS == "" ? "N/A" : SystemInfos.SysInfo.BIOS.Trim(); reclogon.MoreMachineHash = MD5Utilities.CalcMD5(Check); Network net = Utilities.NoConnectNetwork(); RecoveryData rd = net.GetRecoveryLogon(reclogon); if (rd == null) { return; } if (rd.Worked == false) { return; } string Registry = "Windows Registry Editor Version 5.00\r\n\r\n[HKEY_LOCAL_MACHINE\\SOFTWARE\\Fox\\SDC]\r\n\"ID\"=\"" + rd.MachineID + "\"\r\n\"PassID\"=\"" + rd.MachinePassword + "\""; try { File.WriteAllText(Environment.ExpandEnvironmentVariables("%SYSTEMROOT%\\Fox SDC MachinePW.reg"), Registry, Encoding.Unicode); } catch { } return; } if (UsePipeAction == false && UseScreenAction == false) { using (RegistryKey installer = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + VulpesBranding.MSIGUID, false)) { if (installer == null) { FoxEventLog.WriteEventLog("Missing installer key in HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + VulpesBranding.MSIGUID + ". This can lead to erratic behavoir of the program!", EventLogEntryType.Warning); } } if (CPP.SetToken() == false) { FoxEventLog.WriteEventLog("Cannot setup token - 0x" + Marshal.GetLastWin32Error().ToString("X") + " - " + new Win32Exception(Marshal.GetLastWin32Error()).Message, System.Diagnostics.EventLogEntryType.Error); return; } } if (UsePipeAction == true) { try { Process2ProcessCommClient.RunPipeClient(); } catch (Exception ee) { FoxEventLog.WriteEventLog("Pipe SEH " + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); } return; } if (UseScreenAction == true) { try { if (SystemInfos.CollectSystemInfo() != 0) { return; } MainScreenSystemClient.RunPipeClient(); } catch (Exception ee) { FoxEventLog.WriteEventLog("Screen Pipe SEH " + ee.ToString(), System.Diagnostics.EventLogEntryType.Error); } return; } if (SystemInfos.CollectSystemInfo() != 0) { #if !DEBUG || DEBUGSERVICE service.Stop(); #endif return; } RegistryData.InstallPath = AppPath; if (ApplicationCertificate.LoadCertificate() == false) { FoxEventLog.WriteEventLog("Cannot load certificate", System.Diagnostics.EventLogEntryType.Error); #if !DEBUG || DEBUGSERVICE service.Stop(); #endif return; } if (FilesystemData.LoadCertificates() == false) { #if !DEBUG || DEBUGSERVICE service.Stop(); #endif return; } if (FilesystemData.LoadPolicies() == false) { #if !DEBUG || DEBUGSERVICE service.Stop(); #endif return; } FilesystemData.LoadLocalPackageData(); FilesystemData.LoadLocalPackages(); FilesystemData.LoadUserPackageData(); FilesystemData.LoadEventLogList(); SyncPolicy.ApplyPolicy(SyncPolicy.ApplyPolicyFunction.ApplySystem); PipeCommunicationSRV.StartPipeSrv(); try { string p = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); if (p.EndsWith("\\") == false) { p += "\\"; } using (RegistryKey reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { reg.SetValue("FoxSDCAgent", p + "FoxSDC_Agent_UI.exe", RegistryValueKind.String); reg.SetValue("FoxSDCAgentApply", p + "FoxSDC_ApplyUserSettings.exe", RegistryValueKind.String); reg.Close(); } } catch { } try { using (RegistryKey reg = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true)) { object o = reg.GetValue("SoftwareSASGeneration", null); int rvalue = 0; if (o != null) { rvalue = Convert.ToInt32(o); } if (rvalue != 1 && rvalue != 3) { reg.SetValue("SoftwareSASGeneration", 1); } reg.Close(); } } catch { } Threads.StartAllThreads(); }
public static bool DoSyncPolicy() { RequestCertPolicyID = 0; RequestCertPolicyMessageID = 0; RequestCertPolicyCERData = null; List <Int64> ProcessedPolicies = new List <long>(); Network net; net = Utilities.ConnectNetwork(9); if (net == null) { return(false); } Status.UpdateMessage(9, "Downloading client settings"); FoxEventLog.VerboseWriteEventLog("Downloading client settings", System.Diagnostics.EventLogEntryType.Information); ClientSettings settings = net.GetClientSettings(); if (settings != null) { RegistryData.AdministratorName = settings.AdministratorName; RegistryData.MessageDisclaimer = settings.MessageDisclaimer; } Status.UpdateMessage(9, "Downloading policies"); FoxEventLog.VerboseWriteEventLog("Downloading policies", System.Diagnostics.EventLogEntryType.Information); PolicyObjectListSigned policieslistsigned = net.GetPoliciesForComputer(); List <PolicyObjectSigned> policies = policieslistsigned == null ? null : policieslistsigned.Items; if (policies == null) { FoxEventLog.VerboseWriteEventLog("Downloading policies - nix", System.Diagnostics.EventLogEntryType.Information); Status.UpdateMessage(9); net.CloseConnection(); return(true); } if (FilesystemData.LoadedCertificates.Count > 0) { bool SignatureOK = false; foreach (FilesystemCertificateData cer in FilesystemData.LoadedCertificates) { if (Certificates.Verify(policieslistsigned, cer.Certificate) == true) { SignatureOK = true; break; } } if (SignatureOK == false) { FoxEventLog.WriteEventLog("Invalid signature for PolicyList - no policies will be processed.", System.Diagnostics.EventLogEntryType.Error); net.CloseConnection(); return(true); } } if (RegistryData.Verbose == 1) { string data = "Got policy:\r\n"; foreach (PolicyObjectSigned obj in policies) { data += obj.Policy.Name + " [ID: " + obj.Policy.ID + " VER: " + obj.Policy.Version + "]\r\n"; } FoxEventLog.VerboseWriteEventLog("Downloading policies " + data, System.Diagnostics.EventLogEntryType.Information); } if (FilesystemData.LoadedCertificates.Count > 0) { foreach (PolicyObjectSigned obj in policies) { if (ApplicationCertificate.Verify(obj) == false) { FoxEventLog.WriteEventLog("One or more policies were tampered - no policies will be processed.", System.Diagnostics.EventLogEntryType.Error); net.CloseConnection(); return(true); } } } #region Certificate Checks foreach (PolicyObjectSigned obj in policies) { if (obj.Policy.Type == PolicyIDs.SignCertificate) { if (FilesystemData.ContainsPolicy(obj.Policy, false, false) == true) { continue; } PolicyObjectSigned objj = net.GetPolicyObjectSigned(obj.Policy.ID); //do not verify signing here - that won't work! - Fox PolicySigningCertificates Cert = JsonConvert.DeserializeObject <PolicySigningCertificates>(objj.Policy.Data); if (FilesystemData.ContainsLoadedCert(Convert.FromBase64String(Cert.UUCerFile)) == true) { continue; } bool sig = Certificates.Verify(Convert.FromBase64String(Cert.UUCerFile), Convert.FromBase64String(Cert.UUSignFile), InternalCertificate.Main); if (sig == false) { RequestCertPolicyID = objj.Policy.ID; RequestCertPolicyCERData = Convert.FromBase64String(Cert.UUCerFile); string CN = Certificates.GetCN(Convert.FromBase64String(Cert.UUCerFile)); if (CN == null) { FoxEventLog.WriteEventLog("Invalid certificate from server (Policy ID=" + objj.Policy.ID.ToString() + " Name=" + objj.Policy.Name + ")", System.Diagnostics.EventLogEntryType.Error); continue; } Status.RequestCertificateConfirm("The certificate with " + CN + " is not signed by Vulpes. This may that someone tampered the connection, or a false certificate is installed on the server.\nDo you want to continue, and trust this certificate?", RequestCertPolicyID); RequestCertPolicyMessageID = Status.MessageID; FoxEventLog.WriteEventLog("Got unsinged certificate (Policy ID=" + objj.Policy.ID.ToString() + " Name=" + objj.Policy.Name + " " + CN + ")", System.Diagnostics.EventLogEntryType.Warning); } else { string CN = Certificates.GetCN(Convert.FromBase64String(Cert.UUCerFile)); if (CN == null) { FoxEventLog.WriteEventLog("Invalid (Vulpes signed) certificate from server (Policy ID=" + objj.Policy.ID.ToString() + " Name=" + objj.Policy.Name + ")", System.Diagnostics.EventLogEntryType.Error); continue; } FilesystemData.InstallCertificate(Convert.FromBase64String(Cert.UUCerFile)); } } } #endregion if (FilesystemData.LoadedCertificates.Count > 0) { foreach (PolicyObjectSigned obj in policies) { if (FilesystemData.ContainsPolicy(obj.Policy, false, false) == true) { if (ProcessedPolicies.Contains(obj.Policy.ID) == false) { ProcessedPolicies.Add(obj.Policy.ID); } FilesystemData.UpdatePolicyOrder(obj.Policy, obj.Policy.Order); continue; } PolicyObjectSigned objj = net.GetPolicyObjectSigned(obj.Policy.ID); if (objj == null) { FoxEventLog.WriteEventLog("No data for policy - not applying (Policy ID=" + obj.Policy.ID.ToString() + " Name=" + obj.Policy.Name + ")", System.Diagnostics.EventLogEntryType.Error); continue; } if (ApplicationCertificate.Verify(objj) == false) { FoxEventLog.WriteEventLog("Policy was tampered - not applying (Policy ID=" + objj.Policy.ID.ToString() + " Name=" + objj.Policy.Name + ")", System.Diagnostics.EventLogEntryType.Error); continue; } if (FilesystemData.InstallPolicy(objj.Policy, obj.Policy.Order) == false) { continue; } if (ProcessedPolicies.Contains(obj.Policy.ID) == false) { ProcessedPolicies.Add(obj.Policy.ID); } } List <LoadedPolicyObject> RemovePol = new List <LoadedPolicyObject>(); foreach (LoadedPolicyObject lobj in FilesystemData.LoadedPolicyObjects) { if (ProcessedPolicies.Contains(lobj.PolicyObject.ID) == false) { RemovePol.Add(lobj); } } foreach (LoadedPolicyObject lobj in RemovePol) { FilesystemData.DeletePolicy(lobj); } } net.CloseConnection(); if (RequestCertPolicyID == 0) { Status.UpdateMessage(9); } FoxEventLog.VerboseWriteEventLog("Downloading policies - DONE", System.Diagnostics.EventLogEntryType.Information); return(true); }
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); }
static public bool UpdatePolicyOrder(PolicyObject obj, Int64 Order) { if (LoadedPolicyObjects == null) { LoadedPolicyObjects = new List <LoadedPolicyObject>(); } foreach (LoadedPolicyObject pol in LoadedPolicyObjects) { if (pol.PolicyObject.ID != obj.ID) { continue; } if (pol.PolicyObject.Name != obj.Name) { continue; } if (pol.PolicyObject.Type != obj.Type) { continue; } if (pol.PolicyObject.Version != obj.Version) { continue; } if (pol.PolicyObject.DT != obj.DT) { continue; } pol.PolicyObject.Order = Order; byte[] ppol = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(pol.PolicyObject)); byte[] sign = ApplicationCertificate.Sign(ppol); if (sign == null) { FoxEventLog.WriteEventLog("Cannot sign policy for saving", System.Diagnostics.EventLogEntryType.Error); return(false); } try { File.WriteAllBytes(pol.Filename, ppol); } catch { FoxEventLog.WriteEventLog("Cannot save policy", System.Diagnostics.EventLogEntryType.Error); return(false); } try { File.WriteAllBytes(pol.SignFilename, sign); } catch { try { File.Delete(pol.Filename); } catch { } FoxEventLog.WriteEventLog("Cannot save policy signature", System.Diagnostics.EventLogEntryType.Error); return(false); } break; } return(true); }
static public void LoadLocalPackages() { string PackagesFolder = SystemInfos.ProgramData + "Packages\\"; if (Directory.Exists(PackagesFolder) == false) { Directory.CreateDirectory(PackagesFolder); } if (LocalPackages == null) { LocalPackages = new List <PackagesToInstall>(); } try { if (File.Exists(PackagesFolder + "List.json") == true && File.Exists(PackagesFolder + "List.sign") == true) { byte[] list = File.ReadAllBytes(PackagesFolder + "List.json"); byte[] sign = File.ReadAllBytes(PackagesFolder + "List.sign"); if (ApplicationCertificate.Verify(list, sign) == false) { File.Delete(PackagesFolder + "List.json"); File.Delete(PackagesFolder + "List.sign"); FoxEventLog.WriteEventLog("Package list signature is invalid: deleting the files!", EventLogEntryType.Warning); LocalPackages = new List <PackagesToInstall>(); } else { LocalPackages = JsonConvert.DeserializeObject <List <PackagesToInstall> >(Encoding.UTF8.GetString(list)); } } } catch (Exception ee) { Debug.WriteLine(ee.ToString()); FoxEventLog.WriteEventLog("Error decoding package list data", EventLogEntryType.Warning); LocalPackages = new List <PackagesToInstall>(); } try { foreach (PackagesToInstall pkg in LocalPackages) { if (pkg.Filename != null) { if (File.Exists(PackagesFolder + pkg.Filename) == false) { FoxEventLog.WriteEventLog("Referenced package " + pkg.Filename + " does not exist", EventLogEntryType.Warning); pkg.Filename = null; } } if (pkg.MetaFilename != null) { if (File.Exists(PackagesFolder + pkg.MetaFilename) == false) { FoxEventLog.WriteEventLog("Referenced meta package " + pkg.Filename + " does not exist", EventLogEntryType.Warning); pkg.MetaFilename = null; } } } List <PackagesToInstall> RemovePackages = new List <PackagesToInstall>(); foreach (PackagesToInstall pkg in LocalPackages) { if (pkg.MetaFilename == null) { RemovePackages.Add(pkg); } } foreach (PackagesToInstall pkg in RemovePackages) { LocalPackages.Remove(pkg); if (pkg.MetaFilename != null) { File.Delete(PackagesFolder + pkg.MetaFilename); } if (pkg.Filename != null) { File.Delete(PackagesFolder + pkg.Filename); } } } catch (Exception ee) { Debug.WriteLine(ee.ToString()); FoxEventLog.WriteEventLog("Error processing package list data", EventLogEntryType.Error); LocalPackages = new List <PackagesToInstall>(); } }
static public bool LoadCertificates(bool Shutup = false) { LoadedCertificates = new List <FilesystemCertificateData>(); string CertFolder = SystemInfos.ProgramData + "Certificates\\"; if (Directory.Exists(CertFolder) == false) { Directory.CreateDirectory(CertFolder); } for (int i = 1; i < 100; i++) { if (File.Exists(CertFolder + "Certificate" + i.ToString("00") + ".cer") == false) { if (File.Exists(CertFolder + "Certificate" + i.ToString("00") + ".sign") == true) { FoxEventLog.WriteEventLog("Found lonely file \"Certificate" + i.ToString("00") + ".sign\" - deleting the file", System.Diagnostics.EventLogEntryType.Warning); File.Delete(CertFolder + "Certificate" + i.ToString("00") + ".sign"); } } else { if (File.Exists(CertFolder + "Certificate" + i.ToString("00") + ".sign") == false) { FoxEventLog.WriteEventLog("Found \"Certificate" + i.ToString("00") + ".cer\" but no signature - deleting the file", System.Diagnostics.EventLogEntryType.Warning); File.Delete(CertFolder + "Certificate" + i.ToString("00") + ".cer"); } else { FileInfo file; file = new FileInfo(CertFolder + "Certificate" + i.ToString("00") + ".cer"); if (file.Length > 5242880) { FoxEventLog.WriteEventLog("File \"Certificate" + i.ToString("00") + ".cer\" too large (>5MB) - deleting the files", System.Diagnostics.EventLogEntryType.Warning); File.Delete(CertFolder + "Certificate" + i.ToString("00") + ".cer"); File.Delete(CertFolder + "Certificate" + i.ToString("00") + ".sign"); continue; } file = new FileInfo(CertFolder + "Certificate" + i.ToString("00") + ".sign"); if (file.Length > 5242880) { FoxEventLog.WriteEventLog("File \"Certificate" + i.ToString("00") + ".sign\" too large (>5MB) - deleting the files", System.Diagnostics.EventLogEntryType.Warning); File.Delete(CertFolder + "Certificate" + i.ToString("00") + ".cer"); File.Delete(CertFolder + "Certificate" + i.ToString("00") + ".sign"); continue; } byte[] cert = File.ReadAllBytes(CertFolder + "Certificate" + i.ToString("00") + ".cer"); byte[] sign = File.ReadAllBytes(CertFolder + "Certificate" + i.ToString("00") + ".sign"); if (ApplicationCertificate.Verify(cert, sign) == false) { FoxEventLog.WriteEventLog("File \"Certificate" + i.ToString("00") + ".cer\" and \"Certificate" + i.ToString("00") + ".sign\" do not match - deleting the files", System.Diagnostics.EventLogEntryType.Warning); File.Delete(CertFolder + "Certificate" + i.ToString("00") + ".cer"); File.Delete(CertFolder + "Certificate" + i.ToString("00") + ".sign"); continue; } if (ContainsLoadedCert(cert) == true) { FoxEventLog.WriteEventLog("File \"Certificate" + i.ToString("00") + ".cer\" this certificate is already loaded from a different file - deleting the files", System.Diagnostics.EventLogEntryType.Warning); File.Delete(CertFolder + "Certificate" + i.ToString("00") + ".cer"); File.Delete(CertFolder + "Certificate" + i.ToString("00") + ".sign"); continue; } Debug.WriteLine(CertFolder + "Certificate" + i.ToString("00") + ".cer loaded"); FilesystemCertificateData fscer = new FilesystemCertificateData(); fscer.FSFilename = "Certificate" + i.ToString("00") + ".cer"; fscer.Certificate = cert; LoadedCertificates.Add(fscer); if (Shutup == false) { FoxEventLog.WriteEventLog("Certificate " + Certificates.GetCN(cert) + " loaded", System.Diagnostics.EventLogEntryType.Information); } } } } return(true); }
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); }