<commit_before>require 'resolv'
require 'nifty/utils/random_string'

module Postal
  module SMTPServer
    class Client

      CRAM_MD5_DIGEST = OpenSSL::Digest.new('md5')

      attr_reader :logging_enabled

      def initialize(ip_address)
        @logging_enabled = true
        @ip_address = ip_address
        if @ip_address
          check_ip_address
          @state = :welcome
        else
          @state = :preauth
        end
        transaction_reset
      end

      def check_ip_address
        if @ip_address && Postal.config.smtp_server.log_exclude_ips && @ip_address =~ Regexp.new(Postal.config.smtp_server.log_exclude_ips)
          @logging_enabled = false
        end
      end

      def transaction_reset
        @recipients = []
        @mail_from = nil
        @data = nil
        @headers = nil
      end

      def id
        @id ||= Nifty::Utils::RandomString.generate(:length => 6).upcase
      end

      def handle(data)
        if @state == :preauth
          proxy(data)
        else
          if @proc
            log "\e[32m<= #{data.strip}\e[0m"
            @proc.call(data)
          else
            log "\e[32m<= #{data.strip}\e[0m"
            handle_command(data)
          end
        end
      end

      def finished?
        @finished || false
      end

      def start_tls?
        @start_tls || false
      end

      def start_tls=(value)
        @start_tls = value
      end

      def handle_command(data)
        case data
        when /^QUIT/i           then quit
        when /^STARTTLS/i       then starttls
        when /^EHLO/i           then ehlo(data)
        when /^HELO/i           then helo(data)
        when /^RSET/i           then rset
        when /^NOOP/i           then noop
        when /^AUTH PLAIN/i     then auth_plain(data)
        when /^AUTH LOGIN/i     then auth_login(data)
        when /^AUTH CRAM-MD5/i  then auth_cram_md5(data)
        when /^MAIL FROM/i      then mail_from(data)
        when /^RCPT TO/i        then rcpt_to(data)
        when /^DATA/i           then data(data)
        else
          '502 Invalid/unsupported command'
        end
      end

      def log(text)
        return false unless @logging_enabled
        Postal.logger_for(:smtp_server).debug "[#{id}] #{text}"
      end

      private

      def resolve_hostname
        @hostname = Resolv.new.getname(@ip_address) rescue @ip_address
      end

      def proxy(data)
        if m = data.match(/\APROXY (.+) (.+) (.+) (.+) (.+)\z/)
          @ip_address = m[2]
          check_ip_address
          @state = :welcome
          log "\e[35m   Client identified as #{@ip_address}\e[0m"
          "220 #{Postal.config.dns.smtp_server_hostname} ESMTP Postal/#{id}"
        else
          @finished = true
          '502 Proxy Error'
        end
      end

      def quit
        @finished = true
        "221 Closing Connection"
      end

      def starttls
        if Postal.config.smtp_server.tls_enabled?
          @start_tls = true
          @tls = true
          "220 Ready to start TLS"
        else
          "502 TLS not available"
        end
      end

      def ehlo(data)
        resolve_hostname
        @helo_name = data.strip.split(' ', 2)[1]
        transaction_reset
        @state = :welcomed
        ["250-My capabilities are", Postal.config.smtp_server.tls_enabled? && !@tls ? "250-STARTTLS" : nil, "250 AUTH CRAM-MD5 PLAIN LOGIN", ]
      end

      def helo(data)
        resolve_hostname
        @helo_name = data.strip.split(' ', 2)[1]
        transaction_reset
        @state = :welcomed
        "250 #{Postal.config.dns.smtp_server_hostname}"
      end

      def rset
        transaction_reset
        @state = :welcomed
        '250 OK'
      end

      def noop
        '250 OK'
      end

      def auth_plain(data)
        handler = Proc.new do |data|
          @proc = nil
          data = Base64.decode64(data)
          parts = data.split("\0")
          username, password = parts[-2], parts[-1]
          unless username && password
            next '535 Authenticated failed - protocol error'
          end
          authenticate(password)
        end

        data = data.gsub(/AUTH PLAIN ?/i, '')
        if data.strip == ''
          @proc = handler
          '334'
        else
          handler.call(data)
        end
      end

      def auth_login(data)
        password_handler = Proc.new do |data|
          @proc = nil
          password = Base64.decode64(data)
          authenticate(password)
        end

        username_handler = Proc.new do |data|
          @proc = password_handler
          '<PASSWORD>'
        end

        data = data.gsub!(/AUTH LOGIN ?/i, '')
        if data.strip == ''
          @proc = username_handler
          '334 VXNlcm5hbWU6'
        end
      end

      def authenticate(password)
        if @credential = Credential.where(:type => 'SMTP', :key => password).first
          @credential.use
          "235 Granted for #{@credential.server.organization.permalink}/#{@credential.server.permalink}"
        else
          "535 Invalid credential"
        end
      end

      def auth_cram_md5(data)
        challenge = Digest::SHA1.hexdigest(Time.now.to_i.to_s + rand(100000).to_s)
        challenge = "<#{challenge[0,20]}@#{Postal.config.dns.smtp_server_hostname}>"

        handler = Proc.new do |data|
          @proc = nil
          username, password = Base64.decode64(data).split(' ', 2).map{ |a| a.chomp }
          org_permlink, server_permalink = username.split(/[\/\_]/, 2)
          server = ::Server.includes(:organization).where(:organizations => {:permalink => org_permlink}, :permalink => server_permalink).first<commit_msg>allow auth login with inline username<commit_after>require 'resolv'
require 'nifty/utils/random_string'

