Пример #1
0
        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);
        }
Пример #2
0
        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));
            }
        }
Пример #3
0
        public UrlAcl(HTTP_SERVICE_CONFIG_URLACL_SET urlacl)
        {
            this.UrlPrefix = urlacl.KeyDesc.pUrlPrefix;

            var match = Regex.Match(this.UrlPrefix, ":([0-9]+?)/");
            var port  = match.Groups[1].Value;

            this.Port = port;
        }
Пример #4
0
        private static HTTP_SERVICE_CONFIG_URLACL_SET CreateUrlAclSet(string key, string param)
        {
            HTTP_SERVICE_CONFIG_URLACL_SET ReturnValue;

            ReturnValue = new HTTP_SERVICE_CONFIG_URLACL_SET()
            {
                KeyDesc   = new HTTP_SERVICE_CONFIG_URLACL_KEY(key),
                ParamDesc = new HTTP_SERVICE_CONFIG_URLACL_PARAM(param)
            };

            return(ReturnValue);
        }
Пример #5
0
        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));
            }
        }
Пример #6
0
        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));
            }
        }
Пример #7
0
        public static void CreateUrlAcl(HttpConfigUrlAclEntry entry)
        {
            if (entry == null)
            {
                return;
            }
            HTTP_SERVICE_CONFIG_URLACL_SET configInformation = new HTTP_SERVICE_CONFIG_URLACL_SET();

            configInformation.KeyDesc   = new HTTP_SERVICE_CONFIG_URLACL_KEY(entry.UriPrefix);
            configInformation.ParamDesc = new HTTP_SERVICE_CONFIG_URLACL_PARAM(entry.SecurityDescriptor.GetSddlForm(AccessControlSections.All));
            ErrorCode errorCode = HttpSetServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, ref configInformation, Marshal.SizeOf(configInformation), IntPtr.Zero);

            if (errorCode != ErrorCode.ERROR_SUCCESS)
            {
                throw new Win32Exception((int)errorCode);
            }
        }
Пример #8
0
        public static void DeleteUrlAcl(string uriPrefix)
        {
            if (uriPrefix == null)
            {
                return;
            }
            HTTP_SERVICE_CONFIG_URLACL_SET configInformation = new HTTP_SERVICE_CONFIG_URLACL_SET();

            configInformation.KeyDesc = new HTTP_SERVICE_CONFIG_URLACL_KEY(uriPrefix);
            int       configInformationSize = Marshal.SizeOf(configInformation);
            ErrorCode errorCode             = HttpDeleteServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, ref configInformation, configInformationSize, IntPtr.Zero);

            if (errorCode != ErrorCode.ERROR_FILE_NOT_FOUND && errorCode != ErrorCode.ERROR_SUCCESS)
            {
                throw new Win32Exception((int)errorCode);
            }
        }
Пример #9
0
        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;
        }
Пример #10
0
        /// <summary>
        /// Removes a URL ACL exception via the HTTP API.
        /// </summary>
        /// <param name="url">A string containing the URL to remove.</param>
        /// <param name="consoleOutput">An IConsoleOutputWrapper that provides a console for output. If NULL, then the default System.Console will be used.</param>
        /// <returns>A bool that indicates whether the exception was removed successfully.</returns>
        public static bool RemoveUrlException(string url, IConsoleOutputWrapper consoleOutput = null)
        {
            uint   Result;
            bool   ReturnValue = false;
            IntPtr UrlAclSetPointer;

            if (consoleOutput == null)
            {
                consoleOutput = new ConsoleOutput();
            }

            InitialiseHttpApi();

            HTTP_SERVICE_CONFIG_URLACL_SET UrlAclSet = new HTTP_SERVICE_CONFIG_URLACL_SET()
            {
                KeyDesc   = new HTTP_SERVICE_CONFIG_URLACL_KEY(url),
                ParamDesc = new HTTP_SERVICE_CONFIG_URLACL_PARAM("")
            };

            consoleOutput.WriteLine("Delete configuration for '{0}'", UrlAclSet.KeyDesc.pUrlPrefix);
            UrlAclSetPointer = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_SET)));
            Marshal.StructureToPtr(UrlAclSet, UrlAclSetPointer, false);

            // Attempt to set the configuration and process the result
            Result = NativeMethods.HttpDeleteServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, UrlAclSetPointer, Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_SET)), IntPtr.Zero);
            switch (Result)
            {
            case 0:
                consoleOutput.WriteLine("Success.");
                ReturnValue = true;
                break;

            default:
                consoleOutput.WriteLine("Error {0} returned.", Result);
                break;
            }

            TerminateHttpApi();

            consoleOutput.WriteLine();

            return(ReturnValue);
        }
