Esempio n. 1
0
        public bool ApplyUserSettings(string Filename, List <byte[]> CerCertificates, out string ErrorText, out PKGStatus res)
        {
            ErrorText = "";
            res       = PKGStatus.Unknown;

            if (CerCertificates == null)
            {
                CerCertificates = new List <byte[]>();
            }
            CerCertificates.Add(Resources.Vulpes_Main);

            PKGRunningPackageData RunningPKG;
            PKGRecieptData        RunningReciept = new PKGRecieptData();

            RunningReciept.HeaderID = "FoxRecieptScriptV1";

            if (InitPackageZipFile(Filename, CerCertificates, true, out ErrorText, out RunningPKG, "") == false)
            {
                return(false);
            }

            using (FileStream ZipFile = File.Open(Filename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (ZipArchive ZipArch = new ZipArchive(ZipFile, ZipArchiveMode.Read, true, Encoding.UTF8))
                {
                    RunningReciept.scriptsource = RunningPKG.scriptsource;
                    RunningReciept.PackageID    = RunningPKG.PackageID;
                    RunningReciept.InstalledOn  = DateTime.Now;
                    RunningReciept.Description  = RunningPKG.Description;

                    try
                    {
                        res = RunningPKG.script.ApplyUserSettings(RunningPKG);
                    }
                    catch (Exception ee)
                    {
                        Debug.WriteLine(ee.ToString());
                        ErrorText = "Script Error:\r\n" + ee.ToString();
                        return(false);
                    }
                }
            }

            return(true);
        }
Esempio n. 2
0
        public bool InstallPackage(string Filename, List <byte[]> CerCertificates, InstallMode Mode, bool ZipIsMetaOnly,
                                   out string ErrorText, out PKGStatus res, out PKGRecieptData Reciept, string OtherDLL = "")
        {
            ErrorText = "";
            Reciept   = null;
            PKGRunningPackageData RunningPKG;
            PKGRecieptData        RunningReciept = new PKGRecieptData();

            RunningReciept.HeaderID = "FoxRecieptScriptV1";
            res = PKGStatus.Unknown;

            if (CerCertificates == null)
            {
                CerCertificates = new List <byte[]>();
            }
            CerCertificates.Add(Resources.Vulpes_Main);

            if (InitPackageZipFile(Filename, CerCertificates, ZipIsMetaOnly, out ErrorText, out RunningPKG, OtherDLL) == false)
            {
                return(false);
            }

            RunningPKG.ApplicationPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            if (RunningPKG.ApplicationPath.EndsWith("\\") == false)
            {
                RunningPKG.ApplicationPath += "\\";
            }

            using (FileStream ZipFile = File.Open(Filename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (ZipArchive ZipArch = new ZipArchive(ZipFile, ZipArchiveMode.Read, true, Encoding.UTF8))
                {
                    RunningReciept.scriptsource = RunningPKG.scriptsource;
                    RunningReciept.PackageID    = RunningPKG.PackageID;
                    RunningReciept.InstalledOn  = DateTime.Now;
                    RunningReciept.Description  = RunningPKG.Description;

                    if (Mode == InstallMode.TestPackageOnly || Mode == InstallMode.ApplyUserSettingsTest)
                    {
                        return(true);
                    }

                    if (ZipIsMetaOnly == true)
                    {
                        if (Mode == InstallMode.Install || Mode == InstallMode.Update)
                        {
                            Debug.WriteLine("Cannot install/update a Meta-Package");
                            ErrorText = "Cannot install/update a Meta-Package";
                            return(false);
                        }
                    }

                    try
                    {
                        UpdateText("Running pre install script");

                        PKGInstallState inst = PKGInstallState.NotSet;
                        switch (Mode)
                        {
                        case InstallMode.Install:
                            inst = PKGInstallState.Install; break;

                        case InstallMode.Test:
                            inst = PKGInstallState.Test; break;

                        case InstallMode.TestPackageOnly:
                            inst = PKGInstallState.Test; break;

                        case InstallMode.Update:
                            inst = PKGInstallState.Update; break;

                        case InstallMode.UpdateTest:
                            inst = PKGInstallState.UpdateTest; break;
                        }

                        res = RunningPKG.script.CheckInstallationStatus(RunningPKG, inst);
                        if (res == PKGStatus.DependencyFailed)
                        {
                            RequiredDependencies = RunningPKG.script.GetDependencies(RunningPKG);
                            ErrorText            = RunningPKG.ErrorText;
                            return(false);
                        }
                        if (res == PKGStatus.NotNeeded)
                        {
                            ErrorText = RunningPKG.ErrorText;
                            return(true);  //no installation
                        }
                        if (res == PKGStatus.Failed)
                        {
                            ErrorText = RunningPKG.ErrorText;
                            return(false);
                        }

                        if (Mode == InstallMode.Test || Mode == InstallMode.UpdateTest)
                        {
                            ErrorText = "";
                            res       = PKGStatus.Success;
                            return(true);
                        }

                        res = RunningPKG.script.PreInstall(RunningPKG);
                        if (res != PKGStatus.Success)
                        {
                            ErrorText = RunningPKG.ErrorText;
                            return(false);
                        }
                    }
                    catch (Exception ee)
                    {
                        Debug.WriteLine(ee.ToString());
                        ErrorText = "Script Error:\r\n" + ee.ToString();
                        return(false);
                    }

                    RunningReciept.CreatedFolders = new List <string>();
                    RunningReciept.InstalledFiles = new List <string>();

                    int lasterr;
                    CommonUtilities.ResetLastError();
                    List <string> DeleteFiles = new List <string>();

                    using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
                    {
                        Alphaleonis.Win32.Filesystem.KernelTransaction kt = new Alphaleonis.Win32.Filesystem.KernelTransaction();
                        lasterr = Marshal.GetLastWin32Error();
                        if (lasterr != 0)
                        {
                            ErrorText = "Cannot start transaction 0x" + lasterr.ToString("X");
                            return(false);
                        }

                        foreach (PKGFile file in RunningPKG.Files)
                        {
                            string fldr = Environment.ExpandEnvironmentVariables(file.FolderName);
                            if (fldr.EndsWith("\\") == false)
                            {
                                fldr += "\\";
                            }
                            string fullfilename = fldr + file.FileName;
                            string renamedfile  = fldr + "FOX-" + CommonUtilities.NewGUID + ".PENDING";

                            bool RestartMove = false;

                            try
                            {
                                if (Alphaleonis.Win32.Filesystem.File.ExistsTransacted(kt, fullfilename) == true)
                                {
                                    try
                                    {
                                        Alphaleonis.Win32.Filesystem.File.MoveTransacted(kt, fullfilename, renamedfile);

                                        lasterr = Marshal.GetLastWin32Error();
                                        if (lasterr != 0)
                                        {
                                            string SubErr;
                                            RollbackFiles(RunningPKG, out SubErr);
                                            kt.Rollback();
                                            ErrorText = "Cannot move file " + fullfilename + " => " + renamedfile + " 0x" + lasterr.ToString("X") + "\r\n" + SubErr;
                                            return(false);
                                        }
                                        DeleteFiles.Add(renamedfile);
                                    }
                                    catch
                                    {
                                        RestartMove = true;
                                        string tmp = fullfilename;
                                        fullfilename = renamedfile;
                                        renamedfile  = tmp;
                                    }
                                }
                            }
                            catch (Exception ee)
                            {
                                Debug.WriteLine(ee.ToString());

                                UpdateText("Rolling back");

                                string SubErr;
                                RollbackFiles(RunningPKG, out SubErr);
                                kt.Rollback();
                                ErrorText = "Move Error:\r\n" + ee.ToString() + "\r\n" + SubErr;
                                return(false);
                            }

                            UpdateText("Installing " + fullfilename);
                            try
                            {
                                string mkdir = Path.GetDirectoryName(fldr);
                                if (Alphaleonis.Win32.Filesystem.Directory.ExistsTransacted(kt, mkdir) == false)
                                {
                                    try
                                    {
                                        Alphaleonis.Win32.Filesystem.Directory.CreateDirectoryTransacted(kt, mkdir);
                                        RunningReciept.CreatedFolders.Add(fldr);

                                        //lasterr = Marshal.GetLastWin32Error();
                                        //if (lasterr != 0)
                                        //{
                                        //    string SubErr;
                                        //    RollbackFiles(RunningPKG, out SubErr);
                                        //    kt.Rollback();
                                        //    ErrorText = "Cannot create folder " + mkdir + " 0x" + lasterr.ToString("X") + "\r\n" + SubErr;
                                        //    return (false);
                                        //}
                                    }
                                    catch (Exception ee)
                                    {
                                        Debug.WriteLine(ee.ToString());
                                    }
                                }
                            }
                            catch (Exception ee)
                            {
                                Debug.WriteLine(ee.ToString());
                            }

                            CommonUtilities.ResetLastError();
                            try
                            {
                                if (OutputToFile(kt, file.SrcFile, fullfilename, ZipArch, out ErrorText) == false)
                                {
                                    UpdateText("Rolling back");

                                    string SubErr;
                                    RollbackFiles(RunningPKG, out SubErr);
                                    kt.Rollback();
                                    ErrorText += "\r\n" + SubErr;
                                    return(false);
                                }
                            }
                            catch (Exception ee)
                            {
                                Debug.WriteLine(ee.ToString());

                                UpdateText("Rolling back");

                                string SubErr;
                                RollbackFiles(RunningPKG, out SubErr);
                                kt.Rollback();
                                ErrorText = "File Output Error:\r\n" + ee.ToString() + "\r\n" + SubErr;
                                return(false);
                            }

                            RunningReciept.InstalledFiles.Add(RestartMove == true ? renamedfile : fullfilename);

                            if (RestartMove == true)
                            {
                                try
                                {
                                    Alphaleonis.Win32.Filesystem.File.CopyMoveCore(false, kt, renamedfile, null, true, null, Alphaleonis.Win32.Filesystem.MoveOptions.DelayUntilReboot, null, null, null, Alphaleonis.Win32.Filesystem.PathFormat.RelativePath);
                                    Alphaleonis.Win32.Filesystem.File.CopyMoveCore(false, kt, fullfilename, renamedfile, true, null, Alphaleonis.Win32.Filesystem.MoveOptions.DelayUntilReboot, null, null, null, Alphaleonis.Win32.Filesystem.PathFormat.RelativePath);

                                    lasterr = Marshal.GetLastWin32Error();
                                    if (lasterr != 0)
                                    {
                                        string SubErr;
                                        RollbackFiles(RunningPKG, out SubErr);
                                        kt.Rollback();
                                        ErrorText = "Cannot pending-move file " + fullfilename + " => " + renamedfile + " 0x" + lasterr.ToString("X") + "\r\n" + SubErr;
                                        return(false);
                                    }
                                }
                                catch (Exception ee)
                                {
                                    Debug.WriteLine(ee.ToString());

                                    UpdateText("Rolling back");

                                    string SubErr;
                                    RollbackFiles(RunningPKG, out SubErr);
                                    kt.Rollback();
                                    ErrorText = "Restart Move Error:\r\n" + ee.ToString() + "\r\n" + SubErr;
                                    return(false);
                                }
                            }
                        }

                        try
                        {
                            res = RunningPKG.script.PostInstall(RunningPKG);
                            if (res != PKGStatus.Success)
                            {
                                ErrorText = "PostInstall failed.";
                                UpdateText("Rolling back");
                                RollbackFiles(RunningPKG, out ErrorText);
                                kt.Rollback();
                                return(false);
                            }
                        }
                        catch (Exception ee)
                        {
                            Debug.WriteLine(ee.ToString());

                            UpdateText("Rolling back");

                            string SubErr;
                            RollbackFiles(RunningPKG, out SubErr);
                            kt.Rollback();
                            ErrorText = "PostInstall Script Error:\r\n" + ee.ToString() + "\r\n" + SubErr;
                            return(false);
                        }

                        try
                        {
                            kt.Commit();
                        }
                        catch (Exception ee)
                        {
                            Debug.WriteLine(ee.ToString());

                            UpdateText("Rolling back");

                            string SubErr;
                            RollbackFiles(RunningPKG, out SubErr);
                            kt.Rollback();
                            ErrorText = "Commit Error:\r\n" + ee.ToString() + "\r\n" + SubErr;
                            return(false);
                        }
                    }

                    if (RunningPKG.NoReciept == false)
                    {
                        Reciept = RunningReciept;
                    }
                    else
                    {
                        Reciept = null;
                    }

                    //Clean up mess here
                    foreach (string file in DeleteFiles)
                    {
                        try
                        {
                            File.Delete(file);
                        }
                        catch
                        {
                            CommonUtilities.PendingMove(file, null);
                        }
                    }

                    res = PKGStatus.Success;
                }
            }
            return(true);
        }
Esempio n. 3
0
        public bool PackageInfo(string Filename, List <byte[]> CerCertificates, out string ErrorText)
        {
            ErrorText = "";
            bool ScriptSignFailed = false;

            UpdateText("Initializing");
            if (CerCertificates == null)
            {
                CerCertificates = new List <byte[]>();
            }
            CerCertificates.Add(Resources.Vulpes_Main);

            try
            {
                if (File.Exists(Filename) == false)
                {
                    ErrorText = "File does not exist";
                    return(false);
                }

                using (FileStream ZipFile = File.Open(Filename, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (ZipArchive ZipArch = new ZipArchive(ZipFile, ZipArchiveMode.Read, true, Encoding.UTF8))
                    {
                        ZipArchiveEntry entry;
                        entry = ZipArch.GetEntry("Script.json");
                        if (entry == null)
                        {
                            ZipFile.Close();
                            ErrorText = "Missing Script.json";
                            return(false);
                        }

                        if (TestSignatureBySignFile("Script.json", ZipArch, CerCertificates) == false)
                        {
                            ScriptSignFailed = true;
                        }

                        byte[] packagedata;

                        packagedata = GetZipData(entry);
                        PKGRootData Package;
                        string      script = Encoding.UTF8.GetString(packagedata);
                        try
                        {
                            Package = JsonConvert.DeserializeObject <PKGRootData>(script);
                            if (Package.HeaderID != "FoxPackageScriptV1")
                            {
                                ErrorText = "Script.json is not the format wanted (Header)!";
                                ZipFile.Close();
                                return(false);
                            }
                        }
                        catch (Exception ee)
                        {
                            Debug.WriteLine(ee.ToString());
                            ErrorText = "Script.json is not the format wanted (Format)!";
                            ZipFile.Close();
                            return(false);
                        }

                        if (CheckScript(Package, ZipArch, false, false, out ErrorText) == false)
                        {
                            return(false);
                        }

                        ErrorText  = "Package Details:\n";
                        ErrorText += "Name:         " + Package.Title + "\n";
                        ErrorText += "ID:           " + Package.PackageID + "\n";
                        ErrorText += "Description:  " + Package.Description + "\n";
                        ErrorText += "Version ID:   " + Package.VersionID.ToString() + "\n";
                        if (ScriptSignFailed == true)
                        {
                            ErrorText += "!QUICK SIGNATURE CHECK FAILED!\n";
                        }

                        ZipFile.Close();

                        PackageInfoData = Package;
                    }
                }
            }
            catch (Exception ee)
            {
                Debug.WriteLine(ee.ToString());
                ErrorText = "Internal error, likely a corrupted ZIP file";
                return(false);
            }
            return(true);
        }