Example #1
0
        public PKGStatus PreInstall(PKGRunningPackageData Package)
        {
            PKGStatus p = CheckReq(Package);

            if (p != PKGStatus.Success)
            {
                Package.ErrorText = "Not needed";
            }

            return(p);
        }
Example #2
0
        public PKGStatus CheckInstallationStatus(PKGRunningPackageData Package, PKGInstallState state)
        {
            State = state;
            Package.SetInstallPath(InstallPath);
            Package.ErrorText = "";

            PKGStatus p = CheckReq(Package);

            if (p != PKGStatus.Success)
            {
                Package.ErrorText = "Not needed";
            }

            return(p);
        }
Example #3
0
        static int Main(string[] args)
        {
            Console.WriteLine("Fox Installer");
            Console.WriteLine("");
            if (args.Length < 2)
            {
                Help();
                return(1);
            }

            ComputerCertificate ccert = new ComputerCertificate();

            if (ccert.GetCertificate() == false)
            {
                Console.WriteLine("Cannot load computer certificate from Fox SDC");
#if DEBUG
                Console.WriteLine("Press any key . . .");
                Console.ReadKey(true);
#endif
                return(2);
            }

            PackageInstaller pkgi      = new PackageInstaller();
            PKGStatus        pkgres    = PKGStatus.NotNeeded;
            PKGRecieptData   reciept   = null;
            string           ErrorText = "";
            bool             res       = false;
            List <byte[]>    cer       = new List <byte[]>();

            switch (args[0].ToLower().Trim())
            {
            case "-install":
                Console.WriteLine("Installing " + args[1].Trim());
                cer = LoadCERFiles(args);
                res = pkgi.InstallPackage(args[1].Trim(), cer, PackageInstaller.InstallMode.Install, false, out ErrorText, out pkgres, out reciept);
                break;

            case "-test":
                Console.WriteLine("Testing " + args[1].Trim());
                cer = LoadCERFiles(args);
                res = pkgi.InstallPackage(args[1].Trim(), cer, PackageInstaller.InstallMode.Test, false, out ErrorText, out pkgres, out reciept);
                break;

            case "-info":
                cer = LoadCERFiles(args);
                res = pkgi.PackageInfo(args[1].Trim(), cer, out ErrorText);
                Console.WriteLine(ErrorText);
#if DEBUG
                Console.WriteLine("Press any key . . .");
                Console.ReadKey(true);
#endif
                return(res == true ? 0 : 1);

            default:
                Console.WriteLine("Invalid argurments");
#if DEBUG
                Console.WriteLine("Press any key . . .");
                Console.ReadKey(true);
#endif
                return(1);
            }

            Console.WriteLine("Status: " + pkgres.ToString());

            if (res == false)
            {
                Console.WriteLine(ErrorText);
            }
            else
            {
                if (reciept != null)
                {
                    string recppath = Path.GetDirectoryName(args[1].Trim());
                    if (recppath.EndsWith("\\") == false)
                    {
                        recppath += "\\";
                    }
                    string recieptfile     = recppath + Path.GetFileNameWithoutExtension(args[1].Trim()) + ".foxrecp";
                    string recieptfilesign = recppath + Path.GetFileNameWithoutExtension(args[1].Trim()) + ".sign";
#if DEBUG
                    string recps = JsonConvert.SerializeObject(reciept, Formatting.Indented);
#else
                    string recps = JsonConvert.SerializeObject(reciept);
#endif
                    File.WriteAllText(recieptfile, recps, Encoding.UTF8);
                    byte[] sign = ccert.Sign(Encoding.UTF8.GetBytes(recps));
                    File.WriteAllBytes(recieptfilesign, sign);
                }
            }
#if DEBUG
            Console.WriteLine("Press any key . . .");
            Console.ReadKey(true);
#endif
            return(0);
        }
Example #4
0
        public static bool InvokeInstallPackage(string Filename, List <byte[]> CerCertificates, InstallMode Mode, bool ZipIsMetaOnly,
                                                out string ErrorText, out PKGStatus res, out PKGRecieptData Reciept, string OtherDLL = "")
        {
            ErrorText = "Internal issues";
            res       = PKGStatus.Failed;
            Reciept   = null;

            DataHInstallPackageTODO inst = new DataHInstallPackageTODO();

            inst.Filename        = Filename;
            inst.CerCertificates = CerCertificates;
            inst.Mode            = Mode;
            inst.ZipIsMetaOnly   = ZipIsMetaOnly;
            inst.OtherDLL        = OtherDLL;

            Process2ProcessComm p2p = new Process2ProcessComm();

            p2p.SetTODO("INSTALL", inst);
            if (p2p.StartPipe() == false)
            {
                FoxEventLog.WriteEventLog("Cannot start P2PC for INSTALL " + p2p.GetGUID(), EventLogEntryType.Error);
                return(false);
            }

            Process p = new Process();

            p.StartInfo.Arguments       = "-pipeaction " + p2p.GetGUID();
            p.StartInfo.FileName        = Assembly.GetExecutingAssembly().Location;
            p.StartInfo.UseShellExecute = false;
            if (p.Start() == false)
            {
                FoxEventLog.WriteEventLog("Cannot start P2PC Process for INSTALL " + p2p.GetGUID(), EventLogEntryType.Error);
                p2p.ClosePipe();
                return(false);
            }
            p.WaitForExit();

            DataHInstallPackageResult ores = p2p.GetResult <DataHInstallPackageResult>();

            p2p.ClosePipe();
            if (ores == null)
            {
                FoxEventLog.WriteEventLog("P2PC didn't return any data for INSTALL " + p2p.GetGUID(), EventLogEntryType.Error);
                return(false);
            }

            ErrorText = ores.ErrorText;
            res       = ores.res;
            Reciept   = ores.Reciept;
            if (ores.TempDLLFilename != null)
            {
                try
                {
                    File.Delete(ores.TempDLLFilename);
                }
                catch
                {
                }
            }

            return(ores.Return);
        }
Example #5
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);
        }
Example #6
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);
        }