internal static ComInfo[] GetComInfo(string path) { XmlDocument document = GetXmlDocument(path); XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(document.NameTable); string manifestFileName = Path.GetFileName(path); var comInfoList = new List <ComInfo>(); XmlNodeList comNodes = document.SelectNodes(XPaths.comFilesPath, nsmgr); foreach (XmlNode comNode in comNodes) { XmlNode nameNode = comNode.SelectSingleNode(XPaths.fileNameAttribute, nsmgr); string componentFileName = nameNode?.Value; XmlNodeList clsidNodes = comNode.SelectNodes(XPaths.clsidAttribute, nsmgr); foreach (XmlNode clsidNode in clsidNodes) { comInfoList.Add(new ComInfo(manifestFileName, componentFileName, clsidNode.Value, null)); } XmlNodeList tlbidNodes = comNode.SelectNodes(XPaths.tlbidAttribute, nsmgr); foreach (XmlNode tlbidNode in tlbidNodes) { comInfoList.Add(new ComInfo(manifestFileName, componentFileName, null, tlbidNode.Value)); } } return(comInfoList.ToArray()); }
// Returns permission set sub-element, creating a full-trust permission-set if one doesn't exist private XmlElement GetPermissionSetElement(XmlDocument document) { Debug.Assert(document != null, "GetPermissionSetElement was passed a null document"); XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(document.NameTable); XmlElement trustInfoElement = document.DocumentElement; var securityElement = (XmlElement)trustInfoElement.SelectSingleNode(XPaths.securityElement, nsmgr); if (securityElement == null) { securityElement = document.CreateElement(XmlUtil.TrimPrefix(XPaths.securityElement), XmlNamespaces.asmv2); trustInfoElement.AppendChild(securityElement); } XmlElement applicationRequestMinimumElement = (XmlElement)securityElement.SelectSingleNode(XPaths.applicationRequestMinimumElement, nsmgr); if (applicationRequestMinimumElement == null) { applicationRequestMinimumElement = document.CreateElement(XmlUtil.TrimPrefix(XPaths.applicationRequestMinimumElement), XmlNamespaces.asmv2); securityElement.AppendChild(applicationRequestMinimumElement); } XmlElement permissionSetElement = (XmlElement)applicationRequestMinimumElement.SelectSingleNode(XPaths.permissionSetElement, nsmgr); if (permissionSetElement == null) { permissionSetElement = document.CreateElement(XmlUtil.TrimPrefix(XPaths.permissionSetElement), XmlNamespaces.asmv2); applicationRequestMinimumElement.AppendChild(permissionSetElement); XmlAttribute unrestrictedAttribute = document.CreateAttribute(XmlUtil.TrimPrefix(XPaths.unrestrictedAttribute), XmlNamespaces.asmv2); unrestrictedAttribute.Value = _isFullTrust.ToString().ToLowerInvariant(); permissionSetElement.Attributes.Append(unrestrictedAttribute); } return(permissionSetElement); }
private static AssemblyIdentity FromManifest(XmlDocument document) { XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(document.NameTable); var element = (XmlElement)document.SelectSingleNode(XPaths.assemblyIdentityPath, nsmgr); if (element == null) { return(null); } XmlNode node = element.Attributes.GetNamedItem("name"); string name = node?.Value; node = element.Attributes.GetNamedItem("version"); string version = node?.Value; node = element.Attributes.GetNamedItem("publicKeyToken"); string publicKeyToken = node?.Value; node = element.Attributes.GetNamedItem("language"); string culture = node?.Value; node = element.Attributes.GetNamedItem("processorArchitecture"); string processorArchitecture = node?.Value; node = element.Attributes.GetNamedItem("type"); string type = node?.Value; return(new AssemblyIdentity(name, version, publicKeyToken, culture, processorArchitecture, type)); }
private static string[] XmlToIdentityList(XmlElement psElement) { XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(psElement.OwnerDocument.NameTable); XmlNodeList nodes = psElement.SelectNodes(XPaths.permissionClassAttributeQuery, nsmgr); if (nodes == null || nodes.Count == 0) { nodes = psElement.SelectNodes(XmlUtil.TrimPrefix(XPaths.permissionClassAttributeQuery)); } string[] a; if (nodes != null) { a = new string[nodes.Count]; int i = 0; foreach (XmlNode node in nodes) { a[i++] = node.Value; } } else { a = Array.Empty <string>(); } return(a); }
private XmlElement GetInputRequestedPrivilegeElement() { if (_inputTrustInfoDocument == null) { return(null); } XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(_inputTrustInfoDocument.NameTable); XmlElement trustInfoElement = _inputTrustInfoDocument.DocumentElement; XmlElement securityElement = (XmlElement)trustInfoElement?.SelectSingleNode(XPaths.securityElement, nsmgr); XmlElement requestedPrivilegeElement = (XmlElement)securityElement?.SelectSingleNode(XPaths.requestedPrivilegeElement, nsmgr); return(requestedPrivilegeElement); }
private void FixupPermissionSetElement(XmlElement permissionSetElement) { XmlDocument document = permissionSetElement.OwnerDocument; XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(document.NameTable); if (PreserveFullTrustPermissionSet) { var unrestrictedAttribute = (XmlAttribute)permissionSetElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix(XPaths.unrestrictedAttribute)); if (_isFullTrust) { if (unrestrictedAttribute == null) { unrestrictedAttribute = document.CreateAttribute(XmlUtil.TrimPrefix(XPaths.unrestrictedAttribute)); permissionSetElement.Attributes.Append(unrestrictedAttribute); } unrestrictedAttribute.Value = "true"; } else { if (unrestrictedAttribute != null) { permissionSetElement.Attributes.RemoveNamedItem(XmlUtil.TrimPrefix(XPaths.unrestrictedAttribute)); } } } else { if (_isFullTrust) { var unrestrictedAttribute = (XmlAttribute)permissionSetElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix(XPaths.unrestrictedAttribute)); if (unrestrictedAttribute == null) { unrestrictedAttribute = document.CreateAttribute(XmlUtil.TrimPrefix(XPaths.unrestrictedAttribute)); permissionSetElement.Attributes.Append(unrestrictedAttribute); } unrestrictedAttribute.Value = "true"; while (permissionSetElement.FirstChild != null) { permissionSetElement.RemoveChild(permissionSetElement.FirstChild); } } } // Add ID="Custom" attribute if there's not one already var idAttribute = (XmlAttribute)permissionSetElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix(XPaths.idAttribute)); if (idAttribute == null) { idAttribute = document.CreateAttribute(XmlUtil.TrimPrefix(XPaths.idAttribute)); permissionSetElement.Attributes.Append(idAttribute); } if (String.IsNullOrEmpty(idAttribute.Value)) { idAttribute.Value = "Custom"; } AddSameSiteAttribute(permissionSetElement); if (permissionSetElement.ParentNode == null || permissionSetElement.ParentNode.NodeType == XmlNodeType.Document) { return; } XmlAttribute idrefAttribute = null; XmlElement defaultAssemblyRequestElement = (XmlElement)permissionSetElement.ParentNode.SelectSingleNode(XPaths.defaultAssemblyRequestElement, nsmgr); if (defaultAssemblyRequestElement == null) { defaultAssemblyRequestElement = document.CreateElement(XmlUtil.TrimPrefix(XPaths.defaultAssemblyRequestElement), XmlNamespaces.asmv2); permissionSetElement.ParentNode.AppendChild(defaultAssemblyRequestElement); } idrefAttribute = (XmlAttribute)permissionSetElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix(XPaths.permissionSetReferenceAttribute)); if (idrefAttribute == null) { idrefAttribute = document.CreateAttribute(XmlUtil.TrimPrefix(XPaths.permissionSetReferenceAttribute)); defaultAssemblyRequestElement.Attributes.Append(idrefAttribute); } if (String.Compare(idAttribute.Value, idrefAttribute.Value, StringComparison.Ordinal) != 0) { idrefAttribute.Value = idAttribute.Value; } }
private static XmlElement GetRequestedPrivilegeElement(XmlElement inputRequestedPrivilegeElement, XmlDocument document) { // <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3"> // <!-- // UAC Manifest Options // If you want to change the Windows User Account Control level replace the // requestedExecutionLevel node with one of the following . // <requestedExecutionLevel level="asInvoker" /> // <requestedExecutionLevel level="requireAdministrator" /> // <requestedExecutionLevel level="highestAvailable" /> // If you want to utilize File and Registry Virtualization for backward compatibility // delete the requestedExecutionLevel node. // --> // <requestedExecutionLevel level="asInvoker" /> // </requestedPrivileges> // we always create a requestedPrivilege node to put into the generated TrustInfo document // XmlElement requestedPrivilegeElement = document.CreateElement(XmlUtil.TrimPrefix(XPaths.requestedPrivilegeElement), XmlNamespaces.asmv3); document.AppendChild(requestedPrivilegeElement); // our three cases we need to handle are: // (a) no requestedPrivilege node (and therefore no requestedExecutionLevel node as well) - use default values // (b) requestedPrivilege node and no requestedExecutionLevel node - omit the requestedExecutionLevel node // (c) requestedPrivilege node and requestedExecutionLevel node - use the incoming requestedExecutionLevel node values // // using null for both values is case (b) above -- do not output values // string executionLevelString = null; string executionUIAccessString = null; string commentString = null; // case (a) above -- load default values // if (inputRequestedPrivilegeElement == null) { // If UAC requestedPrivilege node is missing (possibly due to upgraded project) then automatically // add a default UAC requestedPrivilege node with a default requestedExecutionLevel node set to // the expected ClickOnce level (asInvoker) with uiAccess as false // executionLevelString = Constants.UACAsInvoker; executionUIAccessString = Constants.UACUIAccess; // load up a default comment string that we put in front of the requestedExecutionLevel node // here so we can allow the passed-in node to override it if there is a comment present // System.Resources.ResourceManager resources = new System.Resources.ResourceManager("net.r_eg.IeXod.Tasks.Core.Strings.ManifestUtilities", typeof(SecurityUtilities).Module.Assembly); commentString = resources.GetString("TrustInfo.RequestedExecutionLevelComment");; } else { // we need to see if the requestedExecutionLevel node is present to decide whether or not to create one. // XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(document.NameTable); XmlElement inputRequestedExecutionLevel = (XmlElement)inputRequestedPrivilegeElement.SelectSingleNode(XPaths.requestedExecutionLevelElement, nsmgr); // case (c) above -- use incoming values [note that we should do nothing for case (b) above // because the default values will make us not emit the requestedExecutionLevel and comment] // if (inputRequestedExecutionLevel != null) { XmlNode previousNode = inputRequestedExecutionLevel.PreviousSibling; // fetch the current comment node if there is one (if there is not one, simply // keep the default null value which means we will not create one in the // output document) // if (previousNode != null && previousNode.NodeType == XmlNodeType.Comment) { commentString = ((XmlComment)previousNode).Data; } // fetch the current requestedExecutionLevel node's level attribute if there is one // if (inputRequestedExecutionLevel.HasAttribute("level")) { executionLevelString = inputRequestedExecutionLevel.GetAttribute("level"); } // fetch the current requestedExecutionLevel node's uiAccess attribute if there is one // if (inputRequestedExecutionLevel.HasAttribute("uiAccess")) { executionUIAccessString = inputRequestedExecutionLevel.GetAttribute("uiAccess"); } } } if (commentString != null) { XmlComment requestedPrivilegeComment = document.CreateComment(commentString); requestedPrivilegeElement.AppendChild(requestedPrivilegeComment); } if (executionLevelString != null) { XmlElement requestedExecutionLevelElement = document.CreateElement(XmlUtil.TrimPrefix(XPaths.requestedExecutionLevelElement), XmlNamespaces.asmv3); requestedPrivilegeElement.AppendChild(requestedExecutionLevelElement); XmlAttribute levelAttribute = document.CreateAttribute("level"); levelAttribute.Value = executionLevelString; requestedExecutionLevelElement.Attributes.Append(levelAttribute); if (executionUIAccessString != null) { XmlAttribute uiAccessAttribute = document.CreateAttribute("uiAccess"); uiAccessAttribute.Value = executionUIAccessString; requestedExecutionLevelElement.Attributes.Append(uiAccessAttribute); } } return(requestedPrivilegeElement); }
public static Stream Format(Stream input) { int t1 = Environment.TickCount; var r = new XmlTextReader(input) { DtdProcessing = DtdProcessing.Ignore, WhitespaceHandling = WhitespaceHandling.None }; XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(r.NameTable); var m = new MemoryStream(); var w = new XmlTextWriter(m, Encoding.UTF8) { Formatting = Formatting.Indented, Indentation = 2 }; w.WriteStartDocument(); while (r.Read()) { switch (r.NodeType) { case XmlNodeType.Element: w.WriteStartElement(r.Prefix, r.LocalName, r.NamespaceURI); if (r.HasAttributes) { string elementQName = XmlUtil.GetQName(r, nsmgr); for (int i = 0; i < r.AttributeCount; ++i) { r.MoveToAttribute(i); string attributeQName = XmlUtil.GetQName(r, nsmgr); string xpath = elementQName + "/@" + attributeQName; // Filter out language="*" if ((xpath.Equals(XPaths.languageAttribute1, StringComparison.Ordinal) || xpath.Equals( XPaths.languageAttribute2, StringComparison.Ordinal)) && String.Equals( r.Value, "*", StringComparison.Ordinal)) { continue; } // Filter out attributes with empty values if attribute is on the list... if (String.IsNullOrEmpty(r.Value) && Array.BinarySearch(XPaths.emptyAttributeList, xpath) >= 0) { continue; } w.WriteAttributeString(r.Prefix, r.LocalName, r.NamespaceURI, r.Value); } r.MoveToElement(); //Moves the reader back to the element node. } if (r.IsEmptyElement) { w.WriteEndElement(); } break; case XmlNodeType.EndElement: w.WriteEndElement(); break; case XmlNodeType.Comment: w.WriteComment(r.Value); break; case XmlNodeType.CDATA: w.WriteCData(r.Value); break; case XmlNodeType.Text: w.WriteString(r.Value); break; } } w.WriteEndDocument(); w.Flush(); m.Position = 0; Util.WriteLog(String.Format(CultureInfo.CurrentCulture, "ManifestWriter.Format t={0}", Environment.TickCount - t1)); return(m); }