internal void FlushForWriteCompleted()
        {
#if !FEATURE_PAL // FEATURE_PAL does not enable COM
            if (_IsDirty && _MemoryStream != null)
            {
                WindowsImpersonationContext wiContext = null;

                try
                {
                    ////////////////////////////////////////////////////////////
                    // Step 1: Set the impersonation if required
                    if (_Identity != null)
                    {
                        wiContext = _Identity.Impersonate();
                    }

                    try
                    {
                        IRemoteWebConfigurationHostServer remoteSrv = RemoteWebConfigurationHost.CreateRemoteObject(_Server, _Username, _Domain, _Password);
                        try
                        {
                            remoteSrv.WriteData(_FileName, _TemplateFileName, _MemoryStream.ToArray(), ref _ReadTime);
                        }
                        catch
                        {
                            throw;
                        }
                        finally
                        {
                            while (Marshal.ReleaseComObject(remoteSrv) > 0)
                            {
                            }
                        }
                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        if (wiContext != null)
                        {
                            wiContext.Undo();
                        }
                    }
                }
                catch
                {
                    throw;
                }

                _MemoryStream.Flush();
                _IsDirty = false;
            }
#else // !FEATURE_PAL
            throw new NotSupportedException();
#endif // !FEATURE_PAL
        }
Ejemplo n.º 2
0
        private string CallEncryptOrDecrypt(bool doEncrypt, string xmlString, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)
        {
            string str = null;
            WindowsImpersonationContext context    = null;
            string           assemblyQualifiedName = protectionProvider.GetType().AssemblyQualifiedName;
            ProviderSettings settings = protectedConfigSection.Providers[protectionProvider.Name];

            if (settings == null)
            {
                throw System.Web.Util.ExceptionUtil.ParameterInvalid("protectionProvider");
            }
            NameValueCollection parameters = settings.Parameters;

            if (parameters == null)
            {
                parameters = new NameValueCollection();
            }
            string[] allKeys         = parameters.AllKeys;
            string[] parameterValues = new string[allKeys.Length];
            for (int i = 0; i < allKeys.Length; i++)
            {
                parameterValues[i] = parameters[allKeys[i]];
            }
            if (this._Identity != null)
            {
                context = this._Identity.Impersonate();
            }
            try
            {
                try
                {
                    IRemoteWebConfigurationHostServer o = CreateRemoteObject(this._Server, this._Username, this._Domain, this._Password);
                    try
                    {
                        str = o.DoEncryptOrDecrypt(doEncrypt, xmlString, protectionProvider.Name, assemblyQualifiedName, allKeys, parameterValues);
                    }
                    finally
                    {
                        while (Marshal.ReleaseComObject(o) > 0)
                        {
                        }
                    }
                    return(str);
                }
                finally
                {
                    if (context != null)
                    {
                        context.Undo();
                    }
                }
            }
            catch
            {
            }
            return(str);
        }
 private void Init()
 {
     if (this._MemoryStream == null)
     {
         byte[] buffer = null;
         WindowsImpersonationContext context = null;
         try
         {
             if (this._Identity != null)
             {
                 context = this._Identity.Impersonate();
             }
             try
             {
                 IRemoteWebConfigurationHostServer o = RemoteWebConfigurationHost.CreateRemoteObject(this._Server, this._Username, this._Domain, this._Password);
                 try
                 {
                     buffer = o.GetData(this._FileName, this._streamForWrite, out this._ReadTime);
                 }
                 finally
                 {
                     while (Marshal.ReleaseComObject(o) > 0)
                     {
                     }
                 }
             }
             catch
             {
                 throw;
             }
             finally
             {
                 if (context != null)
                 {
                     context.Undo();
                 }
             }
         }
         catch
         {
             throw;
         }
         if ((buffer == null) || (buffer.Length < 1))
         {
             this._MemoryStream = new MemoryStream();
         }
         else
         {
             this._MemoryStream = new MemoryStream(buffer.Length);
             this._MemoryStream.Write(buffer, 0, buffer.Length);
             this._MemoryStream.Position = 0L;
         }
     }
 }
 internal void FlushForWriteCompleted()
 {
     if (this._IsDirty && (this._MemoryStream != null))
     {
         WindowsImpersonationContext context = null;
         try
         {
             if (this._Identity != null)
             {
                 context = this._Identity.Impersonate();
             }
             try
             {
                 IRemoteWebConfigurationHostServer o = RemoteWebConfigurationHost.CreateRemoteObject(this._Server, this._Username, this._Domain, this._Password);
                 try
                 {
                     o.WriteData(this._FileName, this._TemplateFileName, this._MemoryStream.ToArray(), ref this._ReadTime);
                 }
                 catch
                 {
                     throw;
                 }
                 finally
                 {
                     while (Marshal.ReleaseComObject(o) > 0)
                     {
                     }
                 }
             }
             catch
             {
                 throw;
             }
             finally
             {
                 if (context != null)
                 {
                     context.Undo();
                 }
             }
         }
         catch
         {
             throw;
         }
         this._MemoryStream.Flush();
         this._IsDirty = false;
     }
 }
        /////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////
        public override object GetStreamVersion(string streamName)
        {
#if FEATURE_PAL // FEATURE_PAL: singelton version
            return(s_version);
#else
            // for now, assume it is the same
            // return s_version;
            bool exists;
            long size, createDate, lastWriteDate;
            WindowsImpersonationContext wiContext = null;

            try {
                if (_Identity != null)
                {
                    wiContext = _Identity.Impersonate();
                }

                try {
                    //////////////////////////////////////////////////////////////////
                    // Step 3: Get the type and create the object on the remote server
                    IRemoteWebConfigurationHostServer remoteSrv = CreateRemoteObject(_Server, _Username, _Domain, _Password);
                    try {
                        //////////////////////////////////////////////////////////////////
                        // Step 4: Call the API
                        remoteSrv.GetFileDetails(streamName, out exists, out size, out createDate, out lastWriteDate);
                    }
                    finally {
                        while (Marshal.ReleaseComObject(remoteSrv) > 0)
                        {
                        }                                                   // release the COM object
                    }
                }
                finally {
                    if (wiContext != null)
                    {
                        wiContext.Undo(); // revert impersonation
                    }
                }
            }
            catch {
                // Wrap finally clause with a try to avoid exception clauses being run
                // while the thread is impersonated.
                throw;
            }

            return(new FileDetails(exists, size, DateTime.FromFileTimeUtc(createDate), DateTime.FromFileTimeUtc(lastWriteDate)));
#endif // FEATURE_PAL
        }
