1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/samba_ynh.git synced 2024-09-03 20:16:27 +02:00
samba_ynh/scripts/config
2024-03-29 05:45:14 +01:00

128 lines
4.3 KiB
Bash

#!/bin/bash
# In simple cases, you don't need a config script.
# With a simple config_panel.toml, you can write in the app settings, in the
# upstream config file or replace complete files (logo ...) and restart services.
# The config scripts allows you to go further, to handle specific cases
# (validation of several interdependent fields, specific getter/setter for a value,
# display dynamic informations or choices, pre-loading of config type .cube... ).
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source /usr/share/yunohost/helpers
ynh_abort_if_errors
data_dir=$(ynh_app_setting_get --app=$app --key=data_dir)
install_dir=$(ynh_app_setting_get --app=$app --key=install_dir)
#=================================================
# recreate the smb.conf from smb.conf.d
#=================================================
write_smb_conf() {
cat > $install_dir/smb.conf <<EOF
# =================================================
# DO NOT EDIT THIS FILE
# EDIT SUBPARTS IN /etc/samba/smb.conf.d
# =================================================
EOF
cat $install_dir/smb.conf.d/*.conf >> $install_dir/smb.conf
}
#=================================================
# SPECIFIC SETTERS FOR TOML SHORT KEYS
#=================================================
set__directories() {
mkdir -p $install_dir/smb.conf.d
#---------------------------------------------
# IMPORTANT: setter are trigger only if a change is detected
#---------------------------------------------
for directory in $(echo $directories | sed "s/,/ /g"); do
# Create yunohost permission
if ! ynh_permission_exists --permission=$directory ; then
ynh_permission_create --permission="$directory" --allowed=all_users --show_tile=false
fi
# Create the directory
mkdir -p $data_dir/$directory
chmod 750 "$data_dir/$directory"
chmod -R o-rwx "$data_dir/$directory"
chown -R root:root "$data_dir/$directory"
setfacl -R -m g:samba.$directory:rwx,d:g:samba.$directory:rwx $data_dir/$directory
# Add the configuration in /etc/samba/smb.conf if needed
cat > $install_dir/smb.conf.d/$directory.conf <<END
[$directory]
comment = $directory
read only = no
path = $data_dir/$directory
guest ok = no
browsable = yes
valid users = @samba.$directory
create mask = 0660
directory mask = 770
vfs objects = dfs_samba4 acl_xattr recycle
recycle:repository = .recycle
recycle:keeptree = yes
recycle:versions = yes
END
done
# Remove configuration for unlisted directories
for directory in $(find $install_dir/smb.conf.d/ -maxdepth 1 -type f -not -name '0-global.conf' -exec basename {} \;); do
if ! [[ "${directory%.conf}" =~ $(echo "^($(echo $directories | sed 's/,\| /|/g'))$") ]]; then
ynh_secure_remove "$install_dir/smb.conf.d/$directory"
fi
done
write_smb_conf
#---------------------------------------------
# IMPORTANT: to be able to upgrade properly, you have to saved the value in settings too
#---------------------------------------------
ynh_app_setting_set $app directories "$directories"
}
set__readonly_dir() {
local value
for directory in $(echo $directories | sed "s/,/ /g"); do
value="no"
if [[ -n "$readonly_dir" && $directory =~ $(echo "^($(echo $readonly_dir | sed 's/,\| /|/g'))$") ]]; then
value="yes"
fi
ynh_write_var_in_file --file=$install_dir/smb.conf.d/$directory.conf --key="read only" --value="$value"
done
ynh_app_setting_set $app readonly_dir "$readonly_dir"
write_smb_conf
}
set__unbrowseable() {
local value
for directory in $(echo $directories | sed "s/,/ /g"); do
value="yes"
if [[ -n "$unbrowseable" && $directory =~ $(echo "^($(echo $unbrowseable | sed 's/,\| /|/g'))$") ]]; then
value="no"
fi
ynh_write_var_in_file --file=$install_dir/smb.conf.d/$directory.conf --key="browsable" --value="$value"
done
ynh_app_setting_set $app unbrowseable "$unbrowseable"
write_smb_conf
}
#=================================================
# GENERIC FINALIZATION
#=================================================
ynh_app_config_run $1