more tests, better interface between lua and chatctl, also passing dovecot extras

This commit is contained in:
holger krekel
2023-10-12 18:50:40 +02:00
parent 067252703f
commit 940b39bce7
2 changed files with 57 additions and 24 deletions

View File

@@ -6,12 +6,18 @@ if sys.argv[1] == "hexauth":
login = base64.b16decode(sys.argv[2]) login = base64.b16decode(sys.argv[2])
password = base64.b16decode(sys.argv[3]) password = base64.b16decode(sys.argv[3])
if login == b"link2xt@instant2.testrun.org" and password == b"Ahyei6ie": if login == b"link2xt@instant2.testrun.org" and password == b"Ahyei6ie":
sys.exit(0) print("status=ok")
print("homedir=/home/vmail/link2xt")
print("gid=vmail")
print("uid=vmail")
else: else:
sys.exit(1) print("status=fail")
elif sys.argv[1] == "hexlookup": elif sys.argv[1] == "hexlookup":
login = base64.b16decode(sys.argv[2]) login = base64.b16decode(sys.argv[2])
if login == b"link2xt@instant2.testrun.org": if login == b"link2xt@instant2.testrun.org":
sys.exit(0) print("status=ok")
print("homedir=/home/vmail/link2xt")
print("gid=vmail")
print("uid=vmail")
else: else:
sys.exit(1) print("status=fail")

View File

@@ -4,8 +4,10 @@
if dovecot == nil then if dovecot == nil then
dovecot = { dovecot = {
auth = { auth = {
PASSDB_RESULT_OK="OK", PASSDB_RESULT_OK="PASSWORD-OK",
PASSDB_RESULT_PASSWORD_MISMATCH="MISMATCH" PASSDB_RESULT_PASSWORD_MISMATCH="PASSWORD-MISMATCH",
USERDB_RESULT_OK="USERDB-OK",
USERDB_RESULT_USER_UNKNOWN="USERDB-UNKNOWN"
} }
} }
end end
@@ -19,17 +21,29 @@ end
-- call out to python program to actually manage authentication for dovecot -- call out to python program to actually manage authentication for dovecot
function chatctl_verify(user, password) function chatctl_verify(user, password)
return os.execute("python chatctl.py hexauth "..escape(user).." "..escape(password)) local handle = io.popen("python chatctl.py hexauth "..escape(user).." "..escape(password))
local result = handle:read("*a")
handle:close()
return split_chatctl(result)
end end
function chatctl_lookup(hex, user) function chatctl_lookup(user)
return os.execute("python chatctl.py hexlookup "..escape(user)) assert(user)
local handle = io.popen("python chatctl.py hexlookup "..escape(user))
local result = handle:read("*a")
handle:close()
return split_chatctl(result)
end
function get_extra_dovecot_output(res)
return {homedir=res.homedir, uid=res.uid, gid=res.gid}
end end
function auth_password_verify(request, password) function auth_passdb_verify(request, password)
if chatctl_verify(request.user, password) then local res = chatctl_verify(request.user, password)
return dovecot.auth.PASSDB_RESULT_OK, {} if res.status == "ok" then
return dovecot.auth.PASSDB_RESULT_OK, get_extra_dovecot_output(res)
end end
return dovecot.auth.PASSDB_RESULT_PASSWORD_MISMATCH, "" return dovecot.auth.PASSDB_RESULT_PASSWORD_MISMATCH, ""
end end
@@ -42,8 +56,9 @@ function auth_passdb_lookup(request)
end end
function auth_userdb_lookup(request) function auth_userdb_lookup(request)
if chatctl_lookup(request.user) then local res = chatctl_lookup(request.user)
return dovecot.auth.USERDB_RESULT_OK, "uid=vmail gid=vmail" if res.status == "ok" then
return dovecot.auth.USERDB_RESULT_OK, get_extra_dovecot_output(res)
end end
return dovecot.auth.USERDB_RESULT_USER_UNKNOWN, "no such user" return dovecot.auth.USERDB_RESULT_USER_UNKNOWN, "no such user"
end end
@@ -58,16 +73,27 @@ end
-- Tests for testing the lua<->python interaction -- Tests for testing the lua<->python interaction
function test_verify_ok(user, password) function test_passdb_verify_ok(user, password)
local res = auth_password_verify({user=user}, password) local res, extra = auth_passdb_verify({user=user}, password)
assert(res=="OK") assert(res==dovecot.auth.PASSDB_RESULT_OK)
print("OK test_verify_ok "..user.." "..password) assert(extra.uid == "vmail")
assert(extra.gid == "vmail")
-- assert(extra.homedir == "/home/vmail/link2xt")
print("OK test_passdb_verify_ok "..user.." "..password)
end end
function test_verify_mismatch(user, password) function test_passdb_verify_mismatch(user, password)
local res = auth_password_verify({user=user}, password) local res = auth_passdb_verify({user=user}, password)
assert(res == "MISMATCH") assert(res == dovecot.auth.PASSDB_RESULT_PASSWORD_MISMATCH)
print("OK test_verify_mismatch "..user.." "..password) print("OK test_passdb_verify_mismatch "..user.." "..password)
end
function test_userdb_lookup_ok(user)
local res, extra = auth_userdb_lookup({user=user})
assert(extra.uid == "vmail")
assert(extra.gid == "vmail")
assert(res == dovecot.auth.USERDB_RESULT_OK)
print("OK test_lookup_ok "..user)
end end
function test_split_chatctl() function test_split_chatctl()
@@ -79,6 +105,7 @@ function test_split_chatctl()
end end
test_split_chatctl() test_split_chatctl()
test_verify_ok("link2xt@instant2.testrun.org", "Ahyei6ie") test_passdb_verify_ok("link2xt@instant2.testrun.org", "Ahyei6ie")
test_verify_mismatch("link2xt@instant2.testrun.org", "Aqwlek") test_passdb_verify_mismatch("link2xt@instant2.testrun.org", "Aqwlek")
test_userdb_lookup_ok("link2xt@instant2.testrun.org")