2018-02-27 13:29:28 +00:00
|
|
|
require "lualdap"
|
|
|
|
|
|
|
|
function auth_passdb_lookup(req)
|
|
|
|
local ldap_host = "{{ getenv "LDAP_HOST" }}"
|
|
|
|
local ldap_bin_dn = "{{ getenv "LDAP_BIND_DN" }}"
|
|
|
|
local ldap_bind_password = "{{ getenv "LDAP_BIND_PASSWORD" }}"
|
2018-02-27 16:02:32 +00:00
|
|
|
local ldap_use_tls = {{ if eq (getenv "LDAP_USE_TLS") "yes" }}true{{ else }}false{{ end }}
|
2018-02-27 13:29:28 +00:00
|
|
|
|
|
|
|
ld = assert (lualdap.open_simple(
|
|
|
|
ldap_host,
|
|
|
|
ldap_bin_dn,
|
|
|
|
ldap_bind_password,
|
|
|
|
ldap_use_tls))
|
|
|
|
|
2018-02-27 14:05:17 +00:00
|
|
|
local username = req.user
|
2018-02-27 13:29:28 +00:00
|
|
|
local ldap_pass_filter = "{{ getenv "LDAP_PASS_FILTER" }}"
|
|
|
|
local ldap_pass_filter_formatted = ldap_pass_filter:gsub("%%u", username)
|
2018-02-27 13:52:38 +00:00
|
|
|
local ldap_base_dn = "{{ getenv "LDAP_BASE_DN" }}"
|
2018-02-27 13:29:28 +00:00
|
|
|
|
|
|
|
local user_count = 0
|
|
|
|
for dn, attribs in ld:search { base = ldap_base_dn, scope = "subtree", filter = ldap_pass_filter_formatted } do
|
|
|
|
user_count = user_count + 1
|
|
|
|
end
|
|
|
|
|
2018-02-27 17:18:49 +00:00
|
|
|
local return_code = dovecot.auth.PASSDB_RESULT_NEXT
|
|
|
|
local return_text = ""
|
|
|
|
|
2018-02-27 13:29:28 +00:00
|
|
|
local user_exists = user_count == 1
|
|
|
|
if user_exists then
|
|
|
|
local app_base_dn = "{{ getenv "LDAP_APP_PASSWORDS_BASE_DN" }}"
|
|
|
|
local app_base_dn_formatted = app_base_dn:gsub("%%u", username)
|
|
|
|
local app_pass_filter = "{{ getenv "LDAP_APP_PASSWORDS_FILTER" }}"
|
2018-02-27 16:02:32 +00:00
|
|
|
local ldap_user_attribute = "{{ getenv "LDAP_USER_ATTRIBUTE" "cn" }}"
|
2018-02-27 13:29:28 +00:00
|
|
|
|
2018-02-27 14:15:24 +00:00
|
|
|
local user_password = req.password
|
2018-02-27 13:29:28 +00:00
|
|
|
|
|
|
|
for dn, attribs in ld:search { base = app_base_dn_formatted, scope = "subtree", filter = app_pass_filter } do
|
2018-02-27 17:18:49 +00:00
|
|
|
req:log_info(string.format("trying %s...", dn))
|
|
|
|
|
2018-02-27 16:02:32 +00:00
|
|
|
local test_conn = lualdap.open_simple(
|
2018-02-27 13:29:28 +00:00
|
|
|
ldap_host,
|
|
|
|
dn,
|
|
|
|
user_password,
|
|
|
|
ldap_use_tls)
|
|
|
|
if test_conn ~= nil then
|
2018-02-27 17:19:20 +00:00
|
|
|
req:log_info(string.format("%s suceeded!", dn))
|
2018-02-27 17:18:49 +00:00
|
|
|
return dovecot.auth.PASSDB_RESULT_OK, string.format("password=%s user=%s", user_password, username)
|
2018-02-27 13:29:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return dovecot.auth.PASSDB_RESULT_USER_UNKNOWN, "no such user"
|
|
|
|
end
|
|
|
|
|
2018-02-27 17:18:49 +00:00
|
|
|
return return_code, return_text
|
2018-02-27 13:29:28 +00:00
|
|
|
end
|