Skip to content

Instantly share code, notes, and snippets.

@abloom
Created February 18, 2011 17:16
Show Gist options
  • Save abloom/834015 to your computer and use it in GitHub Desktop.
Save abloom/834015 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'logger'
require 'rest_client'
$stdout.sync = true
$stdin.sync = true
path = "/usr/local/var/log/ejabberd/auth.log"
file = File.open(path, File::WRONLY | File::APPEND | File::CREAT)
file.sync = true
logger = Logger.new(file)
logger.level = Logger::DEBUG
def auth(username, password)
RestClient.post("http://my-rails-app.com/login", :username => username, :password => password)
return true
rescue RestClient::Exception
return false
end
logger.info "Starting ejabberd authentication service"
loop do
begin
$stdin.eof? # wait for input
start = Time.now
msg = $stdin.read(2)
length = msg.unpack('n').first
msg = $stdin.read(length)
cmd, *data = msg.split(":")
logger.info "Incoming Request: '#{cmd}'"
success = case cmd
when "auth"
logger.info "Authenticating #{data[0]}@#{data[1]}"
auth(data[0], data[2])
else
false
end
bool = success ? 1 : 0
$stdout.write [2, bool].pack("nn")
logger.info "Response: #{success ? "success" : "failure"}"
rescue => e
logger.error "#{e.class.name}: #{e.message}"
logger.error e.backtrace.join("\n\t")
end
end
@dittert
Copy link

dittert commented Jan 3, 2013

Thanks for sharing this. Please note that your script fails if the password contains a colon. The reason is the split on the string in line 33 that doesn't handle this case properly.

@m4tm4t
Copy link

m4tm4t commented Jan 7, 2013

Sometimes, the script fail at

length = msg.unpack('n').first` 

Undefined method unpack for nil class

it's not related to your module (I use another similar one) but I think Ejabberd is sending a bad thing to the script then the loop raise error infinitely and take heavy CPU load.

Did you experienced or have some informations on this issue ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment