private static void freeURL(string networkURL, string securityDescriptor) { uint retVal = (uint)ErrorCodes.NOERROR; retVal = NativeMethods.HttpInitialize(HttpApiConstants.HTTPAPI_VERSION_1, HttpApiConstants.HTTP_INITIALIZE_CONFIG, IntPtr.Zero); if ((uint)ErrorCodes.NOERROR == retVal) { HTTP_SERVICE_CONFIG_URLACL_KEY urlAclKey = new HTTP_SERVICE_CONFIG_URLACL_KEY(networkURL); HTTP_SERVICE_CONFIG_URLACL_PARAM urlAclParam = new HTTP_SERVICE_CONFIG_URLACL_PARAM(securityDescriptor); HTTP_SERVICE_CONFIG_URLACL_SET urlAclSet = new HTTP_SERVICE_CONFIG_URLACL_SET(); urlAclSet.KeyDesc = urlAclKey; urlAclSet.ParamDesc = urlAclParam; IntPtr configInformation = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_SET))); Marshal.StructureToPtr(urlAclSet, configInformation, false); int configInformationSize = Marshal.SizeOf(urlAclSet); retVal = NativeMethods.HttpDeleteServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, configInformation, configInformationSize, IntPtr.Zero); Marshal.FreeCoTaskMem(configInformation); NativeMethods.HttpTerminate(HttpApiConstants.HTTP_INITIALIZE_CONFIG, IntPtr.Zero); } if ((uint)ErrorCodes.NOERROR != retVal) { throw new Win32Exception(Convert.ToInt32(retVal)); } }
public static void ReserveURL(string networkURL, string securityDescriptor) { uint retVal = (uint)ErrorCodes.NOERROR; // NOERROR = 0 HttpApi.HTTPAPI_VERSION HttpApiVersion = new HttpApi.HTTPAPI_VERSION(1, 0); retVal = HttpApi.HttpInitialize(HttpApiVersion, HttpApi.HTTP_INITIALIZE_CONFIG, IntPtr.Zero); if ((uint)ErrorCodes.NOERROR == retVal) { HTTP_SERVICE_CONFIG_URLACL_KEY keyDesc = new HTTP_SERVICE_CONFIG_URLACL_KEY(networkURL); HTTP_SERVICE_CONFIG_URLACL_PARAM paramDesc = new HTTP_SERVICE_CONFIG_URLACL_PARAM(securityDescriptor); HTTP_SERVICE_CONFIG_URLACL_SET inputConfigInfoSet = new HTTP_SERVICE_CONFIG_URLACL_SET(); inputConfigInfoSet.KeyDesc = keyDesc; inputConfigInfoSet.ParamDesc = paramDesc; IntPtr pInputConfigInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_SET))); Marshal.StructureToPtr(inputConfigInfoSet, pInputConfigInfo, false); retVal = HttpApi.HttpSetServiceConfiguration(IntPtr.Zero, HttpApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoSet), IntPtr.Zero); if ((uint)ErrorCodes.ERROR_ALREADY_EXISTS == retVal) // ERROR_ALREADY_EXISTS = 183 { retVal = HttpApi.HttpDeleteServiceConfiguration(IntPtr.Zero, HttpApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoSet), IntPtr.Zero); if ((uint)ErrorCodes.NOERROR == retVal) { retVal = HttpApi.HttpSetServiceConfiguration(IntPtr.Zero, HttpApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoSet), IntPtr.Zero); } } Marshal.FreeCoTaskMem(pInputConfigInfo); HttpApi.HttpTerminate(HttpApi.HTTP_INITIALIZE_CONFIG, IntPtr.Zero); } if ((uint)ErrorCodes.NOERROR != retVal) { throw new Win32Exception(Convert.ToInt32(retVal)); } }
private static void reserveURL(string networkURL, string securityDescriptor, bool deleteIfExists) { uint retVal = NativeMethods.HttpInitialize(HTTPAPI_VERSION.VERSION_1, Flags.HTTP_INITIALIZE_CONFIG, IntPtr.Zero); if (retVal == ReturnCodes.NO_ERROR) { var keyDesc = new HTTP_SERVICE_CONFIG_URLACL_KEY(networkURL); var paramDesc = new HTTP_SERVICE_CONFIG_URLACL_PARAM(securityDescriptor); var inputConfigInfoSet = new HTTP_SERVICE_CONFIG_URLACL_SET() { KeyDesc = keyDesc, ParamDesc = paramDesc }; IntPtr pInputConfigInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_SET))); Marshal.StructureToPtr(inputConfigInfoSet, pInputConfigInfo, false); retVal = NativeMethods.HttpSetServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoSet), IntPtr.Zero); if (ReturnCodes.ERROR_ALREADY_EXISTS == retVal && deleteIfExists) { retVal = NativeMethods.HttpDeleteServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoSet), IntPtr.Zero); if (retVal == ReturnCodes.NO_ERROR) { retVal = NativeMethods.HttpSetServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoSet), IntPtr.Zero); } } Marshal.FreeCoTaskMem(pInputConfigInfo); NativeMethods.HttpTerminate(Flags.HTTP_INITIALIZE_CONFIG, IntPtr.Zero); } if (retVal != ReturnCodes.NO_ERROR) { throw new Win32Exception(Convert.ToInt32(retVal)); } }
private void SetUrlAcl() { var retVal = PInvoke.HttpInitialize(new HTTPAPI_VERSION(2, 0), PInvoke.HTTP_INITIALIZE_CONFIG, IntPtr.Zero); if ((uint)PInvoke.NO_ERROR != retVal) { throw new Exception(string.Format("Could not set the initialize the HTTP API. Code {0}", retVal)); } var url = string.Format("http://{0}.ngrok.com:{1}/", this.Subdomain, this.LocalhostPort); HTTP_SERVICE_CONFIG_URLACL_KEY keyDesc = new HTTP_SERVICE_CONFIG_URLACL_KEY(url); HTTP_SERVICE_CONFIG_URLACL_PARAM paramDesc = new HTTP_SERVICE_CONFIG_URLACL_PARAM("D:(A;;GX;;;WD)"); HTTP_SERVICE_CONFIG_URLACL_SET inputConfigInfoSet = new HTTP_SERVICE_CONFIG_URLACL_SET(); inputConfigInfoSet.KeyDesc = keyDesc; inputConfigInfoSet.ParamDesc = paramDesc; IntPtr pInputConfigInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_SET))); Marshal.StructureToPtr(inputConfigInfoSet, pInputConfigInfo, false); retVal = PInvoke.HttpSetServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoSet), IntPtr.Zero); Marshal.FreeCoTaskMem(pInputConfigInfo); PInvoke.HttpTerminate(PInvoke.HTTP_INITIALIZE_CONFIG, IntPtr.Zero); if (PInvoke.NO_ERROR != retVal) { // Error Codes: // 5 - ERROR_ACCESS_DENIED // 183 - ERROR_ALREADY_EXISTS throw new Exception(string.Format("Could not set the Url Acl. Code {0}", retVal)); } _hasUrlAcl = true; }
public static void ReserveUrl(string networkUrl, string securityDescriptor) { TraceSource.WriteInfo( PortAclUtility.TraceType, "Started to Reserve Url. networkUrl: {0}, securityDescriptor: {1}.", networkUrl, securityDescriptor); uint retVal = (uint)NOERROR; // NOERROR = 0 HTTPAPI_VERSION httpApiVersion = new HTTPAPI_VERSION(1, 0); retVal = HttpInitialize(httpApiVersion, HTTP_INITIALIZE_CONFIG, IntPtr.Zero); TraceSource.WriteInfo( PortAclUtility.TraceType, "HttpInitialize completed with return value: {0}.", retVal); if ((uint)NOERROR == retVal) { HTTP_SERVICE_CONFIG_URLACL_KEY keyDesc = new HTTP_SERVICE_CONFIG_URLACL_KEY(networkUrl); HTTP_SERVICE_CONFIG_URLACL_PARAM paramDesc = new HTTP_SERVICE_CONFIG_URLACL_PARAM(securityDescriptor); HTTP_SERVICE_CONFIG_URLACL_SET inputConfigInfoSet = new HTTP_SERVICE_CONFIG_URLACL_SET { KeyDesc = keyDesc, ParamDesc = paramDesc }; IntPtr pInputConfigInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_SET))); Marshal.StructureToPtr(inputConfigInfoSet, pInputConfigInfo, false); retVal = HttpSetServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoSet), IntPtr.Zero); TraceSource.WriteInfo( PortAclUtility.TraceType, "HttpSetServiceConfiguration completed with return value: {0}.", retVal); if ((uint)ERROR_ALREADY_EXISTS == retVal) // ERROR_ALREADY_EXISTS = 183 { retVal = HttpDeleteServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoSet), IntPtr.Zero); TraceSource.WriteInfo( PortAclUtility.TraceType, "HttpDeleteServiceConfiguration(outer) completed with return value: {0}.", retVal); if ((uint)ERROR_FILE_NOT_FOUND == retVal) { // This means its possible that the URL is ACLed for the same port but different protocol (http/https) // Try deleting ACL using the other protocol var networkUrlInner = networkUrl.ToLower(); if (networkUrlInner.Contains("https")) { networkUrlInner = networkUrlInner.Replace("https", "http"); } else { networkUrlInner = networkUrlInner.Replace("http", "https"); } HTTP_SERVICE_CONFIG_URLACL_KEY keyDescInner = new HTTP_SERVICE_CONFIG_URLACL_KEY(networkUrlInner); HTTP_SERVICE_CONFIG_URLACL_SET inputConfigInfoSetInner = new HTTP_SERVICE_CONFIG_URLACL_SET { KeyDesc = keyDescInner }; IntPtr pInputConfigInfoInner = IntPtr.Zero; try { pInputConfigInfoInner = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_SET))); Marshal.StructureToPtr(inputConfigInfoSetInner, pInputConfigInfoInner, false); retVal = HttpDeleteServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfoInner, Marshal.SizeOf(inputConfigInfoSetInner), IntPtr.Zero); TraceSource.WriteWarning( PortAclUtility.TraceType, "HttpDeleteServiceConfiguration(Inner) completed with return value: {0}.", retVal); } finally { if (IntPtr.Zero != pInputConfigInfoInner) { Marshal.FreeCoTaskMem(pInputConfigInfoInner); } } } if ((uint)NOERROR == retVal) { retVal = HttpSetServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoSet), IntPtr.Zero); TraceSource.WriteInfo( PortAclUtility.TraceType, "HttpSetServiceConfiguration completed with return value: {0}.", retVal); } } Marshal.FreeCoTaskMem(pInputConfigInfo); HttpTerminate(HTTP_INITIALIZE_CONFIG, IntPtr.Zero); } if ((uint)NOERROR != retVal) { TraceSource.WriteError( PortAclUtility.TraceType, "ReserveUrl failed with error: {0}.", retVal); throw new Win32Exception(Convert.ToInt32(retVal)); } }
private void SetHttpUrlSddl() { InitializeHttp(); try { var keyDesc = new HTTP_SERVICE_CONFIG_URLACL_KEY(Url); var sddl = GetSecurityDescriptorSddlForm(AccessControlSections.Access); var paramDesc = new HTTP_SERVICE_CONFIG_URLACL_PARAM(sddl); var inputConfigInfoSet = new HTTP_SERVICE_CONFIG_URLACL_SET {KeyDesc = keyDesc, ParamDesc = paramDesc}; var pInputConfigInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof (HTTP_SERVICE_CONFIG_URLACL_SET))); Marshal.StructureToPtr(inputConfigInfoSet, pInputConfigInfo, false); ulong retVal; var currentAccess = GetAccessRules(true, true, typeof (SecurityIdentifier)); if (currentAccess.Count == 0) { retVal = HttpDeleteServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoSet), IntPtr.Zero); } else { retVal = HttpSetServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoSet), IntPtr.Zero); if (Win32ErrorCodes.AlreadyExists == retVal) { retVal = HttpDeleteServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoSet), IntPtr.Zero); if (Win32ErrorCodes.Ok == retVal) { retVal = HttpSetServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoSet), IntPtr.Zero); } } } if (Win32ErrorCodes.Ok != retVal) { throw new Win32Exception(); } Marshal.FreeCoTaskMem(pInputConfigInfo); } finally { TerminateHttp(); } }
/// <summary> /// Initializes a new instance of the <see cref="HTTP_SERVICE_CONFIG_URLACL_SET"/> struct /// with the specified <paramref name="urlPrefix"/> and <paramref name="stringSecurityDescriptor"/>. /// </summary> /// <param name="urlPrefix">A pointer to the UrlPrefix string that defines the portion of the URL namespace to which this reservation pertains.</param> /// <param name="stringSecurityDescriptor">A pointer to a Security Descriptor Definition Language (SDDL) string that contains the permissions associated with this URL namespace reservation record.</param> public HTTP_SERVICE_CONFIG_URLACL_SET(string urlPrefix, string stringSecurityDescriptor) { this.KeyDesc = new HTTP_SERVICE_CONFIG_URLACL_KEY(urlPrefix); this.ParamDesc = new HTTP_SERVICE_CONFIG_URLACL_PARAM(stringSecurityDescriptor); }
public static void ReserveURL(string networkURL, string securityDescriptor) { if (String.IsNullOrEmpty(networkURL)) { throw new ArgumentNullException("networkURL"); } if (String.IsNullOrEmpty(securityDescriptor)) { throw new ArgumentNullException("securityDescriptor"); } CallHttpApi( delegate { uint retVal; HTTP_SERVICE_CONFIG_URLACL_KEY keyDesc = new HTTP_SERVICE_CONFIG_URLACL_KEY(networkURL); HTTP_SERVICE_CONFIG_URLACL_PARAM paramDesc = new HTTP_SERVICE_CONFIG_URLACL_PARAM(securityDescriptor); HTTP_SERVICE_CONFIG_URLACL_SET inputConfigInfoSet = new HTTP_SERVICE_CONFIG_URLACL_SET { KeyDesc = keyDesc, ParamDesc = paramDesc }; IntPtr pInputConfigInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_SET))); Marshal.StructureToPtr(inputConfigInfoSet, pInputConfigInfo, false); try { retVal = HttpSetServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoSet), IntPtr.Zero); if (ERROR_ALREADY_EXISTS == retVal) { retVal = DeleteReservation(networkURL); if (ERROR_FILE_NOT_FOUND == retVal) { string networkURLOld = networkURL.Contains("https") ? networkURL.Replace("https", "http") : networkURL.Replace("http", "https"); retVal = DeleteReservation(networkURLOld); } if (NOERROR == retVal) { retVal = HttpSetServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoSet), IntPtr.Zero); } } } finally { Marshal.FreeCoTaskMem(pInputConfigInfo); } if (NOERROR != retVal) { ThrowWin32ExceptionIfError(retVal); } }); }