File: RemoteWebConfigurationHost.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (611 lines) | stat: -rw-r--r-- 30,259 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//------------------------------------------------------------------------------
// <copyright file="RemoteWebConfigurationHost.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Web.Configuration {
    using System.Collections;
    using System.Configuration;
    using System.Configuration.Internal;
    using System.Web;
    using System.Web.Util;
    using System.Security;
    using System.IO;
    using System.Web.Hosting;
    using System.Runtime.InteropServices;
    using System.Reflection;
    using System.Collections.Specialized;
    using System.Xml;
    using System.Security.Principal;
    using System.Threading;
    using System.Globalization;
    using System.Security.Permissions;

    [SecurityPermission(SecurityAction.Demand, Unrestricted = true)]
    internal sealed class RemoteWebConfigurationHost : DelegatingConfigHost
    {
        private const string        KEY_MACHINE = "MACHINE";

        private static object       s_version = new object();

        private string              _Server;
        private string              _Username;
        private string              _Domain;
        private string              _Password;
        private WindowsIdentity     _Identity;

        private Hashtable           _PathMap;   // configPath -> configFile
        private string              _ConfigPath;

        /////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////
        internal RemoteWebConfigurationHost() {}

        public override void Init(IInternalConfigRoot configRoot, params object[] hostInitParams) {
            throw ExceptionUtil.UnexpectedError("RemoteWebConfigurationHost::Init");
        }

        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;
        }

        // config path support
        public override bool IsConfigRecordRequired(string configPath) {
            // a record is required for every part of the config path
            return configPath.Length <= _ConfigPath.Length;
        }

        /////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////
        public override string GetStreamName(string configPath)
        {
            return (string) _PathMap[configPath];
        }

        /////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////
        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
        }

        /////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////
        public override Stream OpenStreamForRead(string streamName) {
            RemoteWebConfigurationHostStream rcs = new RemoteWebConfigurationHostStream(false, _Server, streamName, null, _Username, _Domain, _Password, _Identity);
            if (rcs == null || rcs.Length < 1)
                return null;
            return rcs;
        }

        /////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////
        public override Stream OpenStreamForWrite(string streamName, string templateStreamName, ref object writeContext) {
            RemoteWebConfigurationHostStream rcs = new RemoteWebConfigurationHostStream(true, _Server, streamName, templateStreamName, _Username, _Domain, _Password, _Identity);
            writeContext = rcs;
            return rcs;
        }

        /////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////
        public override void DeleteStream(string StreamName)
        {
            // 
        }

        /////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////
        public override void WriteCompleted(string streamName, bool success, object writeContext)
        {
            if (success) {
                RemoteWebConfigurationHostStream rcs = (RemoteWebConfigurationHostStream)writeContext;
                rcs.FlushForWriteCompleted();
            }
        }

        /////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////
        public override bool IsFile(string StreamName)
        {
            return false;
        }

        /////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////
        public override bool PrefetchAll(string configPath, string StreamName)
        {
            return true;
        }

        /////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////
        public override bool PrefetchSection(string sectionGroupName, string sectionName)
        {
            return true;
        }

        /////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////
        public override void GetRestrictedPermissions(IInternalConfigRecord configRecord, out PermissionSet permissionSet, out bool isHostReady) {
            WebConfigurationHost.StaticGetRestrictedPermissions(configRecord, out permissionSet, out isHostReady);
        }

        /////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////
        public override bool IsRemote {
            get { return true; }
        }
        
        /////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////
        // Encrypt/decrypt support
        public override string DecryptSection(string encryptedXmlString, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection) {
            return CallEncryptOrDecrypt(false, encryptedXmlString, protectionProvider, protectedConfigSection);
        }
        public override string EncryptSection(string clearTextXmlString, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection) {
            return CallEncryptOrDecrypt(true, clearTextXmlString, protectionProvider, protectedConfigSection);
        }

        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
        }

        ///////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////
        private static string GetUserNameFromFullName(string fullUserName) {
            if (string.IsNullOrEmpty(fullUserName))
                return null;
            if (fullUserName.Contains("@")) {
                return fullUserName;
            }
            string[] splitted = fullUserName.Split(new char[] { '\\' });
            if (splitted.Length == 1)
                return fullUserName;
            else
                return splitted[1];
        }
        private static string GetDomainFromFullName(string fullUserName) {
            if (string.IsNullOrEmpty(fullUserName))
                return null;
            if (fullUserName.Contains("@"))
                return null;
            string[] splitted = fullUserName.Split(new char[] { '\\' });
            if (splitted.Length == 1)
                return ".";
            return splitted[0];
        }

        // impersonation support: create an identity from credentials
