Пример #1
0
        protected static void AuthCRAMMD5(SmtpServerSession context, SmtpClientInputLine input)
        {
            try
            {
                const string AuthCRAMMD5Key = "AuthCRAMMD5Key";
                context._CurrentHandlers = _AuthCRAMMD5Handlers;
                if (input.Command.ToUpper() == "AUTH")
                {
                    context.ClearIdentity();
                    context.Items[AuthCRAMMD5Key] = Guid.NewGuid().ToByteArray().ToBase64();
                    context.WriteLine(334, context.Items[AuthCRAMMD5Key] as string);
                }
                else
                {
                    var pair = input.RawInput.Base64ToBytes().ToString(Encoding.UTF8).Split(' ');
                    context._User.Name = pair.First();
                    var credential = context.Server
                                     .Credentials
                                     .FirstOrDefault(c => c.UserName == context._User.Name);
                    if (credential == null)
                    {
                        FailAuthentication(context);
                        return;
                    }

                    var hmacmd5str = new HMACMD5(credential.Password.GetBytes())
                                     .ComputeHash(context.Items[AuthCRAMMD5Key].ToString().Base64ToBytes())
                                     .ToHexString();
                    context.Items.Remove(AuthCRAMMD5Key);
                    if (hmacmd5str != pair.Last())
                    {
                        FailAuthentication(context);
                    }
                    else
                    {
                        Authentication(context);
                    }
                }
            }
            catch (FormatException)
            {
                context._CurrentHandlers = _InitialHandlers;
                UnknownCommand(context, input);
            }
        }
Пример #2
0
 protected static void AuthPlain(SmtpServerSession context, SmtpClientInputLine input)
 {
     try
     {
         context._CurrentHandlers = _AuthPlainHandlers;
         if (input.Command.ToUpper() == "AUTH")
         {
             context.ClearIdentity();
             var user = input.Params.Skip(1).FirstOrDefault() ?? "";
             if (user.IsNullOrEmpty())
             {
                 context.WriteLine(334, "Username:"******"Password:"******"Password:".GetBytes().ToBase64());
         }
         else
         {
             var password = input.RawInput.Base64ToBytes().ToString(Encoding.UTF8);
             var found    = context.Server.Credentials.Any(c => c.UserName == context.User.Name && c.Password == password);
             if (found == false)
             {
                 FailAuthentication(context);
             }
             else
             {
                 Authentication(context);
             }
         }
     }
     catch (FormatException)
     {
         context._CurrentHandlers = _InitialHandlers;
         UnknownCommand(context, input);
     }
 }
 protected static void FailAuthentication(SmtpServerSession context)
 {
     context.ClearIdentity();
     context._CurrentHandlers = _InitialHandlers;
     context.WriteLine(535, " Error: authentication failed");
 }
 protected static void AuthPlain(SmtpServerSession context, SmtpClientInputLine input)
 {
     try
     {
         context._CurrentHandlers = _AuthPlainHandlers;
         if (input.Command.ToUpper() == "AUTH")
         {
             context.ClearIdentity();
             var user = input.Params.Skip(1).FirstOrDefault() ?? "";
             if (user.IsNullOrEmpty())
             {
                 context.WriteLine(334, "Username:"******"Password:"******"Password:".GetBytes().ToBase64());
         }
         else
         {
             var password = input.RawInput.Base64ToBytes().ToString(Encoding.UTF8);
             var found = context.Server.Credentials.Any(c => c.UserName == context.User.Name && c.Password == password);
             if (found == false)
             {
                 FailAuthentication(context);
             }
             else
             {
                 Authentication(context);
             }
         }
     }
     catch (FormatException)
     {
         context._CurrentHandlers = _InitialHandlers;
         UnknownCommand(context, input);
     }
 }
        protected static void AuthCRAMMD5(SmtpServerSession context, SmtpClientInputLine input)
        {
            try
            {
                const string AuthCRAMMD5Key = "AuthCRAMMD5Key";
                context._CurrentHandlers = _AuthCRAMMD5Handlers;
                if (input.Command.ToUpper() == "AUTH")
                {
                    context.ClearIdentity();
                    context.Items[AuthCRAMMD5Key] = Guid.NewGuid().ToByteArray().ToBase64();
                    context.WriteLine(334, context.Items[AuthCRAMMD5Key] as string);
                }
                else
                {
                    var pair = input.RawInput.Base64ToBytes().ToString(Encoding.UTF8).Split(' ');
                    context._User.Name = pair.First();
                    var credential = context.Server
                        .Credentials
                        .FirstOrDefault(c => c.UserName == context._User.Name);
                    if (credential == null)
                    {
                        FailAuthentication(context);
                        return;
                    }

                    var hmacmd5str = new HMACMD5(credential.Password.GetBytes())
                        .ComputeHash(context.Items[AuthCRAMMD5Key].ToString().Base64ToBytes())
                        .ToHexString();
                    context.Items.Remove(AuthCRAMMD5Key);
                    if (hmacmd5str != pair.Last())
                    {
                        FailAuthentication(context);
                    }
                    else
                    {
                        Authentication(context);
                    }
                }
            }
            catch (FormatException)
            {
                context._CurrentHandlers = _InitialHandlers;
                UnknownCommand(context, input);
            }
        }
Пример #6
0
 protected static void FailAuthentication(SmtpServerSession context)
 {
     context.ClearIdentity();
     context._CurrentHandlers = _InitialHandlers;
     context.WriteLine(535, " Error: authentication failed");
 }