Exemplo n.º 1
0
        bool SkipCommand(string command, byte[] buffer, ref int index, int endIndex)
        {
            while (index < endIndex && commandIndex < command.Length)
            {
                if (buffer[index] != (byte)command[commandIndex])
                {
                    state = SmtpAuthCommandState.Error;
                    break;
                }

                commandIndex++;
                index++;
            }

            return(commandIndex == command.Length);
        }
Exemplo n.º 2
0
        public IList <AuthenticationSecret> DetectSecrets(byte[] buffer, int offset, int count)
        {
            if (!IsAuthenticating || state == SmtpAuthCommandState.Error || count == 0)
            {
                return(EmptyAuthSecrets);
            }

            int endIndex = offset + count;
            int index    = offset;

            if (state == SmtpAuthCommandState.Auth)
            {
                if (SkipCommand("AUTH ", buffer, ref index, endIndex))
                {
                    state = SmtpAuthCommandState.AuthMechanism;
                }

                if (index >= endIndex || state == SmtpAuthCommandState.Error)
                {
                    return(EmptyAuthSecrets);
                }
            }

            if (state == SmtpAuthCommandState.AuthMechanism)
            {
                while (index < endIndex && buffer[index] != (byte)' ' && buffer[index] != (byte)'\r')
                {
                    index++;
                }

                if (index < endIndex)
                {
                    if (buffer[index] == (byte)' ')
                    {
                        state = SmtpAuthCommandState.AuthToken;
                    }
                    else
                    {
                        state = SmtpAuthCommandState.AuthNewLine;
                    }

                    index++;
                }

                if (index >= endIndex)
                {
                    return(EmptyAuthSecrets);
                }
            }

            if (state == SmtpAuthCommandState.AuthNewLine)
            {
                if (buffer[index] == (byte)'\n')
                {
                    state = SmtpAuthCommandState.AuthToken;
                    index++;
                }
                else
                {
                    state = SmtpAuthCommandState.Error;
                }

                if (index >= endIndex || state == SmtpAuthCommandState.Error)
                {
                    return(EmptyAuthSecrets);
                }
            }

            int startIndex = index;

            while (index < endIndex && buffer[index] != (byte)'\r')
            {
                index++;
            }

            if (index < endIndex)
            {
                state = SmtpAuthCommandState.AuthNewLine;
            }

            if (index == startIndex)
            {
                return(EmptyAuthSecrets);
            }

            var secret = new AuthenticationSecret(startIndex, index - startIndex);

            if (state == SmtpAuthCommandState.AuthNewLine)
            {
                index++;

                if (index < endIndex)
                {
                    if (buffer[index] == (byte)'\n')
                    {
                        state = SmtpAuthCommandState.AuthToken;
                    }
                    else
                    {
                        state = SmtpAuthCommandState.Error;
                    }
                }
            }

            return(new AuthenticationSecret[] { secret });
        }