Ejemplo n.º 6
0
        public override object GetStreamVersion(string streamName)
        {
            bool flag;
            long num;
            long num2;
            long num3;
            WindowsImpersonationContext context = null;

            try
            {
                if (this._Identity != null)
                {
                    context = this._Identity.Impersonate();
                }
                try
                {
                    IRemoteWebConfigurationHostServer o = CreateRemoteObject(this._Server, this._Username, this._Domain, this._Password);
                    try
                    {
                        o.GetFileDetails(streamName, out flag, out num, out num2, out num3);
                    }
                    finally
                    {
                        while (Marshal.ReleaseComObject(o) > 0)
                        {
                        }
                    }
                }
                finally
                {
                    if (context != null)
                    {
                        context.Undo();
                    }
                }
            }
            catch
            {
                throw;
            }
            return(new FileDetails(flag, num, DateTime.FromFileTimeUtc(num2), DateTime.FromFileTimeUtc(num3)));
        }
        private void Init()
        {
#if !FEATURE_PAL // FEATURE_PAL does not enable COM
            if (_MemoryStream != null)
            {
                return;
            }

            byte[] buf = null;
            WindowsImpersonationContext wiContext = null;

            try
            {
                ////////////////////////////////////////////////////////////
                // Step 1: Set the impersonation if required
                if (_Identity != null)
                {
                    wiContext = _Identity.Impersonate();
                }

                try
                {
                    IRemoteWebConfigurationHostServer remoteSrv = RemoteWebConfigurationHost.CreateRemoteObject(_Server, _Username, _Domain, _Password);
                    try
                    {
                        // If we open the stream for writing, we only need to get the _ReadTime because
                        // we will create an empty memory stream for write.
                        buf = remoteSrv.GetData(_FileName, _streamForWrite, out _ReadTime);
                    }
                    finally
                    {
                        while (Marshal.ReleaseComObject(remoteSrv) > 0)
                        {
                        }
                    }
                }
                catch
                {
                    throw;
                }
                finally
                {
                    if (wiContext != null)
                    {
                        wiContext.Undo(); // revert impersonation
                    }
                }
            }
            catch
            {
                throw;
            }

            if (buf == null || buf.Length < 1)
            {
                _MemoryStream = new MemoryStream();
            }
            else
            {
                _MemoryStream = new MemoryStream(buf.Length);
                _MemoryStream.Write(buf, 0, buf.Length);
                _MemoryStream.Position = 0;
            }
#else // !FEATURE_PAL
            throw new NotSupportedException();
#endif // !FEATURE_PAL
        }
        public override void InitForConfiguration(ref string locationSubPath, out string configPath, out string locationConfigPath,
                                                  IInternalConfigRoot root, params object[] hostInitConfigurationParams)
        {
            WebLevel webLevel = (WebLevel)hostInitConfigurationParams[0];
            // ConfigurationFileMap    fileMap = (ConfigurationFileMap)    hostInitConfigurationParams[1];
            string path = (string)hostInitConfigurationParams[2];
            string site = (string)hostInitConfigurationParams[3];

            if (locationSubPath == null)
            {
                locationSubPath = (string)hostInitConfigurationParams[4];
            }

            string server      = (string)hostInitConfigurationParams[5];
            string userName    = (string)hostInitConfigurationParams[6];
            string password    = (string)hostInitConfigurationParams[7];
            IntPtr tokenHandle = (IntPtr)hostInitConfigurationParams[8];

            configPath         = null;
            locationConfigPath = null;

            _Server   = server;
            _Username = GetUserNameFromFullName(userName);
            _Domain   = GetDomainFromFullName(userName);
            _Password = password;
            _Identity = (tokenHandle == IntPtr.Zero) ? null : new WindowsIdentity(tokenHandle); //CreateWindowsIdentity(username, domain, password, tokenHandle);

            _PathMap = new Hashtable(StringComparer.OrdinalIgnoreCase);

#if !FEATURE_PAL // FEATURE_PAL does not have WindowsImpersonationContext, COM objects
            //
            // Send the path arguments to the server for parsing,
            // and retreive the normalized paths and path mapping
            // from config paths to file paths.
            //
            string filePaths;
            try {
                WindowsImpersonationContext wiContext = (_Identity != null) ? _Identity.Impersonate() : null;
                try {
                    IRemoteWebConfigurationHostServer remoteSrv = RemoteWebConfigurationHost.CreateRemoteObject(server, _Username, _Domain, password); //(IRemoteWebConfigurationHostServer) Activator.CreateInstance(type);
                    try {
                        filePaths = remoteSrv.GetFilePaths((int)webLevel, path, site, locationSubPath);
                    } finally {
                        // Release COM objects
                        while (Marshal.ReleaseComObject(remoteSrv) > 0)
                        {
                        }
                    }
                } finally {
                    if (wiContext != null)
                    {
                        wiContext.Undo();
                    }
                }
            }
            catch {
                // Wrap finally clause with a try to avoid exception clauses being run
                // while the thread is impersonated.
                throw;
            }

            if (filePaths == null)
            {
                throw ExceptionUtil.UnexpectedError("RemoteWebConfigurationHost::InitForConfiguration");
            }

            //
            // Format of filePaths:
            //      appPath < appSiteName < appSiteID < configPath < locationConfigPath [< configPath < fileName]+
            //
            string[] parts = filePaths.Split(RemoteWebConfigurationHostServer.FilePathsSeparatorParams);

            if (parts.Length < 7 || (parts.Length - 5) % 2 != 0)
            {
                throw ExceptionUtil.UnexpectedError("RemoteWebConfigurationHost::InitForConfiguration");
            }

            // convert empty strings to nulls
            for (int i = 0; i < parts.Length; i++)
            {
                if (parts[i].Length == 0)
                {
                    parts[i] = null;
                }
            }

            // get config paths
            string appPath     = parts[0];
            string appSiteName = parts[1];
            string appSiteID   = parts[2];
            configPath         = parts[3];
            locationConfigPath = parts[4];
            _ConfigPath        = configPath;

            // Create a WebConfigurationFileMap to be used when we later initialize our delegating WebConfigurationHost
            WebConfigurationFileMap configFileMap      = new WebConfigurationFileMap();
            VirtualPath             appPathVirtualPath = VirtualPath.CreateAbsoluteAllowNull(appPath);

            configFileMap.Site = appSiteID;

            // populate the configpath->physical path mapping
            for (int i = 5; i < parts.Length; i += 2)
            {
                string configPathTemp   = parts[i];
                string physicalFilePath = parts[i + 1];

                _PathMap.Add(configPathTemp, physicalFilePath);

                // Update the WebConfigurationFileMap
                if (WebConfigurationHost.IsMachineConfigPath(configPathTemp))
                {
                    configFileMap.MachineConfigFilename = physicalFilePath;
                }
                else
                {
                    string vPathString;
                    bool   isRootApp;

                    if (WebConfigurationHost.IsRootWebConfigPath(configPathTemp))
                    {
                        vPathString = null;
                        isRootApp   = false;
                    }
                    else
                    {
                        VirtualPath vPath;
                        string      dummy;

                        WebConfigurationHost.GetSiteIDAndVPathFromConfigPath(configPathTemp, out dummy, out vPath);
                        vPathString = VirtualPath.GetVirtualPathString(vPath);
                        isRootApp   = (vPath == appPathVirtualPath);
                    }

                    configFileMap.VirtualDirectories.Add(vPathString,
                                                         new VirtualDirectoryMapping(Path.GetDirectoryName(physicalFilePath), isRootApp));
                }
            }
#else // !FEATURE_PAL: set dummy config path
            string appPath = null;
            _ConfigPath = configPath;
#endif // !FEATURE_PAL

            // Delegate to a WebConfigurationHost for unhandled methods.
            WebConfigurationHost webConfigurationHost = new WebConfigurationHost();
            webConfigurationHost.Init(root, true, new UserMapPath(configFileMap, /*pathsAreLocal*/ false), null, appPath, appSiteName, appSiteID);
            Host = webConfigurationHost;
        }
        private string CallEncryptOrDecrypt(bool doEncrypt, string xmlString, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)
        {
#if !FEATURE_PAL // FEATURE_PAL has no COM objects => no encryption
            // ROTORTODO: COM Objects are not implemented.
            // CORIOLISTODO: COM Objects are not implemented.
            ProviderSettings    ps;
            NameValueCollection nvc;
            string  []          paramKeys;
            string  []          paramValues;
            string returnString = null;
            string typeName;
            WindowsImpersonationContext wiContext = null;

            ////////////////////////////////////////////////////////////
            // Step 1: Create list of parameters for the protection provider
            typeName = protectionProvider.GetType().AssemblyQualifiedName;
            ps       = protectedConfigSection.Providers[protectionProvider.Name];
            if (ps == null)
            {
                throw ExceptionUtil.ParameterInvalid("protectionProvider");
            }

            nvc = ps.Parameters;
            if (nvc == null)
            {
                nvc = new NameValueCollection();
            }

            paramKeys   = nvc.AllKeys;
            paramValues = new string[paramKeys.Length];
            for (int iter = 0; iter < paramKeys.Length; iter++)
            {
                paramValues[iter] = nvc[paramKeys[iter]];
            }

            ////////////////////////////////////////////////////////////
            // Step 2: Set the impersonation if required
            if (_Identity != null)
            {
                wiContext = _Identity.Impersonate();
            }

            try {
                try {
                    //////////////////////////////////////////////////////////////////
                    // Step 3: Get the type and create the object on the remote server
                    IRemoteWebConfigurationHostServer remoteSrv = CreateRemoteObject(_Server, _Username, _Domain, _Password);
                    try {
                        //////////////////////////////////////////////////////////////////
                        // Step 4: Call the API
                        returnString = remoteSrv.DoEncryptOrDecrypt(doEncrypt, xmlString, protectionProvider.Name, typeName, paramKeys, paramValues);
                    } finally {
                        while (Marshal.ReleaseComObject(remoteSrv) > 0)
                        {
                        }                                                   // release the COM object
                    }
                } finally {
                    if (wiContext != null)
                    {
                        wiContext.Undo(); // revert impersonation
                    }
                }
            }
            catch {
            }

            return(returnString);
#else       // !FEATURE_PAL
            throw new NotImplementedException("ROTORTODO");
#endif      // !FEATURE_PAL
        }
