Forums | Admin

Discussion Forums: open-discussion

Start New Thread Start New Thread

 

By: kohei kajimoto
IMAP NTLM Authentication [ reply ]  
2006-09-08 16:28
# here is an example file for imap ntlm authentication.

require "net/imap"
require "ntlm"

$host = "localhost"
$port = 143
$ssl = false
$user = "koheik"
$pass = "koheik"

module Net
class IMAP
class NtlmAuthenticator
def process(data)
case @state
when 1
@state = 2
t1 = NTLM::Message::Type1.new()
return t1.serialize
when 2
@state = 3
t2 = NTLM::Message.parse(data)
t3 = t2.response({:user => @user, :password => @password}, {:ntlmv2 => (@ntlm_type == "ntlmv2")})
return t3.serialize
end
end

private

def initialize(user, password, ntlm_type = "ntlmv2")
@user = user
@password = password
@ntlm_type = @ntlm_type
@state = 1
end
end
add_authenticator "NTLM", NtlmAuthenticator

class ResponseParser
def continue_req
match(T_PLUS)
if lookahead.symbol == T_CRLF # means empty message
return ContinuationRequest.new(ResponseText.new(nil, ""), @str)
end
match(T_SPACE)
return ContinuationRequest.new(resp_text, @str)
end
end
end
end

Net::IMAP::debug = true

imap = Net::IMAP.new($host, $port, $ssl)
imap.authenticate("NTLM", $user, $pass)
imap.examine("Inbox")
# imap.search(["RECENT"]).each do |message_id|
# envelope = imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
# from = envelope.from.nil? ? "" : envelope.from[0].name
# subject = envelope.subject
# puts "#{message_id} #{from}: \t#{subject}"
# end
imap.logout
# imap.disconnect