#if OLD_WAY
        private static WindowsIdentity CreateWindowsIdentity(string userName, string password, IntPtr tokenHandle) {

            //////////////////////////////////////////////////////////////////
            // Step 0: Most common case: check if no credentials are supplied
            if (string.IsNullOrEmpty(userName) && tokenHandle == IntPtr.Zero && string.IsNullOrEmpty(password))
                return null;

            //////////////////////////////////////////////////////////////////
            // Step 1: Make sure that either username & password OR token is supplied
            if ((string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password)) || (!string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(password)))
                throw ExceptionUtil.ParameterNullOrEmpty("password");

            if (!string.IsNullOrEmpty(userName) && tokenHandle != IntPtr.Zero)
                throw ExceptionUtil.ParameterInvalid("tokenHandle");

            //////////////////////////////////////////////////////////////////
            // Step 2: Create token if not supplied
            if (!string.IsNullOrEmpty(userName))
                tokenHandle = CreateUserToken(userName, password);

            //////////////////////////////////////////////////////////////////
            // Step 3: Create a windows identity from the token
            WindowsIdentity wi = new WindowsIdentity(tokenHandle);

            if (!string.IsNullOrEmpty(userName) && tokenHandle != IntPtr.Zero) // Close the handle if we created it.
                UnsafeNativeMethods.CloseHandle(tokenHandle);
            return wi;
        }

        private static IntPtr CreateUserToken(string fullUserName, string password) {
            string userName, domain;
            if (fullUserName.Contains("@")) {
                userName = fullUserName;
                domain = null;
            } else {
                string[] splitted = fullUserName.Split(new char[] { '\\' });
                if (splitted.Length == 1) {
                    userName = fullUserName;
                    domain = ".";
                } else {
                    userName = splitted[1];
                    domain = splitted[0];
                }
            }
            //This parameter causes LogonUser to create a primary token.
            const int LOGON32_PROVIDER_DEFAULT        = 0;
            const int LOGON32_LOGON_INTERACTIVE       = 2;
            const int LOGON32_LOGON_NETWORK           = 3;
            const int LOGON32_LOGON_BATCH             = 4;
            const int LOGON32_LOGON_SERVICE           = 5;
            const int LOGON32_LOGON_NETWORK_CLEARTEXT = 8;

            int[] Logon32Values = new int[] { LOGON32_LOGON_INTERACTIVE, LOGON32_LOGON_BATCH, LOGON32_LOGON_SERVICE, LOGON32_LOGON_NETWORK_CLEARTEXT, LOGON32_LOGON_NETWORK};

            IntPtr tokenHandle = IntPtr.Zero;
            int lastError = 0;
            if (UnsafeNativeMethods.LogonUser(userName, domain, password, Logon32Values[0], LOGON32_PROVIDER_DEFAULT, ref tokenHandle) != 0)
                return tokenHandle;
            lastError = Marshal.GetHRForLastWin32Error();
            for (int iter = 1; iter < Logon32Values.Length; iter++)
                if (UnsafeNativeMethods.LogonUser(userName, domain, password, Logon32Values[iter], LOGON32_PROVIDER_DEFAULT, ref tokenHandle) != 0)
                    return tokenHandle;
            Marshal.ThrowExceptionForHR(lastError);
            return IntPtr.Zero;
        }
#endif
        internal static IRemoteWebConfigurationHostServer CreateRemoteObject(string server, string username, string domain, string password) {

#if !FEATURE_PAL // FEATURE_PAL has no COM objects
            try {
                if (string.IsNullOrEmpty(username))
                    return CreateRemoteObjectUsingGetTypeFromCLSID(server);
                if (IntPtr.Size == 8)
                    return CreateRemoteObjectOn64BitPlatform(server, username, domain, password);
                return CreateRemoteObjectOn32BitPlatform(server, username, domain, password);
            } catch (COMException ex) {
                if ((uint)ex.ErrorCode == 0x80040154)
                    throw new Exception(SR.GetString(SR.Make_sure_remote_server_is_enabled_for_config_access));
                throw;
            }
        }
        private static IRemoteWebConfigurationHostServer CreateRemoteObjectUsingGetTypeFromCLSID(string server) {
            Type type = Type.GetTypeFromCLSID(typeof(RemoteWebConfigurationHostServer).GUID, server, true);
            return (IRemoteWebConfigurationHostServer)Activator.CreateInstance(type);
        }

        private static IRemoteWebConfigurationHostServer CreateRemoteObjectOn32BitPlatform(string server, string username, string domain, string password)
        {
            MULTI_QI []     amqi            = new MULTI_QI[1];
            IntPtr          guidbuf         = IntPtr.Zero;
            COAUTHINFO      ca              = null;
            IntPtr          captr           = IntPtr.Zero;
            COSERVERINFO    cs              = null;
            Guid            clsid           = typeof(RemoteWebConfigurationHostServer).GUID;
            int             hr              = 0;
            COAUTHIDENTITY  ci              = null;
            IntPtr          ciptr           = IntPtr.Zero;

            try {
                guidbuf = Marshal.AllocCoTaskMem(16);
                Marshal.StructureToPtr(typeof(IRemoteWebConfigurationHostServer).GUID, guidbuf, false);
                amqi[0] = new MULTI_QI(guidbuf);

                ci = new COAUTHIDENTITY(username, domain, password);
                ciptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(ci));
                Marshal.StructureToPtr(ci, ciptr, false);

                ca = new COAUTHINFO(RpcAuthent.WinNT, RpcAuthor.None, null, /*RpcLevel.Connect*/ RpcLevel.Default, RpcImpers.Impersonate, ciptr);
                captr = Marshal.AllocCoTaskMem(Marshal.SizeOf(ca));
                Marshal.StructureToPtr(ca, captr, false);

                cs = new COSERVERINFO(server, captr);
                hr = UnsafeNativeMethods.CoCreateInstanceEx(ref clsid, IntPtr.Zero, (int)ClsCtx.RemoteServer, cs, 1, amqi);
                if ((uint)hr == 0x80040154)
                        throw new Exception(SR.GetString(SR.Make_sure_remote_server_is_enabled_for_config_access));
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);
                if (amqi[0].hr < 0)
                    Marshal.ThrowExceptionForHR(amqi[0].hr);
                hr = UnsafeNativeMethods.CoSetProxyBlanket(amqi[0].pItf, RpcAuthent.WinNT, RpcAuthor.None, null, /*RpcLevel.Connect*/ RpcLevel.Default, RpcImpers.Impersonate, ciptr, 0);
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);
                return (IRemoteWebConfigurationHostServer)Marshal.GetObjectForIUnknown(amqi[0].pItf);
            } finally {
                if (amqi[0].pItf != IntPtr.Zero)
                {
                    Marshal.Release(amqi[0].pItf);
                    amqi[0].pItf = IntPtr.Zero;
                }
                amqi[0].piid = IntPtr.Zero;
                if (captr != IntPtr.Zero) {
                    Marshal.DestroyStructure(captr, typeof(COAUTHINFO));
                    Marshal.FreeCoTaskMem(captr);
                }
                if (ciptr != IntPtr.Zero) {
                    Marshal.DestroyStructure(ciptr, typeof(COAUTHIDENTITY));
                    Marshal.FreeCoTaskMem(ciptr);
                }
                if (guidbuf != IntPtr.Zero)
                    Marshal.FreeCoTaskMem(guidbuf);
            }
