From bd343798275fc23151de14385c39aaab9e4d731f Mon Sep 17 00:00:00 2001 From: Christoph Date: Sat, 4 Dec 2021 13:47:14 +0100 Subject: [PATCH] add_user_to_group.sh,remove_user_from_group.sh: some minor changes to be script-compartible.. --- add_user_to_group.sh | 220 +++++++++++++++++++++++++++++++------- remove_user_from_group.sh | 218 ++++++++++++++++++++++++++++++------- 2 files changed, 361 insertions(+), 77 deletions(-) diff --git a/add_user_to_group.sh b/add_user_to_group.sh index fa8de3e..c37aea0 100755 --- a/add_user_to_group.sh +++ b/add_user_to_group.sh @@ -19,6 +19,22 @@ backup_date=$(date +%Y-%m-%d-%H%M) clean_up() { + + if [[ -f "$_backup_crontab_file" ]]; then + + echononl " (Re)Install previously saved crontab .." + + crontab $_backup_crontab_file >> $log_file 2>&1 + + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $log_file)" + fi + + fi + # Perform program exit housekeeping rm -f $log_file blank_line @@ -123,6 +139,35 @@ info (){ echo "" } +detect_os_1 () { + + if $(which lsb_release > /dev/null 2>&1) ; then + + os_dist="$(lsb_release -i | awk '{print tolower($3)}')" + os_version="$(lsb_release -r | awk '{print tolower($2)}')" + os_codename="$(lsb_release -c | awk '{print tolower($2)}')" + + if [[ "$os_dist" = "debian" ]]; then + if $(echo "$os_version" | grep -q '\.') ; then + os_version=$(echo "$os_version" | cut --delimiter='.' -f1) + fi + fi + + elif [[ -e "/etc/os-release" ]]; then + + . /etc/os-release + + os_dist=$ID + os_version=${VERSION_ID} + + fi + + # remove whitespace from os_dist and os_version + os_dist="${os_dist// /}" + os_version="${os_version// /}" + +} + # - Remove leading/trailling whitespaces # - trim() { @@ -197,8 +242,6 @@ fi #clear -echo "" -echo "" echo "" echo -e "\033[32m-----\033[m" echo -e "Add User to Group - \033[1mBoth, User and Group has to exist\033[m" @@ -223,6 +266,7 @@ read _IN if is_number "$_IN" && [[ -n ${website_arr[$_IN]} ]]; then IFS=':' read -a _arr <<< ${website_arr[$_IN]} conf_file=${_arr[1]} + DEFAULT_WEBSITE="${_arr[0]}" _OK=true else echo "" @@ -245,11 +289,12 @@ fi echo "" + # ============= # --- Some checks # ============= -DEFAULT_WEB_BASE_DIR="/var/www/$WEBSITE" +DEFAULT_SRC_BASE_DIR="/usr/local/src/nextcloud" DEFAULT_HTTP_USER="www-data" DEFAULT_HTTP_GROUP="www-data" DEFAULT_PHP_ENGINE="FPM" @@ -269,26 +314,6 @@ fi [[ -n "$PHP_ENGINE" ]] || PHP_ENGINE=$DEFAULT_PHP_ENGINE -# Check PHP Version -# -if [[ "$PHP_ENGINE" = "FPM" ]] ; then - if [[ -z "$PHP_VERSION" ]] ; then - if [[ -z "$VHOST_CONFIG_FILE" ]] ; then - if [[ -f "/usr/local/apache2/conf/vhosts/${WEBSITE}.conf.php-fpm" ]] ; then - VHOST_CONFIG_FILE="/usr/local/apache2/conf/vhosts/${WEBSITE}.conf.php-fpm" - elif [[ -f "/usr/local/apache2/conf/vhosts/${WEBSITE}.conf" ]] ; then - VHOST_CONFIG_FILE="/usr/local/apache2/conf/vhosts/${WEBSITE}.conf" - elif [[ -f "/etc/apache2/sites-enabled/${WEBSITE}.conf" ]] ; then - VHOST_CONFIG_FILE="/etc/apache2/sites-enabled/${WEBSITE}.conf" - fi - fi - PHP_VERSION="$(grep -o -E "php-?.{1}\..{1}-fpm" $VHOST_CONFIG_FILE | grep -o -E ".{1}\..{1}")" - if [[ -z "$PHP_VERSION" ]] ; then - fatal "PHP Version must be givven if running PHP-FPM engine (parameter 'PHP_VERSION')" - fi - fi -fi - if [[ "$DATABASE_TYPE" != "postgres" ]] && [[ "$DATABASE_TYPE" != "mysql" ]]; then fatal "Wrong or missing database type (parameter 'DATABASE_TYPE')" fi @@ -307,32 +332,86 @@ if [[ "$DATABASE_TYPE" = "postgres" ]]; then fi fi -# - Determin user/group of the webserver -# - -httpd_binary="`which httpd`" -if [ -z "$httpd_binary" ]; then - httpd_binary="$(ps -axu | grep httpd | grep -e "^root" | grep -v grep | awk '{print$11}')" - if [ -z "$httpd_binary" ]; then - if [ -x "/usr/local/apache2/bin/httpd" ]; then - httpd_binary="/usr/local/apache2/bin/httpd" +NGINX_IS_ENABLED=false +APACHE2_IS_ENABLED=false + +# Check if NGINX webserver is ctive +# +if $(systemctl -q is-enabled nginx 2> /dev/null) ; then + + NGINX_IS_ENABLED=true + + # - Determin user of the webserver + # - + nginx_binary="$(which nginx)" + if [[ -z "$nginx_binary" ]] ; then + nginx_binary="$(ps -axu | grep -E "nginx:.*master" | grep -v grep | grep -o -E "\S+/nginx")" + if [[ -z "$nginx_binary" ]] ; then + if [[ -x "/usr/local/nginx/bin/nginx" ]]; then + nginx_binary="/usr/local/nginx/bin/nginx" + elif [[ -x "/usr/local/nginx/sbin/nginx" ]]; then + nginx_binary="/usr/local/nginx/sbin/nginx" + fi fi fi -fi -if [ -x "$httpd_binary" ];then - # - Determin websever user - # - - _HTTP_USER="`$httpd_binary -t -D DUMP_RUN_CFG | grep -i -e "^User" | awk '{print$2}' | cut -d\"=\" -f2 | tr -d '"'`" - _HTTP_GROUP="`$httpd_binary -t -D DUMP_RUN_CFG | grep -i -e "^Group" | awk '{print$2}' | cut -d\"=\" -f2 | tr -d '"'`" + if [[ -x "$nginx_binary" ]] ; then + _HTTP_USER="$($nginx_binary -T 2> /dev/null | grep -E "^\s*user\s+\S+;" | grep -o -E "\S+;$" | sed -e 's/;$//')" + fi # - Is webserver running ? # - - PID=$(ps aux | grep "$(realpath $httpd_binary)" | grep -e "^root" | grep -v grep | awk '{print$2}') + PID=$(ps aux | grep "$(realpath $nginx_binary)" | grep -e "^root" | grep -v grep | awk '{print$2}') if [[ "X${PID}X" = "XX" ]] ;then IS_HTTPD_RUNNING=false else IS_HTTPD_RUNNING=true fi + + +elif $(systemctl -q is-enabled apache2 2> /dev/null) ; then + + APACHE2_IS_ENABLED=true + + # - Determin user of the webserver + # - + httpd_binary="`which httpd`" + if [ -z "$httpd_binary" ]; then + httpd_binary="$(ps -axu | grep httpd | grep -e "^root" | grep -v grep | awk '{print$11}')" + if [ -z "$httpd_binary" ]; then + if [ -x "/usr/local/apache2/bin/httpd" ]; then + httpd_binary="/usr/local/apache2/bin/httpd" + fi + fi + fi + if [ -x "$httpd_binary" ];then + + # - Determin websever user + # - + _HTTP_USER="`$httpd_binary -t -D DUMP_RUN_CFG | grep -i -e "^User" | awk '{print$2}' | cut -d\"=\" -f2 | tr -d '"'`" + _HTTP_GROUP="`$httpd_binary -t -D DUMP_RUN_CFG | grep -i -e "^Group" | awk '{print$2}' | cut -d\"=\" -f2 | tr -d '"'`" + + # - Is webserver running ? + # - + PID=$(ps aux | grep "$(realpath $httpd_binary)" | grep -e "^root" | grep -v grep | awk '{print$2}') + if [[ "X${PID}X" = "XX" ]] ;then + IS_HTTPD_RUNNING=false + else + IS_HTTPD_RUNNING=true + fi + fi + +else + error "Neither \033[1mapache2\033[m nor \033[1mnginx\033[m is enabled on this machine" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/nno]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Interrupted by user" fi if [[ -n "$_HTTP_USER" ]] ; then @@ -354,6 +433,69 @@ else [[ -n "$HTTP_GROUP" ]] || HTTP_GROUP=$DEFAULT_HTTP_GROUP fi +# Check PHP Version +# +if [[ "$PHP_ENGINE" = "FPM" ]] ; then + if [[ -z "$PHP_VERSION" ]] ; then + if [[ -z "$VHOST_CONFIG_FILE" ]] ; then + if $NGINX_IS_ENABLED ; then + + if [[ -f "/etc/nginx/sites-enabled/${WEBSITE}.conf" ]] ; then + VHOST_CONFIG_FILE="$(realpath "/etc/nginx/sites-enabled/${WEBSITE}.conf")" + fi + + elif $APACHE2_IS_ENABLED ; then + + if [[ -f "/usr/local/apache2/conf/vhosts/${WEBSITE}.conf.php-fpm" ]] ; then + VHOST_CONFIG_FILE="/usr/local/apache2/conf/vhosts/${WEBSITE}.conf.php-fpm" + elif [[ -f "/usr/local/apache2/conf/vhosts/${WEBSITE}.conf" ]] ; then + VHOST_CONFIG_FILE="/usr/local/apache2/conf/vhosts/${WEBSITE}.conf" + elif [[ -f "/etc/apache2/sites-enabled/${WEBSITE}.conf" ]] ; then + VHOST_CONFIG_FILE="/etc/apache2/sites-enabled/${WEBSITE}.conf" + fi + + fi + fi + if [[ -n "$VHOST_CONFIG_FILE" ]] ; then + PHP_VERSION="$(grep -o -E "^[^#]*php-?[[:digit:]]{1,2}\.[[:digit:]]{1}-fpm" $VHOST_CONFIG_FILE \ + | grep -o -E "[[:digit:]]{1,2}\.[[:digit:]]{1}")" + fi + if [[ -z "$PHP_VERSION" ]] ; then + + warn "The PHP version was not specified and cannot be determined!" + + main_version_regex="^[[:digit:]]{1,2}\.[[:digit:]]{1}$" + + echo "" + echo -e "\033[32m--\033[m" + echo "" + echo "Enter the PHP main version, e.g. 7.4 or 8.0 .." + echo "" + echo "" + PHP_VERSION= + while [ "X$PHP_VERSION" = "X" ] + do + echononl " PHP main version: " + read PHP_VERSION + if [ "X$PHP_VERSION" = "X" ]; then + echo "" + echo -e "\033[33m\033[1mInput is required !!\033[m" + echo "" + fi + if [[ ! $PHP_VERSION =~ $main_version_regex ]] ; then + echo "" + echo -e "\033[33m\033[1mWrong entry (${PHP_VERSION}) for main PHP version !!\033[m" + echo "" + PHP_VERSION= + fi + + done + + + fi + fi +fi + CURRENT_INSTALL_DIR=`realpath ${WEB_BASE_DIR}/nextcloud` CURRENT_DATA_DIR=`realpath ${WEB_BASE_DIR}/data` CURRENT_VERSION=`basename $CURRENT_INSTALL_DIR | cut -d"-" -f2` @@ -446,7 +588,7 @@ echo -e "\033[37m\033[1mMain part of the script\033[m" echo "" -# - Create missing indices +# - Add user '$USER' to group '$GROUP' # - echononl " Add user \033[37m\033[1m$USER\033[m to group \033[37m\033[1m$GROUP\033[m .." diff --git a/remove_user_from_group.sh b/remove_user_from_group.sh index 42f945a..e3010ed 100755 --- a/remove_user_from_group.sh +++ b/remove_user_from_group.sh @@ -19,6 +19,22 @@ backup_date=$(date +%Y-%m-%d-%H%M) clean_up() { + + if [[ -f "$_backup_crontab_file" ]]; then + + echononl "(Re)Install previously saved crontab from '$_backup_crontab_file'.." + + crontab $_backup_crontab_file >> $log_file 2>&1 + + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $log_file)" + fi + + fi + # Perform program exit housekeeping rm -f $log_file blank_line @@ -123,6 +139,35 @@ info (){ echo "" } +detect_os_1 () { + + if $(which lsb_release > /dev/null 2>&1) ; then + + os_dist="$(lsb_release -i | awk '{print tolower($3)}')" + os_version="$(lsb_release -r | awk '{print tolower($2)}')" + os_codename="$(lsb_release -c | awk '{print tolower($2)}')" + + if [[ "$os_dist" = "debian" ]]; then + if $(echo "$os_version" | grep -q '\.') ; then + os_version=$(echo "$os_version" | cut --delimiter='.' -f1) + fi + fi + + elif [[ -e "/etc/os-release" ]]; then + + . /etc/os-release + + os_dist=$ID + os_version=${VERSION_ID} + + fi + + # remove whitespace from os_dist and os_version + os_dist="${os_dist// /}" + os_version="${os_version// /}" + +} + # - Remove leading/trailling whitespaces # - trim() { @@ -197,8 +242,6 @@ fi #clear -echo "" -echo "" echo "" echo -e "\033[32m-----\033[m" echo -e "Remove User from Group - \033[1mBoth, User and Group has to exist\033[m" @@ -223,6 +266,7 @@ read _IN if is_number "$_IN" && [[ -n ${website_arr[$_IN]} ]]; then IFS=':' read -a _arr <<< ${website_arr[$_IN]} conf_file=${_arr[1]} + DEFAULT_WEBSITE="${_arr[0]}" _OK=true else echo "" @@ -245,11 +289,12 @@ fi echo "" + # ============= # --- Some checks # ============= -DEFAULT_WEB_BASE_DIR="/var/www/$WEBSITE" +DEFAULT_SRC_BASE_DIR="/usr/local/src/nextcloud" DEFAULT_HTTP_USER="www-data" DEFAULT_HTTP_GROUP="www-data" DEFAULT_PHP_ENGINE="FPM" @@ -269,26 +314,6 @@ fi [[ -n "$PHP_ENGINE" ]] || PHP_ENGINE=$DEFAULT_PHP_ENGINE -# Check PHP Version -# -if [[ "$PHP_ENGINE" = "FPM" ]] ; then - if [[ -z "$PHP_VERSION" ]] ; then - if [[ -z "$VHOST_CONFIG_FILE" ]] ; then - if [[ -f "/usr/local/apache2/conf/vhosts/${WEBSITE}.conf.php-fpm" ]] ; then - VHOST_CONFIG_FILE="/usr/local/apache2/conf/vhosts/${WEBSITE}.conf.php-fpm" - elif [[ -f "/usr/local/apache2/conf/vhosts/${WEBSITE}.conf" ]] ; then - VHOST_CONFIG_FILE="/usr/local/apache2/conf/vhosts/${WEBSITE}.conf" - elif [[ -f "/etc/apache2/sites-enabled/${WEBSITE}.conf" ]] ; then - VHOST_CONFIG_FILE="/etc/apache2/sites-enabled/${WEBSITE}.conf" - fi - fi - PHP_VERSION="$(grep -o -E "php-?.{1}\..{1}-fpm" $VHOST_CONFIG_FILE | grep -o -E ".{1}\..{1}")" - if [[ -z "$PHP_VERSION" ]] ; then - fatal "PHP Version must be givven if running PHP-FPM engine (parameter 'PHP_VERSION')" - fi - fi -fi - if [[ "$DATABASE_TYPE" != "postgres" ]] && [[ "$DATABASE_TYPE" != "mysql" ]]; then fatal "Wrong or missing database type (parameter 'DATABASE_TYPE')" fi @@ -307,32 +332,86 @@ if [[ "$DATABASE_TYPE" = "postgres" ]]; then fi fi -# - Determin user/group of the webserver -# - -httpd_binary="`which httpd`" -if [ -z "$httpd_binary" ]; then - httpd_binary="$(ps -axu | grep httpd | grep -e "^root" | grep -v grep | awk '{print$11}')" - if [ -z "$httpd_binary" ]; then - if [ -x "/usr/local/apache2/bin/httpd" ]; then - httpd_binary="/usr/local/apache2/bin/httpd" +NGINX_IS_ENABLED=false +APACHE2_IS_ENABLED=false + +# Check if NGINX webserver is ctive +# +if $(systemctl -q is-enabled nginx 2> /dev/null) ; then + + NGINX_IS_ENABLED=true + + # - Determin user of the webserver + # - + nginx_binary="$(which nginx)" + if [[ -z "$nginx_binary" ]] ; then + nginx_binary="$(ps -axu | grep -E "nginx:.*master" | grep -v grep | grep -o -E "\S+/nginx")" + if [[ -z "$nginx_binary" ]] ; then + if [[ -x "/usr/local/nginx/bin/nginx" ]]; then + nginx_binary="/usr/local/nginx/bin/nginx" + elif [[ -x "/usr/local/nginx/sbin/nginx" ]]; then + nginx_binary="/usr/local/nginx/sbin/nginx" + fi fi fi -fi -if [ -x "$httpd_binary" ];then - # - Determin websever user - # - - _HTTP_USER="`$httpd_binary -t -D DUMP_RUN_CFG | grep -i -e "^User" | awk '{print$2}' | cut -d\"=\" -f2 | tr -d '"'`" - _HTTP_GROUP="`$httpd_binary -t -D DUMP_RUN_CFG | grep -i -e "^Group" | awk '{print$2}' | cut -d\"=\" -f2 | tr -d '"'`" + if [[ -x "$nginx_binary" ]] ; then + _HTTP_USER="$($nginx_binary -T 2> /dev/null | grep -E "^\s*user\s+\S+;" | grep -o -E "\S+;$" | sed -e 's/;$//')" + fi # - Is webserver running ? # - - PID=$(ps aux | grep "$(realpath $httpd_binary)" | grep -e "^root" | grep -v grep | awk '{print$2}') + PID=$(ps aux | grep "$(realpath $nginx_binary)" | grep -e "^root" | grep -v grep | awk '{print$2}') if [[ "X${PID}X" = "XX" ]] ;then IS_HTTPD_RUNNING=false else IS_HTTPD_RUNNING=true fi + + +elif $(systemctl -q is-enabled apache2 2> /dev/null) ; then + + APACHE2_IS_ENABLED=true + + # - Determin user of the webserver + # - + httpd_binary="`which httpd`" + if [ -z "$httpd_binary" ]; then + httpd_binary="$(ps -axu | grep httpd | grep -e "^root" | grep -v grep | awk '{print$11}')" + if [ -z "$httpd_binary" ]; then + if [ -x "/usr/local/apache2/bin/httpd" ]; then + httpd_binary="/usr/local/apache2/bin/httpd" + fi + fi + fi + if [ -x "$httpd_binary" ];then + + # - Determin websever user + # - + _HTTP_USER="`$httpd_binary -t -D DUMP_RUN_CFG | grep -i -e "^User" | awk '{print$2}' | cut -d\"=\" -f2 | tr -d '"'`" + _HTTP_GROUP="`$httpd_binary -t -D DUMP_RUN_CFG | grep -i -e "^Group" | awk '{print$2}' | cut -d\"=\" -f2 | tr -d '"'`" + + # - Is webserver running ? + # - + PID=$(ps aux | grep "$(realpath $httpd_binary)" | grep -e "^root" | grep -v grep | awk '{print$2}') + if [[ "X${PID}X" = "XX" ]] ;then + IS_HTTPD_RUNNING=false + else + IS_HTTPD_RUNNING=true + fi + fi + +else + error "Neither \033[1mapache2\033[m nor \033[1mnginx\033[m is enabled on this machine" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/nno]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Interrupted by user" fi if [[ -n "$_HTTP_USER" ]] ; then @@ -354,6 +433,69 @@ else [[ -n "$HTTP_GROUP" ]] || HTTP_GROUP=$DEFAULT_HTTP_GROUP fi +# Check PHP Version +# +if [[ "$PHP_ENGINE" = "FPM" ]] ; then + if [[ -z "$PHP_VERSION" ]] ; then + if [[ -z "$VHOST_CONFIG_FILE" ]] ; then + if $NGINX_IS_ENABLED ; then + + if [[ -f "/etc/nginx/sites-enabled/${WEBSITE}.conf" ]] ; then + VHOST_CONFIG_FILE="$(realpath "/etc/nginx/sites-enabled/${WEBSITE}.conf")" + fi + + elif $APACHE2_IS_ENABLED ; then + + if [[ -f "/usr/local/apache2/conf/vhosts/${WEBSITE}.conf.php-fpm" ]] ; then + VHOST_CONFIG_FILE="/usr/local/apache2/conf/vhosts/${WEBSITE}.conf.php-fpm" + elif [[ -f "/usr/local/apache2/conf/vhosts/${WEBSITE}.conf" ]] ; then + VHOST_CONFIG_FILE="/usr/local/apache2/conf/vhosts/${WEBSITE}.conf" + elif [[ -f "/etc/apache2/sites-enabled/${WEBSITE}.conf" ]] ; then + VHOST_CONFIG_FILE="/etc/apache2/sites-enabled/${WEBSITE}.conf" + fi + + fi + fi + if [[ -n "$VHOST_CONFIG_FILE" ]] ; then + PHP_VERSION="$(grep -o -E "^[^#]*php-?[[:digit:]]{1,2}\.[[:digit:]]{1}-fpm" $VHOST_CONFIG_FILE \ + | grep -o -E "[[:digit:]]{1,2}\.[[:digit:]]{1}")" + fi + if [[ -z "$PHP_VERSION" ]] ; then + + warn "The PHP version was not specified and cannot be determined!" + + main_version_regex="^[[:digit:]]{1,2}\.[[:digit:]]{1}$" + + echo "" + echo -e "\033[32m--\033[m" + echo "" + echo "Enter the PHP main version, e.g. 7.4 or 8.0 .." + echo "" + echo "" + PHP_VERSION= + while [ "X$PHP_VERSION" = "X" ] + do + echononl " PHP main version: " + read PHP_VERSION + if [ "X$PHP_VERSION" = "X" ]; then + echo "" + echo -e "\033[33m\033[1mInput is required !!\033[m" + echo "" + fi + if [[ ! $PHP_VERSION =~ $main_version_regex ]] ; then + echo "" + echo -e "\033[33m\033[1mWrong entry (${PHP_VERSION}) for main PHP version !!\033[m" + echo "" + PHP_VERSION= + fi + + done + + + fi + fi +fi + CURRENT_INSTALL_DIR=`realpath ${WEB_BASE_DIR}/nextcloud` CURRENT_DATA_DIR=`realpath ${WEB_BASE_DIR}/data` CURRENT_VERSION=`basename $CURRENT_INSTALL_DIR | cut -d"-" -f2`