Exemple #1
0
        //
        //this function generates an XML settings file by serializing a CodewordSettingsTemplate
        //object, which simply stores the values of all form elements.
        //
        //argument 'silent' is optional and only used internally to generate the settings file
        //that the deployed agent uses.  using this option suppresses the dialog box and automatically
        //saves the settings file to CwAgentConfiguration.xml
        //
        public static void SaveSettingsXML(string filename, string[] ElementNames, string[] ElementValues)
        {
            //-----------------------------------------------
            //            GENERATE SERIALIZABLE
            //              DATA STRUCTURES
            //-----------------------------------------------
            CodewordSettingsTemplate cwSettings = new CodewordSettingsTemplate();
            cwSettings.FormElementNames = ElementNames;
            cwSettings.FormElementValues = ElementValues;

            //-----------------------------------------------
            //            ENCRYPT SENSITIVE FIELDS
            //-----------------------------------------------
            //encrypt any sensitive items before serializing to an XML file
            //
            string[] EncryptElementValues = new string[]{ 
                "Reporting_Auth_UserName","Reporting_Auth_Password","AgentPFXPassword","Reporting_Archive_Password"};

            int count = 0;
            foreach (string formElementName in cwSettings.FormElementNames)
            {
                //once we find the element value for one of the element names, encrypt it.
                if (Array.IndexOf(EncryptElementValues, formElementName) >= 0)
                {
                    string plainText = cwSettings.FormElementValues[count];
                    cwSettings.FormElementValues[count] = CwBasicEncryption.Encrypt(plainText);
                }
                count++;
            }

            //serialize the objects into an XML document
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(CodewordSettingsTemplate));
                TextWriter writer = new StreamWriter(filename);
                serializer.Serialize(writer, cwSettings);
                writer.Close();
            }
            catch (Exception e)
            {
                throw new Exception("Serialization error:  " + e.Message);
            }
        }
Exemple #2
0
        public CodewordSettingsTemplate LoadSettingsXML(string filename)
        {
            //-----------------------------------------------
            //              DESERIALIZE XML FILE
            //-----------------------------------------------
            XmlSerializer serializer;

            //
            //deserialize the XML settings file into an instantation of the CwSettings class
            try
            {
                serializer = new XmlSerializer(typeof(CodewordSettingsTemplate));
            }
            catch (Exception ex)
            {
                throw new Exception("Failed processing XML from settings file.\n\n" + ex.Message);
            }

            //handle unknown XML nodes/attributes
            serializer.UnknownNode += new XmlNodeEventHandler(ImportErrorUnknownXMLNode);
            serializer.UnknownAttribute += new XmlAttributeEventHandler(ImportErrorUnknownXMLAttribute);

            FileStream fstream;

            //attempt to read XML document
            try
            {
                fstream = new FileStream(filename, FileMode.Open);
            }
            catch (Exception ex)
            {
                throw new Exception("File read error:  " + ex.Message);
            }

            //try to deserialize
            CodewordSettingsTemplate cst;

            try
            {
                //restore the object's state with data from the XML document
                cst = (CodewordSettingsTemplate)serializer.Deserialize(fstream);
            }
            catch (Exception ex)
            {
                fstream.Close();
                throw new Exception("Deserialization error:  " + ex.Message);
            }

            //make sure there are just as many form names as there are form values
            if (cst.FormElementNames.Length != cst.FormElementValues.Length)
            {
                fstream.Close();
                throw new Exception("Error:  length mismatch!");
            }

            fstream.Close();

            //-----------------------------------------------
            //            DECRYPT SENSITIVE FIELDS
            //-----------------------------------------------
            //decrypt any sensitive items before deserializing into the form
            //
            string[] EncryptElementValues = new string[]{ 
                "Reporting_Auth_UserName","Reporting_Auth_Password","AgentPFXPassword","Reporting_Archive_Password"};

            int count = 0;
            foreach (string formElementName in cst.FormElementNames)
            {
                //once we find the element value for one of the element names, encrypt it.
                if (Array.IndexOf(EncryptElementValues, formElementName) >= 0)
                {
                    string cypherText = cst.FormElementValues[count];
                    cst.FormElementValues[count] = CwBasicEncryption.GetStringFromSecureString(CwBasicEncryption.Decrypt(cypherText));
                }
                count++;
            }

            return cst;
        }