コード例 #1
0
        public void Process(IConnection connection, SmtpCommand command)
        {
            if (command.Arguments.Length > 0)
            {
                if (connection.Session.Authenticated)
                {
                    throw new SmtpServerException(new SmtpResponse(StandardSmtpResponseCode.BadSequenceOfCommands,
                                                                   "Already authenticated"));
                }

                string         mechanismId = command.Arguments[0];
                IAuthMechanism mechanism   = AuthExtensionProcessor.MechanismMap.Get(mechanismId);

                if (mechanism == null)
                {
                    throw new SmtpServerException(
                              new SmtpResponse(StandardSmtpResponseCode.CommandParameterNotImplemented,
                                               "Specified AUTH mechanism not supported"));
                }

                if (!AuthExtensionProcessor.IsMechanismEnabled(mechanism))
                {
                    throw new SmtpServerException(
                              new SmtpResponse(StandardSmtpResponseCode.AuthenticationFailure,
                                               "Specified AUTH mechanism not allowed right now (might require secure connection etc)"));
                }

                IAuthMechanismProcessor authMechanismProcessor =
                    mechanism.CreateAuthMechanismProcessor(connection);

                AuthMechanismProcessorStatus status =
                    authMechanismProcessor.ProcessResponse(string.Join(" ", command.Arguments.Skip(1).ToArray()));
                while (status == AuthMechanismProcessorStatus.Continue)
                {
                    string response = connection.ReadLine();

                    if (response == "*")
                    {
                        connection.WriteResponse(new SmtpResponse(StandardSmtpResponseCode.SyntaxErrorInCommandArguments, "Authentication aborted"));
                        return;
                    }

                    status = authMechanismProcessor.ProcessResponse(response);
                }

                if (status == AuthMechanismProcessorStatus.Success)
                {
                    connection.WriteResponse(new SmtpResponse(StandardSmtpResponseCode.AuthenitcationOK,
                                                              "Authenticated OK"));
                    connection.Session.Authenticated             = true;
                    connection.Session.AuthenticationCredentials = authMechanismProcessor.Credentials;
                }
                else
                {
                    connection.WriteResponse(new SmtpResponse(StandardSmtpResponseCode.AuthenticationFailure,
                                                              "Authentication failure"));
                }
            }
            else
            {
                throw new SmtpServerException(new SmtpResponse(StandardSmtpResponseCode.SyntaxErrorInCommandArguments,
                                                               "Must specify AUTH mechanism as a parameter"));
            }
        }
コード例 #2
0
ファイル: AuthVerb.cs プロジェクト: rodrigoieh/smtpserver
        /// <inheritdoc/>
        public async Task Process(IConnection connection, SmtpCommand command)
        {
            ArgumentsParser argumentsParser = new ArgumentsParser(command.ArgumentsText);

            if (argumentsParser.Arguments.Count > 0)
            {
                if (connection.Session.Authenticated)
                {
                    throw new SmtpServerException(new SmtpResponse(
                                                      StandardSmtpResponseCode.BadSequenceOfCommands,
                                                      "Already authenticated"));
                }

                string         mechanismId = argumentsParser.Arguments.First();
                IAuthMechanism mechanism   = this.AuthExtensionProcessor.MechanismMap.Get(mechanismId);

                if (mechanism == null)
                {
                    throw new SmtpServerException(
                              new SmtpResponse(
                                  StandardSmtpResponseCode.CommandParameterNotImplemented,
                                  "Specified AUTH mechanism not supported"));
                }

                if (!await this.AuthExtensionProcessor.IsMechanismEnabled(mechanism).ConfigureAwait(false))
                {
                    throw new SmtpServerException(
                              new SmtpResponse(
                                  StandardSmtpResponseCode.AuthenticationFailure,
                                  "Specified AUTH mechanism not allowed right now (might require secure connection etc)"));
                }

                IAuthMechanismProcessor authMechanismProcessor =
                    mechanism.CreateAuthMechanismProcessor(connection);

                string initialData = null;
                if (argumentsParser.Arguments.Count > 1)
                {
                    initialData = string.Join(" ", argumentsParser.Arguments.Skip(1).ToArray());
                }

                AuthMechanismProcessorStatus status =
                    await authMechanismProcessor.ProcessResponse(initialData).ConfigureAwait(false);

                while (status == AuthMechanismProcessorStatus.Continue)
                {
                    string response = await connection.ReadLine().ConfigureAwait(false);

                    if (response == "*")
                    {
                        await connection.WriteResponse(new SmtpResponse(StandardSmtpResponseCode.SyntaxErrorInCommandArguments, "Authentication aborted")).ConfigureAwait(false);

                        return;
                    }

                    status = await authMechanismProcessor.ProcessResponse(response).ConfigureAwait(false);
                }

                if (status == AuthMechanismProcessorStatus.Success)
                {
                    await connection.WriteResponse(new SmtpResponse(
                                                       StandardSmtpResponseCode.AuthenticationOK,
                                                       "Authenticated OK")).ConfigureAwait(false);

                    connection.Session.Authenticated             = true;
                    connection.Session.AuthenticationCredentials = authMechanismProcessor.Credentials;
                }
                else
                {
                    await connection.WriteResponse(new SmtpResponse(
                                                       StandardSmtpResponseCode.AuthenticationFailure,
                                                       "Authentication failure")).ConfigureAwait(false);
                }
            }
            else
            {
                throw new SmtpServerException(new SmtpResponse(
                                                  StandardSmtpResponseCode.SyntaxErrorInCommandArguments,
                                                  "Must specify AUTH mechanism as a parameter"));
            }
        }