From 2592e4b8e3400fea2b040396aa64d61b514ca912 Mon Sep 17 00:00:00 2001 From: Matthew Saunders Brown Date: Thu, 4 May 2023 17:15:27 -0700 Subject: [PATCH] change write flag, defaults to enabled --- bin/vhost-deploy.sh | 23 ++++++++++------------- bin/vhost-mysql-db-add.sh | 10 ++++++++-- bin/vhost-user-add.sh | 17 ++++++++++++----- bin/vhost.sh | 7 ++++++- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/bin/vhost-deploy.sh b/bin/vhost-deploy.sh index e58472a..9a480c6 100755 --- a/bin/vhost-deploy.sh +++ b/bin/vhost-deploy.sh @@ -13,15 +13,15 @@ help() thisfilename=$(basename -- "$0") echo "Add virtualhost to this server, including shell user and MySQL database." echo "" - echo "usage: $thisfilename -d [-u ] [-p ] [-x ] [-j <0|1>] [-w] [-h]" + echo "usage: $thisfilename -d [-u ] [-p ] [-x ] [-j <0|1>] [-w <0|1>] [-h]" echo "" echo " -h Print this help." echo " -d Domain name of VirtualHost to remove." echo " -u Username to use for this virtualhost. Optional, defaults to first 8 alphanumeric characters of virtualhost." echo " -p Password for username. Optional, random password generated if none specified." echo " -x PHP-FPM pm.max_children. Optional, defaults to 4, recommended range 2-12 on Shared Server." - echo " -j <0|1> Whether or not to jail the user. 0 = no, 1 = yes. Default is 1, which can be overriden in main config." - echo " -w Write user & mysql info to files." + echo " -j <0|1> Whether or not to jail the user. 0 = no, 1 = yes. Default is 1, which can be overridden in main config." + echo " -w <0|1> Write user & mysql info to files. 0 = no, 1 = yes. Default is 1, which can be overridden in main config." exit } @@ -88,6 +88,11 @@ if [[ -z "$username" ]]; then fi fi +# check for and set write option +if [[ -z $write ]]; then + write=$WRITE_INFO +fi + if ! grep -q "^$username:" /etc/passwd; then # check for and set password if [[ -z "$password" ]]; then @@ -98,11 +103,7 @@ if ! grep -q "^$username:" /etc/passwd; then fpmmax=$FPM_MAX fi # add user - if [[ -n $write ]]; then - /usr/local/bin/vhost-user-add.sh -u $username -p "$password" -x $fpmmax -w - else - /usr/local/bin/vhost-user-add.sh -u $username -p "$password" -x $fpmmax - fi + /usr/local/bin/vhost-user-add.sh -u $username -p "$password" -x $fpmmax -w $write # check for and set jail option if [[ -z $jail ]]; then jail=$JAIL_USER @@ -120,8 +121,4 @@ fi /usr/local/bin/vhost-add.sh -d $domain -u $username > /dev/null 2>&1 # add mysql database -if [[ -n $write ]]; then - /usr/local/bin/vhost-mysql-db-add.sh -d $domain -w > /dev/null 2>&1 -else - /usr/local/bin/vhost-mysql-db-add.sh -d $domain > /dev/null 2>&1 -fi +/usr/local/bin/vhost-mysql-db-add.sh -d $domain -w $write> /dev/null 2>&1 diff --git a/bin/vhost-mysql-db-add.sh b/bin/vhost-mysql-db-add.sh index 8bb868a..f4a1931 100755 --- a/bin/vhost-mysql-db-add.sh +++ b/bin/vhost-mysql-db-add.sh @@ -19,7 +19,8 @@ help() echo " -d Domain name of VirtualHost to add db for." echo " -u Username for accessing the database. Optional, autogenerated if none specified." echo " -p Password for username. Optional, random password generated if none specified." - echo " -w Write db info to /srv/www/domain/.my.cnf and create include in /home/username/.my.cnf." + echo " -w <0|1> Write db info to /srv/www/domain/.my.cnf and create include in /home/username/.my.cnf." + echo " 0 = no, 1 = yes. Default is 1, which can be overridden in main config." echo " -v Verbose - output newly created db info to console." echo "" echo " MySQL database names is based on virtualhost with . replaced by the word 'dot'" @@ -54,6 +55,11 @@ if [[ -d /var/lib/mysql/$database ]]; then exit 1 fi +# check for and set write option +if [[ -z $write ]]; then + write=$WRITE_INFO +fi + # get & set username of virtualhost vhost_username=$(stat -c '%U' /srv/www/$domain) @@ -73,7 +79,7 @@ mysql -e "GRANT ALL PRIVILEGES ON $database.* TO '$username'@'localhost';" mysqladmin flush-privileges # save mysql db info to file -if [[ -n $write ]]; then +if [[ $write == 1 ]]; then touch /srv/www/$domain/.my.cnf chown $vhost_username:$vhost_username /srv/www/$domain/.my.cnf chmod 640 /srv/www/$domain/.my.cnf diff --git a/bin/vhost-user-add.sh b/bin/vhost-user-add.sh index b26dfd4..4bfe299 100755 --- a/bin/vhost-user-add.sh +++ b/bin/vhost-user-add.sh @@ -13,14 +13,14 @@ help() thisfilename=$(basename -- "$0") echo "Add system user to server." echo "" - echo "usage: $thisfilename -u [-p [-i ] [-x ] [-w] [-h]" + echo "usage: $thisfilename -u [-p [-i ] [-x ] [-w <0|1>] [-h]" echo "" echo " -h Print this help." echo " -u System username to add to server." echo " -p Password for username. Optional, random password generated if none specified." echo " -i Numberic User ID to assign to user. Optional, next available uid set if none specified." echo " -x PHP-FPM pm.max_children. Optional, defaults to 4, recommended range 2-12 on Shared Server." - echo " -w Write user info to /home/username/.passwd." + echo " -w <0|1> Write user info to /home/username/.passwd. 0 = no, 1 = yes. Default is 1, which can be overridden in main config." exit } @@ -37,6 +37,11 @@ if [ -z "$password" ]; then password=`/usr/bin/pwgen 12 1` fi +# check for and set write option +if [[ -z $write ]]; then + write=$WRITE_INFO +fi + # get next UID if none specified if [ -z "$uid" ]; then uid=`awk -F: '{uid[$3]=1}END{for(x=1000; x<=65534; x++) {if(uid[x] != ""){}else{print x; exit;}}}' /etc/passwd` @@ -79,14 +84,16 @@ if [[ ! -f "/home/$username/.profile" ]]; then install -o $username -g $username -m 640 /etc/skel/.profile /home/$username fi -if [[ -n $write ]]; then +if [[ $write == 1 ]]; then vhost::set-opensslpass encryptedpass=`echo -n "$password" | openssl aes-256-cbc -a -salt -pass pass:$opensslpass -pbkdf2` userpasswdinfo="$username:$encryptedpass:$uid:$uid::/home/$username:/bin/bash" - if [[ ! -f "/home/$username/.passwd" ]]; then + if [[ -f "/home/$username/.passwd" ]]; then + chmod 640 /home/$username/.passwd + else install -o $username -g $username -m 640 /dev/null /home/$username/.passwd - echo "$userpasswdinfo" > /home/$username/.passwd fi + echo "$userpasswdinfo" > /home/$username/.passwd fi # php-fpm pool diff --git a/bin/vhost.sh b/bin/vhost.sh index 49fa143..78fd863 100755 --- a/bin/vhost.sh +++ b/bin/vhost.sh @@ -14,6 +14,7 @@ fi FPM_MAX=4 JAIL_USER=1 +WRITE_INFO=1 # functions @@ -102,7 +103,11 @@ function vhost:getoptions () { verbose=true ;; w ) # write - store data in file - write=true + write=${OPTARG} + if [[ $write != "0" ]] && [[ $write != "1" ]]; then + echo "ERROR: Invalid write setting: -j $write" + exit 1 + fi ;; x ) # php-fpm pm.max_children fpmmax=${OPTARG}