internal bool LoadXmlFile(XmlFileLoadInfo info, TypeInfoDataBase db, MshExpressionFactory expressionFactory, AuthorizationManager authorizationManager, PSHost host, bool preValidated) { if (info == null) { throw PSTraceSource.NewArgumentNullException("info"); } if (info.filePath == null) { throw PSTraceSource.NewArgumentNullException("info.filePath"); } if (db == null) { throw PSTraceSource.NewArgumentNullException("db"); } if (expressionFactory == null) { throw PSTraceSource.NewArgumentNullException("expressionFactory"); } base.displayResourceManagerCache = db.displayResourceManagerCache; base.expressionFactory = expressionFactory; base.SetDatabaseLoadingInfo(info); base.ReportTrace("loading file started"); XmlDocument doc = null; bool isFullyTrusted = false; doc = base.LoadXmlDocumentFromFileLoadingInfo(authorizationManager, host, out isFullyTrusted); if (SystemPolicy.GetSystemLockdownPolicy() == SystemEnforcementMode.Enforce) { base.SetLoadingInfoIsFullyTrusted(isFullyTrusted); } if (doc == null) { return false; } bool suppressValidation = this.suppressValidation; try { this.suppressValidation = preValidated; try { this.LoadData(doc, db); } catch (TooManyErrorsException) { return false; } catch (Exception exception) { base.ReportError(StringUtil.Format(FormatAndOutXmlLoadingStrings.ErrorInFile, base.FilePath, exception.Message)); throw; } if (base.HasErrors) { return false; } } finally { this.suppressValidation = suppressValidation; } base.ReportTrace("file loaded with no errors"); return true; }
protected XmlDocument LoadXmlDocumentFromFileLoadingInfo(AuthorizationManager authorizationManager, PSHost host, out bool isFullyTrusted) { // get file contents ExternalScriptInfo ps1xmlInfo = new ExternalScriptInfo(FilePath, FilePath); string fileContents = ps1xmlInfo.ScriptContents; isFullyTrusted = false; if (ps1xmlInfo.DefiningLanguageMode == PSLanguageMode.FullLanguage) { isFullyTrusted = true; } if (authorizationManager != null) { try { authorizationManager.ShouldRunInternal(ps1xmlInfo, CommandOrigin.Internal, host); } catch (PSSecurityException reason) { string errorMessage = StringUtil.Format(TypesXmlStrings.ValidationException, string.Empty /* TODO/FIXME snapin */, FilePath, reason.Message); ReportLogEntryHelper(errorMessage, XmlLoaderLoggerEntry.EntryType.Error, failToLoadFile: true); return null; } } // load file into XML document try { XmlDocument doc = InternalDeserializer.LoadUnsafeXmlDocument( fileContents, true, /* preserve whitespace, comments, etc. */ null); /* default maxCharacters */ this.ReportTrace("XmlDocument loaded OK"); return doc; } catch (XmlException e) { this.ReportError(StringUtil.Format(FormatAndOutXmlLoadingStrings.ErrorInFile, FilePath, e.Message)); this.ReportTrace("XmlDocument discarded"); return null; } catch (Exception e) // will rethrow { CommandProcessor.CheckForSevereException(e); throw; } }
/// <summary> /// entry point for the loader algorithm /// </summary> /// <param name="info">information needed to load the file</param> /// <param name="db">database instance to load the file into</param> /// <param name="expressionFactory">expression factory to validate script blocks</param> /// <param name="authorizationManager"> /// Authorization manager to perform signature checks before reading ps1xml files (or null of no checks are needed) /// </param> /// <param name="host"> /// Host passed to <paramref name="authorizationManager"/>. Can be null if no interactive questions should be asked. /// </param> /// <param name="preValidated"> /// True if the format data has been pre-validated (build time, manual testing, etc) so that validation can be /// skipped at runtime. /// </param> /// <returns>true if successful</returns> internal bool LoadXmlFile( XmlFileLoadInfo info, TypeInfoDataBase db, MshExpressionFactory expressionFactory, AuthorizationManager authorizationManager, PSHost host, bool preValidated) { if (info == null) throw PSTraceSource.NewArgumentNullException("info"); if (info.filePath == null) throw PSTraceSource.NewArgumentNullException("info.filePath"); if (db == null) throw PSTraceSource.NewArgumentNullException("db"); if (expressionFactory == null) throw PSTraceSource.NewArgumentNullException("expressionFactory"); if (SecuritySupport.IsProductBinary(info.filePath)) { this.SetLoadingInfoIsProductCode(true); } this.displayResourceManagerCache = db.displayResourceManagerCache; this.expressionFactory = expressionFactory; this.SetDatabaseLoadingInfo(info); this.ReportTrace("loading file started"); // load file into XML document XmlDocument newDocument = null; bool isFullyTrusted = false; newDocument = LoadXmlDocumentFromFileLoadingInfo(authorizationManager, host, out isFullyTrusted); // If we're not in a locked-down environment, types and formatting are allowed based just on the authorization // manager. If we are in a locked-down environment, additionally check the system policy. if (SystemPolicy.GetSystemLockdownPolicy() == SystemEnforcementMode.Enforce) { SetLoadingInfoIsFullyTrusted(isFullyTrusted); } if (newDocument == null) { return false; } // load the XML document into a copy of the // in memory database bool previousSuppressValidation = _suppressValidation; try { _suppressValidation = preValidated; try { this.LoadData(newDocument, db); } catch (TooManyErrorsException) { // already logged an error before throwing return false; } catch (Exception e) // will rethrow { //Error in file {0}: {1} this.ReportError(StringUtil.Format(FormatAndOutXmlLoadingStrings.ErrorInFile, FilePath, e.Message)); throw; } if (this.HasErrors) { return false; } } finally { _suppressValidation = previousSuppressValidation; } this.ReportTrace("file loaded with no errors"); return true; }
protected XmlDocument LoadXmlDocumentFromFileLoadingInfo(AuthorizationManager authorizationManager, PSHost host, out bool isFullyTrusted) { XmlDocument document2; ExternalScriptInfo commandInfo = new ExternalScriptInfo(this.FilePath, this.FilePath); string scriptContents = commandInfo.ScriptContents; isFullyTrusted = false; if (((PSLanguageMode) commandInfo.DefiningLanguageMode) == PSLanguageMode.FullLanguage) { isFullyTrusted = true; } if (authorizationManager != null) { try { authorizationManager.ShouldRunInternal(commandInfo, CommandOrigin.Internal, host); } catch (PSSecurityException exception) { string message = StringUtil.Format(TypesXmlStrings.ValidationException, new object[] { string.Empty, this.FilePath, exception.Message }); this.ReportLogEntryHelper(message, XmlLoaderLoggerEntry.EntryType.Error, true); return null; } } try { XmlDocument document = InternalDeserializer.LoadUnsafeXmlDocument(scriptContents, true, null); this.ReportTrace("XmlDocument loaded OK"); document2 = document; } catch (XmlException exception2) { this.ReportError(StringUtil.Format(FormatAndOutXmlLoadingStrings.ErrorInFile, this.FilePath, exception2.Message)); this.ReportTrace("XmlDocument discarded"); document2 = null; } catch (Exception exception3) { CommandProcessorBase.CheckForSevereException(exception3); throw; } return document2; }