Exemplo n.º 1
0
 internal void Validate()
 {
     if (string.IsNullOrEmpty(this.Name) || string.IsNullOrEmpty(this.Version))
     {
         throw new DlpPolicyParsingException(Strings.DlpPolicyXmlMissingElements);
     }
     if (new Version(this.Version) > DlpUtils.MaxSupportedVersion)
     {
         throw new DlpPolicyParsingException(Strings.DlpPolicyVersionUnsupported);
     }
     if (this.Keywords.Any(new Func <string, bool>(string.IsNullOrEmpty)))
     {
         throw new DlpPolicyParsingException(Strings.DlpPolicyContainsEmptyKeywords);
     }
     DlpPolicyTemplateMetaData.ValidateFieldSize("name", this.Name, 64);
     DlpPolicyTemplateMetaData.ValidateFieldSize("version", this.Version, 16);
     DlpPolicyTemplateMetaData.ValidateFieldSize("contentVersion", this.ContentVersion, 16);
     DlpPolicyTemplateMetaData.ValidateFieldSize("publisherName", this.PublisherName, 256);
     DlpPolicyTemplateMetaData.ValidateFieldSize("description", this.Description, 1024);
     this.Keywords.ForEach(delegate(string x)
     {
         DlpPolicyTemplateMetaData.ValidateFieldSize("keyword", x, 64);
     });
     this.PolicyCommands.ForEach(delegate(string command)
     {
         DlpPolicyTemplateMetaData.ValidateFieldSize("commandBlock", command, 4096);
     });
     this.PolicyCommands.ForEach(delegate(string command)
     {
         DlpPolicyTemplateMetaData.ValidateCmdletParameters(command);
     });
 }
Exemplo n.º 2
0
        private DlpPolicyTemplateMetaData LoadDlpPolicyFromInstalledTemplate()
        {
            DlpPolicyTemplateMetaData dlpPolicyTemplateMetaData = DlpUtils.LoadOutOfBoxDlpTemplate(this.taskObject.DomainController, this.taskObject.Template);

            if (dlpPolicyTemplateMetaData == null)
            {
                this.taskObject.WriteError(new ArgumentException(Strings.ErrorDlpPolicyTemplateNotFound(this.taskObject.Template)), ErrorCategory.InvalidArgument, "Template");
            }
            return(dlpPolicyTemplateMetaData);
        }
Exemplo n.º 3
0
 internal DlpPolicyMetaData(DlpPolicyTemplateMetaData dlpTemplate, CultureInfo culture = null)
 {
     this.Name           = DlpPolicyTemplateMetaData.GetLocalizedStringValue(dlpTemplate.LocalizedNames, culture);
     this.Description    = DlpPolicyTemplateMetaData.GetLocalizedStringValue(dlpTemplate.LocalizedDescriptions, culture);
     this.ContentVersion = dlpTemplate.ContentVersion;
     this.Version        = dlpTemplate.Version;
     this.Mode           = dlpTemplate.Mode;
     this.State          = dlpTemplate.State;
     this.PublisherName  = dlpTemplate.PublisherName;
     this.ImmutableId    = Guid.NewGuid();
     this.Keywords       = (from keyword in dlpTemplate.LocalizedKeywords
                            select DlpPolicyTemplateMetaData.GetLocalizedStringValue(keyword, culture)).ToArray <string>().ToList <string>();
     this.PolicyCommands = dlpTemplate.PolicyCommands;
 }
