Example #1
0
 public ClientMessage[] SetPromptForCredentialReply(string userName, char[] password)
 {
     ClientMessage[] clientMessages;
     PowwaEvents.PowwaEVENT_DEBUG_LOG0("SetPromptForCredentialReply(): Enter");
     try
     {
         lock (this.clientRequestLock)
         {
             lock (this.sessionStateLock)
             {
                 PromptForCredentialMessage promptForCredentialMessage = this.ValidateSessionStateForMessageReply <PromptForCredentialMessage>();
                 if (promptForCredentialMessage.DomainCredentials)
                 {
                     StringBuilder stringBuilder  = new StringBuilder(0x202);
                     StringBuilder stringBuilder1 = new StringBuilder(0x152);
                     uint          num            = PowwaSession.CredUIParseUserName(userName, stringBuilder, stringBuilder.Capacity, stringBuilder1, stringBuilder1.Capacity);
                     if (num != 0)
                     {
                         PowwaEvents.PowwaEVENT_DEBUG_LOG2("SetPromptForCredentialReply(): Invalid UserName", "userName", userName, "errorCode", num.ToString(CultureInfo.InvariantCulture));
                         throw PowwaException.CreateValidationErrorException(Resources.InvalidUserNameInDomainCredentials);
                     }
                 }
                 PSCredential pSCredential = new PSCredential(userName, PowwaSession.ToSecureString(password));
                 this.messageQueue.SetInputMessageReply(pSCredential);
             }
             clientMessages = this.GetClientMessages();
         }
     }
     finally
     {
         PowwaEvents.PowwaEVENT_DEBUG_LOG0("SetPromptForCredentialReply(): Exit");
     }
     return(clientMessages);
 }
Example #2
0
        public ClientMessage[] GetClientMessages()
        {
            ClientMessage[] messages;
            lock (this.clientRequestLock)
            {
                bool flag = false;
                lock (this.sessionStateLock)
                {
                    PowwaSession.SessionState state = this.State;
                    switch (state)
                    {
                    case PowwaSession.SessionState.Available:
                    {
                        break;
                    }

                    case PowwaSession.SessionState.ExecutingCommand:
                    case PowwaSession.SessionState.CancellingCommand:
                    {
                        flag = true;
                        break;
                    }

                    case PowwaSession.SessionState.Closed:
                    {
                        throw PowwaException.CreateInvalidSessionException();
                    }

                    default:
                    {
                        break;
                    }
                    }
                }
                if (flag)
                {
                    this.messageQueue.WaitForMessages();
                }
                lock (this.sessionStateLock)
                {
                    if (this.State != PowwaSession.SessionState.Closed)
                    {
                        messages = this.messageQueue.GetMessages();
                    }
                    else
                    {
                        PowwaEvents.PowwaEVENT_DEBUG_LOG1("GetClientMessages() Invalid Session State", "SessionState", this.State.ToString());
                        throw PowwaException.CreateInvalidSessionException();
                    }
                }
            }
            return(messages);
        }