module Postal
  module SMTPServer
    class Client

      CRAM_MD5_DIGEST = OpenSSL::Digest.new('md5')

      attr_reader :logging_enabled

      def initialize(ip_address)
        @logging_enabled = true
        @ip_address = ip_address
        if @ip_address
          check_ip_address
          @state = :welcome
        else
          @state = :preauth
        end
        transaction_reset
      end

      def check_ip_address
        if @ip_address && Postal.config.smtp_server.log_exclude_ips && @ip_address =~ Regexp.new(Postal.config.smtp_server.log_exclude_ips)
          @logging_enabled = false
        end
      end

      def transaction_reset
        @recipients = []
        @mail_from = nil
        @data = nil
        @headers = nil
      end

      def id
        @id ||= Nifty::Utils::RandomString.generate(:length => 6).upcase
      end

      def handle(data)
        if @state == :preauth
          proxy(data)
        else
          if @proc
            log "\e[32m<= #{data.strip}\e[0m"
            @proc.call(data)
          else
            log "\e[32m<= #{data.strip}\e[0m"
            handle_command(data)
          end
        end
      end

      def finished?
        @finished || false
      end

      def start_tls?
        @start_tls || false
      end

      def start_tls=(value)
        @start_tls = value
      end

      def handle_command(data)
        case data
        when /^QUIT/i           then quit
        when /^STARTTLS/i       then starttls
        when /^EHLO/i           then ehlo(data)
        when /^HELO/i           then helo(data)
        when /^RSET/i           then rset
        when /^NOOP/i           then noop
        when /^AUTH PLAIN/i     then auth_plain(data)
        when /^AUTH LOGIN/i     then auth_login(data)
        when /^AUTH CRAM-MD5/i  then auth_cram_md5(data)
        when /^MAIL FROM/i      then mail_from(data)
        when /^RCPT TO/i        then rcpt_to(data)
        when /^DATA/i           then data(data)
        else
          '502 Invalid/unsupported command'
        end
      end

      def log(text)
        return false unless @logging_enabled
        Postal.logger_for(:smtp_server).debug "[#{id}] #{text}"
      end

      private

      def resolve_hostname
        @hostname = Resolv.new.getname(@ip_address) rescue @ip_address
      end

      def proxy(data)
        if m = data.match(/\APROXY (.+) (.+) (.+) (.+) (.+)\z/)
          @ip_address = m[2]
          check_ip_address
          @state = :welcome
          log "\e[35m   Client identified as #{@ip_address}\e[0m"
          "220 #{Postal.config.dns.smtp_server_hostname} ESMTP Postal/#{id}"
        else
          @finished = true
          '502 Proxy Error'
        end
      end

      def quit
        @finished = true
        "221 Closing Connection"
      end

      def starttls
        if Postal.config.smtp_server.tls_enabled?
          @start_tls = true
          @tls = true
          "220 Ready to start TLS"
        else
          "502 TLS not available"
        end
      end

      def ehlo(data)
        resolve_hostname
        @helo_name = data.strip.split(' ', 2)[1]
        transaction_reset
        @state = :welcomed
        ["250-My capabilities are", Postal.config.smtp_server.tls_enabled? && !@tls ? "250-STARTTLS" : nil, "250 AUTH CRAM-MD5 PLAIN LOGIN", ]
      end

      def helo(data)
        resolve_hostname
        @helo_name = data.strip.split(' ', 2)[1]
        transaction_reset
        @state = :welcomed
        "250 #{Postal.config.dns.smtp_server_hostname}"
      end

      def rset
        transaction_reset
        @state = :welcomed
        '250 OK'
      end

      def noop
        '250 OK'
      end

      def auth_plain(data)
        handler = Proc.new do |data|
          @proc = nil
          data = Base64.decode64(data)
          parts = data.split("\0")
          username, password = parts[-2], parts[-1]
          unless username && password
            next '535 Authenticated failed - protocol error'
          end
          authenticate(password)
        end

        data = data.gsub(/AUTH PLAIN ?/i, '')
        if data.strip == ''
          @proc = handler
          '334'
        else
          handler.call(data)
        end
      end

      def auth_login(data)
        password_handler = Proc.new do |data|
          @proc = nil
          password = Base64.decode64(data)
          authenticate(password)
        end

        username_handler = Proc.new do |data|
          @proc = password_handler
          '<PASSWORD>'
        end

        data = data.gsub!(/AUTH LOGIN ?/i, '')
        if data.strip == ''
          @proc = username_handler
          '334 VXNlcm5hbWU6'
        else
          @proc = password_handler
          '<PASSWORD>'
        end
      end

      def authenticate(password)
        if @credential = Credential.where(:type => 'SMTP', :key => password).first
          @credential.use
          "235 Granted for #{@credential.server.organization.permalink}/#{@credential.server.permalink}"
        else
          "535 Invalid credential"
        end
      end

      def auth_cram_md5(data)
        challenge = Digest::SHA1.hexdigest(Time.now.to_i.to_s + rand(100000).to_s)
        challenge = "<#{challenge[0,20]}@#{Postal.config.dns.smtp_server_hostname}>"

        handler = Proc.new do |data|
          @proc = nil
          username, password = Base64.decode64(data).split(' ', 2).map{ |a| a.chomp }
          org_permlink, server_permalink = username.split(/[\/\_]/, 2)
          server = ::Server.includes(:organization).where(:organizations => {:permalink => org_permlink}, :permalink => server_permalink).first
{'fraction_non_alphanumeric': 0.10380078636959371, 'fraction_numerical': 0.01764962865880297, 'mean_word_length': 1.655068429598701, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1762749', 'n_tokens_mistral': 3713, 'n_tokens_neox': 3481, 'n_words': 808}