Пример #11
0
        public void SetHttpNamespaceAcl(string urlPrefix, string acl)
        {
            HTTPAPI_VERSION version = new HTTPAPI_VERSION();

            version.HttpApiMajorVersion = 1;
            version.HttpApiMinorVersion = 0;

            HttpInitialize(version, HTTP_INITIALIZE_CONFIG, IntPtr.Zero);

            HTTP_SERVICE_CONFIG_URLACL_SET urlAclConfig = new HTTP_SERVICE_CONFIG_URLACL_SET();

            urlAclConfig.KeyDesc.pUrlPrefix = urlPrefix;
            urlAclConfig.ParamDesc.pStringSecurityDescriptor = acl;

            IntPtr UrlAclConfig = Marshal.AllocHGlobal(Marshal.SizeOf(urlAclConfig));

            Marshal.StructureToPtr(urlAclConfig, UrlAclConfig, false);

            try
            {
                uint retval = HttpSetServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, UrlAclConfig, (uint)Marshal.SizeOf(urlAclConfig), IntPtr.Zero);

                if (retval != 0 && retval != 183)
                {
                    throw new ExternalException("Error Setting Configuration: " + SecurityIdentity.GetErrorMessage(retval));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
            finally
            {
                if (UrlAclConfig != IntPtr.Zero)
                {
                    Marshal.DestroyStructure(UrlAclConfig, typeof(HTTP_SERVICE_CONFIG_URLACL_SET));
                    Marshal.FreeHGlobal(UrlAclConfig);
                }
            }
        }
Пример #12
0
        public static uint DeleteReservation(string networkURL)
        {
            uint retVal = NOERROR;

            if (String.IsNullOrEmpty(networkURL))
            {
                throw new ArgumentNullException("networkURL");
            }
            HTTP_SERVICE_CONFIG_URLACL_SET oldReservation = QueryReservation(networkURL);

            if (oldReservation.KeyDesc.pUrlPrefix == "" && networkURL.Contains("+"))
            {
                networkURL     = networkURL.Replace('+', '*');
                oldReservation = QueryReservation(networkURL);
            }
            if (oldReservation.KeyDesc.pUrlPrefix == "")
            {
                return(ERROR_FILE_NOT_FOUND);
            }
            CallHttpApi(
                delegate
            {
                IntPtr pInputConfigInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_SET)));
                Marshal.StructureToPtr(oldReservation, pInputConfigInfo, false);
                try
                {
                    retVal = HttpDeleteServiceConfiguration(IntPtr.Zero,
                                                            HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo,
                                                            pInputConfigInfo,
                                                            Marshal.SizeOf(oldReservation),
                                                            IntPtr.Zero);
                }
                finally
                {
                    Marshal.FreeCoTaskMem(pInputConfigInfo);
                }
            });
            return(retVal);
        }
Пример #13
0
 static extern ErrorCode HttpDeleteServiceConfiguration(IntPtr serviceIntPtr, HTTP_SERVICE_CONFIG_ID configId, ref HTTP_SERVICE_CONFIG_URLACL_SET configInformation, int configInformationLength, IntPtr pOverlapped);
Пример #14
0
        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);
                }
            });
        }
Пример #15
0
        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);
        }
Пример #16
0
 public static extern uint HttpDeleteServiceConfiguration(
     IntPtr serviceIntPtr,
     HTTP_SERVICE_CONFIG_ID configId,
     HTTP_SERVICE_CONFIG_URLACL_SET pConfigInformation,
     int configInformationLength,
     IntPtr pOverlapped);
Пример #17
0
        private static bool CreateUrlAclSetFromUserAccount(string key, string userAccount, out HTTP_SERVICE_CONFIG_URLACL_SET urlAclSet)
        {
            bool ReturnValue = false;

            // Defaults
            urlAclSet = default(HTTP_SERVICE_CONFIG_URLACL_SET);

            if (CreateSddlFromUserAccount(userAccount, out string SecurityDescriptor))
            {
                urlAclSet   = CreateUrlAclSet(key, SecurityDescriptor);
                ReturnValue = true;
            }

            return(ReturnValue);
        }
Пример #18
0
		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();
			}
		}
Пример #19
0
        /// <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);
        }
Пример #20
0
 internal static extern Win32Error HttpSetServiceConfiguration(IntPtr serviceHandle, HTTP_SERVICE_CONFIG_ID configId, HTTP_SERVICE_CONFIG_URLACL_SET pConfigInformation, int configInformationLength, IntPtr pOverlapped);
Пример #21
0
        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));
        }
Пример #22
0
        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));
            }
        }
Пример #23
0
 static extern ErrorCode HttpSetServiceConfiguration(IntPtr nullHandle, HTTP_SERVICE_CONFIG_ID configId, ref HTTP_SERVICE_CONFIG_URLACL_SET configInformation, int configInformationLength, IntPtr pOverlapped);
Пример #24
0
 static extern int HttpDeleteServiceConfigurationAcl(
     IntPtr mustBeZero, int configID,
     [In] ref HTTP_SERVICE_CONFIG_URLACL_SET configInfo,
     int configInfoLength, IntPtr mustBeZero2);
Пример #25
0
        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);
        }
