Skip to content

Commit

Permalink
Merge pull request #1 from sudoman/master
Browse files Browse the repository at this point in the history
  • Loading branch information
cdauth authored Dec 19, 2016
2 parents 4b46474 + 1028ecf commit 3a5f616
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions auth_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#Setup
########################################################################

import sys, logging, struct, hashlib, MySQLdb, crypt, random, atexit
import sys, logging, struct, hashlib, MySQLdb, crypt, random, atexit, time

sys.stderr = open('/var/log/ejabberd/extauth_err.log', 'a')
logging.basicConfig(level=logging.INFO,
Expand All @@ -53,7 +53,8 @@
try:
database=MySQLdb.connect(db_host, db_user, db_pass, db_name)
except:
logging.debug("Unable to initialize database, check settings!")
logging.error("Unable to initialize database, check settings!")
time.sleep(10)
sys.exit(1)

@atexit.register
Expand All @@ -77,7 +78,7 @@ def ejabberd_in():
logging.debug("trying to read 2 bytes from ejabberd:")

input_length = sys.stdin.read(2)

if len(input_length) is not 2:
logging.debug("ejabberd sent us wrong things!")
raise EjabberdInputError('Wrong input from ejabberd!')
Expand All @@ -95,11 +96,11 @@ def ejabberd_in():

def ejabberd_out(bool):
logging.debug("Ejabberd gets: %s" % bool)

token = genanswer(bool)

logging.debug("sent bytes: %#x %#x %#x %#x" % (ord(token[0]), ord(token[1]), ord(token[2]), ord(token[3])))

sys.stdout.write(token)
sys.stdout.flush()

Expand Down Expand Up @@ -154,7 +155,7 @@ def auth(user, host, password):
def setpass(user, host, password):
if db_query_setpass == "":
return False

database.ping(True)
with database as dbcur:
dbcur.execute(db_query_setpass, {"user": user, "host": host, "password": password_hash(password)})
Expand All @@ -168,11 +169,11 @@ def setpass(user, host, password):
def tryregister(user, host, password):
if db_query_register == "":
return False

if isuser(user, host):
logging.info("Could not register user %s@%s as it already exists." % (user, host))
return False

database.ping(True)
with database as dbcur:
dbcur.execute(db_query_register, {"user": user, "host": host, "password": password_hash(password)})
Expand All @@ -196,7 +197,7 @@ def removeuser(user, host):
def removeuser3(user, host, password):
if db_query_unregister == "":
return False

return auth(user, host, password) and removeuser(user, host)


Expand All @@ -208,17 +209,17 @@ def removeuser3(user, host, password):

while True:
logging.debug("start of infinite loop")
try:

try:
ejab_request = ejabberd_in()
except EOFError:
break
except Exception as e:
logging.exception("Exception occured while reading stdin")
raise

logging.debug('operation: %s' % (":".join(ejab_request)))

op_result = False
try:
if ejab_request[0] == "auth":
Expand All @@ -235,11 +236,11 @@ def removeuser3(user, host, password):
op_result = removeuser3(ejab_request[1], ejab_request[2], ejab_request[3])
except Exception:
logging.exception("Exception occured")

ejabberd_out(op_result)
logging.debug("successful" if op_result else "unsuccessful")

logging.debug("end of infinite loop")
logging.info('extauth script terminating')
database.close()
sys.exit(exitcode)
sys.exit(exitcode)

0 comments on commit 3a5f616

Please sign in to comment.