#else // !FEATURE_PAL
            throw new NotSupportedException();
#endif // !FEATURE_PAL
        }

        private static IRemoteWebConfigurationHostServer CreateRemoteObjectOn64BitPlatform(string server, string username, string domain, string password)
        {
            MULTI_QI_X64[]      amqi            = new MULTI_QI_X64[1];
            IntPtr              guidbuf         = IntPtr.Zero;
            COAUTHINFO_X64      ca              = null;
            IntPtr              captr           = IntPtr.Zero;
            COSERVERINFO_X64    cs              = null;
            Guid                clsid           = typeof(RemoteWebConfigurationHostServer).GUID;
            int                 hr              = 0;
            COAUTHIDENTITY_X64  ci              = null;
            IntPtr              ciptr           = IntPtr.Zero;

            try {
                guidbuf = Marshal.AllocCoTaskMem(16);
                Marshal.StructureToPtr(typeof(IRemoteWebConfigurationHostServer).GUID, guidbuf, false);
                amqi[0] = new MULTI_QI_X64(guidbuf);

                ci = new COAUTHIDENTITY_X64(username, domain, password);
                ciptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(ci));
                Marshal.StructureToPtr(ci, ciptr, false);

                ca = new COAUTHINFO_X64(RpcAuthent.WinNT, RpcAuthor.None, null, /*RpcLevel.Connect*/ RpcLevel.Default, RpcImpers.Impersonate, ciptr);
                captr = Marshal.AllocCoTaskMem(Marshal.SizeOf(ca));
                Marshal.StructureToPtr(ca, captr, false);

                cs = new COSERVERINFO_X64(server, captr);
                hr = UnsafeNativeMethods.CoCreateInstanceEx(ref clsid, IntPtr.Zero, (int)ClsCtx.RemoteServer, cs, 1, amqi);
                if ((uint)hr == 0x80040154)
                    throw new Exception(SR.GetString(SR.Make_sure_remote_server_is_enabled_for_config_access));
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);
                if (amqi[0].hr < 0)
                    Marshal.ThrowExceptionForHR(amqi[0].hr);
                hr = UnsafeNativeMethods.CoSetProxyBlanket(amqi[0].pItf, RpcAuthent.WinNT, RpcAuthor.None, null, /*RpcLevel.Connect*/ RpcLevel.Default, RpcImpers.Impersonate, ciptr, 0);
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);
                return (IRemoteWebConfigurationHostServer)Marshal.GetObjectForIUnknown(amqi[0].pItf);
            } finally {
                if (amqi[0].pItf != IntPtr.Zero) {
                    Marshal.Release(amqi[0].pItf);
                    amqi[0].pItf = IntPtr.Zero;
                }
                amqi[0].piid = IntPtr.Zero;
                if (captr != IntPtr.Zero) {
                    Marshal.DestroyStructure(captr, typeof(COAUTHINFO_X64));
                    Marshal.FreeCoTaskMem(captr);
                }
                if (ciptr != IntPtr.Zero) {
                    Marshal.DestroyStructure(ciptr, typeof(COAUTHIDENTITY_X64));
                    Marshal.FreeCoTaskMem(ciptr);
                }
                if (guidbuf != IntPtr.Zero)
                    Marshal.FreeCoTaskMem(guidbuf);
            }
        }
    }
}