Exemplo n.º 4
0
        public override void Validate()
        {
            Exception exception = null;

            this.ValidateParameterSets();
            DlpPolicyTemplateMetaData dlpPolicyTemplateMetaData = null;

            if (this.taskObject.Fields.IsModified("TemplateData"))
            {
                dlpPolicyTemplateMetaData = this.LoadDlpPolicyFromCustomTemplateData();
            }
            if (this.taskObject.Fields.IsModified("Template"))
            {
                dlpPolicyTemplateMetaData = this.LoadDlpPolicyFromInstalledTemplate();
            }
            if (this.taskObject.Fields.IsModified("Name"))
            {
                this.dlpPolicy.Name = this.taskObject.Name;
            }
            if (dlpPolicyTemplateMetaData != null)
            {
                this.dlpPolicy = new DlpPolicyMetaData(dlpPolicyTemplateMetaData, this.taskObject.CommandRuntime.Host.CurrentCulture);
                if (!string.IsNullOrEmpty(this.taskObject.Name))
                {
                    this.dlpPolicy.Name = this.taskObject.Name;
                }
                this.dlpPolicy.PolicyCommands = NewDlpPolicyImpl.ParameterizeCmdlets(this.dlpPolicy.Name, dlpPolicyTemplateMetaData.PolicyCommands, dlpPolicyTemplateMetaData.RuleParameters, this.taskObject.Parameters, new NewDlpPolicy.WarningWriterDelegate(this.taskObject.WriteWarning), out exception);
                this.WriteParameterErrorIfExceptionOccurred(exception, "Parameters");
                this.dlpPolicy.PolicyCommands = DlpPolicyTemplateMetaData.LocalizeCmdlets(this.dlpPolicy.PolicyCommands, dlpPolicyTemplateMetaData.LocalizedPolicyCommandResources, this.taskObject.CommandRuntime.Host.CurrentCulture).ToList <string>();
                this.dlpPolicy.PolicyCommands.ForEach(delegate(string command)
                {
                    DlpPolicyTemplateMetaData.ValidateCmdletParameters(command);
                });
            }
            if (this.taskObject.Fields.IsModified("State"))
            {
                this.dlpPolicy.State = this.taskObject.State;
            }
            if (this.taskObject.Fields.IsModified("Mode"))
            {
                this.dlpPolicy.Mode = this.taskObject.Mode;
            }
            if (this.taskObject.Fields.IsModified("Description"))
            {
                this.dlpPolicy.Description = this.taskObject.Description;
            }
            this.ValidateDlpPolicyName();
        }
