Example #1
0
        public string GetNameForExport(bool is64bit)
        {
            var expectedClient = new ServiceVersion().Get(1).LatestClientVersion;

            var newVersion     = expectedClient.Split('.');
            var v              = string.Join(".", newVersion.Take(newVersion.Length - 1));
            var type           = is64bit ? "-x64.msi" : "-x86.msi";
            var outputFileName = $"Toec-{v}{type}";

            return(outputFileName);
        }
Example #2
0
        public byte[] UpdateMsis(bool is64bit)
        {
            var expectedClient = new ServiceVersion().Get(1).LatestClientVersion;

            if (string.IsNullOrEmpty(expectedClient))
            {
                Logger.Error("Cannot Create MSI.  Unknown Expected Toec Version");
                return(null);
            }


            var type              = is64bit ? "-x64.msi" : "-x86.msi";
            var basePath          = Path.Combine(HttpContext.Current.Server.MapPath("~"), "private", "agent");
            var stockFileFullPath = Path.Combine(basePath, $"Toec-{expectedClient}{type}");

            if (!File.Exists(stockFileFullPath))
            {
                Logger.Debug("Cannot Create MSI.  Could Not Locate Stock MSI");
                return(null);
            }

            var ca = new ServiceCertificate().GetCAPublic();

            if (ca == null)
            {
                Logger.Debug("Cannot Create MSI.  Certificate Chain Must First Be Created. ");
                return(null);
            }

            var newVersion         = expectedClient.Split('.');
            var v                  = string.Join(".", newVersion.Take(newVersion.Length - 1));
            var outputFileName     = $"Toec-{v}{type}";
            var outputFileFullPath = Path.Combine(basePath, outputFileName);

            try
            {
                File.Delete(outputFileFullPath);
            }
            catch { //ignored
            }

            try
            {
                File.Copy(stockFileFullPath, outputFileFullPath);
            }
            catch (Exception ex)
            {
                Logger.Error("Could Not Create MSI.");
                Logger.Error(ex.Message);
                return(null);
            }

            Stream   stream     = new MemoryStream(ca);
            Database database   = null;
            View     serverKey  = null;
            View     thumbprint = null;
            View     comServers = null;
            View     cert       = null;
            Record   rec        = null;

            GetMsiArgs();
            using (database = new Database(outputFileFullPath, DatabaseOpenMode.Transact))
            {
                try
                {
                    serverKey = database.OpenView(String.Format("INSERT INTO Property (Property, Value) VALUES ('{0}', '{1}')", "SERVER_KEY", _serverKey));
                    serverKey.Execute();
                    serverKey.Close();

                    comServers = database.OpenView(String.Format("INSERT INTO Property (Property, Value) VALUES ('{0}', '{1}')", "COM_SERVERS", _comServers));
                    comServers.Execute();
                    comServers.Close();

                    thumbprint = database.OpenView(String.Format("INSERT INTO Property (Property, Value) VALUES ('{0}', '{1}')", "CA_THUMBPRINT", _thumbprint));
                    thumbprint.Execute();
                    thumbprint.Close();

                    cert = database.OpenView("UPDATE `Binary` SET `Data` = ? WHERE `Name` = 'ToemsCA.Binary'");
                    rec  = new Record(1);
                    rec.SetStream(1, stream);
                    cert.Execute(rec);
                    cert.Close();

                    database.Commit();
                }
                catch (Exception ex)
                {
                    Logger.Error("Could Not Create Msi.");
                    Logger.Error(ex.Message);
                    return(null);
                }
                finally
                {
                    if (rec != null)
                    {
                        rec.Close();
                    }
                    if (serverKey != null)
                    {
                        serverKey.Close();
                    }
                    if (thumbprint != null)
                    {
                        thumbprint.Close();
                    }
                    if (comServers != null)
                    {
                        comServers.Close();
                    }
                    if (cert != null)
                    {
                        cert.Close();
                    }
                    if (database != null)
                    {
                        database.Close();
                    }
                }
            }

            var file = File.ReadAllBytes(outputFileFullPath);

            return(file);
        }