Example #3
0
        private static PowwaReturnValue <T> HttpEndpointWrapper <T>(string sessionKey, Func <PowwaSession, T> function)
        {
            PowwaReturnValue <T> powwaReturnValue;
            PowwaSession         session = null;

            try
            {
                try
                {
                    session = PowwaSessionManager.Instance.GetSession(SessionHelper.GetSessionId());
                }
                catch (ArgumentException argumentException)
                {
                    powwaReturnValue = PowwaReturnValue <T> .CreateError(PowwaException.CreateInvalidSessionException());

                    return(powwaReturnValue);
                }
                if (string.Compare(sessionKey, session.SessionKey, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    if (string.Compare(SessionHelper.GetAuthenticatedUser(), session.UserName, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        powwaReturnValue = PowwaReturnValue <T> .CreateSuccess(function(session));
                    }
                    else
                    {
                        PowwaEvents.PowwaEVENT_INVALID_SESSION_USER(session.Name, SessionHelper.GetAuthenticatedUser(), session.UserName, SessionHelper.GetSourceIPAddressRemoteAddr(), SessionHelper.GetSourceIPAddressHttpXForwardedFor());
                        powwaReturnValue = PowwaReturnValue <T> .CreateError(PowwaException.CreateInvalidSessionException());
                    }
                }
                else
                {
                    PowwaEvents.PowwaEVENT_INVALID_SESSION_KEY(session.Name, SessionHelper.GetAuthenticatedUser(), SessionHelper.GetSourceIPAddressRemoteAddr(), SessionHelper.GetSourceIPAddressHttpXForwardedFor());
                    powwaReturnValue = PowwaReturnValue <T> .CreateError(PowwaException.CreateInvalidSessionException());
                }
            }
            catch (PowwaException powwaException1)
            {
                PowwaException powwaException = powwaException1;
                powwaReturnValue = PowwaReturnValue <T> .CreateError(powwaException);
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                PowwaEvents.PowwaEVENT_GENERIC_FAILURE(session.Name, exception.Message);
                powwaReturnValue = PowwaReturnValue <T> .CreateGenericError(exception);
            }
            return(powwaReturnValue);
        }
Example #4
0
        private static void ValidateCharactersInString(string s)
        {
            char[] chrArray  = new char[] { '\0', '\uFFF9', '\uFFFA', '\uFFFB', '\uFFFC', '\uFFFD', '\uFFFE', '\uFFFF' };
            char[] chrArray1 = chrArray;
            char[] chrArray2 = chrArray1;
            int    num       = 0;

            while (num < (int)chrArray2.Length)
            {
                char chr = chrArray2[num];
                if (s.IndexOf(chr) < 0)
                {
                    num++;
                }
                else
                {
                    object[] objArray = new object[1];
                    objArray[0] = (int)chr;
                    throw PowwaException.CreateValidationErrorException(string.Format(CultureInfo.CurrentUICulture, Resources.LogonError_InvalidCharacterFormat, objArray));
                }
            }
        }
Example #5
0
        private void CreateSession(LogOn.FormInfo formInfo)
        {
            string str;
            string applicationName;
            string str1;
            string originalString;
            WSManConnectionInfo wSManConnectionInfo = null;

            if (formInfo.ConfigurationName.Length == 0)
            {
                str = null;
            }
            else
            {
                str = string.Concat("http://schemas.microsoft.com/powershell/", formInfo.ConfigurationName);
            }
            string       str2         = str;
            PSCredential pSCredential = new PSCredential(formInfo.DestinationUserName, formInfo.DestinationPassword);

            if (!formInfo.IsUriConnection)
            {
                if (string.Compare(formInfo.ApplicationName, "WSMAN", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    applicationName = null;
                }
                else
                {
                    applicationName = formInfo.ApplicationName;
                }
                string str3 = applicationName;
                try
                {
                    wSManConnectionInfo = new WSManConnectionInfo(formInfo.UseSsl, formInfo.ComputerName, formInfo.Port, str3, str2, pSCredential);
                }
                catch (UriFormatException uriFormatException)
                {
                    throw PowwaException.CreateValidationErrorException(Resources.LogonError_InvalidComputerNameUriFormat);
                }
                wSManConnectionInfo.AuthenticationMechanism = formInfo.AuthenticationType;
                PowwaEvents.PowwaEVENT_DEBUG_CONNECT_USING_COMPUTERNAME(formInfo.DestinationUserName, wSManConnectionInfo.ComputerName, wSManConnectionInfo.Port, wSManConnectionInfo.AppName, wSManConnectionInfo.ShellUri, wSManConnectionInfo.AuthenticationMechanism.ToString());
            }
            else
            {
                wSManConnectionInfo = new WSManConnectionInfo(formInfo.ConnectionUri, str2, pSCredential);
                if (!formInfo.AllowRedirection)
                {
                    wSManConnectionInfo.MaximumConnectionRedirectionCount = 0;
                }
                PowwaEvents.PowwaEVENT_DEBUG_CONNECT_USING_URI(formInfo.DestinationUserName, wSManConnectionInfo.ConnectionUri.AbsoluteUri, wSManConnectionInfo.ShellUri);
            }
            string sourceIPAddressRemoteAddr        = SessionHelper.GetSourceIPAddressRemoteAddr();
            string sourceIPAddressHttpXForwardedFor = SessionHelper.GetSourceIPAddressHttpXForwardedFor();

            if (formInfo.IsUriConnection)
            {
                str1 = null;
            }
            else
            {
                str1 = PswaHelper.TranslateLocalComputerName(formInfo.ComputerName);
            }
            string str4 = str1;

            PowwaAuthorizationManager.Instance.CheckLogOnCredential(formInfo.UserName, formInfo.Password, str4, formInfo.ConnectionUri, formInfo.ConfigurationName, sourceIPAddressRemoteAddr, sourceIPAddressHttpXForwardedFor);
            ClientInfo   clientInfo   = new ClientInfo(HttpContext.Current.Request.UserAgent, CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);
            PowwaSession powwaSession = PowwaSessionManager.Instance.CreateSession(this.Session.SessionID, wSManConnectionInfo, clientInfo, formInfo.UserName);
            string       name         = powwaSession.Name;
            string       userName     = formInfo.UserName;
            string       str5         = sourceIPAddressRemoteAddr;
            string       str6         = sourceIPAddressHttpXForwardedFor;

            if (formInfo.IsUriConnection)
            {
                originalString = wSManConnectionInfo.ConnectionUri.OriginalString;
            }
            else
            {
                originalString = wSManConnectionInfo.ComputerName;
            }
            PowwaEvents.PowwaEVENT_SESSION_START(name, userName, str5, str6, originalString, formInfo.DestinationUserName, wSManConnectionInfo.Port, wSManConnectionInfo.AppName, wSManConnectionInfo.ShellUri);
            HttpCookie item = base.Request.Cookies["ASP.NET_SessionId"];

            if (FormsAuthentication.RequireSSL && item != null)
            {
                item.Secure = true;
            }
            FormsAuthentication.SetAuthCookie(formInfo.UserName, false, "/");
            base.Response.Redirect("~/default.aspx");
            //FormsAuthentication.RedirectFromLoginPage(formInfo.UserName, false);
        }
Example #6
0
        private LogOn.FormInfo ValidateForm()
        {
            string           value;
            HtmlInputControl htmlInputControl;

            LogOn.FormInfo formInfo = new LogOn.FormInfo();
            LogOn.ValidateCharactersInString(this.userNameTextBox.Value);
            LogOn.ValidateCharactersInString(this.passwordTextBox.Value);
            LogOn.ValidateCharactersInString(this.altUserNameTextBox.Value);
            LogOn.ValidateCharactersInString(this.passwordTextBox.Value);
            LogOn.ValidateCharactersInString(this.connectionUriTextBox.Value);
            LogOn.ValidateCharactersInString(this.targetNodeTextBox.Value);
            LogOn.ValidateCharactersInString(this.configurationNameTextBox.Value);
            LogOn.ValidateCharactersInString(this.applicationNameTextBox.Value);
            formInfo.UserName = PswaHelper.TranslateLocalAccountName(this.userNameTextBox.Value);
            formInfo.Password = this.passwordTextBox.Value;
            LogOn.FormInfo formInfo1 = formInfo;
            if (this.altUserNameTextBox.Value.Length > 0)
            {
                value = this.altUserNameTextBox.Value;
            }
            else
            {
                value = formInfo.UserName;
            }
            formInfo1.DestinationUserName = value;
            if (this.altPasswordTextBox.Value.Length > 0)
            {
                htmlInputControl = this.altPasswordTextBox;
            }
            else
            {
                htmlInputControl = this.passwordTextBox;
            }
            char[] charArray = htmlInputControl.Value.ToCharArray();
            formInfo.DestinationPassword = new SecureString();
            for (int i = 0; i < (int)charArray.Length; i++)
            {
                formInfo.DestinationPassword.AppendChar(charArray[i]);
                charArray[i] = '*';
            }
            formInfo.IsUriConnection = string.Compare(this.connectionTypeSelection.Value, "connection-uri", StringComparison.OrdinalIgnoreCase) == 0;
            if (!formInfo.IsUriConnection)
            {
                formInfo.ComputerName = this.targetNodeTextBox.Value;
                formInfo.UseSsl       = this.useSslSelection.Value == "1";
                if (this.portTextBox.Value.Length != 0)
                {
                    if (!int.TryParse(this.portTextBox.Value, out formInfo.Port))
                    {
                        throw PowwaException.CreateValidationErrorException(Resources.LogonError_InvalidPort);
                    }
                }
                else
                {
                    formInfo.Port = 0x1761;
                }
                formInfo.ApplicationName = this.applicationNameTextBox.Value;
            }
            else
            {
                try
                {
                    formInfo.ConnectionUri = new Uri(this.connectionUriTextBox.Value);
                }
                catch (UriFormatException uriFormatException1)
                {
                    UriFormatException uriFormatException = uriFormatException1;
                    object[]           message            = new object[1];
                    message[0] = uriFormatException.Message;
                    throw PowwaException.CreateValidationErrorException(string.Format(CultureInfo.CurrentUICulture, Resources.LogonError_InvalidUri, message));
                }
                formInfo.AllowRedirection = this.allowRedirectionSelection.Value == "1";
            }
            formInfo.ConfigurationName = this.configurationNameTextBox.Value;
            string str  = this.authenticationTypeSelection.Value;
            string str1 = str;

            if (str != null)
            {
                if (str1 == "0")
                {
                    formInfo.AuthenticationType = AuthenticationMechanism.Default;
                }
                else if (str1 == "1")
                {
                    formInfo.AuthenticationType = AuthenticationMechanism.Basic;
                }
                else if (str1 == "2")
                {
                    formInfo.AuthenticationType = AuthenticationMechanism.Negotiate;
                }
                else if (str1 == "4")
                {
                    formInfo.AuthenticationType = AuthenticationMechanism.Credssp;
                }
                else if (str1 == "5")
                {
                    formInfo.AuthenticationType = AuthenticationMechanism.Digest;
                }
                else if (str1 == "6")
                {
                    formInfo.AuthenticationType = AuthenticationMechanism.Kerberos;
                }
                else
                {
                    throw PowwaException.CreateValidationErrorException(Resources.InternalError_InvalidAuthenticationMechanism);
                }
                return(formInfo);
            }
            throw PowwaException.CreateValidationErrorException(Resources.InternalError_InvalidAuthenticationMechanism);
        }
Example #7
0
        private static void GetLogonErrorMessage(Exception exception, out string userMessage, out string adminMessage)
        {
            PSRemotingTransportException pSRemotingTransportException = exception as PSRemotingTransportException;

            if (pSRemotingTransportException == null)
            {
                PowwaException powwaException = exception as PowwaException;
                if (powwaException == null)
                {
                    ThreadAbortException threadAbortException = exception as ThreadAbortException;
                    if (threadAbortException == null)
                    {
                        userMessage = Resources.LogonError_UnknownError;
                        object[] message = new object[1];
                        message[0]   = exception.Message;
                        adminMessage = string.Format(CultureInfo.CurrentCulture, Resources.LogonError_UnknownErrorAdminMessage, message);
                        return;
                    }
                    else
                    {
                        string logonErrorThreadAborted = Resources.LogonError_ThreadAborted;
                        string str = logonErrorThreadAborted;
                        adminMessage = logonErrorThreadAborted;
                        userMessage  = str;
                        return;
                    }
                }
                else
                {
                    string message1 = powwaException.Message;
                    string str1     = message1;
                    adminMessage = message1;
                    userMessage  = str1;
                    return;
                }
            }
            else
            {
                int errorCode = pSRemotingTransportException.ErrorCode;
                if (errorCode > -2144108250)
                {
                    if (errorCode > 5)
                    {
                        if (errorCode == 53)
                        {
                            userMessage = Resources.LogonError_InvalidComputer;
                        }
                        else
                        {
                            if (errorCode == 0x51f)
                            {
                                userMessage = Resources.LogonError_NoLogonServers;
                            }
                            else
                            {
                                if (errorCode != 0x52e)
                                {
                                    goto Label0;
                                }
                                userMessage = Resources.LogonError_InvalidCredentials;
                            }
                        }
                    }
                    else
                    {
                        if (errorCode == -2144108126)
                        {
                            userMessage = Resources.LogonError_InvalidAuthenticationTypeCredSPP;
                        }
                        else
                        {
                            if (errorCode != 5)
                            {
                                goto Label0;
                            }
                            userMessage = Resources.LogonError_AccessDenied;
                        }
                    }
                }
                else
                {
                    if (errorCode > -2144108485)
                    {
                        if (errorCode == -2144108322)
                        {
                            userMessage = Resources.LogonError_InvalidAuthenticationTypeBasicOrDigest;
                        }
                        else
                        {
                            if (errorCode != -2144108250)
                            {
                                goto Label0;
                            }
                            userMessage = Resources.LogonError_InvalidEndpoint;
                        }
                    }
                    else
                    {
                        if (errorCode == -2144108526)
                        {
                            userMessage = Resources.LogonError_RemotingNotEnabled;
                        }
                        else
                        {
                            if (errorCode != -2144108485)
                            {
                                goto Label0;
                            }
                            userMessage = Resources.LogonError_InvalidConfigurationName;
                        }
                    }
                }
                adminMessage = pSRemotingTransportException.Message;
                return;
            }
Label0:
            if (((long)pSRemotingTransportException.ErrorCode & (long)-65536) != (long)-2144141312)
            {
                userMessage  = Resources.LogonError_ConnectionError;
                adminMessage = pSRemotingTransportException.Message;
                return;
            }
            else
            {
                object[] transportMessage = new object[1];
                transportMessage[0] = pSRemotingTransportException.TransportMessage;
                userMessage         = string.Format(CultureInfo.CurrentCulture, Resources.LogonError_ConnectionErrorExtended, transportMessage);
                adminMessage        = pSRemotingTransportException.Message;
                return;
            }
        }
Example #8
0
        public static PowwaReturnValue <T> CreateError(PowwaException exception)
        {
            T t = default(T);

            return(new PowwaReturnValue <T>(exception.Status, t, exception.Message));
        }
Example #9
0
        public void CheckLogOnCredential(string userName, string password, string computerName, Uri connectionUri, string configuration, string sourceIPAddressRemoteAddr, string sourceIPAddressHttpXForwardedFor)
        {
            string statusSuccess;
            string originalString;

            if (computerName != null ^ connectionUri != null)
            {
                if (this.AuthenticateUser(userName, password))
                {
                    if (this.AuthorizeSession(userName, computerName, connectionUri, configuration))
                    {
                        bool   flag = this.CheckUserSessionLimit(userName);
                        string str  = userName;
                        if (flag)
                        {
                            statusSuccess = Resources.Status_Success;
                        }
                        else
                        {
                            statusSuccess = Resources.Status_Failure;
                        }
                        PowwaEvents.PowwaEVENT_SESSION_LIMIT_CHECK(str, statusSuccess);
                        if (flag)
                        {
                            return;
                        }
                        else
                        {
                            PowwaEvents.PowwaEVENT_SESSION_LIMIT_REACHED(userName);
                            object[] objArray = new object[1];
                            objArray[0] = userName;
                            throw PowwaException.CreateLogOnFailureException(string.Format(CultureInfo.CurrentCulture, Resources.UserActiveSessionLimitReached, objArray));
                        }
                    }
                    else
                    {
                        string str1 = userName;
                        string str2 = sourceIPAddressRemoteAddr;
                        string str3 = sourceIPAddressHttpXForwardedFor;
                        string gatewayAuthorizationFailure = Resources.GatewayAuthorizationFailure;
                        if (connectionUri != null)
                        {
                            originalString = connectionUri.OriginalString;
                        }
                        else
                        {
                            originalString = computerName;
                        }
                        string str4  = configuration;
                        string empty = str4;
                        if (str4 == null)
                        {
                            empty = string.Empty;
                        }
                        PowwaEvents.PowwaEVENT_GATEWAY_AUTHORIZATION_FAILURE(str1, str2, str3, gatewayAuthorizationFailure, originalString, empty);
                        throw PowwaException.CreateLogOnFailureException(Resources.GatewayAuthorizationFailure);
                    }
                }
                else
                {
                    PowwaEvents.PowwaEVENT_AUTHENTICATION_FAILURE(userName, sourceIPAddressRemoteAddr, sourceIPAddressHttpXForwardedFor, Resources.LoginFailure);
                    throw PowwaException.CreateLogOnFailureException(Resources.LoginFailure);
                }
            }
            else
            {
                throw new ArgumentException("computerName and connectionUri are mutually exclusive", "computerName");
            }
        }
Example #10
0
        internal bool AuthorizeSession(string userName, string computerName, Uri connectionUri, string configuration)
        {
            bool   flag;
            object obj;
            string statusSuccess;

            PowwaEvents.PowwaEVENT_GATEWAY_AUTHORIZATION_START(userName);
            bool length = false;
            PswaAuthorizationRuleManager instance = PswaAuthorizationRuleManager.Instance;

            instance.TestRuleInvalidRule += new EventHandler <TestRuleInvalidRuleEventArgs>(this.OnInvalidRule);
            try
            {
                ArrayList arrayLists = new ArrayList();
                SortedList <int, PswaAuthorizationRule> nums = PswaAuthorizationRuleManager.Instance.LoadFromFile(arrayLists);
                this.CheckLoadError(arrayLists);
                if (nums != null)
                {
                    PswaAuthorizationRule[] array = nums.Values.ToArray <PswaAuthorizationRule>();
                    this.invalidRules = 0;
                    if (computerName == null)
                    {
                        length = (int)PswaAuthorizationRuleManager.Instance.TestRule(array, userName, connectionUri, configuration, false, MatchingWildcard.None).Length > 0;
                    }
                    else
                    {
                        length = (int)PswaAuthorizationRuleManager.Instance.TestRule(array, userName, computerName, configuration, false, MatchingWildcard.None).Length > 0;
                    }
                    if (length || this.invalidRules <= 0)
                    {
                        flag = length;
                    }
                    else
                    {
                        CultureInfo invariantCulture = CultureInfo.InvariantCulture;
                        string      str      = "Test-PswaAuthorizationRule -UserName '{0}' -{1} '{2}' -ConfigurationName '{3}'";
                        object[]    objArray = new object[4];
                        objArray[0] = userName;
                        object[] objArray1 = objArray;
                        int      num       = 1;
                        if (computerName != null)
                        {
                            obj = "ComputerName";
                        }
                        else
                        {
                            obj = "ConnectionUri";
                        }
                        objArray1[num] = obj;
                        objArray[2]    = computerName;
                        objArray[3]    = configuration;
                        string str1 = string.Format(invariantCulture, str, objArray);
                        PowwaEvents.PowwaEVENT_AUTHORIZATION_FAILURE_INVALID_RULES(str1);
                        throw PowwaException.CreateLogOnFailureException(Resources.GatewayAuthorizationFailureInvalidRules);
                    }
                }
                else
                {
                    bool flag1 = false;
                    length = flag1;
                    flag   = flag1;
                }
            }
            finally
            {
                PswaAuthorizationRuleManager pswaAuthorizationRuleManager = PswaAuthorizationRuleManager.Instance;
                pswaAuthorizationRuleManager.TestRuleInvalidRule -= new EventHandler <TestRuleInvalidRuleEventArgs>(this.OnInvalidRule);
                string str2 = userName;
                if (length)
                {
                    statusSuccess = Resources.Status_Success;
                }
                else
                {
                    statusSuccess = Resources.Status_Failure;
                }
                PowwaEvents.PowwaEVENT_GATEWAY_AUTHORIZATION_STOP(str2, statusSuccess);
            }
            return(flag);
        }