Пример #26
0
        /// <summary>
        /// Sets the application access rules for the specified URL.
        /// </summary>
        public static void SetAccessRules(string url, IList <HttpAccessRule> rules, bool replaceExisting)
        {
            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);
            }

            // fetch existing rules if not replacing them.
            if (!replaceExisting)
            {
                IList <HttpAccessRule> existingRules = GetAccessRules(url);

                if (existingRules.Count > 0)
                {
                    List <HttpAccessRule> mergedRules = new List <HttpAccessRule>(existingRules);
                    mergedRules.AddRange(rules);
                    rules = mergedRules;
                }
            }

            HTTP_SERVICE_CONFIG_URLACL_SET update = new HTTP_SERVICE_CONFIG_URLACL_SET();

            update.KeyDesc.pUrlPrefix = url;
            update.ParamDesc.pStringSecurityDescriptor = FormatSddl(rules);

            IntPtr pStruct    = IntPtr.Zero;
            int    updateSize = Marshal.SizeOf <HTTP_SERVICE_CONFIG_URLACL_SET>();

            try
            {
                pStruct = Marshal.AllocHGlobal(updateSize);
                NativeMethods.ZeroMemory(pStruct, updateSize);

                Marshal.StructureToPtr(update, pStruct, false);

                error = NativeMethods.HttpDeleteServiceConfiguration(
                    IntPtr.Zero,
                    HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo,
                    pStruct,
                    updateSize,
                    IntPtr.Zero);

                if (error != HttpError.ERROR_FILE_NOT_FOUND && error != HttpError.NO_ERROR)
                {
                    throw ServiceResultException.Create(
                              StatusCodes.BadUnexpectedError,
                              "Could not delete existing access rules for HTTP url.\r\nError={1}, Url={0}",
                              url,
                              error);
                }

                if (rules.Count > 0)
                {
                    error = NativeMethods.HttpSetServiceConfiguration(
                        IntPtr.Zero,
                        HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo,
                        pStruct,
                        updateSize,
                        IntPtr.Zero);

                    if (error != HttpError.NO_ERROR)
                    {
                        throw ServiceResultException.Create(
                                  StatusCodes.BadUnexpectedError,
                                  "Could not set the access rules for HTTP url.\r\nError={1}, Url={0}",
                                  url,
                                  error);
                    }
                }
            }
            finally
            {
                if (pStruct != IntPtr.Zero)
                {
                    Marshal.DestroyStructure <HTTP_SERVICE_CONFIG_URLACL_SET>(pStruct);
                    Marshal.FreeHGlobal(pStruct);
                }

                NativeMethods.HttpTerminate(HttpInitFlag.HTTP_INITIALIZE_CONFIG, IntPtr.Zero);
            }
        }
Пример #27
0
        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);
        }
Пример #28
0
        /// <summary>
        /// Sets the application access rules for the specified URL. 
        /// </summary>
        public static void SetAccessRules(string url, IList<HttpAccessRule> rules, bool replaceExisting)
        {
            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);
            }

            // fetch existing rules if not replacing them.
            if (!replaceExisting)
            {
                IList<HttpAccessRule> existingRules = GetAccessRules(url);

                if (existingRules.Count > 0)
                {                
                    List<HttpAccessRule> mergedRules = new List<HttpAccessRule>(existingRules);
                    mergedRules.AddRange(rules);
                    rules = mergedRules;
                }
            }

            HTTP_SERVICE_CONFIG_URLACL_SET update = new HTTP_SERVICE_CONFIG_URLACL_SET();

            update.KeyDesc.pUrlPrefix = url;
            update.ParamDesc.pStringSecurityDescriptor = FormatSddl(rules);
            
            IntPtr pStruct = IntPtr.Zero;
            int updateSize = Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_SET));

            try
            {
                pStruct = Marshal.AllocHGlobal(updateSize);
                NativeMethods.ZeroMemory(pStruct, updateSize);

                Marshal.StructureToPtr(update, pStruct, false);

                error = NativeMethods.HttpDeleteServiceConfiguration(
                    IntPtr.Zero,
                    HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo,
                    pStruct,
                    updateSize,
                    IntPtr.Zero);
                       
                if (error != HttpError.ERROR_FILE_NOT_FOUND && error != HttpError.NO_ERROR)
                {
                    throw ServiceResultException.Create(
                        StatusCodes.BadUnexpectedError,
                        "Could not delete existing access rules for HTTP url.\r\nError={1}, Url={0}",
                        url,
                        error);
                }
                
                if (rules.Count > 0)
                {
                    error = NativeMethods.HttpSetServiceConfiguration(
                        IntPtr.Zero,
                        HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo,
                        pStruct,
                        updateSize,
                        IntPtr.Zero);

                    if (error != HttpError.NO_ERROR)
                    {
                        throw ServiceResultException.Create(
                            StatusCodes.BadUnexpectedError,
                            "Could not set the access rules for HTTP url.\r\nError={1}, Url={0}",
                            url,
                            error);
                    }        
                }
            }
            finally
            {
                if (pStruct != IntPtr.Zero)
                {
                    Marshal.DestroyStructure(pStruct, typeof(HTTP_SERVICE_CONFIG_URLACL_SET));
                    Marshal.FreeHGlobal(pStruct);
                }

                NativeMethods.HttpTerminate(HttpInitFlag.HTTP_INITIALIZE_CONFIG, IntPtr.Zero);
            }
        }
Пример #29
0
        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);
        }