Exemplo n.º 1
0
        //
        // ResolveFiles - parse files referenced with <file src="" />
        //
        static void ResolveFiles(ParseState parseState, object configurationContext)
        {
            //
            // 1) get the directory of the configuration file currently being parsed
            //

            HttpConfigurationContext httpConfigurationContext = (HttpConfigurationContext)configurationContext;
            string configurationDirectory = null;
            bool   useAssert = false;

            //
            // Only assert to read cap files when parsing machine.config
            // (allow device updates to work in restricted trust levels).
            //
            // Machine.config can be securely identified by the context being
            // an HttpConfigurationContext with null path.
            //
            try {
                if (httpConfigurationContext.VirtualPath == null)
                {
                    useAssert = true;
                    // we need to assert here to get the file path from ConfigurationException
                    FileIOPermission fiop = new FileIOPermission(PermissionState.None);
                    fiop.AllFiles = FileIOPermissionAccess.PathDiscovery;
                    fiop.Assert();
                }

                Pair    pair0        = (Pair)parseState.FileList[0];
                XmlNode srcAttribute = (XmlNode)pair0.Second;
                configurationDirectory = Path.GetDirectoryName(ConfigurationErrorsException.GetFilename(srcAttribute));
            }
            finally {
                if (useAssert)
                {
                    CodeAccessPermission.RevertAssert();
                }
            }

            //
            // 2) iterate through list of referenced files, builing rule lists for each
            //
            foreach (Pair pair in parseState.FileList)
            {
                string srcFilename  = (string)pair.First;
                string fullFilename = Path.Combine(configurationDirectory, srcFilename);

                XmlNode section;
                try {
                    if (useAssert)
                    {
                        InternalSecurityPermissions.FileReadAccess(fullFilename).Assert();
                    }

                    Exception fcmException = null;

                    try {
                        HttpConfigurationSystem.AddFileDependency(fullFilename);
                    }
                    catch (Exception e) {
                        fcmException = e;
                    }

                    ConfigXmlDocument configDoc = new ConfigXmlDocument();

                    try {
                        configDoc.Load(fullFilename);
                        section = configDoc.DocumentElement;
                    }
                    catch (Exception e) {
                        throw new ConfigurationErrorsException(SR.GetString(SR.Error_loading_XML_file, fullFilename, e.Message),
                                                               e, (XmlNode)pair.Second);
                    }

                    if (fcmException != null)
                    {
                        throw fcmException;
                    }
                }
                finally {
                    if (useAssert)
                    {
                        // Cannot apply next FileReadAccess PermissionSet unless
                        // current set is explicitly reverted.  Also minimizes
                        // granted permissions.
                        CodeAccessPermission.RevertAssert();
                    }
                }

                if (section.Name != parseState.SectionName)
                {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Capability_file_root_element, parseState.SectionName),
                                                           section);
                }

                HandlerBase.CheckForUnrecognizedAttributes(section);

                ArrayList sublist = RuleListFromElement(parseState, section, true);

                if (sublist.Count > 0)
                {
                    parseState.RuleList.Add(new CapabilitiesSection(CapabilitiesRule.Filter, null, null, sublist));
                }
            }
        }
        private static void ResolveFiles(ParseState parseState, object configurationContext)
        {
            HttpConfigurationContext context = (HttpConfigurationContext)configurationContext;
            string directoryName             = null;
            bool   flag = false;

            try
            {
                if (context.VirtualPath == null)
                {
                    flag = true;
                    new FileIOPermission(PermissionState.None)
                    {
                        AllFiles = FileIOPermissionAccess.PathDiscovery
                    }.Assert();
                }
                Pair    pair   = (Pair)parseState.FileList[0];
                XmlNode second = (XmlNode)pair.Second;
                directoryName = Path.GetDirectoryName(ConfigurationErrorsException.GetFilename(second));
            }
            finally
            {
                if (flag)
                {
                    CodeAccessPermission.RevertAssert();
                }
            }
            foreach (Pair pair2 in parseState.FileList)
            {
                XmlNode documentElement;
                string  first    = (string)pair2.First;
                string  filename = Path.Combine(directoryName, first);
                try
                {
                    if (flag)
                    {
                        InternalSecurityPermissions.FileReadAccess(filename).Assert();
                    }
                    Exception exception = null;
                    try
                    {
                        HttpConfigurationSystem.AddFileDependency(filename);
                    }
                    catch (Exception exception2)
                    {
                        exception = exception2;
                    }
                    ConfigXmlDocument document = new ConfigXmlDocument();
                    try
                    {
                        document.Load(filename);
                        documentElement = document.DocumentElement;
                    }
                    catch (Exception exception3)
                    {
                        throw new ConfigurationErrorsException(System.Web.SR.GetString("Error_loading_XML_file", new object[] { filename, exception3.Message }), exception3, (XmlNode)pair2.Second);
                    }
                    if (exception != null)
                    {
                        throw exception;
                    }
                }
                finally
                {
                    if (flag)
                    {
                        CodeAccessPermission.RevertAssert();
                    }
                }
                if (documentElement.Name != parseState.SectionName)
                {
                    throw new ConfigurationErrorsException(System.Web.SR.GetString("Capability_file_root_element", new object[] { parseState.SectionName }), documentElement);
                }
                System.Web.Configuration.HandlerBase.CheckForUnrecognizedAttributes(documentElement);
                ArrayList rulelist = RuleListFromElement(parseState, documentElement, true);
                if (rulelist.Count > 0)
                {
                    parseState.RuleList.Add(new CapabilitiesSection(2, null, null, rulelist));
                }
            }
        }