Commit 8f0fe412 authored by Cornelius Kölbel's avatar Cornelius Kölbel

add pam module tests

Closes #116
parent cb910475
...@@ -75,7 +75,7 @@ def pam_sm_authenticate(pamh, flags, argv): ...@@ -75,7 +75,7 @@ def pam_sm_authenticate(pamh, flags, argv):
"%s: user %s in realm %s" % (__name__, user, "%s: user %s in realm %s" % (__name__, user,
realm)) realm))
# First we try to authenticate against the sqlitedb # First we try to authenticate against the sqlitedb
if check_otp(user, pamh.authtok, sqlfile, window=10): if check_offline_otp(user, pamh.authtok, sqlfile, window=10):
syslog.syslog(syslog.LOG_DEBUG, syslog.syslog(syslog.LOG_DEBUG,
"%s: successfully authenticated against offline " "%s: successfully authenticated against offline "
"database %s" % (__name__, sqlfile)) "database %s" % (__name__, sqlfile))
...@@ -111,8 +111,9 @@ def pam_sm_authenticate(pamh, flags, argv): ...@@ -111,8 +111,9 @@ def pam_sm_authenticate(pamh, flags, argv):
result.get("error").get("message"))) result.get("error").get("message")))
rval = pamh.PAM_SYSTEM_ERR rval = pamh.PAM_SYSTEM_ERR
except pamh.exception as exx: except Exception as exx:
rval = exx.pam_result syslog.syslog(syslog.LOG_ERR, "%s: %s" % (__name__, exx))
rval = pamh.PAM_AUTH_ERR
except requests.exceptions.SSLError: except requests.exceptions.SSLError:
syslog.syslog(syslog.LOG_CRIT, "%s: SSL Validation error. Get a valid " syslog.syslog(syslog.LOG_CRIT, "%s: SSL Validation error. Get a valid "
"SSL " "SSL "
...@@ -141,7 +142,7 @@ def pam_sm_chauthtok(pamh, flags, argv): ...@@ -141,7 +142,7 @@ def pam_sm_chauthtok(pamh, flags, argv):
return pamh.PAM_SUCCESS return pamh.PAM_SUCCESS
def check_otp(user, otp, sqlfile, window=10): def check_offline_otp(user, otp, sqlfile, window=10):
""" """
compare the given otp values with the next hashes of the user. compare the given otp values with the next hashes of the user.
...@@ -156,15 +157,19 @@ def check_otp(user, otp, sqlfile, window=10): ...@@ -156,15 +157,19 @@ def check_otp(user, otp, sqlfile, window=10):
res = False res = False
conn = sqlite3.connect(sqlfile) conn = sqlite3.connect(sqlfile)
c = conn.cursor() c = conn.cursor()
_create_table(c)
c.execute("SELECT counter, user, otp FROM authitems WHERE user='%s' " c.execute("SELECT counter, user, otp FROM authitems WHERE user='%s' "
"ORDER by counter" % user) "ORDER by counter" % user)
for x in range(0, window): for x in range(0, window):
r = c.fetchone() r = c.fetchone()
if r:
hash_value = r[2] hash_value = r[2]
if passlib.hash.pbkdf2_sha512.verify(otp, hash_value): if passlib.hash.pbkdf2_sha512.verify(otp, hash_value):
res = True res = True
counter = r[0] counter = r[0]
break break
else:
break
# We found a matching password, so we remove the old entries # We found a matching password, so we remove the old entries
if res: if res:
c.execute("DELETE from authitems WHERE counter <= %i" % counter) c.execute("DELETE from authitems WHERE counter <= %i" % counter)
...@@ -194,11 +199,7 @@ def save_auth_item(sqlfile, user, authitem): ...@@ -194,11 +199,7 @@ def save_auth_item(sqlfile, user, authitem):
conn = sqlite3.connect(sqlfile) conn = sqlite3.connect(sqlfile)
c = conn.cursor() c = conn.cursor()
# Create the table if necessary # Create the table if necessary
try: _create_table(c)
c.execute("CREATE TABLE authitems "
"(counter int, user text, tokenowner text, otp text)")
except:
pass
syslog.syslog(syslog.LOG_DEBUG, "%s: offline save authitem: %s" % ( syslog.syslog(syslog.LOG_DEBUG, "%s: offline save authitem: %s" % (
__name__, authitem)) __name__, authitem))
...@@ -217,3 +218,15 @@ def save_auth_item(sqlfile, user, authitem): ...@@ -217,3 +218,15 @@ def save_auth_item(sqlfile, user, authitem):
# We can also close the connection if we are done with it. # We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost. # Just be sure any changes have been committed or they will be lost.
conn.close() conn.close()
def _create_table(c):
"""
Create table if necessary
:param c: The connection cursor
"""
try:
c.execute("CREATE TABLE authitems "
"(counter int, user text, tokenowner text, otp text)")
except:
pass
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment