2017-02-13 20:43:41 +01:00
#!/bin/bash
2018-01-30 23:44:49 +01:00
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
2019-04-30 19:15:33 +02:00
source _common.sh
source experimental_helper.sh
2017-07-21 22:28:49 +02:00
source /usr/share/yunohost/helpers
2018-01-30 23:44:49 +01:00
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
2023-11-01 18:52:07 +01:00
if [ "$server_name" == "Same than the domain" ]; then
2022-09-15 13:55:46 +02:00
server_name=$domain
2023-11-01 18:52:07 +01:00
ynh_app_setting_set --app=$app --key=server_name --value=$server_name
2019-10-30 21:15:22 +01:00
fi
2018-01-30 23:44:49 +01:00
#=================================================
2023-11-01 18:52:07 +01:00
## SET STANDARD SETTINGS FROM DEFAULT CONFIG
2018-01-30 23:44:49 +01:00
#=================================================
2017-02-13 20:43:41 +01:00
2023-11-01 18:52:07 +01:00
ynh_script_progression --message="Storing installation settings..." --weight=1
2020-12-07 16:34:41 +01:00
2023-11-01 18:52:07 +01:00
report_stats="false"
e2e_enabled_by_default="off"
allow_public_rooms_without_auth="false"
allow_public_rooms_over_federation="false"
2023-11-22 18:58:42 +01:00
max_upload_size="100M"
2023-11-01 18:52:07 +01:00
disable_msisdn_registration="true"
registrations_require_3pid="none"
allowed_local_3pids_email=""
allowed_local_3pids_msisdn=""
allow_guest_access="false"
account_threepid_delegates_msisdn=""
default_identity_server="https://matrix.org"
auto_join_rooms=""
autocreate_auto_join_rooms="false"
auto_join_rooms_for_guests="true"
enable_notifs="true"
notif_for_new_users="true"
enable_group_creation="true"
push_include_content="true"
2018-01-30 23:44:49 +01:00
2023-11-01 18:52:07 +01:00
if [ "$is_free_registration" -eq 0 ]
2023-01-11 17:11:43 +01:00
then
enable_registration="false"
turn_allow_guests="false"
sso_enabled="true"
password_enabled="false"
2023-01-19 10:08:18 +01:00
enable_3pid_lookup="false"
2023-01-11 17:11:43 +01:00
else
enable_registration="true"
turn_allow_guests="true"
sso_enabled="false"
password_enabled="true"
2023-01-19 10:08:18 +01:00
enable_3pid_lookup="true"
2023-01-11 17:11:43 +01:00
fi
2023-01-10 02:05:30 +01:00
element_ynh_url="https://matrix.to/"
2023-01-09 23:26:39 +01:00
# Get app name of first Element Instance (can be changed later in Config Panel)
element_instance="element"
2023-01-10 02:05:30 +01:00
if yunohost --output-as plain app list | grep -q "^$element_instance$"; then
2023-11-01 18:52:07 +01:00
element_domain=$(ynh_app_setting_get --app $element_instance --key domain)
element_path=$(ynh_app_setting_get --app $element_instance --key path)
2023-01-11 17:36:28 +01:00
element_ynh_url="https://""$element_domain""$element_path"
2023-01-09 23:26:39 +01:00
fi
2023-01-11 14:03:52 +01:00
web_client_location=$element_ynh_url
client_base_url=$element_ynh_url
invite_client_location=$element_ynh_url
2023-11-02 23:10:26 +01:00
ynh_app_setting_set --app=$app --key=report_stats --value=$report_stats
2023-11-01 18:52:07 +01:00
ynh_app_setting_set --app=$app --key=e2e_enabled_by_default --value=$e2e_enabled_by_default
2023-01-11 14:03:52 +01:00
ynh_app_setting_set --app=$app --key=web_client_location --value=$web_client_location
ynh_app_setting_set --app=$app --key=client_base_url --value=$client_base_url
ynh_app_setting_set --app=$app --key=invite_client_location --value=$invite_client_location
2023-01-06 14:38:14 +01:00
ynh_app_setting_set --app=$app --key=allow_public_rooms_without_auth --value=$allow_public_rooms_without_auth
ynh_app_setting_set --app=$app --key=allow_public_rooms_over_federation --value=$allow_public_rooms_over_federation
2023-01-13 12:38:30 +01:00
ynh_app_setting_set --app=$app --key=max_upload_size --value=$max_upload_size
2023-01-06 14:38:14 +01:00
ynh_app_setting_set --app=$app --key=disable_msisdn_registration --value=$disable_msisdn_registration
2023-01-10 12:37:02 +01:00
ynh_app_setting_set --app=$app --key=registrations_require_3pid --value=$registrations_require_3pid
2023-01-19 10:08:18 +01:00
ynh_app_setting_set --app=$app --key=allowed_local_3pids_email --value=$allowed_local_3pids_email
ynh_app_setting_set --app=$app --key=allowed_local_3pids_msisdn --value=$allowed_local_3pids_msisdn
2023-08-23 22:18:17 +02:00
ynh_app_setting_set --app=$app --key=account_threepid_delegates_msisdn --value=$account_threepid_delegates_msisdn
2023-01-06 14:38:14 +01:00
ynh_app_setting_set --app=$app --key=allow_guest_access --value=$allow_guest_access
ynh_app_setting_set --app=$app --key=default_identity_server --value=$default_identity_server
ynh_app_setting_set --app=$app --key=auto_join_rooms --value=$auto_join_rooms
2023-01-10 04:14:11 +01:00
ynh_app_setting_set --app=$app --key=autocreate_auto_join_rooms --value=$autocreate_auto_join_rooms
2023-01-06 14:38:14 +01:00
ynh_app_setting_set --app=$app --key=auto_join_rooms_for_guests --value=$auto_join_rooms_for_guests
2023-11-01 18:52:07 +01:00
ynh_app_setting_set --app=$app --key=password_enabled --value=$password_enabled
2023-01-06 14:38:14 +01:00
ynh_app_setting_set --app=$app --key=enable_notifs --value=$enable_notifs
ynh_app_setting_set --app=$app --key=notif_for_new_users --value=$notif_for_new_users
ynh_app_setting_set --app=$app --key=enable_group_creation --value=$enable_group_creation
2023-01-30 18:00:49 +01:00
ynh_app_setting_set --app=$app --key=push_include_content --value=$push_include_content
2023-11-01 18:52:07 +01:00
ynh_app_setting_set --app=$app --key=enable_registration --value=$enable_registration
ynh_app_setting_set --app=$app --key=turn_allow_guests --value=$turn_allow_guests
ynh_app_setting_set --app=$app --key=sso_enabled --value=$sso_enabled
ynh_app_setting_set --app=$app --key=password_enabled --value=$password_enabled
ynh_app_setting_set --app=$app --key=enable_3pid_lookup --value=$enable_3pid_lookup
2023-01-06 14:38:14 +01:00
2018-01-30 23:44:49 +01:00
#=================================================
# STANDARD MODIFICATIONS
2019-02-08 11:24:08 +01:00
#=================================================
# CREATE A DH FILE
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Creating a dh file..." --weight=3
2019-02-08 11:24:08 +01:00
# WARNING : theses command are used in INSTALL, UPGRADE, RESTORE
# For any update do it in all files
2019-04-30 19:15:33 +02:00
# Make dh cert for synapse if it doesn't exist
if [ ! -e /etc/ssl/private/dh2048.pem ]
2019-02-08 11:24:08 +01:00
then
2019-04-30 19:15:33 +02:00
ynh_exec_warn_less openssl dhparam -out /etc/ssl/private/dh2048.pem -outform PEM -2 2048 -dsaparam
2019-02-08 11:24:08 +01:00
chown root:ssl-cert /etc/ssl/private/dh2048.pem
chmod 640 /etc/ssl/private/dh2048.pem
fi
2018-01-30 23:44:49 +01:00
#=================================================
# CREATE DEDICATED USER
#=================================================
2023-11-01 18:52:07 +01:00
ynh_script_progression --message="Creating $app user..." --weight=1
2018-01-30 23:44:49 +01:00
2023-11-01 18:52:07 +01:00
synapse_user_app_pwd="$(ynh_string_random --length=30)"
ynh_app_setting_set --app=$app --key=synapse_user_app_pwd --value=$synapse_user_app_pwd
2023-12-18 14:35:56 +01:00
# yunohost user create $app -F "Synapse Application" -d $domain -p "$synapse_user_app_pwd"
2018-01-30 23:44:49 +01:00
2023-12-18 14:35:56 +01:00
adduser $app ssl-cert
2023-11-01 18:52:07 +01:00
adduser turnserver ssl-cert
2017-02-13 20:43:41 +01:00
2023-11-01 19:25:19 +01:00
#=================================================
# FIX DB CONFIG
#=================================================
ynh_script_progression --message="Fixing database type..." --weight=1
ynh_psql_execute_as_root \
--sql="update pg_database set datcollate='C', datctype='C' where datname='$db_name';"
2018-01-30 23:44:49 +01:00
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Setting up source files..." --weight=50
2017-02-13 20:43:41 +01:00
2018-01-30 23:44:49 +01:00
# Create empty dir for synapse
2018-08-03 15:58:40 +02:00
# WARNING : theses command are used in INSTALL, UPGRADE
2018-01-30 23:44:49 +01:00
# For any update do it in all files
mkdir -p /var/log/matrix-$app
mkdir -p /etc/matrix-$app/conf.d
2020-09-13 20:23:55 +02:00
mkdir -p /etc/matrix-$app/app-service
2023-05-25 12:38:09 +02:00
touch /etc/matrix-$app/conf.d/app_service.yaml
2018-01-30 23:44:49 +01:00
# Install synapse in virtualenv
2020-07-29 22:52:58 +02:00
install_sources
2018-01-30 23:44:49 +01:00
2020-02-22 00:42:32 +01:00
#=================================================
# CREATE SMALL CAS SERVER
#=================================================
# WARNING : theses command are used in INSTALL, UPGRADE
# For any update do it in all files
2023-11-01 18:52:07 +01:00
cp ../sources/cas_server.php $install_dir/
chmod u=rwX,g=rX,o= -R $install_dir
2023-12-18 14:35:56 +01:00
chown $app:$app -R $install_dir
2020-02-22 00:42:32 +01:00
2018-08-01 00:32:10 +02:00
#=================================================
2019-02-08 11:24:08 +01:00
# CREATE SYNAPSE CONFIG
2018-08-01 00:32:10 +02:00
#=================================================
2020-02-22 00:42:32 +01:00
2022-10-28 10:59:40 +02:00
ynh_script_progression --message="Creating Synapse config..." --weight=3
2018-08-01 00:32:10 +02:00
# Go in virtualenvironnement
2023-11-01 18:52:07 +01:00
u_arg='u'
set +$u_arg;
source $code_dir/bin/activate
set -$u_arg;
2018-08-01 00:32:10 +02:00
2019-02-08 11:24:08 +01:00
# Generate config
2022-09-15 13:55:46 +02:00
python -m synapse.app.homeserver --keys-directory /etc/matrix-$app/ --generate-config --server-name $server_name --report-stats=no -c homeserver.yml
2018-08-01 00:32:10 +02:00
2023-11-01 18:52:07 +01:00
# This function was defined when we called "source $code_dir/bin/activate". With this function we undo what "$code_dir/bin/activate" does
set +$u_arg;
2018-08-01 00:32:10 +02:00
deactivate
2023-11-01 18:52:07 +01:00
set -$u_arg;
2018-08-01 00:32:10 +02:00
2018-08-21 07:27:01 +02:00
# Get random values from config
2019-11-19 20:29:11 +01:00
registration_shared_secret=$(egrep "^registration_shared_secret:" homeserver.yml | cut -d'"' -f2)
form_secret=$(egrep "^form_secret:" homeserver.yml | cut -d'"' -f2)
macaroon_secret_key=$(egrep "^macaroon_secret_key:" homeserver.yml | cut -d'"' -f2)
2018-08-01 00:32:10 +02:00
# store in yunohost settings
2019-04-30 19:15:33 +02:00
ynh_app_setting_set --app=$app --key=registration_shared_secret --value="$registration_shared_secret"
ynh_app_setting_set --app=$app --key=form_secret --value="$form_secret"
2019-11-12 21:59:46 +01:00
ynh_app_setting_set --app=$app --key=macaroon_secret_key --value="$macaroon_secret_key"
2018-08-01 00:32:10 +02:00
2018-01-30 23:44:49 +01:00
#=================================================
# SETUP SYSTEMD
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Configuring a systemd service..." --weight=2
2017-02-16 22:54:57 +01:00
2018-01-19 22:05:39 +01:00
# Create systemd service for synapse and turnserver
cp ../conf/default_matrix-synapse /etc/default/matrix-$app
2024-02-06 20:42:00 +01:00
ynh_add_systemd_config --service=matrix-$app --template=synapse.service
2018-01-19 22:05:39 +01:00
cp ../conf/default_coturn /etc/default/coturn-$app
2024-02-06 20:42:00 +01:00
ynh_add_systemd_config --service=$app-coturn --template=synapse-coturn.service
2017-02-13 20:43:41 +01:00
2018-01-30 23:44:49 +01:00
#=================================================
# NGINX CONFIGURATION
#=================================================
2022-10-28 10:59:40 +02:00
ynh_script_progression --message="Configuring NGINX web server..." --weight=2
2018-01-30 23:44:49 +01:00
2020-02-22 00:42:32 +01:00
# Create a dedicated php-fpm config
ynh_script_progression --message="Configuring application..."
2023-08-26 12:07:09 +02:00
ynh_add_fpm_config --usage=low --footprint=low
2020-02-22 00:42:32 +01:00
2023-11-01 18:52:07 +01:00
configure_nginx
2020-05-07 14:41:35 +02:00
2018-01-30 23:44:49 +01:00
#=================================================
# SET SYNAPSE CONFIG
#=================================================
2022-10-28 10:59:40 +02:00
ynh_script_progression --message="Configuring Synapse..." --weight=2
2018-01-30 23:44:49 +01:00
# Find password for turnserver and database
2019-04-30 19:15:33 +02:00
turnserver_pwd=$(ynh_string_random --length=30)
ynh_app_setting_set --app=$app --key=turnserver_pwd --value=$turnserver_pwd
2018-01-30 23:44:49 +01:00
2023-11-01 18:52:07 +01:00
configure_synapse
2018-01-30 23:44:49 +01:00
#=================================================
# SET COTURN CONFIG
#=================================================
2022-10-28 10:59:40 +02:00
ynh_script_progression --message="Configuring Coturn..." --weight=1
2018-01-30 23:44:49 +01:00
2023-11-01 18:52:07 +01:00
configure_coturn
2018-01-30 23:44:49 +01:00
#=================================================
# SETUP LOGROTATE
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Configuring log rotation..." --weight=2
2017-02-13 20:43:41 +01:00
2019-09-30 10:37:22 +02:00
ynh_use_logrotate --logfile "/var/log/matrix-$app"
2017-02-13 20:43:41 +01:00
2018-06-20 19:16:01 +02:00
#=================================================
2020-07-24 23:33:53 +02:00
# ADD SCRIPT FOR COTURN CRON AND APP SERVICE
2018-06-20 19:16:01 +02:00
#=================================================
2018-08-03 15:58:40 +02:00
# WARNING : theses command are used in INSTALL, UPGRADE
2018-06-20 19:16:01 +02:00
# For any update do it in all files
2023-11-01 18:52:07 +01:00
ynh_add_config --template="../sources/Coturn_config_rotate.sh" --destination="$code_dir/Coturn_config_rotate.sh"
ynh_add_config --template="../sources/update_synapse_for_appservice.sh" --destination="$code_dir/update_synapse_for_appservice.sh"
2018-06-20 19:16:01 +02:00
2018-01-30 23:44:49 +01:00
#=================================================
# GENERIC FINALIZATION
#=================================================
2020-12-15 22:25:29 +01:00
# SETUP PERMISSIONS
2018-01-30 23:44:49 +01:00
#=================================================
2020-12-15 22:25:29 +01:00
2023-10-31 22:55:01 +01:00
ynh_script_progression --message="Configuring permissions..." --weight=1
2018-01-30 23:44:49 +01:00
2021-05-09 15:25:02 +02:00
if yunohost --output-as plain domain list | grep -q "^$server_name$"; then
2023-11-01 18:52:07 +01:00
ynh_""permission_create --permission=server_client_infos --url=$server_name/.well-known/matrix \
2021-05-09 15:25:02 +02:00
--label="Server info for clients. (well-known)" --show_tile=false --allowed=visitors \
--auth_header=false --protected=true
fi
2022-09-15 13:55:46 +02:00
2019-09-03 15:26:09 +02:00
#=================================================
# UPDATE HOOKS
#=================================================
# WARNING : theses command are used in INSTALL, UPGRADE
# For any update do it in all files
ynh_replace_string __APP__ $app ../hooks/post_cert_update
ynh_replace_string __DOMAIN__ $domain ../hooks/post_cert_update
2018-01-30 23:44:49 +01:00
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
2023-11-01 18:52:07 +01:00
ynh_script_progression --message="Protecting directories..." --weight=3
set_permissions
2018-01-30 23:44:49 +01:00
#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
2018-01-13 01:07:17 +01:00
2024-02-06 20:42:00 +01:00
yunohost service add $app --log "/var/log/matrix-$app/homeserver.log" --needs_exposed_ports $port_synapse_tls
yunohost service add $app-coturn --needs_exposed_ports $port_turnserver_tls
2017-05-26 15:29:12 +02:00
2018-01-30 23:44:49 +01:00
#=================================================
# RELOAD SERVICES
#=================================================
2022-10-28 10:59:40 +02:00
ynh_script_progression --message="Restarting Synapse services..." --weight=11
2018-01-30 23:44:49 +01:00
2024-02-06 20:42:00 +01:00
ynh_systemd_action --service_name=$app-coturn.service --action=restart
ynh_systemd_action --service_name=$app.service --action=restart --line_match="Synapse now listening on TCP port $port_synapse_tls" --log_path="/var/log/matrix-$app/homeserver.log" --timeout=300
2018-03-13 16:50:27 +01:00
#=================================================
2019-02-12 21:24:25 +01:00
# SETUP FAIL2BAN
#=================================================
2022-10-28 10:59:40 +02:00
ynh_script_progression --message="Configuring Fail2Ban..." --weight=10
2019-02-12 21:24:25 +01:00
2019-04-30 19:15:33 +02:00
ynh_add_fail2ban_config --use_template
2019-02-12 21:24:25 +01:00
2019-04-30 19:15:33 +02:00
#=================================================
# END OF SCRIPT
#=================================================
2018-03-13 16:50:27 +01:00
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Installation of $app completed" --last