Ejemplo n.º 10
0
        public override void InitForConfiguration(ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
        {
            string   str6;
            WebLevel level = (WebLevel)hostInitConfigurationParams[0];
            string   str   = (string)hostInitConfigurationParams[2];
            string   site  = (string)hostInitConfigurationParams[3];

            if (locationSubPath == null)
            {
                locationSubPath = (string)hostInitConfigurationParams[4];
            }
            string str3         = (string)hostInitConfigurationParams[5];
            string fullUserName = (string)hostInitConfigurationParams[6];
            string password     = (string)hostInitConfigurationParams[7];
            IntPtr userToken    = (IntPtr)hostInitConfigurationParams[8];

            configPath         = null;
            locationConfigPath = null;
            this._Server       = str3;
            this._Username     = GetUserNameFromFullName(fullUserName);
            this._Domain       = GetDomainFromFullName(fullUserName);
            this._Password     = password;
            this._Identity     = (userToken == IntPtr.Zero) ? null : new WindowsIdentity(userToken);
            this._PathMap      = new Hashtable(StringComparer.OrdinalIgnoreCase);
            try
            {
                WindowsImpersonationContext context = (this._Identity != null) ? this._Identity.Impersonate() : null;
                try
                {
                    IRemoteWebConfigurationHostServer o = CreateRemoteObject(str3, this._Username, this._Domain, password);
                    try
                    {
                        str6 = o.GetFilePaths((int)level, str, site, locationSubPath);
                    }
                    finally
                    {
                        while (Marshal.ReleaseComObject(o) > 0)
                        {
                        }
                    }
                }
                finally
                {
                    if (context != null)
                    {
                        context.Undo();
                    }
                }
            }
            catch
            {
                throw;
            }
            if (str6 == null)
            {
                throw System.Web.Util.ExceptionUtil.UnexpectedError("RemoteWebConfigurationHost::InitForConfiguration");
            }
            string[] strArray = str6.Split(RemoteWebConfigurationHostServer.FilePathsSeparatorParams);
            if ((strArray.Length < 7) || (((strArray.Length - 5) % 2) != 0))
            {
                throw System.Web.Util.ExceptionUtil.UnexpectedError("RemoteWebConfigurationHost::InitForConfiguration");
            }
            for (int i = 0; i < strArray.Length; i++)
            {
                if (strArray[i].Length == 0)
                {
                    strArray[i] = null;
                }
            }
            string virtualPath = strArray[0];
            string str8        = strArray[1];
            string str9        = strArray[2];

            configPath         = strArray[3];
            locationConfigPath = strArray[4];
            this._ConfigPath   = configPath;
            WebConfigurationFileMap fileMap = new WebConfigurationFileMap();
            VirtualPath             path    = VirtualPath.CreateAbsoluteAllowNull(virtualPath);

            fileMap.Site = str9;
            for (int j = 5; j < strArray.Length; j += 2)
            {
                string key   = strArray[j];
                string str11 = strArray[j + 1];
                this._PathMap.Add(key, str11);
                if (WebConfigurationHost.IsMachineConfigPath(key))
                {
                    fileMap.MachineConfigFilename = str11;
                }
                else
                {
                    string virtualPathString;
                    bool   flag;
                    if (WebConfigurationHost.IsRootWebConfigPath(key))
                    {
                        virtualPathString = null;
                        flag = false;
                    }
                    else
                    {
                        VirtualPath path2;
                        string      str13;
                        WebConfigurationHost.GetSiteIDAndVPathFromConfigPath(key, out str13, out path2);
                        virtualPathString = VirtualPath.GetVirtualPathString(path2);
                        flag = path2 == path;
                    }
                    fileMap.VirtualDirectories.Add(virtualPathString, new VirtualDirectoryMapping(Path.GetDirectoryName(str11), flag));
                }
            }
            WebConfigurationHost host = new WebConfigurationHost();

            object[] hostInitParams = new object[6];
            hostInitParams[0] = true;
            hostInitParams[1] = new UserMapPath(fileMap, false);
            hostInitParams[3] = virtualPath;
            hostInitParams[4] = str8;
            hostInitParams[5] = str9;
            host.Init(root, hostInitParams);
            base.Host = host;
        }