Exemplo n.º 5
0
        internal static DlpPolicyTemplateMetaData ParseDlpPolicyTemplate(string data)
        {
            DlpPolicyTemplateMetaData result;

            try
            {
                XElement root = XDocument.Parse(data).Root;
                DlpPolicyTemplateMetaData dlpPolicyTemplateMetaData = new DlpPolicyTemplateMetaData();
                dlpPolicyTemplateMetaData.Version               = root.Attribute("version").Value.Trim();
                dlpPolicyTemplateMetaData.State                 = (RuleState)Enum.Parse(typeof(RuleState), root.Attribute("state").Value.Trim());
                dlpPolicyTemplateMetaData.Mode                  = (RuleMode)Enum.Parse(typeof(RuleMode), root.Attribute("mode").Value.Trim());
                dlpPolicyTemplateMetaData.ImmutableId           = ((root.Attribute("id") == null) ? Guid.Empty : Guid.Parse(root.Attribute("id").Value.Trim()));
                dlpPolicyTemplateMetaData.ContentVersion        = root.Element("contentVersion").Value.Trim();
                dlpPolicyTemplateMetaData.PublisherName         = root.Element("publisherName").Value.Trim();
                dlpPolicyTemplateMetaData.LocalizedNames        = DlpPolicyParser.ParseLocalizedString(root.Element("name"));
                dlpPolicyTemplateMetaData.LocalizedDescriptions = DlpPolicyParser.ParseLocalizedString(root.Element("description"));
                dlpPolicyTemplateMetaData.LocalizedKeywords     = (from localizedKeyword in root.Element("keywords").Elements("keyword")
                                                                   select DlpPolicyParser.ParseLocalizedString(localizedKeyword)).ToList <Dictionary <string, string> >();
                dlpPolicyTemplateMetaData.RuleParameters = (from ruleParameter in root.Element("ruleParameters").Elements("ruleParameter")
                                                            select new DlpTemplateRuleParameter
                {
                    Type = ruleParameter.Attribute("type").Value.Trim(),
                    Required = bool.Parse(ruleParameter.Attribute("required").Value),
                    Token = ruleParameter.Attribute("token").Value.Trim(),
                    LocalizedDescriptions = DlpPolicyParser.ParseLocalizedString(ruleParameter.Element("description"))
                }).ToList <DlpTemplateRuleParameter>();
                dlpPolicyTemplateMetaData.PolicyCommands = (from policyCommand in root.Element("policyCommands").Elements("commandBlock")
                                                            select policyCommand.Value.Trim()).ToList <string>();
                dlpPolicyTemplateMetaData.LocalizedPolicyCommandResources = (from localizedResource in root.Element("policyCommandsResources").Elements("resource")
                                                                             select new KeyValuePair <string, Dictionary <string, string> >(localizedResource.Attribute("token").Value.Trim(), DlpPolicyParser.ParseLocalizedString(localizedResource))).ToDictionary((KeyValuePair <string, Dictionary <string, string> > pair) => pair.Key, (KeyValuePair <string, Dictionary <string, string> > pair) => pair.Value, StringComparer.OrdinalIgnoreCase);
                DlpPolicyTemplateMetaData dlpPolicyTemplateMetaData2 = dlpPolicyTemplateMetaData;
                dlpPolicyTemplateMetaData2.Validate();
                result = dlpPolicyTemplateMetaData2;
            }
            catch (ArgumentException innerException)
            {
                throw new DlpPolicyParsingException(Strings.DlpPolicyStateStateInvalid, innerException);
            }
            catch (NullReferenceException innerException2)
            {
                throw new DlpPolicyParsingException(Strings.DlpPolicyXmlMissingElements, innerException2);
            }
            catch (XmlException innerException3)
            {
                throw new DlpPolicyParsingException(Strings.DlpPolicyXmlInvalid, innerException3);
            }
            return(result);
        }
        internal ADComplianceProgram ToAdObject()
        {
            string transportRulesXml;

            using (MemoryStream memoryStream = new MemoryStream(DlpPolicyParser.SerializeDlpPolicyTemplate(this)))
            {
                StreamReader streamReader = new StreamReader(memoryStream);
                transportRulesXml = streamReader.ReadToEnd();
            }
            ADComplianceProgram adcomplianceProgram = new ADComplianceProgram();

            adcomplianceProgram.Name        = DlpPolicyTemplateMetaData.GetLocalizedStringValue(this.LocalizedNames, null);
            adcomplianceProgram.Description = DlpPolicyTemplateMetaData.GetLocalizedStringValue(this.LocalizedDescriptions, null);
            adcomplianceProgram.ImmutableId = this.ImmutableId;
            adcomplianceProgram.Keywords    = (from keyword in this.LocalizedKeywords
                                               select DlpPolicyTemplateMetaData.GetLocalizedStringValue(keyword, DlpPolicyTemplateMetaData.DefaultCulture)).ToArray <string>();
            adcomplianceProgram.PublisherName     = this.PublisherName;
            adcomplianceProgram.State             = DlpUtils.RuleStateToDlpState(this.State, this.Mode);
            adcomplianceProgram.TransportRulesXml = transportRulesXml;
            adcomplianceProgram.Version           = this.Version;
            return(adcomplianceProgram);
        }
        internal void Validate()
        {
            if (this.LocalizedNames == null || !this.LocalizedNames.Any <KeyValuePair <string, string> >() || string.IsNullOrEmpty(this.Version) || this.ImmutableId == Guid.Empty || string.IsNullOrEmpty(this.ContentVersion) || string.IsNullOrEmpty(this.PublisherName) || this.LocalizedDescriptions == null || !this.LocalizedDescriptions.Any <KeyValuePair <string, string> >() || this.PolicyCommands == null || !this.PolicyCommands.Any <string>())
            {
                throw new DlpPolicyParsingException(Strings.DlpPolicyXmlMissingElements);
            }
            if (new Version(this.Version) > DlpUtils.MaxSupportedVersion)
            {
                throw new DlpPolicyParsingException(Strings.DlpPolicyVersionUnsupported);
            }
            if (this.LocalizedKeywords.Any((Dictionary <string, string> keywords) => keywords.Any((KeyValuePair <string, string> keyword) => string.IsNullOrEmpty(keyword.Value))))
            {
                throw new DlpPolicyParsingException(Strings.DlpPolicyContainsEmptyKeywords);
            }
            DlpPolicyTemplateMetaData.ValidateFieldSize("version", this.Version, 16);
            DlpPolicyTemplateMetaData.ValidateFieldSize("contentVersion", this.ContentVersion, 16);
            DlpPolicyTemplateMetaData.ValidateFieldSize("publisherName", this.PublisherName, 256);
            this.LocalizedNames.Values.ToList <string>().ForEach(delegate(string x)
            {
                DlpPolicyTemplateMetaData.ValidateFieldSize("name", x, 64);
            });
            this.LocalizedDescriptions.Values.ToList <string>().ForEach(delegate(string x)
            {
                DlpPolicyTemplateMetaData.ValidateFieldSize("description", x, 1024);
            });
            this.LocalizedKeywords.ToList <Dictionary <string, string> >().ForEach(delegate(Dictionary <string, string> keywords)
            {
                keywords.Values.ToList <string>().ForEach(delegate(string keyword)
                {
                    DlpPolicyTemplateMetaData.ValidateFieldSize("keyword", keyword, 64);
                });
            });
            List <DlpTemplateRuleParameter> list = this.RuleParameters.ToList <DlpTemplateRuleParameter>();

            list.ForEach(delegate(DlpTemplateRuleParameter x)
            {
                DlpPolicyTemplateMetaData.ValidateFieldSize("type", x.Type, 32);
            });
            list.ForEach(delegate(DlpTemplateRuleParameter x)
            {
                DlpPolicyTemplateMetaData.ValidateFieldSize("token", x.Token, 32);
            });
            list.ForEach(delegate(DlpTemplateRuleParameter x)
            {
                x.LocalizedDescriptions.ToList <KeyValuePair <string, string> >().ForEach(delegate(KeyValuePair <string, string> y)
                {
                    DlpPolicyTemplateMetaData.ValidateFieldSize("description", y.Value, 1024);
                });
            });
            this.LocalizedPolicyCommandResources.Values.ToList <Dictionary <string, string> >().ForEach(delegate(Dictionary <string, string> resources)
            {
                resources.Values.ToList <string>().ForEach(delegate(string resource)
                {
                    DlpPolicyTemplateMetaData.ValidateFieldSize("resource", resource, 1024);
                });
            });
            List <string> list2 = this.PolicyCommands.ToList <string>();

            list2.ForEach(delegate(string x)
            {
                DlpPolicyTemplateMetaData.ValidateFieldSize("commandBlock", x, 4096);
            });
            list2.ForEach(delegate(string command)
            {
                DlpPolicyTemplateMetaData.ValidateCmdletParameters(command);
            });
        }
        internal static IEnumerable <string> LocalizeCmdlets(IEnumerable <string> policyCommands, Dictionary <string, Dictionary <string, string> > policyCommandResources, CultureInfo culture)
        {
            IDictionary <string, string> localizedResources = (from localizedResource in policyCommandResources
                                                               select new KeyValuePair <string, string>(localizedResource.Key, DlpPolicyTemplateMetaData.GetLocalizedStringValue(localizedResource.Value, culture))).ToDictionary((KeyValuePair <string, string> pair) => pair.Key, (KeyValuePair <string, string> pair) => pair.Value, StringComparer.OrdinalIgnoreCase);

            return(from cmdlet in policyCommands
                   select localizedResources.Aggregate(cmdlet, (string current, KeyValuePair <string, string> parameter) => current.Replace(parameter.Key, parameter.Value).Trim()));
        }
