예제 #1
0
        public async Task ProcessAsync(IConnection connection, SmtpCommand command)
        {
            if (connection.CurrentMessage != null)
            {
                await connection.WriteResponseAsync(new SmtpResponse(StandardSmtpResponseCode.BadSequenceOfCommands,
                                                                     "You already told me who the message was from"));

                return;
            }

            if (command.ArgumentsText.Length == 0)
            {
                await connection.WriteResponseAsync(
                    new SmtpResponse(StandardSmtpResponseCode.SyntaxErrorInCommandArguments,
                                     "Must specify from address or <>"));

                return;
            }

            ArgumentsParser argumentsParser = new ArgumentsParser(command.ArgumentsText);

            string[] arguments = argumentsParser.Arguments;

            string from = arguments.First();

            if (from.StartsWith("<"))
            {
                from = from.Remove(0, 1);
            }

            if (from.EndsWith(">"))
            {
                from = from.Remove(from.Length - 1, 1);
            }

            connection.Server.Behaviour.OnMessageStart(connection, from);
            connection.NewMessage();
            connection.CurrentMessage.ReceivedDate = _currentDateTimeProvider.GetCurrentDateTime();
            connection.CurrentMessage.From         = from;

            try
            {
                await ParameterProcessorMap.ProcessAsync(connection, arguments.Skip(1).ToArray(), true);

                await connection.WriteResponseAsync(new SmtpResponse(StandardSmtpResponseCode.OK, "New message started"));
            }
            catch
            {
                connection.AbortMessage();
                throw;
            }
        }
        public AuthMechanismProcessorStatus ProcessResponse(string data)
        {
            if (_challenge == null)
            {
                StringBuilder challenge = new StringBuilder();
                challenge.Append(_random.GenerateRandomInteger(0, Int16.MaxValue));
                challenge.Append(".");
                challenge.Append(_dateTimeProvider.GetCurrentDateTime().Ticks.ToString());
                challenge.Append("@");
                challenge.Append(Connection.Server.Behaviour.DomainName);
                _challenge = challenge.ToString();

                string base64Challenge = Convert.ToBase64String(Encoding.ASCII.GetBytes(challenge.ToString()));
                Connection.WriteResponse(new SmtpResponse(StandardSmtpResponseCode.AuthenticationContinue,
                                                          base64Challenge));
                return(AuthMechanismProcessorStatus.Continue);
            }
            else
            {
                string   response      = ServerUtility.DecodeBase64(data);
                string[] responseparts = response.Split(' ');

                if (responseparts.Length != 2)
                {
                    throw new SmtpServerException(new SmtpResponse(StandardSmtpResponseCode.AuthenticationFailure,
                                                                   "Response in incorrect format - should be USERNAME RESPONSE"));
                }

                string username = responseparts[0];
                string hash     = responseparts[1];

                Credentials = new CramMd5AuthenticationCredentials(username, _challenge, hash);

                AuthenticationResult result =
                    Connection.Server.Behaviour.ValidateAuthenticationCredentials(Connection, Credentials);

                switch (result)
                {
                case AuthenticationResult.Success:
                    return(AuthMechanismProcessorStatus.Success);

                    break;

                default:
                    return(AuthMechanismProcessorStatus.Failed);

                    break;
                }
            }
        }