2015-02-01 15:04:36 +01:00
|
|
|
--
|
|
|
|
-- helpers.lua
|
|
|
|
--
|
|
|
|
-- This is a file called at every request by the `access.lua` file. It contains
|
|
|
|
-- a set of useful functions related to HTTP and LDAP.
|
|
|
|
--
|
|
|
|
|
2015-02-15 13:03:01 +01:00
|
|
|
module('helpers', package.seeall)
|
2015-02-01 15:04:36 +01:00
|
|
|
|
2015-05-16 09:42:26 +02:00
|
|
|
local cache = ngx.shared.cache
|
|
|
|
local conf = config.get_config()
|
2017-05-12 13:54:39 +02:00
|
|
|
local logger = require("log")
|
|
|
|
|
2019-10-03 23:11:52 +02:00
|
|
|
-- url parser, c.f. https://rosettacode.org/wiki/URL_parser#Lua
|
|
|
|
local url_parser = require "socket.url"
|
|
|
|
|
2020-01-17 08:01:24 +01:00
|
|
|
-- Import Perl regular expressions library
|
|
|
|
local rex = require "rex_pcre"
|
|
|
|
|
2020-04-01 00:43:59 +02:00
|
|
|
local is_logged_in = false
|
|
|
|
|
2020-03-29 18:02:49 +02:00
|
|
|
function refresh_config()
|
|
|
|
conf = config.get_config()
|
|
|
|
end
|
|
|
|
|
|
|
|
function get_config()
|
|
|
|
return conf
|
|
|
|
end
|
2020-01-17 08:01:24 +01:00
|
|
|
|
|
|
|
-- The 'match' function uses PCRE regex as default
|
|
|
|
-- If '%.' is found in the regex, we assume it's a LUA regex (legacy code)
|
|
|
|
-- 'match' returns the matched text.
|
|
|
|
function match(s, regex)
|
|
|
|
if not string.find(regex, '%%%.') then
|
|
|
|
return rex.match(s, regex)
|
|
|
|
else
|
|
|
|
return string.match(s,regex)
|
|
|
|
end
|
|
|
|
end
|
2019-10-03 23:11:52 +02:00
|
|
|
|
2015-02-01 15:04:36 +01:00
|
|
|
-- Read a FS stored file
|
|
|
|
function read_file(file)
|
|
|
|
local f = io.open(file, "rb")
|
|
|
|
if not f then return false end
|
|
|
|
local content = f:read("*all")
|
|
|
|
f:close()
|
|
|
|
return content
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Lua has no sugar :D
|
2016-04-30 12:40:59 +02:00
|
|
|
function is_in_table(t, v)
|
2015-02-01 15:04:36 +01:00
|
|
|
for key, value in ipairs(t) do
|
|
|
|
if value == v then return key end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-06-14 12:53:16 +02:00
|
|
|
-- Get the index of a value in a table
|
|
|
|
function index_of(t,val)
|
2017-08-10 16:31:00 +02:00
|
|
|
for k,v in ipairs(t) do
|
2015-06-14 12:53:16 +02:00
|
|
|
if v == val then return k end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-02-01 15:04:36 +01:00
|
|
|
-- Test whether a string starts with another
|
2016-04-30 12:40:59 +02:00
|
|
|
function string.starts(String, Start)
|
2019-01-23 10:22:02 +01:00
|
|
|
if not String then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return string.sub(String, 1, string.len(Start)) == Start
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Test whether a string ends with another
|
2016-04-30 12:40:59 +02:00
|
|
|
function string.ends(String, End)
|
2015-02-01 15:04:36 +01:00
|
|
|
return End=='' or string.sub(String, -string.len(End)) == End
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Find a string by its translate key in the right language
|
2016-04-30 12:40:59 +02:00
|
|
|
function t(key)
|
2017-08-10 16:31:00 +02:00
|
|
|
if conf.lang and i18n[conf.lang] and i18n[conf.lang][key] then
|
|
|
|
return i18n[conf.lang][key]
|
2015-02-01 15:04:36 +01:00
|
|
|
else
|
|
|
|
return i18n[conf["default_language"]][key] or ""
|
|
|
|
end
|
2015-02-12 12:08:52 +01:00
|
|
|
end
|
2015-02-01 15:04:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
-- Store a message in the flash shared table in order to display it at the
|
|
|
|
-- next response
|
2016-04-30 12:40:59 +02:00
|
|
|
function flash(wat, message)
|
2015-02-01 15:04:36 +01:00
|
|
|
if wat == "fail"
|
|
|
|
or wat == "win"
|
|
|
|
or wat == "info"
|
|
|
|
then
|
|
|
|
flashs[wat] = message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-05-18 08:34:36 +02:00
|
|
|
-- Hash a string using hmac_sha512, return a hexa string
|
|
|
|
function hmac_sha512(key, message)
|
2017-05-22 23:01:18 +02:00
|
|
|
local cache_key = key..":"..message
|
|
|
|
|
|
|
|
if not cache:get(cache_key) then
|
|
|
|
-- lua ecosystem is a disaster and it was not possible to find a good
|
|
|
|
-- easily multiplatform integrable code for this
|
|
|
|
--
|
2017-05-27 19:19:48 +02:00
|
|
|
-- this is really dirty and probably leak the key and the message in the process list
|
|
|
|
-- but if someone got there I guess we really have other problems so this is acceptable
|
2017-05-22 23:01:18 +02:00
|
|
|
-- and also this is way better than the previous situation
|
2022-11-06 19:37:45 +01:00
|
|
|
local pipe = io.popen("echo -n '" ..message:gsub("'", "'\\''").. "' | openssl dgst -sha512 -hmac '" ..key:gsub("'", "'\\''").. "'")
|
2017-05-22 23:01:18 +02:00
|
|
|
|
|
|
|
-- openssl returns something like this:
|
|
|
|
-- root@yunohost:~# echo -n "qsd" | openssl sha512 -hmac "key"
|
2022-11-06 19:37:45 +01:00
|
|
|
-- SHA2-512(stdin)= f1c2b1658fe64c5a3d16459f2f4eea213e4181905c190235b060ab2a4e7d6a41c15ea2c246828537a1e32ae524b7a7ed309e6d296089194c3e3e3efb98c1fbe3
|
2017-05-22 23:01:18 +02:00
|
|
|
--
|
2022-11-06 19:37:45 +01:00
|
|
|
-- so we need to remove the "SHA2-512(stdin)= " at the beginning ("(stdin)= " on older openssl version)
|
|
|
|
local line = pipe:read()
|
|
|
|
local hash = line:sub(line:find("=") + 2)
|
2017-05-22 23:01:18 +02:00
|
|
|
pipe:close()
|
|
|
|
|
|
|
|
cache:set(cache_key, hash, conf["session_timeout"])
|
|
|
|
return hash
|
|
|
|
else
|
|
|
|
return cache:get(cache_key)
|
|
|
|
end
|
2017-05-18 08:34:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-02-01 15:04:36 +01:00
|
|
|
-- Convert a table of arguments to an URI string
|
2016-04-30 12:40:59 +02:00
|
|
|
function uri_args_string(args)
|
2015-02-01 15:04:36 +01:00
|
|
|
if not args then
|
|
|
|
args = ngx.req.get_uri_args()
|
|
|
|
end
|
|
|
|
String = "?"
|
|
|
|
for k,v in pairs(args) do
|
|
|
|
String = String..tostring(k).."="..tostring(v).."&"
|
|
|
|
end
|
|
|
|
return string.sub(String, 1, string.len(String) - 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-05-16 09:42:26 +02:00
|
|
|
-- Set the Cross-Domain-Authentication key for a specific user
|
2016-04-30 12:40:59 +02:00
|
|
|
function set_cda_key()
|
2015-05-16 09:42:26 +02:00
|
|
|
local cda_key = random_string()
|
2017-05-17 10:44:26 +02:00
|
|
|
cache:set("CDA|"..cda_key, authUser, 10)
|
2015-05-16 09:42:26 +02:00
|
|
|
return cda_key
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-02-01 15:04:36 +01:00
|
|
|
-- Compute and set the authentication cookie
|
|
|
|
--
|
|
|
|
-- Sets 3 cookies containing:
|
|
|
|
-- * The username
|
|
|
|
-- * The expiration time
|
|
|
|
-- * A hash of those information along with the client IP address and a unique
|
|
|
|
-- session key
|
|
|
|
--
|
|
|
|
-- It enables the SSO to quickly retrieve the username and the session
|
|
|
|
-- expiration time, and to prove their authenticity to avoid session hijacking.
|
|
|
|
--
|
2016-04-30 12:40:59 +02:00
|
|
|
function set_auth_cookie(user, domain)
|
2015-02-01 15:04:36 +01:00
|
|
|
local maxAge = conf["session_max_timeout"]
|
|
|
|
local expire = ngx.req.start_time() + maxAge
|
|
|
|
local session_key = cache:get("session_"..user)
|
|
|
|
if not session_key then
|
2015-04-30 15:08:08 +02:00
|
|
|
session_key = random_string()
|
2015-02-01 15:04:36 +01:00
|
|
|
cache:add("session_"..user, session_key, conf["session_max_timeout"])
|
|
|
|
end
|
2017-05-18 08:34:36 +02:00
|
|
|
local hash = hmac_sha512(srvkey,
|
2017-05-17 21:48:19 +02:00
|
|
|
user..
|
2015-02-01 15:04:36 +01:00
|
|
|
"|"..expire..
|
|
|
|
"|"..session_key)
|
|
|
|
local cookie_str = "; Domain=."..domain..
|
|
|
|
"; Path=/"..
|
2021-04-08 11:11:46 +02:00
|
|
|
"; Expires="..ngx.cookie_time(expire)..
|
2018-08-25 02:29:26 +02:00
|
|
|
"; Secure"..
|
|
|
|
"; HttpOnly"..
|
2020-04-30 15:45:41 +02:00
|
|
|
"; SameSite=Lax"
|
2017-08-10 16:31:00 +02:00
|
|
|
|
2015-05-21 16:11:33 +02:00
|
|
|
ngx.header["Set-Cookie"] = {
|
|
|
|
"SSOwAuthUser="..user..cookie_str,
|
|
|
|
"SSOwAuthHash="..hash..cookie_str,
|
|
|
|
"SSOwAuthExpire="..expire..cookie_str
|
|
|
|
}
|
2019-09-24 17:33:19 +02:00
|
|
|
logger.info("Hash "..hash.." generated for "..user.."@"..ngx.var.remote_addr)
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Expires the 3 session cookies
|
2016-04-30 12:40:59 +02:00
|
|
|
function delete_cookie()
|
2015-02-01 15:04:36 +01:00
|
|
|
for _, domain in ipairs(conf["domains"]) do
|
|
|
|
local cookie_str = "; Domain=."..domain..
|
|
|
|
"; Path=/"..
|
2021-04-08 11:11:46 +02:00
|
|
|
"; Expires="..ngx.cookie_time(0)..
|
2018-08-25 02:29:26 +02:00
|
|
|
"; Secure"..
|
|
|
|
"; HttpOnly"..
|
2020-04-30 15:45:41 +02:00
|
|
|
"; SameSite=Lax"
|
2015-05-21 16:11:33 +02:00
|
|
|
ngx.header["Set-Cookie"] = {
|
|
|
|
"SSOwAuthUser="..cookie_str,
|
|
|
|
"SSOwAuthHash="..cookie_str,
|
|
|
|
"SSOwAuthExpire="..cookie_str
|
|
|
|
}
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Validate authentification
|
|
|
|
--
|
|
|
|
-- Check if the session cookies are set, and rehash server + client information
|
|
|
|
-- to match the session hash.
|
|
|
|
--
|
2020-04-01 00:43:59 +02:00
|
|
|
function refresh_logged_in()
|
2015-05-16 21:03:06 +02:00
|
|
|
local expireTime = ngx.var.cookie_SSOwAuthExpire
|
|
|
|
local user = ngx.var.cookie_SSOwAuthUser
|
|
|
|
local authHash = ngx.var.cookie_SSOwAuthHash
|
|
|
|
|
2020-03-31 02:20:40 +02:00
|
|
|
authUser = nil
|
2020-12-24 10:20:29 +01:00
|
|
|
is_logged_in = false
|
2020-09-20 18:00:49 +02:00
|
|
|
|
2015-05-16 21:03:06 +02:00
|
|
|
if expireTime and expireTime ~= ""
|
|
|
|
and authHash and authHash ~= ""
|
|
|
|
and user and user ~= ""
|
2015-02-01 15:04:36 +01:00
|
|
|
then
|
|
|
|
-- Check expire time
|
2015-05-16 21:03:06 +02:00
|
|
|
if (ngx.req.start_time() <= tonumber(expireTime)) then
|
2015-02-01 15:04:36 +01:00
|
|
|
-- Check hash
|
2015-05-16 21:03:06 +02:00
|
|
|
local session_key = cache:get("session_"..user)
|
2015-02-01 15:04:36 +01:00
|
|
|
if session_key and session_key ~= "" then
|
|
|
|
-- Check cache
|
2015-05-16 21:03:06 +02:00
|
|
|
if cache:get(user.."-password") then
|
2017-05-18 08:34:36 +02:00
|
|
|
local hash = hmac_sha512(srvkey,
|
2020-05-21 22:57:57 +02:00
|
|
|
user..
|
2015-05-16 21:03:06 +02:00
|
|
|
"|"..expireTime..
|
2015-02-01 15:04:36 +01:00
|
|
|
"|"..session_key)
|
2020-06-18 14:48:14 +02:00
|
|
|
is_logged_in = hash == authHash
|
2021-08-14 21:23:53 +02:00
|
|
|
if is_logged_in then
|
2020-05-21 22:57:57 +02:00
|
|
|
authUser = user
|
2021-08-14 21:23:53 +02:00
|
|
|
return true
|
|
|
|
else
|
|
|
|
failReason = "Hash not matching"
|
2017-05-12 13:54:39 +02:00
|
|
|
end
|
2021-08-14 21:23:53 +02:00
|
|
|
else
|
|
|
|
failReason = "No {user}-password entry in cache"
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
2021-08-14 21:23:53 +02:00
|
|
|
else
|
|
|
|
failReason = "No session key"
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
2021-08-14 21:23:53 +02:00
|
|
|
else
|
|
|
|
failReason = "Cookie expired"
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
2021-08-14 21:23:53 +02:00
|
|
|
logger.debug("SSOwat cookies rejected for "..user.."@"..ngx.var.remote_addr.." : "..failReason)
|
|
|
|
return false
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
|
2020-12-24 17:47:55 +01:00
|
|
|
-- If client set the `Proxy-Authorization` header before reaching the SSO,
|
|
|
|
-- we want to match user and password against the user database.
|
|
|
|
--
|
|
|
|
-- It allows to bypass the cookie-based procedure with a per-request
|
|
|
|
-- authentication. This is useful to authenticate on the SSO during
|
|
|
|
-- curl requests for example.
|
2020-09-20 17:53:18 +02:00
|
|
|
|
2020-12-23 18:13:34 +01:00
|
|
|
local auth_header = ngx.req.get_headers()["Proxy-Authorization"]
|
2020-09-20 17:53:18 +02:00
|
|
|
|
|
|
|
if auth_header then
|
|
|
|
_, _, b64_cred = string.find(auth_header, "^Basic%s+(.+)$")
|
2020-12-24 17:47:55 +01:00
|
|
|
if b64_cred == nil then
|
|
|
|
return is_logged_in
|
|
|
|
end
|
|
|
|
_, _, user, password = string.find(ngx.decode_base64(b64_cred), "^(.+):(.+)$")
|
|
|
|
user = authenticate(user, password)
|
|
|
|
if user then
|
|
|
|
logger.debug("User got authenticated through basic auth")
|
|
|
|
authUser = user
|
|
|
|
is_logged_in = true
|
|
|
|
else
|
|
|
|
-- https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/407
|
|
|
|
ngx.status = 407
|
2020-09-20 17:53:18 +02:00
|
|
|
end
|
|
|
|
end
|
2020-12-24 17:47:55 +01:00
|
|
|
|
|
|
|
return is_logged_in
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
|
2020-03-04 11:34:24 +01:00
|
|
|
-- Check whether a user is allowed to access a URL using the `permissions` directive
|
2015-02-01 15:04:36 +01:00
|
|
|
-- of the configuration file
|
2020-04-01 00:43:59 +02:00
|
|
|
function has_access(permission, user)
|
2015-05-16 21:03:06 +02:00
|
|
|
user = user or authUser
|
2015-02-01 15:04:36 +01:00
|
|
|
|
2020-04-01 00:43:59 +02:00
|
|
|
if permission == nil then
|
2020-09-20 18:00:49 +02:00
|
|
|
logger.debug("No permission matching request for "..ngx.var.uri)
|
2020-01-29 12:24:25 +01:00
|
|
|
return false
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
|
2020-04-01 00:43:59 +02:00
|
|
|
-- Public access
|
|
|
|
if user == nil or permission["public"] then
|
2020-05-21 22:57:05 +02:00
|
|
|
user = user or "A visitor"
|
2020-09-20 18:00:49 +02:00
|
|
|
logger.debug(user.." tries to access "..ngx.var.uri.." (corresponding perm: "..permission["id"]..")")
|
2020-04-01 00:43:59 +02:00
|
|
|
return permission["public"]
|
|
|
|
end
|
|
|
|
|
2020-09-20 18:00:49 +02:00
|
|
|
logger.debug("User "..user.." tries to access "..ngx.var.uri.." (corresponding perm: "..permission["id"]..")")
|
2020-04-01 00:43:59 +02:00
|
|
|
|
2020-09-21 14:41:23 +02:00
|
|
|
-- The user has permission to access the content if he is in the list of allowed users
|
|
|
|
if element_is_in_table(user, permission["users"]) then
|
|
|
|
logger.debug("User "..user.." can access "..ngx.var.host..ngx.var.uri..uri_args_string())
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
logger.debug("User "..user.." cannot access "..ngx.var.uri)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function element_is_in_table(element, table)
|
|
|
|
if table then
|
|
|
|
for _, el in pairs(table) do
|
|
|
|
if el == element then
|
2020-01-20 16:59:25 +01:00
|
|
|
return true
|
|
|
|
end
|
2017-05-12 13:54:39 +02:00
|
|
|
end
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
2019-10-03 23:11:52 +02:00
|
|
|
|
2020-01-20 16:59:25 +01:00
|
|
|
return false
|
2019-10-03 23:11:52 +02:00
|
|
|
end
|
|
|
|
|
2015-02-01 15:04:36 +01:00
|
|
|
-- Authenticate a user against the LDAP database using a username or an email
|
|
|
|
-- address.
|
|
|
|
-- Reminder: conf["ldap_identifier"] is "uid" by default
|
2016-04-30 12:40:59 +02:00
|
|
|
function authenticate(user, password)
|
2015-02-01 15:04:36 +01:00
|
|
|
-- Try to find the username from an email address by openning an anonymous
|
|
|
|
-- LDAP connection and check if the email address exists
|
|
|
|
if conf["allow_mail_authentication"] and string.find(user, "@") then
|
|
|
|
ldap = lualdap.open_simple(conf["ldap_host"])
|
|
|
|
for dn, attribs in ldap:search {
|
|
|
|
base = conf["ldap_group"],
|
|
|
|
scope = "onelevel",
|
|
|
|
sizelimit = 1,
|
|
|
|
filter = "(mail="..user..")",
|
|
|
|
attrs = {conf["ldap_identifier"]}
|
|
|
|
} do
|
|
|
|
if attribs[conf["ldap_identifier"]] then
|
2019-10-03 20:42:01 +02:00
|
|
|
logger.debug("Use email: "..user)
|
2015-02-01 15:04:36 +01:00
|
|
|
user = attribs[conf["ldap_identifier"]]
|
|
|
|
else
|
2019-10-03 20:42:01 +02:00
|
|
|
logger.error("Unknown email: "..user)
|
2015-02-01 15:04:36 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ldap:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Now that we have a username, we can try connecting to the LDAP base.
|
|
|
|
connected = lualdap.open_simple (
|
|
|
|
conf["ldap_host"],
|
|
|
|
conf["ldap_identifier"].."=".. user ..","..conf["ldap_group"],
|
|
|
|
password
|
|
|
|
)
|
|
|
|
|
|
|
|
cache:flush_expired()
|
|
|
|
|
|
|
|
-- If we are connected, we can retrieve the password and put it in the
|
|
|
|
-- cache shared table in order to eventually reuse it later when updating
|
|
|
|
-- profile information or just passing credentials to an application.
|
|
|
|
if connected then
|
2017-09-16 19:22:47 +02:00
|
|
|
if conf['ldap_enforce_crypt'] then
|
|
|
|
ensure_user_password_uses_strong_hash(connected, user, password)
|
|
|
|
end
|
2015-02-01 15:04:36 +01:00
|
|
|
cache:add(user.."-password", password, conf["session_timeout"])
|
2020-10-31 13:53:19 +01:00
|
|
|
ngx.log(ngx.NOTICE, "Connected as: "..user)
|
2022-11-06 19:37:45 +01:00
|
|
|
logger.info("User "..user.." successfully authenticated from "..ngx.var.remote_addr)
|
2015-02-01 15:04:36 +01:00
|
|
|
return user
|
|
|
|
|
|
|
|
-- Else, the username/email or the password is wrong
|
|
|
|
else
|
2020-10-31 13:53:19 +01:00
|
|
|
-- N.B. : the ngx.log is important and is related to the regex used by
|
|
|
|
-- the fail2ban rule to detect (and ban) failed login attempts
|
|
|
|
ngx.log(ngx.ERR, "Connection failed for: "..user)
|
2019-10-03 20:42:01 +02:00
|
|
|
logger.error("Authentication failure for user "..user.." from "..ngx.var.remote_addr)
|
2015-02-01 15:04:36 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-15 15:49:53 +01:00
|
|
|
function delete_user_info_cache(user)
|
|
|
|
cache:delete(user.."-"..conf["ldap_identifier"])
|
|
|
|
local i = 2
|
|
|
|
while cache:get(user.."-mail|"..i) do
|
|
|
|
cache:delete(user.."-mail|"..i)
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
local i = 2
|
|
|
|
while cache:get(user.."-maildrop|"..i) do
|
|
|
|
cache:delete(user.."-maildrop|"..i)
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
end
|
2015-02-01 15:04:36 +01:00
|
|
|
|
|
|
|
-- Set the authentication headers in order to pass credentials to the
|
|
|
|
-- application underneath.
|
2016-04-30 12:40:59 +02:00
|
|
|
function set_headers(user)
|
2020-12-17 17:06:19 +01:00
|
|
|
local user = user or authUser
|
2020-12-23 18:39:54 +01:00
|
|
|
-- Set `Authorization` header to enable HTTP authentification
|
|
|
|
ngx.req.set_header("Authorization", "Basic "..ngx.encode_base64(
|
2020-12-17 17:06:19 +01:00
|
|
|
user..":"..cache:get(user.."-password")
|
|
|
|
))
|
|
|
|
|
|
|
|
-- Set optionnal additional headers (typically to pass email address)
|
|
|
|
for k, v in pairs(conf["additional_headers"]) do
|
|
|
|
ngx.req.set_header(k, cache:get(user.."-"..v))
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2015-02-01 15:04:36 +01:00
|
|
|
|
2020-12-17 17:06:19 +01:00
|
|
|
|
|
|
|
function refresh_user_cache(user)
|
2016-11-15 15:49:53 +01:00
|
|
|
-- We definitely don't want to pass credentials on a non-encrypted
|
2015-02-01 15:04:36 +01:00
|
|
|
-- connection.
|
|
|
|
if ngx.var.scheme ~= "https" then
|
|
|
|
return redirect("https://"..ngx.var.host..ngx.var.uri..uri_args_string())
|
|
|
|
end
|
|
|
|
|
2015-05-16 21:03:06 +02:00
|
|
|
local user = user or authUser
|
2015-02-01 15:04:36 +01:00
|
|
|
|
2016-11-15 15:49:53 +01:00
|
|
|
-- If the password is not in cache or if the cache has expired, ask for
|
2015-02-01 15:04:36 +01:00
|
|
|
-- logging.
|
|
|
|
if not cache:get(user.."-password") then
|
|
|
|
flash("info", t("please_login"))
|
|
|
|
local back_url = ngx.var.scheme .. "://" .. ngx.var.host .. ngx.var.uri .. uri_args_string()
|
2015-02-12 12:08:52 +01:00
|
|
|
return redirect(conf.portal_url.."?r="..ngx.encode_base64(back_url))
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- If the user information is not in cache, open an LDAP connection and
|
|
|
|
-- fetch it.
|
|
|
|
if not cache:get(user.."-"..conf["ldap_identifier"]) then
|
2016-01-12 11:48:07 +01:00
|
|
|
ldap = lualdap.open_simple(
|
2017-05-23 07:26:41 +02:00
|
|
|
conf["ldap_host"],
|
|
|
|
conf["ldap_identifier"].."=".. user ..","..conf["ldap_group"],
|
|
|
|
cache:get(user.."-password")
|
|
|
|
)
|
2019-01-17 22:54:25 +01:00
|
|
|
-- If the ldap connection fail (because the password was changed).
|
|
|
|
-- Logout the user and invalid the password
|
|
|
|
if not ldap then
|
2019-10-03 20:42:01 +02:00
|
|
|
logger.debug("LDAP connection failed. Disconnect user : ".. user)
|
2019-01-17 22:54:25 +01:00
|
|
|
cache:delete(authUser.."-password")
|
|
|
|
flash("info", t("please_login"))
|
|
|
|
local back_url = ngx.var.scheme .. "://" .. ngx.var.host .. ngx.var.uri .. uri_args_string()
|
|
|
|
return redirect(conf.portal_url.."?r="..ngx.encode_base64(back_url))
|
|
|
|
end
|
2019-10-03 20:42:01 +02:00
|
|
|
logger.debug("Reloading LDAP values for: "..user)
|
2015-02-01 15:04:36 +01:00
|
|
|
for dn, attribs in ldap:search {
|
|
|
|
base = conf["ldap_identifier"].."=".. user ..","..conf["ldap_group"],
|
|
|
|
scope = "base",
|
|
|
|
sizelimit = 1,
|
|
|
|
attrs = conf["ldap_attributes"]
|
|
|
|
} do
|
|
|
|
for k,v in pairs(attribs) do
|
|
|
|
if type(v) == "table" then
|
|
|
|
for k2,v2 in ipairs(v) do
|
|
|
|
if k2 == 1 then cache:set(user.."-"..k, v2, conf["session_timeout"]) end
|
|
|
|
cache:set(user.."-"..k.."|"..k2, v2, conf["session_max_timeout"])
|
|
|
|
end
|
|
|
|
else
|
|
|
|
cache:set(user.."-"..k, v, conf["session_timeout"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- Else, just revalidate session for another day by default
|
|
|
|
password = cache:get(user.."-password")
|
2021-07-02 19:51:02 +02:00
|
|
|
-- Here we don't use set method to avoid strange logout
|
|
|
|
-- See https://github.com/YunoHost/issues/issues/1830
|
2021-07-02 19:49:17 +02:00
|
|
|
cache:replace(user.."-password", password, conf["session_timeout"])
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Summarize email, aliases and forwards in a table for a specific user
|
|
|
|
function get_mails(user)
|
|
|
|
local mails = { mail = "", mailalias = {}, maildrop = {} }
|
|
|
|
|
|
|
|
-- default mail
|
|
|
|
mails["mail"] = cache:get(user.."-mail")
|
|
|
|
|
|
|
|
-- mail aliases
|
|
|
|
if cache:get(user.."-mail|2") then
|
|
|
|
local i = 2
|
|
|
|
while cache:get(user.."-mail|"..i) do
|
|
|
|
table.insert(mails["mailalias"], cache:get(user.."-mail|"..i))
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- mail forward
|
|
|
|
if cache:get(user.."-maildrop|2") then
|
|
|
|
local i = 2
|
|
|
|
while cache:get(user.."-maildrop|"..i) do
|
|
|
|
table.insert(mails["maildrop"], cache:get(user.."-maildrop|"..i))
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return mails
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Yo dawg, this enables SSOwat to serve files in HTTP in an HTTP server
|
|
|
|
-- Much reliable, very solid.
|
|
|
|
--
|
|
|
|
-- Takes an URI, and returns file content with the proper HTTP headers.
|
|
|
|
-- It is used to render the SSOwat portal *only*.
|
2019-03-19 16:52:43 +01:00
|
|
|
function serve(uri, cache)
|
2019-10-03 20:42:01 +02:00
|
|
|
|
2020-09-20 18:00:49 +02:00
|
|
|
logger.debug("Serving portal uri "..uri)
|
2019-10-03 20:42:01 +02:00
|
|
|
|
2015-02-01 15:04:36 +01:00
|
|
|
rel_path = string.gsub(uri, conf["portal_path"], "/")
|
|
|
|
|
|
|
|
-- Load login.html as index
|
|
|
|
if rel_path == "/" then
|
2020-04-01 00:43:59 +02:00
|
|
|
if is_logged_in then
|
2019-03-19 23:29:46 +01:00
|
|
|
rel_path = "/portal.html"
|
2015-02-01 15:04:36 +01:00
|
|
|
else
|
|
|
|
rel_path = "/login.html"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Access to directory root: forbidden
|
|
|
|
if string.ends(rel_path, "/") then
|
|
|
|
return ngx.exit(ngx.HTTP_FORBIDDEN)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Try to get file content
|
|
|
|
local content = read_file(script_path.."portal"..rel_path)
|
|
|
|
if not content then
|
|
|
|
return ngx.exit(ngx.HTTP_NOT_FOUND)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Extract file extension
|
|
|
|
_, file, ext = string.match(rel_path, "(.-)([^\\/]-%.?([^%.\\/]*))$")
|
|
|
|
|
|
|
|
-- Associate to MIME type
|
|
|
|
mime_types = {
|
|
|
|
html = "text/html",
|
|
|
|
ms = "text/html",
|
|
|
|
js = "text/javascript",
|
|
|
|
map = "text/javascript",
|
|
|
|
css = "text/css",
|
|
|
|
gif = "image/gif",
|
|
|
|
jpg = "image/jpeg",
|
|
|
|
png = "image/png",
|
|
|
|
svg = "image/svg+xml",
|
|
|
|
ico = "image/vnd.microsoft.icon",
|
2021-06-29 18:34:40 +02:00
|
|
|
woff = "font/woff",
|
|
|
|
woff2 = "font/woff2",
|
|
|
|
ttf = "font/ttf",
|
2015-02-01 15:04:36 +01:00
|
|
|
json = "application/json"
|
|
|
|
}
|
|
|
|
|
2021-06-29 18:34:40 +02:00
|
|
|
-- Allow .ms to specify mime type
|
|
|
|
mime = ext
|
|
|
|
if ext == "ms" then
|
|
|
|
subext = string.match(file, "^.+%.(.+)%.ms$")
|
|
|
|
if subext then
|
|
|
|
mime = subext
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-01 15:04:36 +01:00
|
|
|
-- Set Content-Type
|
2021-06-29 18:34:40 +02:00
|
|
|
if mime_types[mime] then
|
|
|
|
ngx.header["Content-Type"] = mime_types[mime]
|
2015-02-01 15:04:36 +01:00
|
|
|
else
|
|
|
|
ngx.header["Content-Type"] = "text/plain"
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Render as mustache
|
|
|
|
if ext == "html" then
|
|
|
|
local data = get_data_for(file)
|
2018-06-07 11:56:34 +02:00
|
|
|
local rendered = lustache:render(read_file(script_path.."portal/header.ms"), data)
|
|
|
|
rendered = rendered..lustache:render(content, data)
|
|
|
|
content = rendered..lustache:render(read_file(script_path.."portal/footer.ms"), data)
|
2015-02-01 15:04:36 +01:00
|
|
|
elseif ext == "ms" then
|
|
|
|
local data = get_data_for(file)
|
2018-06-07 11:56:34 +02:00
|
|
|
content = lustache:render(content, data)
|
2021-06-29 18:34:40 +02:00
|
|
|
elseif uri == "/ynh_userinfo.json" then
|
2015-02-01 15:04:36 +01:00
|
|
|
local data = get_data_for(file)
|
|
|
|
content = json.encode(data)
|
2021-06-29 18:34:40 +02:00
|
|
|
cache = "dynamic"
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Reset flash messages
|
|
|
|
flashs["fail"] = nil
|
|
|
|
flashs["win"] = nil
|
|
|
|
flashs["info"] = nil
|
|
|
|
|
2019-03-19 16:52:43 +01:00
|
|
|
if cache == "static_asset" then
|
|
|
|
ngx.header["Cache-Control"] = "public, max-age=3600"
|
|
|
|
else
|
|
|
|
-- Ain't nobody got time for cache
|
|
|
|
ngx.header["Cache-Control"] = "no-cache"
|
|
|
|
end
|
2015-02-01 15:04:36 +01:00
|
|
|
|
|
|
|
-- Print file content
|
|
|
|
ngx.say(content)
|
|
|
|
|
|
|
|
-- Return 200 :-)
|
|
|
|
return ngx.exit(ngx.HTTP_OK)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Simple controller that computes a data table to populate a specific view.
|
|
|
|
-- The resulting data table typically contains the user information, the page
|
|
|
|
-- title, the flash notifications' content and the translated strings.
|
|
|
|
function get_data_for(view)
|
2015-05-16 21:03:06 +02:00
|
|
|
local user = authUser
|
2015-02-01 15:04:36 +01:00
|
|
|
|
|
|
|
-- For the login page we only need the page title
|
|
|
|
if view == "login.html" then
|
|
|
|
data = {
|
|
|
|
title = t("login"),
|
|
|
|
connected = false
|
|
|
|
}
|
|
|
|
|
|
|
|
-- For those views, we may need user information
|
2019-03-19 23:29:46 +01:00
|
|
|
elseif view == "portal.html"
|
2015-02-01 15:04:36 +01:00
|
|
|
or view == "edit.html"
|
|
|
|
or view == "password.html"
|
2021-06-29 18:34:40 +02:00
|
|
|
or view == "ynh_userinfo.json" then
|
2016-11-15 15:49:53 +01:00
|
|
|
|
|
|
|
-- Invalidate cache before loading these views.
|
|
|
|
-- Needed if the LDAP db is changed outside ssowat (from the cli for example).
|
|
|
|
-- Not doing it for ynhpanel.json only for performance reasons,
|
|
|
|
-- so the panel could show wrong first name, last name or main email address
|
2020-04-01 00:43:59 +02:00
|
|
|
-- TODO: What if we remove info during logout?
|
|
|
|
--if view ~= "ynhpanel.json" then
|
|
|
|
-- delete_user_info_cache(user)
|
|
|
|
--end
|
2016-11-15 15:49:53 +01:00
|
|
|
|
|
|
|
-- Be sure cache is loaded
|
2020-04-01 00:43:59 +02:00
|
|
|
if user then
|
2020-12-17 17:06:19 +01:00
|
|
|
refresh_user_cache(user)
|
2020-04-01 00:43:59 +02:00
|
|
|
|
|
|
|
local mails = get_mails(user)
|
|
|
|
data = {
|
|
|
|
connected = true,
|
|
|
|
theme = conf.theme,
|
|
|
|
portal_url = conf.portal_url,
|
|
|
|
uid = user,
|
|
|
|
cn = cache:get(user.."-cn"),
|
|
|
|
sn = cache:get(user.."-sn"),
|
|
|
|
givenName = cache:get(user.."-givenName"),
|
|
|
|
mail = mails["mail"],
|
|
|
|
mailalias = mails["mailalias"],
|
|
|
|
maildrop = mails["maildrop"],
|
|
|
|
app = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
local sorted_apps = {}
|
|
|
|
|
|
|
|
-- Add user's accessible URLs using the ACLs.
|
|
|
|
-- It is typically used to build the app list.
|
|
|
|
for permission_name, permission in pairs(conf["permissions"]) do
|
|
|
|
-- We want to display a tile, and uris is not empty
|
2020-09-21 14:41:23 +02:00
|
|
|
if permission['show_tile'] and next(permission['uris']) ~= nil and element_is_in_table(user, permission["users"]) then
|
2020-04-01 00:43:59 +02:00
|
|
|
url = permission['uris'][1]
|
|
|
|
name = permission['label']
|
|
|
|
|
|
|
|
if ngx.var.host == conf["local_portal_domain"] then
|
|
|
|
url = string.gsub(url, conf["original_portal_domain"], conf["local_portal_domain"])
|
|
|
|
end
|
2020-03-31 02:20:40 +02:00
|
|
|
|
2020-04-01 00:43:59 +02:00
|
|
|
table.insert(sorted_apps, name)
|
|
|
|
table.sort(sorted_apps)
|
|
|
|
table.insert(data["app"], index_of(sorted_apps, name), { url = url, name = name })
|
2020-03-31 02:20:40 +02:00
|
|
|
end
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-15 13:03:01 +01:00
|
|
|
-- Pass all the translated strings to the view (to use with t_<key>)
|
2017-08-10 16:31:00 +02:00
|
|
|
for k, v in pairs(i18n[conf["default_language"]]) do
|
2017-08-19 04:39:51 +02:00
|
|
|
data["t_"..k] = (i18n[conf.lang] and i18n[conf.lang][k]) or v
|
2015-02-15 13:03:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Pass flash notification content
|
|
|
|
data['flash_fail'] = {flashs["fail"]}
|
|
|
|
data['flash_win'] = {flashs["win"] }
|
|
|
|
data['flash_info'] = {flashs["info"]}
|
2019-02-21 18:27:28 +01:00
|
|
|
data['theme'] = conf["theme"]
|
2015-02-15 13:03:01 +01:00
|
|
|
|
2015-02-01 15:04:36 +01:00
|
|
|
return data
|
|
|
|
end
|
|
|
|
|
2017-08-15 11:41:24 +02:00
|
|
|
-- this function is launched after a successful login
|
|
|
|
-- it checked if the user password is stored using the most secure hashing
|
|
|
|
-- algorithm available
|
|
|
|
-- if it's not the case, it migrates the password to this new hash algorithm
|
|
|
|
function ensure_user_password_uses_strong_hash(ldap, user, password)
|
|
|
|
local current_hashed_password = nil
|
|
|
|
|
|
|
|
for dn, attrs in ldap:search {
|
2017-09-16 19:22:47 +02:00
|
|
|
base = conf['ldap_group'],
|
2017-08-15 11:41:24 +02:00
|
|
|
scope = "onelevel",
|
|
|
|
sizelimit = 1,
|
2017-09-16 19:22:47 +02:00
|
|
|
filter = "("..conf['ldap_identifier'].."="..user..")",
|
2017-08-15 11:41:24 +02:00
|
|
|
attrs = {"userPassword"}
|
|
|
|
} do
|
|
|
|
current_hashed_password = attrs["userPassword"]:sub(0, 10)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- if the password is not hashed using sha-512, which is the strongest
|
|
|
|
-- available hash rehash it using that
|
|
|
|
-- Here "{CRYPT}" means "uses linux auth system"
|
|
|
|
-- "6" means "uses sha-512", any lower number mean a less strong algo (1 == md5)
|
|
|
|
if current_hashed_password:sub(0, 10) ~= "{CRYPT}$6$" then
|
|
|
|
local dn = conf["ldap_identifier"].."="..user..","..conf["ldap_group"]
|
|
|
|
local hashed_password = hash_password(password)
|
|
|
|
ldap:modify(dn, {'=', userPassword = hashed_password })
|
|
|
|
end
|
|
|
|
end
|
2015-02-01 15:04:36 +01:00
|
|
|
|
2018-08-29 00:07:48 +02:00
|
|
|
-- Read result of a command after given it securely the password
|
2018-08-29 00:47:59 +02:00
|
|
|
function secure_cmd_password(cmd, password, start)
|
2018-08-29 00:07:48 +02:00
|
|
|
-- Check password validity
|
2021-07-29 16:34:56 +02:00
|
|
|
local tmp_file = os.tmpname()
|
2018-08-29 02:55:02 +02:00
|
|
|
local w_pwd = io.popen("("..cmd..") | tee -a "..tmp_file, 'w')
|
2018-08-29 00:07:48 +02:00
|
|
|
w_pwd:write(password)
|
2018-08-29 01:08:36 +02:00
|
|
|
-- This second write is just to validate the password question
|
|
|
|
-- Do not remove
|
2018-08-29 00:07:48 +02:00
|
|
|
w_pwd:write("")
|
|
|
|
w_pwd:close()
|
|
|
|
local r_pwd = io.open(tmp_file, 'r')
|
2018-08-29 01:26:19 +02:00
|
|
|
text = r_pwd:read "*a"
|
2018-08-29 03:00:13 +02:00
|
|
|
|
|
|
|
-- Remove the extra end line
|
2018-08-29 02:55:02 +02:00
|
|
|
if text:sub(-1, -1) == "\n" then
|
|
|
|
text = text:sub(1, -2)
|
|
|
|
end
|
2018-08-29 00:07:48 +02:00
|
|
|
r_pwd:close()
|
|
|
|
os.remove(tmp_file)
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
2015-02-01 15:04:36 +01:00
|
|
|
-- Compute the user modification POST request
|
|
|
|
-- It has to update cached information and edit the LDAP user entry
|
|
|
|
-- according to the changes detected.
|
2016-04-30 12:40:59 +02:00
|
|
|
function edit_user()
|
2015-02-01 15:04:36 +01:00
|
|
|
-- We need these calls since we are in a POST request
|
|
|
|
ngx.req.read_body()
|
|
|
|
local args = ngx.req.get_post_args()
|
|
|
|
|
|
|
|
-- Ensure that user is logged in and has passed information
|
|
|
|
-- before continuing.
|
2020-04-01 00:43:59 +02:00
|
|
|
if is_logged_in and args
|
2015-02-01 15:04:36 +01:00
|
|
|
then
|
|
|
|
|
|
|
|
-- Set HTTP status to 201
|
|
|
|
ngx.status = ngx.HTTP_CREATED
|
2015-05-16 21:03:06 +02:00
|
|
|
local user = authUser
|
2015-02-01 15:04:36 +01:00
|
|
|
|
|
|
|
-- In case of a password modification
|
|
|
|
-- TODO: split this into a new function
|
|
|
|
if string.ends(ngx.var.uri, "password.html") then
|
|
|
|
|
|
|
|
-- Check current password against the cached one
|
|
|
|
if args.currentpassword
|
|
|
|
and args.currentpassword == cache:get(user.."-password")
|
|
|
|
then
|
|
|
|
-- and the new password against the confirmation field's content
|
|
|
|
if args.newpassword == args.confirm then
|
2018-08-28 21:33:19 +02:00
|
|
|
-- Check password validity
|
2022-12-06 15:59:32 +01:00
|
|
|
local result_msg = secure_cmd_password("python3 /usr/lib/python3/dist-packages/yunohost/utils/password.py", args.newpassword)
|
2018-08-29 01:26:19 +02:00
|
|
|
validation_error = true
|
2018-10-31 19:55:07 +01:00
|
|
|
if result_msg == nil or result_msg == "" then
|
2018-08-29 01:26:19 +02:00
|
|
|
validation_error = nil
|
2018-08-29 00:07:48 +02:00
|
|
|
end
|
2018-08-28 21:33:19 +02:00
|
|
|
if validation_error == nil then
|
|
|
|
|
|
|
|
local dn = conf["ldap_identifier"].."="..user..","..conf["ldap_group"]
|
|
|
|
|
|
|
|
-- Open the LDAP connection
|
|
|
|
local ldap = lualdap.open_simple(conf["ldap_host"], dn, args.currentpassword)
|
2019-02-01 21:42:41 +01:00
|
|
|
|
2018-08-28 21:33:19 +02:00
|
|
|
local password = hash_password(args.newpassword)
|
|
|
|
|
|
|
|
-- Modify the LDAP information
|
|
|
|
if ldap:modify(dn, {'=', userPassword = password }) then
|
|
|
|
if validation == nil then
|
|
|
|
flash("win", t("password_changed"))
|
|
|
|
else
|
2018-08-29 00:07:48 +02:00
|
|
|
flash("win", t(result_msg))
|
2018-08-28 21:33:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Reset the password cache
|
|
|
|
cache:set(user.."-password", args.newpassword, conf["session_timeout"])
|
2019-03-19 23:29:46 +01:00
|
|
|
return redirect(conf.portal_url.."portal.html")
|
2018-08-28 21:33:19 +02:00
|
|
|
else
|
|
|
|
flash("fail", t("password_changed_error"))
|
|
|
|
end
|
2015-02-01 15:04:36 +01:00
|
|
|
else
|
2018-08-29 00:07:48 +02:00
|
|
|
flash("fail", t(result_msg))
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
flash("fail", t("password_not_match"))
|
|
|
|
end
|
|
|
|
else
|
|
|
|
flash("fail", t("wrong_current_password"))
|
|
|
|
end
|
2015-02-12 12:08:52 +01:00
|
|
|
return redirect(conf.portal_url.."password.html")
|
|
|
|
|
2015-02-01 15:04:36 +01:00
|
|
|
|
|
|
|
-- In case of profile modification
|
|
|
|
-- TODO: split this into a new function
|
|
|
|
elseif string.ends(ngx.var.uri, "edit.html") then
|
|
|
|
|
|
|
|
-- Check that needed arguments exist
|
|
|
|
if args.givenName and args.sn and args.mail then
|
|
|
|
|
|
|
|
-- Unstack mailaliases
|
|
|
|
local mailalias = {}
|
|
|
|
if args["mailalias[]"] then
|
|
|
|
if type(args["mailalias[]"]) == "string" then
|
|
|
|
args["mailalias[]"] = {args["mailalias[]"]}
|
|
|
|
end
|
|
|
|
mailalias = args["mailalias[]"]
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Unstack mail forwards
|
|
|
|
local maildrop = {}
|
|
|
|
if args["maildrop[]"] then
|
|
|
|
if type(args["maildrop[]"]) == "string" then
|
|
|
|
args["maildrop[]"] = {args["maildrop[]"]}
|
|
|
|
end
|
|
|
|
maildrop = args["maildrop[]"]
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Limit domains per user:
|
|
|
|
-- This ensures that a user already has an email address or an
|
|
|
|
-- aliases that ends with a specific domain to claim new aliases
|
|
|
|
-- on this domain.
|
|
|
|
--
|
|
|
|
-- I.E. You need to have xxx@domain.org to claim a
|
|
|
|
-- yyy@domain.org alias.
|
|
|
|
--
|
|
|
|
local domains = {}
|
|
|
|
local ldap = lualdap.open_simple(conf["ldap_host"])
|
|
|
|
for dn, attribs in ldap:search {
|
|
|
|
base = conf["ldap_group"],
|
|
|
|
scope = "onelevel",
|
|
|
|
sizelimit = 1,
|
|
|
|
filter = "(uid="..user..")",
|
|
|
|
attrs = {"mail"}
|
|
|
|
} do
|
2015-06-30 21:02:58 +02:00
|
|
|
-- Construct proper emails array
|
|
|
|
local mail_list = {}
|
|
|
|
local mail_attr = attribs["mail"]
|
|
|
|
if type(mail_attr) == "string" then
|
|
|
|
mail_list = { mail_attr }
|
|
|
|
elseif type(mail_attr) == "table" then
|
|
|
|
mail_list = mail_attr
|
|
|
|
end
|
|
|
|
|
2015-02-01 15:04:36 +01:00
|
|
|
-- Filter configuration's domain list to keep only
|
|
|
|
-- "allowed" domains
|
|
|
|
for _, domain in ipairs(conf["domains"]) do
|
2015-06-02 17:01:56 +02:00
|
|
|
for k, mail in ipairs(mail_list) do
|
2015-02-01 15:04:36 +01:00
|
|
|
if string.ends(mail, "@"..domain) then
|
|
|
|
if not is_in_table(domains, domain) then
|
|
|
|
table.insert(domains, domain)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ldap:close()
|
|
|
|
|
2015-02-15 22:31:20 +01:00
|
|
|
local rex = require "rex_pcre"
|
2015-02-16 16:31:21 +01:00
|
|
|
local rex_flags = rex.flags()
|
2015-03-31 11:29:45 +02:00
|
|
|
local mail_re = rex.new([[^[\w\.\-+%]+@([^\W_A-Z]+([\-]*[^\W_A-Z]+)*\.)+([^\W\d_]{2,})$]], rex_flags.UTF8 + rex_flags.UCP)
|
2015-02-01 15:04:36 +01:00
|
|
|
|
|
|
|
local mails = {}
|
|
|
|
|
|
|
|
-- Build an LDAP filter so that we can ensure that email
|
|
|
|
-- addresses are used only once.
|
|
|
|
local filter = "(|"
|
|
|
|
table.insert(mailalias, 1, args.mail)
|
|
|
|
|
|
|
|
-- Loop through all the aliases
|
|
|
|
for k, mail in ipairs(mailalias) do
|
|
|
|
if mail ~= "" then
|
|
|
|
-- Check the mail pattern
|
2015-02-15 22:31:20 +01:00
|
|
|
if not mail_re:match(mail) then
|
2015-02-01 15:04:36 +01:00
|
|
|
flash("fail", t("invalid_mail")..": "..mail)
|
2015-02-12 12:08:52 +01:00
|
|
|
return redirect(conf.portal_url.."edit.html")
|
2015-02-01 15:04:36 +01:00
|
|
|
|
|
|
|
-- Check that the domain is known and allowed
|
|
|
|
else
|
|
|
|
local domain_valid = false
|
|
|
|
for _, domain in ipairs(domains) do
|
|
|
|
if string.ends(mail, "@"..domain) then
|
|
|
|
domain_valid = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if domain_valid then
|
|
|
|
table.insert(mails, mail)
|
|
|
|
filter = filter.."(mail="..mail..")"
|
|
|
|
else
|
|
|
|
flash("fail", t("invalid_domain").." "..mail)
|
2015-02-12 12:08:52 +01:00
|
|
|
return redirect(conf.portal_url.."edit.html")
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- filter should look like "(|(mail=my@mail.tld)(mail=my@mail2.tld))"
|
|
|
|
filter = filter..")"
|
|
|
|
|
2015-02-12 12:08:52 +01:00
|
|
|
|
2015-02-01 15:04:36 +01:00
|
|
|
-- For email forwards, we only need to check that they look
|
|
|
|
-- like actual emails
|
|
|
|
local drops = {}
|
|
|
|
for k, mail in ipairs(maildrop) do
|
|
|
|
if mail ~= "" then
|
2015-02-15 22:31:20 +01:00
|
|
|
if not mail_re:match(mail) then
|
2015-02-01 15:04:36 +01:00
|
|
|
flash("fail", t("invalid_mailforward")..": "..mail)
|
2015-02-12 12:08:52 +01:00
|
|
|
return redirect(conf.portal_url.."edit.html")
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
table.insert(drops, mail)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.insert(drops, 1, user)
|
|
|
|
|
|
|
|
|
|
|
|
-- We now have a list of validated emails and forwards.
|
|
|
|
-- We need to check if there is a user with a claimed email
|
|
|
|
-- already before writing modifications to the LDAP.
|
|
|
|
local dn = conf["ldap_identifier"].."="..user..","..conf["ldap_group"]
|
|
|
|
local ldap = lualdap.open_simple(conf["ldap_host"], dn, cache:get(user.."-password"))
|
|
|
|
local cn = args.givenName.." "..args.sn
|
|
|
|
|
|
|
|
for dn, attribs in ldap:search {
|
|
|
|
base = conf["ldap_group"],
|
|
|
|
scope = "onelevel",
|
|
|
|
filter = filter,
|
|
|
|
attrs = {conf["ldap_identifier"], "mail"}
|
|
|
|
} do
|
|
|
|
-- Another user with one of these emails has been found.
|
|
|
|
if attribs[conf["ldap_identifier"]] and attribs[conf["ldap_identifier"]] ~= user then
|
2015-06-30 21:02:58 +02:00
|
|
|
-- Construct proper emails array
|
|
|
|
local mail_list = {}
|
|
|
|
local mail_attr = attribs["mail"]
|
|
|
|
if type(mail_attr) == "string" then
|
|
|
|
mail_list = { mail_attr }
|
|
|
|
elseif type(mail_attr) == "table" then
|
|
|
|
mail_list = mail_attr
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
2015-06-30 21:02:58 +02:00
|
|
|
|
|
|
|
for _, mail in ipairs(mail_list) do
|
2015-02-01 15:04:36 +01:00
|
|
|
if is_in_table(mails, mail) then
|
|
|
|
flash("fail", t("mail_already_used").." "..mail)
|
|
|
|
end
|
|
|
|
end
|
2015-02-12 12:08:52 +01:00
|
|
|
return redirect(conf.portal_url.."edit.html")
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- No problem so far, we can write modifications to the LDAP
|
|
|
|
if ldap:modify(dn, {'=', cn = cn,
|
2022-10-09 17:27:04 +02:00
|
|
|
displayName = cn,
|
2015-02-01 15:04:36 +01:00
|
|
|
givenName = args.givenName,
|
|
|
|
sn = args.sn,
|
|
|
|
mail = mails,
|
|
|
|
maildrop = drops })
|
|
|
|
then
|
2016-11-15 15:49:53 +01:00
|
|
|
delete_user_info_cache(user)
|
2015-02-01 15:04:36 +01:00
|
|
|
-- Ugly trick to force cache reloading
|
2020-12-17 17:06:19 +01:00
|
|
|
refresh_user_cache(user)
|
2015-02-01 15:04:36 +01:00
|
|
|
flash("win", t("information_updated"))
|
2019-03-19 23:29:46 +01:00
|
|
|
return redirect(conf.portal_url.."portal.html")
|
2015-02-01 15:04:36 +01:00
|
|
|
|
|
|
|
else
|
|
|
|
flash("fail", t("user_saving_fail"))
|
|
|
|
end
|
|
|
|
else
|
|
|
|
flash("fail", t("missing_required_fields"))
|
|
|
|
end
|
2015-02-12 12:08:52 +01:00
|
|
|
return redirect(conf.portal_url.."edit.html")
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-15 01:30:39 +02:00
|
|
|
-- hash the user password using sha-512 and using {CRYPT} to uses linux auth system
|
|
|
|
-- because ldap doesn't support anything stronger than sha1
|
|
|
|
function hash_password(password)
|
2018-08-29 01:26:19 +02:00
|
|
|
local hashed_password = secure_cmd_password("mkpasswd --method=sha-512", password)
|
2018-08-29 00:47:59 +02:00
|
|
|
hashed_password = "{CRYPT}"..hashed_password
|
2017-08-15 01:30:39 +02:00
|
|
|
return hashed_password
|
|
|
|
end
|
2015-02-01 15:04:36 +01:00
|
|
|
|
|
|
|
-- Compute the user login POST request
|
|
|
|
-- It authenticates the user against the LDAP base then redirects to the portal
|
2016-04-30 12:40:59 +02:00
|
|
|
function login()
|
2015-02-01 15:04:36 +01:00
|
|
|
|
|
|
|
-- We need these calls since we are in a POST request
|
|
|
|
ngx.req.read_body()
|
|
|
|
local args = ngx.req.get_post_args()
|
|
|
|
local uri_args = ngx.req.get_uri_args()
|
|
|
|
|
2015-06-02 17:01:56 +02:00
|
|
|
args.user = string.lower(args.user)
|
2019-02-01 21:42:41 +01:00
|
|
|
|
2015-05-16 21:03:06 +02:00
|
|
|
local user = authenticate(args.user, args.password)
|
2015-02-01 15:04:36 +01:00
|
|
|
if user then
|
|
|
|
ngx.status = ngx.HTTP_CREATED
|
|
|
|
set_auth_cookie(user, ngx.var.host)
|
|
|
|
else
|
|
|
|
ngx.status = ngx.HTTP_UNAUTHORIZED
|
|
|
|
flash("fail", t("wrong_username_password"))
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Forward the `r` URI argument if it exists to redirect
|
|
|
|
-- the user properly after a successful login.
|
|
|
|
if uri_args.r then
|
2015-02-12 12:08:52 +01:00
|
|
|
return redirect(conf.portal_url.."?r="..uri_args.r)
|
2015-02-01 15:04:36 +01:00
|
|
|
else
|
2015-02-12 12:08:52 +01:00
|
|
|
return redirect(conf.portal_url)
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-02-12 12:08:52 +01:00
|
|
|
-- Compute the user logout request
|
2015-02-01 15:04:36 +01:00
|
|
|
-- It deletes session cached information to invalidate client side cookie
|
|
|
|
-- information.
|
2015-02-12 12:08:52 +01:00
|
|
|
function logout()
|
2015-02-01 15:04:36 +01:00
|
|
|
|
|
|
|
-- We need this call since we are in a POST request
|
|
|
|
local args = ngx.req.get_uri_args()
|
|
|
|
|
2017-02-23 17:53:17 +01:00
|
|
|
-- Delete user cookie if logged in (that should always be the case)
|
2020-04-01 00:43:59 +02:00
|
|
|
if is_logged_in then
|
2017-02-28 15:36:45 +01:00
|
|
|
delete_cookie()
|
2015-05-16 21:03:06 +02:00
|
|
|
cache:delete("session_"..authUser)
|
|
|
|
cache:delete(authUser.."-"..conf["ldap_identifier"]) -- Ugly trick to reload cache
|
2019-01-07 10:12:08 +01:00
|
|
|
cache:delete(authUser.."-password")
|
2020-04-01 00:43:59 +02:00
|
|
|
delete_user_info_cache(authUser)
|
2015-02-01 15:04:36 +01:00
|
|
|
flash("info", t("logged_out"))
|
2020-04-01 00:43:59 +02:00
|
|
|
is_logged_in = false
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
2017-02-23 17:53:17 +01:00
|
|
|
|
2018-09-15 09:25:48 +02:00
|
|
|
-- Redirect with the `r` URI argument if it exists or redirect to portal
|
|
|
|
if args.r then
|
|
|
|
return redirect(ngx.decode_base64(args.r))
|
|
|
|
else
|
|
|
|
return redirect(conf.portal_url)
|
|
|
|
end
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Set cookie and redirect (needed to properly set cookie)
|
2016-04-30 12:40:59 +02:00
|
|
|
function redirect(url)
|
2019-10-03 20:42:01 +02:00
|
|
|
logger.debug("Redirecting to "..url)
|
2021-09-19 21:15:54 +02:00
|
|
|
-- For security reason we don't allow to redirect onto unknown domain
|
|
|
|
-- And if `uri_args.r` contains line break, someone is probably trying to
|
|
|
|
-- pass some additional headers
|
|
|
|
|
|
|
|
-- This should cover the following cases:
|
|
|
|
-- https://malicious.domain.tld/foo/bar
|
|
|
|
-- http://malicious.domain.tld/foo/bar
|
|
|
|
-- https://malicious.domain.tld:1234/foo
|
|
|
|
-- malicious.domain.tld/foo/bar
|
|
|
|
-- (/foo/bar, in which case no need to make sure it's prefixed with https://)
|
|
|
|
if not string.starts(url, "/") and not string.starts(url, "http://") and not string.starts(url, "https://") then
|
|
|
|
url = "https://"..url
|
|
|
|
end
|
2021-11-16 21:40:04 +01:00
|
|
|
local is_known_domain = string.starts(url, "/")
|
2021-11-15 19:02:13 +01:00
|
|
|
for _, domain in ipairs(conf["domains"]) do
|
2021-11-16 21:40:04 +01:00
|
|
|
if is_known_domain then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
-- Replace - character to %- because - is a special char for regex in lua
|
|
|
|
domain = string.gsub(domain, "%-","%%-")
|
2021-11-15 19:02:13 +01:00
|
|
|
is_known_domain = is_known_domain or url:match("^https?://"..domain.."/?") ~= nil
|
|
|
|
end
|
|
|
|
if string.match(url, "(.*)\n") or not is_known_domain then
|
2021-09-19 21:15:54 +02:00
|
|
|
logger.debug("Unauthorized redirection to "..url)
|
|
|
|
flash("fail", t("redirection_error_invalid_url"))
|
|
|
|
url = conf.portal_url
|
|
|
|
end
|
2015-02-01 15:04:36 +01:00
|
|
|
return ngx.redirect(url)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Set cookie and go on with the response (needed to properly set cookie)
|
2016-04-30 12:40:59 +02:00
|
|
|
function pass()
|
2019-10-03 20:42:01 +02:00
|
|
|
logger.debug("Allowing to pass through "..ngx.var.uri)
|
2015-02-01 15:04:36 +01:00
|
|
|
|
|
|
|
-- When we are in the SSOwat portal, we need a default `content-type`
|
|
|
|
if string.ends(ngx.var.uri, "/")
|
|
|
|
or string.ends(ngx.var.uri, ".html")
|
|
|
|
or string.ends(ngx.var.uri, ".htm")
|
|
|
|
then
|
|
|
|
ngx.header["Content-Type"] = "text/html"
|
|
|
|
end
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|