Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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 'Start authorization'
- end
- # connect to db
- require 'rubyGems'
- require 'mysql2'
- @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
- debug "Error #{boom}" unless boom.instance_of? EOFError
- ensure
- disconnect
- end
- def auth(username, password)
- log "SELECT 1 FROM users WHERE id = #{username}"
- result = @db.query(
- "SELECT 1 FROM users WHERE id = #{username}"
- ).count
- log "#{result}"
- result > 0 ? 1 : 0
- end
- def isuser(username, password)
- result = @db.query(
- "SELECT 1 FROM users WHERE id = #{username}"
- ).count
- log "#{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 && @cfg['log']['debug']
- end
- end
- NeterDbAuthorization.new
Add Comment
Please, Sign In to add comment