Exemplo n.º 9
0
        public static byte[] SerializeDlpPolicyTemplate(DlpPolicyTemplateMetaData dlpTemplateMetaData)
        {
            XName name = XName.Get("dlpPolicyTemplate");

            object[] array = new object[12];
            array[0] = new XAttribute(XName.Get("version"), dlpTemplateMetaData.Version);
            array[1] = new XAttribute(XName.Get("state"), dlpTemplateMetaData.State);
            array[2] = new XAttribute(XName.Get("mode"), dlpTemplateMetaData.Mode);
            array[3] = new XAttribute(XName.Get("id"), dlpTemplateMetaData.ImmutableId);
            array[4] = new XElement(XName.Get("contentVersion"), dlpTemplateMetaData.ContentVersion);
            array[5] = new XElement(XName.Get("publisherName"), dlpTemplateMetaData.PublisherName);
            array[6] = new XElement(XName.Get("name"), from localizedName in dlpTemplateMetaData.LocalizedNames
                                    select new XElement(XName.Get("localizedString"), new object[]
            {
                new XAttribute(XName.Get("lang"), localizedName.Key),
                localizedName.Value
            }));
            array[7] = new XElement(XName.Get("description"), from localizedDescription in dlpTemplateMetaData.LocalizedDescriptions
                                    select new XElement(XName.Get("localizedString"), new object[]
            {
                new XAttribute(XName.Get("lang"), localizedDescription.Key),
                localizedDescription.Value
            }));
            array[8] = new XElement(XName.Get("keywords"), from keywords in dlpTemplateMetaData.LocalizedKeywords
                                    select new XElement(XName.Get("keyword"), from localizedKeyword in keywords
                                                        select new XElement(XName.Get("localizedString"), new object[]
            {
                new XAttribute(XName.Get("lang"), localizedKeyword.Key),
                localizedKeyword.Value
            })));
            array[9] = new XElement(XName.Get("ruleParameters"), dlpTemplateMetaData.RuleParameters.Select(delegate(DlpTemplateRuleParameter ruleParameter)
            {
                XName name2     = XName.Get("ruleParameter");
                object[] array2 = new object[4];
                array2[0]       = new XAttribute(XName.Get("type"), ruleParameter.Type);
                array2[1]       = new XAttribute(XName.Get("required"), ruleParameter.Required.ToString());
                array2[2]       = new XAttribute(XName.Get("token"), ruleParameter.Token);
                array2[3]       = new XElement(XName.Get("description"), from localizedDescription in ruleParameter.LocalizedDescriptions
                                               select new XElement(XName.Get("localizedString"), new object[]
                {
                    new XAttribute(XName.Get("lang"), localizedDescription.Key),
                    localizedDescription.Value
                }));
                return(new XElement(name2, array2));
            }));
            array[10] = new XElement(XName.Get("policyCommands"), from commandBlock in dlpTemplateMetaData.PolicyCommands
                                     select new XElement(XName.Get("commandBlock"), new XCData(commandBlock)));
            array[11] = new XElement(XName.Get("policyCommandsResources"), dlpTemplateMetaData.LocalizedPolicyCommandResources.Select(delegate(KeyValuePair <string, Dictionary <string, string> > resources)
            {
                XName name2     = XName.Get("resource");
                object[] array2 = new object[2];
                array2[0]       = new XAttribute(XName.Get("token"), resources.Key);
                array2[1]       = from resource in resources.Value
                                  select new XElement(XName.Get("localizedString"), new object[]
                {
                    new XAttribute(XName.Get("lang"), resource.Key),
                    resource.Value
                });
                return(new XElement(name2, array2));
            }));
            XElement xelement = new XElement(name, array);

            byte[] result;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                xelement.Save(memoryStream);
                result = memoryStream.ToArray();
            }
            return(result);
        }