2017-01-28 10:59:40 +01:00
|
|
|
#!/bin/bash
|
2017-01-03 18:32:00 +01:00
|
|
|
#
|
|
|
|
# Common variables
|
|
|
|
#
|
|
|
|
|
2017-12-14 17:26:51 +01:00
|
|
|
pkg_dependencies="php5-gd php5-imagick imagemagick"
|
2017-09-23 10:25:23 +02:00
|
|
|
|
2017-09-10 09:25:22 +02:00
|
|
|
# ============= FUTURE YUNOHOST HELPERS =============
|
|
|
|
|
2017-06-18 18:24:10 +02:00
|
|
|
# Create a dedicated fail2ban config (jail and filter conf files)
|
|
|
|
#
|
|
|
|
# usage: ynh_add_fail2ban_config log_file filter [max_retry [ports]]
|
|
|
|
# | arg: log_file - Log file to be checked by fail2ban
|
|
|
|
# | arg: failregex - Failregex to be looked for by fail2ban
|
|
|
|
# | arg: max_retry - Maximum number of retries allowed before banning IP address - default: 3
|
|
|
|
# | arg: ports - Ports blocked for a banned IP address - default: http,https
|
|
|
|
ynh_add_fail2ban_config () {
|
|
|
|
# Process parameters
|
|
|
|
logpath=$1
|
|
|
|
failregex=$2
|
|
|
|
max_retry=${3:-3}
|
|
|
|
ports=${4:-http,https}
|
|
|
|
|
|
|
|
test -n "$logpath" || ynh_die "ynh_add_fail2ban_config expects a logfile path as first argument and received nothing."
|
|
|
|
test -n "$failregex" || ynh_die "ynh_add_fail2ban_config expects a failure regex as second argument and received nothing."
|
|
|
|
|
2018-03-08 19:08:15 +01:00
|
|
|
finalfail2banjailconf="/etc/fail2ban/jail.d/$app.conf"
|
|
|
|
finalfail2banfilterconf="/etc/fail2ban/filter.d/$app.conf"
|
|
|
|
ynh_backup_if_checksum_is_different "$finalfail2banjailconf" 1
|
|
|
|
ynh_backup_if_checksum_is_different "$finalfail2banfilterconf" 1
|
2017-06-18 18:24:10 +02:00
|
|
|
|
2018-03-08 19:08:15 +01:00
|
|
|
sudo tee $finalfail2banjailconf <<EOF
|
2017-06-18 18:24:10 +02:00
|
|
|
[$app]
|
|
|
|
enabled = true
|
|
|
|
port = $ports
|
|
|
|
filter = $app
|
|
|
|
logpath = $logpath
|
2018-03-08 19:08:15 +01:00
|
|
|
maxretry = $max_retry
|
2017-06-18 18:24:10 +02:00
|
|
|
EOF
|
|
|
|
|
2018-03-08 19:08:15 +01:00
|
|
|
sudo tee $finalfail2banfilterconf <<EOF
|
2017-06-18 18:24:10 +02:00
|
|
|
[INCLUDES]
|
|
|
|
before = common.conf
|
|
|
|
[Definition]
|
|
|
|
failregex = $failregex
|
2018-03-08 19:08:15 +01:00
|
|
|
ignoreregex =
|
2017-06-18 18:24:10 +02:00
|
|
|
EOF
|
|
|
|
|
2018-03-08 19:08:15 +01:00
|
|
|
ynh_store_file_checksum "$finalfail2banjailconf"
|
|
|
|
ynh_store_file_checksum "$finalfail2banfilterconf"
|
2017-06-18 18:24:10 +02:00
|
|
|
|
2018-03-08 19:08:15 +01:00
|
|
|
systemctl restart fail2ban
|
|
|
|
local fail2ban_error="$(journalctl -u fail2ban | tail -n50 | grep "WARNING.*$app.*")"
|
|
|
|
if [ -n "$fail2ban_error" ]
|
|
|
|
then
|
|
|
|
echo "[ERR] Fail2ban failed to load the jail for $app" >&2
|
|
|
|
echo "WARNING${fail2ban_error#*WARNING}" >&2
|
|
|
|
fi
|
2017-06-18 18:24:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Remove the dedicated fail2ban config (jail and filter conf files)
|
|
|
|
#
|
|
|
|
# usage: ynh_remove_fail2ban_config
|
|
|
|
ynh_remove_fail2ban_config () {
|
2018-03-08 19:08:15 +01:00
|
|
|
ynh_secure_remove "/etc/fail2ban/jail.d/$app.conf"
|
2017-06-18 18:24:10 +02:00
|
|
|
ynh_secure_remove "/etc/fail2ban/filter.d/$app.conf"
|
2018-03-08 19:08:15 +01:00
|
|
|
sudo systemctl restart fail2ban
|
2017-09-10 09:25:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Delete a file checksum from the app settings
|
2019-02-17 19:28:37 +01:00
|
|
|
#=================================================
|
|
|
|
|
|
|
|
# Check available space before creating a temp directory.
|
2017-09-10 09:25:22 +02:00
|
|
|
#
|
|
|
|
# $app should be defined when calling this helper
|
2019-02-17 19:28:37 +01:00
|
|
|
# usage: ynh_smart_mktemp --min_size="Min size"
|
2017-09-10 09:25:22 +02:00
|
|
|
#
|
|
|
|
# usage: ynh_remove_file_checksum file
|
|
|
|
# | arg: file - The file for which the checksum will be deleted
|
|
|
|
ynh_delete_file_checksum () {
|
|
|
|
local checksum_setting_name=checksum_${1//[\/ ]/_} # Replace all '/' and ' ' by '_'
|
|
|
|
ynh_app_setting_delete $app $checksum_setting_name
|
2019-02-17 19:28:37 +01:00
|
|
|
# | arg: -s, --min_size= - Minimal size needed for the temporary directory, in Mb
|
|
|
|
ynh_smart_mktemp () {
|
|
|
|
# Declare an array to define the options of this helper.
|
|
|
|
declare -Ar args_array=( [s]=min_size= )
|
|
|
|
local min_size
|
|
|
|
# Manage arguments with getopts
|
|
|
|
ynh_handle_getopts_args "$@"
|
|
|
|
|
|
|
|
min_size="${min_size:-300}"
|
|
|
|
# Transform the minimum size from megabytes to kilobytes
|
|
|
|
min_size=$(( $min_size * 1024 ))
|
|
|
|
|
|
|
|
# Check if there's enough free space in a directory
|
|
|
|
is_there_enough_space () {
|
|
|
|
local free_space=$(df --output=avail "$1" | sed 1d)
|
|
|
|
test $free_space -ge $min_size
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_there_enough_space /tmp; then
|
|
|
|
local tmpdir=/tmp
|
|
|
|
elif is_there_enough_space /var; then
|
|
|
|
local tmpdir=/var
|
|
|
|
elif is_there_enough_space /; then
|
|
|
|
local tmpdir=/
|
|
|
|
elif is_there_enough_space /home; then
|
|
|
|
local tmpdir=/home
|
|
|
|
else
|
|
|
|
ynh_die "Insufficient free space to continue..."
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$(sudo mktemp --directory --tmpdir="$tmpdir")"
|
2017-09-10 09:25:22 +02:00
|
|
|
}
|