Created
April 26, 2012 01:19
-
-
Save huangxiangdan/2495010 to your computer and use it in GitHub Desktop.
a ejabberd extauth script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
class NeterDbAuthorization | |
def initialize(config_file = 'config.yml') | |
# load config | |
require 'yaml' | |
@cfg = YAML.load_file(config_file) | |
# load logger | |
if @cfg['log']['file'] | |
require 'logger' | |
@log = Logger.new(@cfg['log']['file']) | |
@log.level = @cfg['log']['level'] | |
log 'Start authorization' | |
end | |
# connect to db | |
require 'rubyGems' | |
require 'mysql2' | |
while(true) | |
begin | |
@db = Mysql2::Client.new(:host => @cfg['database']['host'], :username => @cfg['database']['username'], | |
:password => @cfg['database']['password'], :database => @cfg['database']['database']) | |
# listen input | |
buffer = String.new | |
while STDIN.sysread(2, buffer) && buffer.length == 2 | |
debug 'Get packet' | |
length = buffer.unpack('n')[0] | |
debug "Packet length #{length}" | |
operation, username, domain, password = STDIN.sysread(length).split(':') | |
debug "Do '#{operation}' for '#{username}', '#{password}'" | |
STDOUT.syswrite([2, | |
case operation | |
when 'auth', 'isuser' | |
debug "test #{username.inspect}" | |
debug "test #{password.inspect}" | |
send(operation, username, password) | |
when 'setpass' | |
0 | |
else | |
log 'Unknown operation: ' + operation | |
0 | |
end ].pack('nn') | |
) | |
end | |
rescue Exception => boom | |
error "Error #{boom}" | |
ensure | |
error 'Port closed' | |
@db.close if @db | |
end | |
end | |
end | |
def auth(username, password) | |
debug "SELECT 1 FROM users WHERE id = #{username}" | |
result = @db.query( | |
"SELECT 1 FROM users WHERE id = #{username}" | |
).count | |
debug "#{result}" | |
result > 0 ? 1 : 0 | |
end | |
def isuser(username, password) | |
result = @db.query( | |
"SELECT 1 FROM users WHERE id = #{username}" | |
).count | |
debug "#{result}" | |
result > 0 ? 1 : 0 | |
end | |
def disconnect | |
log 'Port closed' | |
@db.close if @db | |
exit | |
end | |
def log(message) | |
@log.info message if @log | |
end | |
def debug(message) | |
@log.debug message if @log | |
end | |
def error(message) | |
@log.error message if @log | |
end | |
end | |
NeterDbAuthorization.new |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use Unix::Syslog qw(:macros :subs); | |
my $domain = $ARGV[0] || "example.com"; | |
while(1) | |
{ | |
# my $rin = '',$rout; | |
# vec($rin,fileno(STDIN),1) = 1; | |
# $ein = $rin; | |
# my $nfound = select($rout=$rin,undef,undef,undef); | |
my $buf = ""; | |
syslog LOG_INFO,"waiting for packet"; | |
my $nread = sysread STDIN,$buf,2; | |
do { syslog LOG_INFO,"port closed"; exit; } unless $nread == 2; | |
my $len = unpack "n",$buf; | |
my $nread = sysread STDIN,$buf,$len; | |
my ($op,$user,$host,$password) = split /:/,$buf; | |
#$user =~ s/\./\//og; | |
my $jid = "$user\@$domain"; | |
my $result; | |
syslog(LOG_INFO,"request (%s)", $op); | |
SWITCH: | |
{ | |
$op eq 'auth' and do | |
{ | |
$result = 1; | |
},last SWITCH; | |
$op eq 'setpass' and do | |
{ | |
$result = 1; | |
},last SWITCH; | |
$op eq 'isuser' and do | |
{ | |
# password is null. Return 1 if the user $user\@$domain exitst. | |
$result = 1; | |
},last SWITCH; | |
$op eq 'tryregister' and do | |
{ | |
$result = 1; | |
},last SWITCH; | |
$op eq 'removeuser' and do | |
{ | |
# password is null. Return 1 if the user $user\@$domain exitst. | |
$result = 1; | |
},last SWITCH; | |
$op eq 'removeuser3' and do | |
{ | |
$result = 1; | |
},last SWITCH; | |
}; | |
my $out = pack "nn",2,$result ? 1 : 0; | |
syswrite STDOUT,$out; | |
} | |
closelog; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
log: | |
file: db_auth.log | |
level: 3 | |
database: | |
host: localhost | |
username: kecheng | |
password: kecheng | |
database: kecheng |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment