Exemplo n.º 1
0
        internal static Uri BuildCmdletProxyUri(string targetFqdn, ExchangeRunspaceConfiguration runspaceConfig, int targetVersion)
        {
            if (string.IsNullOrWhiteSpace(targetFqdn))
            {
                throw new ArgumentNullException("targetFqdn");
            }
            if (runspaceConfig == null)
            {
                throw new ArgumentNullException("runspaceConfig");
            }
            ExchangeRunspaceConfigurationSettings configurationSettings = runspaceConfig.ConfigurationSettings;

            ExAssert.RetailAssert(configurationSettings != null, "runspaceConfig.ConfigurationSettings should not be null.");
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("https://");
            stringBuilder.Append(targetFqdn);
            if (targetVersion >= Server.E15MinVersion)
            {
                stringBuilder.Append(":444/powershell/Powershell-proxy?");
            }
            else
            {
                stringBuilder.Append("/Powershell-proxy?");
            }
            stringBuilder.AppendFormat("{0}={1}", "X-Rps-CAT", Uri.EscapeDataString(configurationSettings.UserToken.CommonAccessTokenForCmdletProxy().Serialize()));
            stringBuilder.AppendFormat(";{0}={1}", "serializationLevel", configurationSettings.CurrentSerializationLevel.ToString());
            stringBuilder.AppendFormat(";{0}={1}", "clientApplication", configurationSettings.ClientApplication.ToString());
            if (configurationSettings.ClientApplication == ExchangeRunspaceConfigurationSettings.ExchangeApplication.ECP || configurationSettings.ClientApplication == ExchangeRunspaceConfigurationSettings.ExchangeApplication.EMC || configurationSettings.ClientApplication == ExchangeRunspaceConfigurationSettings.ExchangeApplication.OSP)
            {
                stringBuilder.AppendFormat(";{0}={1}", "proxyFullSerialization", "true");
            }
            string managedOrganization = ProxyHelper.GetManagedOrganization(runspaceConfig);

            if (!string.IsNullOrEmpty(managedOrganization))
            {
                stringBuilder.AppendFormat(";{0}", ProxyHelper.GetOrganizationAppendQueryIfNeeded(managedOrganization));
            }
            stringBuilder.AppendFormat(";{0}={1}", WellKnownHeader.CmdletProxyIsOn, "99C6ECEE-5A4F-47B9-AE69-49EAFB58F368");
            IActivityScope currentActivityScope = ActivityContext.GetCurrentActivityScope();

            if (currentActivityScope != null && currentActivityScope.ActivityId != Guid.Empty)
            {
                stringBuilder.AppendFormat(";{0}={1}", "RequestId48CD6591-0506-4D6E-9131-797489A3260F", currentActivityScope.ActivityId);
            }
            return(new Uri(stringBuilder.ToString()));
        }
Exemplo n.º 2
0
        private static string EscapeSingleQuotedString(string stringContent)
        {
            if (string.IsNullOrEmpty(stringContent))
            {
                return(string.Empty);
            }
            StringBuilder stringBuilder = new StringBuilder(stringContent.Length);

            foreach (char c in stringContent)
            {
                stringBuilder.Append(c);
                if (ProxyHelper.IsSingleQuote(c))
                {
                    stringBuilder.Append(c);
                }
            }
            return(stringBuilder.ToString());
        }
Exemplo n.º 3
0
        internal static IEnumerable <PSObject> RPSProxyExecution(Guid cmdletUniqueId, PSCommand command, string serverFqn, ExchangeRunspaceConfiguration runspaceConfig, int serverVersion, bool asyncProxying, Task.TaskWarningLoggingDelegate writeWarning)
        {
            Uri uri = ProxyHelper.BuildCmdletProxyUri(serverFqn, runspaceConfig, serverVersion);
            IEnumerable <PSObject> result;

            try
            {
                RemoteConnectionInfo connectionInfo = ProxyHelper.BuildProxyWSManConnectionInfo(uri);
                ProxyPSCommand       proxyPSCommand = new ProxyPSCommand(connectionInfo, command, asyncProxying, writeWarning);
                result = proxyPSCommand.Invoke();
            }
            catch (Exception)
            {
                CmdletLogger.SafeAppendGenericInfo(cmdletUniqueId, "TargetUri", uri.ToString());
                throw;
            }
            return(result);
        }
