public static HttpConfigUrlAcl GetHttpReservations() { HttpConfigUrlAcl result = new HttpConfigUrlAcl(); int size; for (int count = 0;; count++) { HTTP_SERVICE_CONFIG_URLACL_QUERY query = new HTTP_SERVICE_CONFIG_URLACL_QUERY(HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryNext, new HTTP_SERVICE_CONFIG_URLACL_KEY(null), count); ErrorCode errorCode = HttpQueryServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, ref query, Marshal.SizeOf(query), null, 0, out size, IntPtr.Zero); if (errorCode != ErrorCode.ERROR_SUCCESS) { if (errorCode == ErrorCode.ERROR_NO_MORE_ITEMS) { break; } if (errorCode != ErrorCode.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception((int)errorCode); } byte[] buffer = new byte[size]; fixed(byte *b = buffer) { errorCode = HttpQueryServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, ref query, Marshal.SizeOf(query), b, size, out size, IntPtr.Zero); if (errorCode != ErrorCode.ERROR_SUCCESS) { throw new Win32Exception((int)errorCode); } HTTP_SERVICE_CONFIG_URLACL_SET output = (HTTP_SERVICE_CONFIG_URLACL_SET)Marshal.PtrToStructure((IntPtr)b, typeof(HTTP_SERVICE_CONFIG_URLACL_SET)); result.Add(output.KeyDesc.pUrlPrefix, output.ParamDesc.pStringSecurityDescriptor); } } } return(result); }
/// <summary> /// Gets the application access rules for the specified URL. /// </summary> public static IList<HttpAccessRule> GetAccessRules(string url) { List<HttpAccessRule> accessRules = new List<HttpAccessRule>(); HttpError error = NativeMethods.HttpInitialize( new HTTPAPI_VERSION(1, 0), HttpInitFlag.HTTP_INITIALIZE_CONFIG, IntPtr.Zero); if (error != HttpError.NO_ERROR) { throw ServiceResultException.Create( StatusCodes.BadUnexpectedError, "Could not initialize HTTP library.\r\nError={0}", error); } HTTP_SERVICE_CONFIG_URLACL_QUERY query = new HTTP_SERVICE_CONFIG_URLACL_QUERY(); query.QueryDesc = HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryNext; if (!String.IsNullOrEmpty(url)) { query.QueryDesc = HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryExact; query.KeyDesc.pUrlPrefix = url; } IntPtr pInput = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_QUERY))); NativeMethods.ZeroMemory(pInput, Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_QUERY))); IntPtr pOutput = IntPtr.Zero; try { for (query.dwToken = 0; error == HttpError.NO_ERROR; query.dwToken++) { Marshal.StructureToPtr(query, pInput, true); int requiredBufferLength = 0; error = NativeMethods.HttpQueryServiceConfiguration( IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInput, Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_QUERY)), pOutput, requiredBufferLength, out requiredBufferLength, IntPtr.Zero); if (error == HttpError.ERROR_NO_MORE_ITEMS) { break; } if (!String.IsNullOrEmpty(url)) { if (error == HttpError.ERROR_FILE_NOT_FOUND) { break; } } if (error != HttpError.ERROR_INSUFFICIENT_BUFFER) { throw ServiceResultException.Create( StatusCodes.BadUnexpectedError, "Could not read access rules for HTTP url.\r\nError={1}, Url={0}", url, error); } pOutput = Marshal.AllocHGlobal(requiredBufferLength); NativeMethods.ZeroMemory(pOutput, requiredBufferLength); error = NativeMethods.HttpQueryServiceConfiguration( IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInput, Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_QUERY)), pOutput, requiredBufferLength, out requiredBufferLength, IntPtr.Zero); if (error != HttpError.NO_ERROR) { throw ServiceResultException.Create( StatusCodes.BadUnexpectedError, "Could not read access rules for HTTP url.\r\nError={1}, Url={0}", url, error); } HTTP_SERVICE_CONFIG_URLACL_SET result = (HTTP_SERVICE_CONFIG_URLACL_SET)Marshal.PtrToStructure( pOutput, typeof(HTTP_SERVICE_CONFIG_URLACL_SET)); // parse the SDDL and update the access list. ParseSddl(result.KeyDesc.pUrlPrefix, result.ParamDesc.pStringSecurityDescriptor, accessRules); Marshal.FreeHGlobal(pOutput); pOutput = IntPtr.Zero; // all done if requesting the results for a single url. if (!String.IsNullOrEmpty(url)) { break; } } } finally { if (pInput != IntPtr.Zero) { Marshal.DestroyStructure(pInput, typeof(HTTP_SERVICE_CONFIG_URLACL_QUERY)); Marshal.FreeHGlobal(pInput); } if (pOutput != IntPtr.Zero) { Marshal.FreeHGlobal(pOutput); } NativeMethods.HttpTerminate(HttpInitFlag.HTTP_INITIALIZE_CONFIG, IntPtr.Zero); } return accessRules; }
/// <summary> /// Gets the application access rules for the specified URL. /// </summary> public static IList <HttpAccessRule> GetAccessRules(string url) { List <HttpAccessRule> accessRules = new List <HttpAccessRule>(); HttpError error = NativeMethods.HttpInitialize( new HTTPAPI_VERSION(1, 0), HttpInitFlag.HTTP_INITIALIZE_CONFIG, IntPtr.Zero); if (error != HttpError.NO_ERROR) { throw ServiceResultException.Create( StatusCodes.BadUnexpectedError, "Could not initialize HTTP library.\r\nError={0}", error); } HTTP_SERVICE_CONFIG_URLACL_QUERY query = new HTTP_SERVICE_CONFIG_URLACL_QUERY(); query.QueryDesc = HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryNext; if (!String.IsNullOrEmpty(url)) { query.QueryDesc = HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryExact; query.KeyDesc.pUrlPrefix = url; } IntPtr pInput = Marshal.AllocHGlobal(Marshal.SizeOf <HTTP_SERVICE_CONFIG_URLACL_QUERY>()); NativeMethods.ZeroMemory(pInput, Marshal.SizeOf <HTTP_SERVICE_CONFIG_URLACL_QUERY>()); IntPtr pOutput = IntPtr.Zero; try { for (query.dwToken = 0; error == HttpError.NO_ERROR; query.dwToken++) { Marshal.StructureToPtr(query, pInput, true); int requiredBufferLength = 0; error = NativeMethods.HttpQueryServiceConfiguration( IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInput, Marshal.SizeOf <HTTP_SERVICE_CONFIG_URLACL_QUERY>(), pOutput, requiredBufferLength, out requiredBufferLength, IntPtr.Zero); if (error == HttpError.ERROR_NO_MORE_ITEMS) { break; } if (!String.IsNullOrEmpty(url)) { if (error == HttpError.ERROR_FILE_NOT_FOUND) { break; } } if (error != HttpError.ERROR_INSUFFICIENT_BUFFER) { throw ServiceResultException.Create( StatusCodes.BadUnexpectedError, "Could not read access rules for HTTP url.\r\nError={1}, Url={0}", url, error); } pOutput = Marshal.AllocHGlobal(requiredBufferLength); NativeMethods.ZeroMemory(pOutput, requiredBufferLength); error = NativeMethods.HttpQueryServiceConfiguration( IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInput, Marshal.SizeOf <HTTP_SERVICE_CONFIG_URLACL_QUERY>(), pOutput, requiredBufferLength, out requiredBufferLength, IntPtr.Zero); if (error != HttpError.NO_ERROR) { throw ServiceResultException.Create( StatusCodes.BadUnexpectedError, "Could not read access rules for HTTP url.\r\nError={1}, Url={0}", url, error); } HTTP_SERVICE_CONFIG_URLACL_SET result = (HTTP_SERVICE_CONFIG_URLACL_SET)Marshal.PtrToStructure <HTTP_SERVICE_CONFIG_URLACL_SET>(pOutput); // parse the SDDL and update the access list. ParseSddl(result.KeyDesc.pUrlPrefix, result.ParamDesc.pStringSecurityDescriptor, accessRules); Marshal.FreeHGlobal(pOutput); pOutput = IntPtr.Zero; // all done if requesting the results for a single url. if (!String.IsNullOrEmpty(url)) { break; } } } finally { if (pInput != IntPtr.Zero) { Marshal.DestroyStructure <HTTP_SERVICE_CONFIG_URLACL_QUERY>(pInput); Marshal.FreeHGlobal(pInput); } if (pOutput != IntPtr.Zero) { Marshal.FreeHGlobal(pOutput); } NativeMethods.HttpTerminate(HttpInitFlag.HTTP_INITIALIZE_CONFIG, IntPtr.Zero); } return(accessRules); }
static extern ErrorCode HttpQueryServiceConfiguration(IntPtr nullHandle, HTTP_SERVICE_CONFIG_ID ConfigId, ref HTTP_SERVICE_CONFIG_URLACL_QUERY pInputConfigInfo, int InputConfigInfoLength, byte *pOutputConfigInfo, int OutputConfigInfoLength, out int pReturnLength, IntPtr pOverlapped);
public static bool FindUrlPrefix(string urlPrefix) { HTTP_SERVICE_CONFIG_URLACL_QUERY query = new HTTP_SERVICE_CONFIG_URLACL_QUERY(); query.QueryDesc = HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryNext; IntPtr pQuery = Marshal.AllocHGlobal(Marshal.SizeOf(query)); try { uint retval = NO_ERROR; for (query.dwToken = 0; ; query.dwToken++) { Marshal.StructureToPtr(query, pQuery, false); try { uint returnSize = 0; // Get Size retval = HttpQueryServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pQuery, (uint)Marshal.SizeOf(query), IntPtr.Zero, 0, ref returnSize, IntPtr.Zero); if (retval == ERROR_NO_MORE_ITEMS) { break; } if (retval != ERROR_INSUFFICIENT_BUFFER) { throw new Exception("HttpQueryServiceConfiguration returned unexpected error code."); } IntPtr pConfig = Marshal.AllocHGlobal((IntPtr)returnSize); string foundPrefix; try { retval = HttpQueryServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pQuery, (uint)Marshal.SizeOf(query), pConfig, returnSize, ref returnSize, IntPtr.Zero); HTTP_SERVICE_CONFIG_URLACL_SET config = (HTTP_SERVICE_CONFIG_URLACL_SET)Marshal.PtrToStructure(pConfig, typeof(HTTP_SERVICE_CONFIG_URLACL_SET)); foundPrefix = config.KeyDesc.pUrlPrefix; Console.WriteLine(foundPrefix); } finally { Marshal.FreeHGlobal(pConfig); } if (foundPrefix == urlPrefix) { return(true); } } finally { Marshal.DestroyStructure(pQuery, typeof(HTTP_SERVICE_CONFIG_URLACL_QUERY)); } } if (retval != ERROR_NO_MORE_ITEMS) { throw new Exception("HttpQueryServiceConfiguration returned unexpected error code."); } } finally { Marshal.FreeHGlobal(pQuery); } return(false); }
private Dictionary <string, string> FindUrlPrefix(string urlPrefix) { Dictionary <string, string> acls = new Dictionary <string, string>(); HTTP_SERVICE_CONFIG_URLACL_QUERY query = new HTTP_SERVICE_CONFIG_URLACL_QUERY(); query.QueryDesc = HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryNext; IntPtr pQuery = Marshal.AllocHGlobal(Marshal.SizeOf(query)); try { uint retval = NO_ERROR; for (query.dwToken = 0; ; query.dwToken++) { Marshal.StructureToPtr(query, pQuery, false); try { uint returnSize = 0; // Get Size retval = HttpQueryServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pQuery, (uint)Marshal.SizeOf(query), IntPtr.Zero, 0, ref returnSize, IntPtr.Zero); if (retval == ERROR_NO_MORE_ITEMS) { break; } if (retval != ERROR_INSUFFICIENT_BUFFER) { throw new Exception("HttpQueryServiceConfiguration returned unexpected error code."); } IntPtr pConfig = Marshal.AllocHGlobal((IntPtr)returnSize); string foundPrefix; string secdescriptor; try { retval = HttpQueryServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pQuery, (uint)Marshal.SizeOf(query), pConfig, returnSize, ref returnSize, IntPtr.Zero); HTTP_SERVICE_CONFIG_URLACL_SET config = (HTTP_SERVICE_CONFIG_URLACL_SET)Marshal.PtrToStructure(pConfig, typeof(HTTP_SERVICE_CONFIG_URLACL_SET)); string permissions = SDDLParser.Parse(config.ParamDesc.pStringSecurityDescriptor); acls.Add(config.KeyDesc.pUrlPrefix, permissions); } finally { Marshal.FreeHGlobal(pConfig); } } finally { Marshal.DestroyStructure(pQuery, typeof(HTTP_SERVICE_CONFIG_URLACL_QUERY)); } } if (retval != ERROR_NO_MORE_ITEMS) { throw new Exception("HttpQueryServiceConfiguration returned unexpected error code."); } } finally { Marshal.FreeHGlobal(pQuery); } return(acls); }
public static ReadOnlyCollection <UrlReservation> GetAll() { List <UrlReservation> revs = new List <UrlReservation>(); uint retVal = (uint)ErrorCodes.NOERROR; // NOERROR = 0 retVal = NativeMethods.HttpInitialize(HttpApiConstants.HTTPAPI_VERSION_1, HttpApiConstants.HTTP_INITIALIZE_CONFIG, IntPtr.Zero); if ((uint)ErrorCodes.NOERROR == retVal) { HTTP_SERVICE_CONFIG_URLACL_QUERY inputConfigInfoSet = new HTTP_SERVICE_CONFIG_URLACL_QUERY(); inputConfigInfoSet.QueryDesc = HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryNext; int i = 0; while (retVal == 0) { inputConfigInfoSet.dwToken = (uint)i; IntPtr pInputConfigInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_QUERY))); Marshal.StructureToPtr(inputConfigInfoSet, pInputConfigInfo, false); HTTP_SERVICE_CONFIG_URLACL_SET outputConfigInfo = new HTTP_SERVICE_CONFIG_URLACL_SET(); IntPtr pOutputConfigInfo = Marshal.AllocCoTaskMem(0); int returnLength = 0; retVal = NativeMethods.HttpQueryServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoSet), pOutputConfigInfo, returnLength, out returnLength, IntPtr.Zero); if (retVal == ErrorCodes.ERROR_INSUFFICIENT_BUFFER) { Marshal.ZeroFreeCoTaskMemUnicode(pOutputConfigInfo); pOutputConfigInfo = Marshal.AllocCoTaskMem(Convert.ToInt32(returnLength)); retVal = NativeMethods.HttpQueryServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoSet), pOutputConfigInfo, returnLength, out returnLength, IntPtr.Zero); } if (ErrorCodes.NOERROR == retVal) { outputConfigInfo = (HTTP_SERVICE_CONFIG_URLACL_SET)Marshal.PtrToStructure(pOutputConfigInfo, typeof(HTTP_SERVICE_CONFIG_URLACL_SET)); UrlReservation rev = new UrlReservation(outputConfigInfo.KeyDesc.pUrlPrefix, securityIdentifiersFromSDDL(outputConfigInfo.ParamDesc.pStringSecurityDescriptor)); revs.Add(rev); } Marshal.FreeCoTaskMem(pOutputConfigInfo); Marshal.FreeCoTaskMem(pInputConfigInfo); i++; } retVal = NativeMethods.HttpTerminate(HttpApiConstants.HTTP_INITIALIZE_CONFIG, IntPtr.Zero); } if ((uint)ErrorCodes.NOERROR != retVal) { throw new Win32Exception(Convert.ToInt32(retVal)); } return(new ReadOnlyCollection <UrlReservation>(revs)); }
public static HttpNamespaceAcl[] QueryHttpNamespaceAcls() { var result = new List<HttpNamespaceAcl>(); CallHttpApi( delegate { uint token = 0; uint retVal; do { HTTP_SERVICE_CONFIG_URLACL_QUERY inputConfigInfoQuery = new HTTP_SERVICE_CONFIG_URLACL_QUERY { QueryDesc = HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryNext, dwToken = token }; IntPtr pInputConfigInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_QUERY))); Marshal.StructureToPtr(inputConfigInfoQuery, pInputConfigInfo, false); IntPtr pOutputConfigInfo = IntPtr.Zero; int returnLength = 0; const HTTP_SERVICE_CONFIG_ID queryType = HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo; try { int inputConfigInfoSize = Marshal.SizeOf(inputConfigInfoQuery); retVal = HttpQueryServiceConfiguration(IntPtr.Zero, queryType, pInputConfigInfo, inputConfigInfoSize, pOutputConfigInfo, returnLength, out returnLength, IntPtr.Zero); if (ERROR_NO_MORE_ITEMS == retVal) break; if (ERROR_INSUFFICIENT_BUFFER == retVal) // ERROR_INSUFFICIENT_BUFFER = 122 { pOutputConfigInfo = Marshal.AllocCoTaskMem(returnLength); try { retVal = HttpQueryServiceConfiguration( IntPtr.Zero, queryType, pInputConfigInfo, inputConfigInfoSize, pOutputConfigInfo, returnLength, out returnLength, IntPtr.Zero); ThrowWin32ExceptionIfError(retVal); var outputConfigInfo = (HTTP_SERVICE_CONFIG_URLACL_SET) Marshal.PtrToStructure(pOutputConfigInfo, typeof(HTTP_SERVICE_CONFIG_URLACL_SET)); var resultItem = new HttpNamespaceAcl { UrlPrefix = outputConfigInfo.KeyDesc.pUrlPrefix, SecurityDescriptor = outputConfigInfo.ParamDesc.pStringSecurityDescriptor }; result.Add(resultItem); token++; } finally { Marshal.FreeCoTaskMem(pOutputConfigInfo); } } else { ThrowWin32ExceptionIfError(retVal); } } finally { Marshal.FreeCoTaskMem(pInputConfigInfo); } } while (NOERROR == retVal); }); return result.ToArray(); }
public static HttpUrlSecurity[] GetHttpUrlSecurity() { InitializeHttp(); try { var urls = new List<HttpUrlSecurity>(); uint recordNum = 0; while (true) { var inputConfigInfoQuery = new HTTP_SERVICE_CONFIG_URLACL_QUERY { QueryDesc = HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryNext, dwToken = recordNum++ }; var size = Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_QUERY)); var pInputConfigInfo = Marshal.AllocCoTaskMem(size); Marshal.StructureToPtr(inputConfigInfoQuery, pInputConfigInfo, false); var pOutputConfigInfo = Marshal.AllocCoTaskMem(0); var returnLength = 0; var retVal = HttpQueryServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoQuery), pOutputConfigInfo, returnLength, out returnLength, IntPtr.Zero); if (Win32ErrorCodes.InsufficientBuffer == retVal) { Marshal.FreeCoTaskMem(pOutputConfigInfo); pOutputConfigInfo = Marshal.AllocCoTaskMem(Convert.ToInt32(returnLength)); retVal = HttpQueryServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoQuery), pOutputConfigInfo, returnLength, out returnLength, IntPtr.Zero); } else if( Win32ErrorCodes.NoMoreItems == retVal ) { break; } if (Win32ErrorCodes.Ok == retVal) { var outputConfigInfo = (HTTP_SERVICE_CONFIG_URLACL_SET) Marshal.PtrToStructure(pOutputConfigInfo, typeof(HTTP_SERVICE_CONFIG_URLACL_SET)); var sd = new CommonSecurityDescriptor(false, false, outputConfigInfo.ParamDesc.pStringSecurityDescriptor); var urlAcl = new HttpUrlSecurity(outputConfigInfo.KeyDesc.pUrlPrefix); foreach (var genericAce in sd.DiscretionaryAcl) { var rule = (CommonAce) genericAce; IdentityReference id = rule.SecurityIdentifier; if (id.IsValidTargetType(typeof(NTAccount))) { try { id = id.Translate(typeof(NTAccount)); } catch { } } urlAcl.AddAccessRule(new HttpUrlAccessRule(id, (HttpUrlAccessRights)rule.AccessMask)); } urls.Add(urlAcl); } else { throw new Win32Exception(); } } return urls.ToArray(); } finally { TerminateHttp(); } }
internal static extern Win32Error HttpQueryServiceConfiguration(IntPtr serviceHandle, HTTP_SERVICE_CONFIG_ID configId, HTTP_SERVICE_CONFIG_URLACL_QUERY pInputConfigInfo, int inputConfigInfoLength, IntPtr pOutputConfigInfo, int outputConfigInfoLength, out int pReturnLength, IntPtr pOverlapped);
private void Load() { 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)); } HTTP_SERVICE_CONFIG_URLACL_QUERY inputConfigInfoQuery = new HTTP_SERVICE_CONFIG_URLACL_QUERY(); inputConfigInfoQuery.QueryDesc = HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryNext; int i = 0; while (retVal == 0) { inputConfigInfoQuery.dwToken = (uint)i; IntPtr pInputConfigInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_QUERY))); Marshal.StructureToPtr(inputConfigInfoQuery, pInputConfigInfo, false); HTTP_SERVICE_CONFIG_URLACL_SET outputConfigInfo = new HTTP_SERVICE_CONFIG_URLACL_SET(); IntPtr pOutputConfigInfo = Marshal.AllocCoTaskMem(0); int returnLength = 0; retVal = PInvoke.HttpQueryServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoQuery), pOutputConfigInfo, returnLength, out returnLength, IntPtr.Zero); if (PInvoke.ERROR_INSUFFICIENT_BUFFER == retVal) { //Marshal the proper buffer size back from the response Marshal.FreeCoTaskMem(pOutputConfigInfo); pOutputConfigInfo = Marshal.AllocCoTaskMem(Convert.ToInt32(returnLength)); retVal = PInvoke.HttpQueryServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputConfigInfo, Marshal.SizeOf(inputConfigInfoQuery), pOutputConfigInfo, returnLength, out returnLength, IntPtr.Zero); } if (PInvoke.NO_ERROR == retVal) { outputConfigInfo = (HTTP_SERVICE_CONFIG_URLACL_SET)Marshal.PtrToStructure(pOutputConfigInfo, typeof(HTTP_SERVICE_CONFIG_URLACL_SET)); Debug.WriteLine(outputConfigInfo.KeyDesc.pUrlPrefix); this.UrlAcls.Add(new UrlAcl(outputConfigInfo)); Marshal.FreeCoTaskMem(pOutputConfigInfo); Marshal.FreeCoTaskMem(pInputConfigInfo); } else if (PInvoke.ERROR_NO_MORE_ITEMS == retVal) { Marshal.FreeCoTaskMem(pOutputConfigInfo); Marshal.FreeCoTaskMem(pInputConfigInfo); } else { Marshal.FreeCoTaskMem(pOutputConfigInfo); Marshal.FreeCoTaskMem(pInputConfigInfo); throw new Exception(string.Format("Could not list Url Acls. Code {0}", retVal)); } //Marshal.FreeCoTaskMem(pOutputConfigInfo); //Marshal.FreeCoTaskMem(pInputConfigInfo); i++; } PInvoke.HttpTerminate(PInvoke.HTTP_INITIALIZE_CONFIG, IntPtr.Zero); }
public static HttpNamespaceAcl[] QueryHttpNamespaceAcls() { var result = new List <HttpNamespaceAcl>(); CallHttpApi( delegate { uint token = 0; uint retVal; do { HTTP_SERVICE_CONFIG_URLACL_QUERY inputConfigInfoQuery = new HTTP_SERVICE_CONFIG_URLACL_QUERY { QueryDesc = HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryNext, dwToken = token }; IntPtr pInputConfigInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_QUERY))); Marshal.StructureToPtr(inputConfigInfoQuery, pInputConfigInfo, false); IntPtr pOutputConfigInfo = IntPtr.Zero; int returnLength = 0; const HTTP_SERVICE_CONFIG_ID queryType = HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo; try { int inputConfigInfoSize = Marshal.SizeOf(inputConfigInfoQuery); retVal = HttpQueryServiceConfiguration(IntPtr.Zero, queryType, pInputConfigInfo, inputConfigInfoSize, pOutputConfigInfo, returnLength, out returnLength, IntPtr.Zero); if (ERROR_NO_MORE_ITEMS == retVal) { break; } if (ERROR_INSUFFICIENT_BUFFER == retVal) // ERROR_INSUFFICIENT_BUFFER = 122 { pOutputConfigInfo = Marshal.AllocCoTaskMem(returnLength); try { retVal = HttpQueryServiceConfiguration( IntPtr.Zero, queryType, pInputConfigInfo, inputConfigInfoSize, pOutputConfigInfo, returnLength, out returnLength, IntPtr.Zero); ThrowWin32ExceptionIfError(retVal); var outputConfigInfo = (HTTP_SERVICE_CONFIG_URLACL_SET) Marshal.PtrToStructure(pOutputConfigInfo, typeof(HTTP_SERVICE_CONFIG_URLACL_SET)); var resultItem = new HttpNamespaceAcl { UrlPrefix = outputConfigInfo.KeyDesc.pUrlPrefix, SecurityDescriptor = outputConfigInfo.ParamDesc.pStringSecurityDescriptor }; result.Add(resultItem); token++; } finally { Marshal.FreeCoTaskMem(pOutputConfigInfo); } } else { ThrowWin32ExceptionIfError(retVal); } } finally { Marshal.FreeCoTaskMem(pInputConfigInfo); } } while (NOERROR == retVal); }); return(result.ToArray()); }
private static HTTP_SERVICE_CONFIG_URLACL_SET QueryReservation(string networkURL) { if (String.IsNullOrEmpty(networkURL)) { throw new ArgumentNullException("networkURL"); } HTTP_SERVICE_CONFIG_URLACL_SET res = new HTTP_SERVICE_CONFIG_URLACL_SET { KeyDesc = new HTTP_SERVICE_CONFIG_URLACL_KEY("") }; CallHttpApi( delegate { HTTP_SERVICE_CONFIG_URLACL_QUERY inputquery = new HTTP_SERVICE_CONFIG_URLACL_QUERY { QueryDesc = HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryExact, KeyDesc = new HTTP_SERVICE_CONFIG_URLACL_KEY(networkURL) }; IntPtr pInputQuery = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_QUERY))); Marshal.StructureToPtr(inputquery, pInputQuery, false); IntPtr pOutputConfigInfo = IntPtr.Zero; int returnLength = 0; try { uint retVal = HttpQueryServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputQuery, Marshal.SizeOf(inputquery), pOutputConfigInfo, returnLength, out returnLength, IntPtr.Zero); if (ERROR_INSUFFICIENT_BUFFER == retVal) { pOutputConfigInfo = Marshal.AllocCoTaskMem(returnLength); retVal = HttpQueryServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, pInputQuery, Marshal.SizeOf(inputquery), pOutputConfigInfo, returnLength, out returnLength, IntPtr.Zero); if (ERROR_FILE_NOT_FOUND == retVal) { return; } ThrowWin32ExceptionIfError(retVal); res = (HTTP_SERVICE_CONFIG_URLACL_SET)Marshal.PtrToStructure(pOutputConfigInfo, typeof(HTTP_SERVICE_CONFIG_URLACL_SET)); } else if (ERROR_FILE_NOT_FOUND != retVal) { ThrowWin32ExceptionIfError(retVal); } } finally { Marshal.FreeCoTaskMem(pInputQuery); if (pOutputConfigInfo != IntPtr.Zero) { Marshal.FreeCoTaskMem(pOutputConfigInfo); } } } ); return(res); }