Exemplo n.º 4
0
        private static object TranslateCmdletProxyCollectionParameter(object paramValue, ExchangeRunspaceConfigurationSettings.ProxyMethod proxyMethod)
        {
            ICollection collection = (ICollection)paramValue;

            if (proxyMethod == ExchangeRunspaceConfigurationSettings.ProxyMethod.RPS)
            {
                Collection <object> collection2 = new Collection <object>();
                foreach (object paramValue2 in collection)
                {
                    collection2.Add(ProxyHelper.TranslateCmdletProxyParameter(paramValue2, proxyMethod));
                }
                return(collection2);
            }
            int           num           = 0;
            StringBuilder stringBuilder = new StringBuilder();

            foreach (object paramValue3 in collection)
            {
                num++;
                object obj = ProxyHelper.TranslateCmdletProxyParameter(paramValue3, proxyMethod);
                string arg;
                if (obj == null)
                {
                    arg = ProxyHelper.nullString;
                }
                else if (obj.GetType() == typeof(bool))
                {
                    arg = (((bool)obj) ? ProxyHelper.trueString : ProxyHelper.falseString);
                }
                else
                {
                    arg = obj.ToString().Replace("'", "''");
                }
                stringBuilder.AppendFormat("'{0}',", arg);
            }
            if (num == 0)
            {
                return(null);
            }
            return(stringBuilder.ToString().Trim().TrimEnd(new char[]
            {
                ','
            }));
        }
Exemplo n.º 5
0
        // Token: 0x06001645 RID: 5701 RVA: 0x00053FB0 File Offset: 0x000521B0
        private PSCommand GenerateProxyCmdlet(CmdletProxyInfo.ChangeCmdletProxyParametersDelegate changeCmdletProxyParameters)
        {
            this.MakeNecessaryChangeOnParametersBeforeProxy(changeCmdletProxyParameters);
            PSCommand pscommand = new PSCommand();
            Command   command   = null;

            if (this.context.InvocationInfo != null)
            {
                command = new Command(this.context.InvocationInfo.CommandName);
            }
            if (this.context.InvocationInfo != null)
            {
                foreach (object obj in this.context.InvocationInfo.UserSpecifiedParameters.Keys)
                {
                    string text = (string)obj;
                    command.Parameters.Add(text, ProxyHelper.TranslateCmdletProxyParameter(this.GetParameterValueToProxy(text), ExchangeRunspaceConfigurationSettings.ProxyMethod.RPS));
                }
            }
            pscommand.AddCommand(command);
            return(pscommand);
        }
Exemplo n.º 6
0
        // Token: 0x06001613 RID: 5651 RVA: 0x00052AE4 File Offset: 0x00050CE4
        private static bool ShouldProxyCmdlet(TaskContext context, string remoteServerFqdn, int remoteServerVersion)
        {
            if (string.IsNullOrWhiteSpace(remoteServerFqdn))
            {
                return(false);
            }
            if (context.ExchangeRunspaceConfig == null)
            {
                return(false);
            }
            if (ExTraceGlobals.FaultInjectionTracer.IsTraceEnabled(TraceType.FaultInjection))
            {
                bool flag = false;
                ProxyHelper.FaultInjection_ShouldForcedlyProxyCmdlet(context.ExchangeRunspaceConfig.ConfigurationSettings.OriginalConnectionUri, remoteServerFqdn, ref flag);
                if (flag)
                {
                    return(true);
                }
            }
            string localServerFqdn = TaskHelper.GetLocalServerFqdn(null);

            if (string.IsNullOrEmpty(localServerFqdn))
            {
                CmdletLogger.SafeAppendGenericError(context.UniqueId, "ShouldProxyCmdlet", "GetLocalServerFqdn returns null/empty value", false);
                return(false);
            }
            if (string.Equals(remoteServerFqdn, localServerFqdn, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }
            if (CmdletProxy.CanServerBeProxiedTo(remoteServerVersion))
            {
                return(true);
            }
            CmdletLogger.SafeAppendGenericInfo(context.UniqueId, "ShouldProxyCmdlet", string.Format("Remote server version {0} doesn't support be proxied.", ProxyHelper.GetFriendlyVersionInformation(remoteServerVersion)));
            return(false);
        }
Exemplo n.º 7
0
        internal static void BuildCommandFromParameter(string parameterName, object parameterValue, StringBuilder commandBuilder)
        {
            if (parameterValue == null)
            {
                commandBuilder.AppendFormat(" -{0}:{1}", parameterName, ProxyHelper.nullString);
                return;
            }
            Type type = parameterValue.GetType();

            if (type == typeof(bool))
            {
                commandBuilder.AppendFormat(" -{0}:${1}", parameterName, parameterValue);
                return;
            }
            if (type == typeof(SwitchParameter))
            {
                commandBuilder.AppendFormat(" -{0}:${1}", parameterName, ((SwitchParameter)parameterValue).IsPresent);
                return;
            }
            string stringContent;

            if (parameterValue is IDictionary)
            {
                stringContent = ProxyHelper.TranslateCmdletProxyDictionaryParameter(parameterValue, ExchangeRunspaceConfigurationSettings.ProxyMethod.PSWS).ToString();
            }
            else
            {
                if (parameterValue is ICollection)
                {
                    string arg = ProxyHelper.TranslateCmdletProxyCollectionParameter(parameterValue, ExchangeRunspaceConfigurationSettings.ProxyMethod.PSWS).ToString();
                    commandBuilder.AppendFormat(" -{0}:{1}", parameterName, arg);
                    return;
                }
                stringContent = parameterValue.ToString();
            }
            commandBuilder.AppendFormat(" -{0}:'{1}'", parameterName, ProxyHelper.EscapeSingleQuotedString(stringContent));
        }
Exemplo n.º 8
0
        internal static NameValueCollection GetPSWSProxyRequestHeaders(ExchangeRunspaceConfiguration runspaceConfig)
        {
            ExchangeRunspaceConfigurationSettings configurationSettings = runspaceConfig.ConfigurationSettings;

            ExAssert.RetailAssert(configurationSettings != null, "runspaceConfig.ConfigurationSettings should not be null.");
            NameValueCollection nameValueCollection = new NameValueCollection();

            nameValueCollection["X-CommonAccessToken"]           = configurationSettings.UserToken.CommonAccessTokenForCmdletProxy().Serialize();
            nameValueCollection["serializationLevel"]            = configurationSettings.CurrentSerializationLevel.ToString();
            nameValueCollection["clientApplication"]             = configurationSettings.ClientApplication.ToString();
            nameValueCollection["X-EncodeDecode-Key"]            = "false";
            nameValueCollection[WellKnownHeader.CmdletProxyIsOn] = "99C6ECEE-5A4F-47B9-AE69-49EAFB58F368";
            if (configurationSettings.ClientApplication == ExchangeRunspaceConfigurationSettings.ExchangeApplication.ECP || configurationSettings.ClientApplication == ExchangeRunspaceConfigurationSettings.ExchangeApplication.EMC || configurationSettings.ClientApplication == ExchangeRunspaceConfigurationSettings.ExchangeApplication.OSP)
            {
                nameValueCollection["proxyFullSerialization"] = "true";
            }
            string managedOrganization = ProxyHelper.GetManagedOrganization(runspaceConfig);

            if (!string.IsNullOrEmpty(managedOrganization))
            {
                nameValueCollection["organization"] = managedOrganization;
            }
            return(nameValueCollection);
        }
Exemplo n.º 9
0
 // Token: 0x06001642 RID: 5698 RVA: 0x000539C4 File Offset: 0x00051BC4
 private bool ProxyCmdlet(CmdletProxyInfo cmdletProxyInfo)
 {
     this.proxiedObjectCount++;
     CmdletLogger.SafeSetLogger(this.context.UniqueId, RpsCmdletMetadata.ProxiedObjectCount, this.proxiedObjectCount);
     this.context.Items["Log_ProxiedObjectCount"] = this.proxiedObjectCount;
     if (cmdletProxyInfo.ConfirmationMessage != LocalizedString.Empty)
     {
         if (!this.shouldProcessHasBeenPrompted && this.context.CommandShell != null && !this.context.CommandShell.ShouldProcess(cmdletProxyInfo.ConfirmationMessage))
         {
             CmdletLogger.SafeAppendGenericInfo(this.context.UniqueId, "Proxy", "CancelAfterConfirm");
             CmdletLogger.SafeSetLogger(this.context.UniqueId, RpsCmdletMetadata.ExecutionResult, "Cancelled");
             this.context.WasCancelled = true;
             return(false);
         }
         SwitchParameter switchParameter = new SwitchParameter(false);
         if (this.ShouldRemoveConfirmParam() && this.context.InvocationInfo != null)
         {
             this.context.InvocationInfo.UserSpecifiedParameters.Remove("Confirm");
         }
         if (this.context.InvocationInfo != null)
         {
             this.context.InvocationInfo.UserSpecifiedParameters.Add("Confirm", switchParameter);
         }
     }
     using (new CmdletMonitoredScope(this.context.UniqueId, "CmdletProxyLatency", "CmdletProxyLatency", LoggerHelper.CmdletPerfMonitors))
     {
         this.ProxyCmdletExecution(cmdletProxyInfo);
     }
     this.context.Items["Log_CmdletProxyInfo"] = string.Format("Cmdlet proxied to remote server {0}, version {1}.", cmdletProxyInfo.RemoteServerFqdn, ProxyHelper.GetFriendlyVersionInformation(cmdletProxyInfo.RemoteServerVersion));
     return(true);
 }
Exemplo n.º 10
0
        // Token: 0x06001644 RID: 5700 RVA: 0x00053BE8 File Offset: 0x00051DE8
        private void ProxyCmdletExecution(CmdletProxyInfo cmdletProxyInfo)
        {
            ExAssert.RetailAssert(this.context.ExchangeRunspaceConfig != null, "this.context.ExchangeRunspaceConfig should not be null.");
            string remoteServerFqdn           = cmdletProxyInfo.RemoteServerFqdn;
            int    remoteServerVersion        = cmdletProxyInfo.RemoteServerVersion;
            string friendlyVersionInformation = ProxyHelper.GetFriendlyVersionInformation(remoteServerVersion);

            CmdletProxyInfo.ChangeCmdletProxyParametersDelegate changeCmdletProxyParameters = cmdletProxyInfo.ChangeCmdletProxyParameters;
            if (Microsoft.Exchange.Diagnostics.Components.Tasks.ExTraceGlobals.FaultInjectionTracer.IsTraceEnabled(TraceType.FaultInjection))
            {
                Microsoft.Exchange.Diagnostics.Components.Tasks.ExTraceGlobals.FaultInjectionTracer.TraceTest <string>(3720752445U, ref remoteServerFqdn);
                UserToken userToken = this.context.ExchangeRunspaceConfig.ConfigurationSettings.UserToken;
                ProxyHelper.FaultInjection_UserSid(ref userToken);
                Microsoft.Exchange.Diagnostics.Components.Tasks.ExTraceGlobals.FaultInjectionTracer.TraceTest <int>(3452316989U, ref remoteServerVersion);
            }
            string text = this.GeneratePswsProxyCmdlet(changeCmdletProxyParameters);

            ExchangeRunspaceConfigurationSettings.ProxyMethod proxyMethod = this.DetermineProxyMethod(remoteServerVersion);
            if (this.context.CommandShell != null)
            {
                this.context.CommandShell.WriteVerbose(Strings.VerboseCmdletProxiedToAnotherServer(text, remoteServerFqdn, friendlyVersionInformation, proxyMethod.ToString()));
            }
            Guid uniqueId = this.context.UniqueId;

            CmdletLogger.SafeAppendColumn(uniqueId, RpsCmdletMetadata.CmdletProxyRemoteServer, this.proxiedObjectCount.ToString(), remoteServerFqdn);
            CmdletLogger.SafeAppendColumn(uniqueId, RpsCmdletMetadata.CmdletProxyRemoteServerVersion, this.proxiedObjectCount.ToString(), friendlyVersionInformation.ToString());
            CmdletLogger.SafeAppendColumn(uniqueId, RpsCmdletMetadata.CmdletProxyMethod, this.proxiedObjectCount.ToString(), proxyMethod.ToString());
            try
            {
                IEnumerable <PSObject> enumerable;
                if (proxyMethod == ExchangeRunspaceConfigurationSettings.ProxyMethod.RPS)
                {
                    PSCommand command = this.GenerateProxyCmdlet(changeCmdletProxyParameters);
                    Task.TaskWarningLoggingDelegate writeWarning = null;
                    if (this.context.CommandShell != null)
                    {
                        writeWarning = new Task.TaskWarningLoggingDelegate(this.context.CommandShell.WriteWarning);
                    }
                    enumerable = ProxyHelper.RPSProxyExecution(this.context.UniqueId, command, remoteServerFqdn, this.context.ExchangeRunspaceConfig, remoteServerVersion, cmdletProxyInfo.ShouldAsyncProxy, writeWarning);
                }
                else
                {
                    enumerable = CommandInvocation.Invoke(this.context.UniqueId, ProxyHelper.GetPSWSProxySiteUri(remoteServerFqdn), text, CredentialCache.DefaultNetworkCredentials, ProxyHelper.GetPSWSProxyRequestHeaders(this.context.ExchangeRunspaceConfig), this.context.ExchangeRunspaceConfig.TypeTable);
                }
                foreach (PSObject psobject in enumerable)
                {
                    object sendToPipeline = psobject;
                    if (psobject.BaseObject != null && !(psobject.BaseObject is PSCustomObject))
                    {
                        sendToPipeline = psobject.BaseObject;
                    }
                    else if (this.context.ExchangeRunspaceConfig != null)
                    {
                        if (this.context.ExchangeRunspaceConfig.ConfigurationSettings.ClientApplication != ExchangeRunspaceConfigurationSettings.ExchangeApplication.ECP)
                        {
                            if (this.context.ExchangeRunspaceConfig.ConfigurationSettings.ClientApplication != ExchangeRunspaceConfigurationSettings.ExchangeApplication.OSP)
                            {
                                goto IL_2CB;
                            }
                        }
                        try
                        {
                            Task.TaskVerboseLoggingDelegate writeVerbose = null;
                            if (this.context.CommandShell != null)
                            {
                                writeVerbose = new Task.TaskVerboseLoggingDelegate(this.context.CommandShell.WriteWarning);
                            }
                            sendToPipeline = ProxyHelper.ConvertPSObjectToOriginalType(psobject, remoteServerVersion, writeVerbose);
                        }
                        catch (Exception ex)
                        {
                            CmdletLogger.SafeAppendGenericError(uniqueId, "ConvertPSObjectToOriginalTyp", ex, new Func <Exception, bool>(TaskHelper.IsTaskUnhandledException));
                            Diagnostics.ReportException(ex, Constants.CoreEventLogger, TaskEventLogConstants.Tuple_UnhandledException, null, null, Microsoft.Exchange.Diagnostics.Components.Configuration.Core.ExTraceGlobals.InstrumentationTracer, "Exception from ProxyHelper.ConvertPSObjectToOriginalType : {0}");
                        }
                    }
IL_2CB:
                    if (this.context.CommandShell != null)
                    {
                        this.context.CommandShell.WriteObject(sendToPipeline);
                    }
                }
            }
            catch (Exception ex2)
            {
                CmdletLogger.SafeAppendGenericError(this.context.UniqueId, "ProxyCmdletExecution", ex2, new Func <Exception, bool>(TaskHelper.IsTaskUnhandledException));
                if (this.context.CommandShell != null)
                {
                    this.context.CommandShell.WriteError(new CmdletProxyException(text, remoteServerFqdn, friendlyVersionInformation, proxyMethod.ToString(), ex2.Message), ExchangeErrorCategory.ServerOperation, null);
                }
            }
        }