change write flag, defaults to enabled

This commit is contained in:
Matthew Saunders Brown 2023-05-04 17:15:27 -07:00
parent fa65c5dac2
commit 2592e4b8e3
4 changed files with 36 additions and 21 deletions

View File

@ -13,15 +13,15 @@ help()
thisfilename=$(basename -- "$0") thisfilename=$(basename -- "$0")
echo "Add virtualhost to this server, including shell user and MySQL database." echo "Add virtualhost to this server, including shell user and MySQL database."
echo "" echo ""
echo "usage: $thisfilename -d <domain> [-u <username>] [-p <password>] [-x <fpmmax>] [-j <0|1>] [-w] [-h]" echo "usage: $thisfilename -d <domain> [-u <username>] [-p <password>] [-x <fpmmax>] [-j <0|1>] [-w <0|1>] [-h]"
echo "" echo ""
echo " -h Print this help." echo " -h Print this help."
echo " -d <domain> Domain name of VirtualHost to remove." echo " -d <domain> Domain name of VirtualHost to remove."
echo " -u <username> Username to use for this virtualhost. Optional, defaults to first 8 alphanumeric characters of virtualhost." echo " -u <username> Username to use for this virtualhost. Optional, defaults to first 8 alphanumeric characters of virtualhost."
echo " -p <password> Password for username. Optional, random password generated if none specified." echo " -p <password> Password for username. Optional, random password generated if none specified."
echo " -x <fpmmax> PHP-FPM pm.max_children. Optional, defaults to 4, recommended range 2-12 on Shared Server." echo " -x <fpmmax> 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 " -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 Write user & mysql info to files." 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 exit
} }
@ -88,6 +88,11 @@ if [[ -z "$username" ]]; then
fi fi
fi fi
# check for and set write option
if [[ -z $write ]]; then
write=$WRITE_INFO
fi
if ! grep -q "^$username:" /etc/passwd; then if ! grep -q "^$username:" /etc/passwd; then
# check for and set password # check for and set password
if [[ -z "$password" ]]; then if [[ -z "$password" ]]; then
@ -98,11 +103,7 @@ if ! grep -q "^$username:" /etc/passwd; then
fpmmax=$FPM_MAX fpmmax=$FPM_MAX
fi fi
# add user # add user
if [[ -n $write ]]; then /usr/local/bin/vhost-user-add.sh -u $username -p "$password" -x $fpmmax -w $write
/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
# check for and set jail option # check for and set jail option
if [[ -z $jail ]]; then if [[ -z $jail ]]; then
jail=$JAIL_USER jail=$JAIL_USER
@ -120,8 +121,4 @@ fi
/usr/local/bin/vhost-add.sh -d $domain -u $username > /dev/null 2>&1 /usr/local/bin/vhost-add.sh -d $domain -u $username > /dev/null 2>&1
# add mysql database # add mysql database
if [[ -n $write ]]; then /usr/local/bin/vhost-mysql-db-add.sh -d $domain -w $write> /dev/null 2>&1
/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

View File

@ -19,7 +19,8 @@ help()
echo " -d <domain> Domain name of VirtualHost to add db for." echo " -d <domain> Domain name of VirtualHost to add db for."
echo " -u <username> Username for accessing the database. Optional, autogenerated if none specified." echo " -u <username> Username for accessing the database. Optional, autogenerated if none specified."
echo " -p <password> Password for username. Optional, random password generated if none specified." echo " -p <password> 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 " -v Verbose - output newly created db info to console."
echo "" echo ""
echo " MySQL database names is based on virtualhost with . replaced by the word 'dot'" 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 exit 1
fi fi
# check for and set write option
if [[ -z $write ]]; then
write=$WRITE_INFO
fi
# get & set username of virtualhost # get & set username of virtualhost
vhost_username=$(stat -c '%U' /srv/www/$domain) 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 mysqladmin flush-privileges
# save mysql db info to file # save mysql db info to file
if [[ -n $write ]]; then if [[ $write == 1 ]]; then
touch /srv/www/$domain/.my.cnf touch /srv/www/$domain/.my.cnf
chown $vhost_username:$vhost_username /srv/www/$domain/.my.cnf chown $vhost_username:$vhost_username /srv/www/$domain/.my.cnf
chmod 640 /srv/www/$domain/.my.cnf chmod 640 /srv/www/$domain/.my.cnf

View File

@ -13,14 +13,14 @@ help()
thisfilename=$(basename -- "$0") thisfilename=$(basename -- "$0")
echo "Add system user to server." echo "Add system user to server."
echo "" echo ""
echo "usage: $thisfilename -u <username> [-p <password> [-i <uid>] [-x <fpmmax>] [-w] [-h]" echo "usage: $thisfilename -u <username> [-p <password> [-i <uid>] [-x <fpmmax>] [-w <0|1>] [-h]"
echo "" echo ""
echo " -h Print this help." echo " -h Print this help."
echo " -u <username> System username to add to server." echo " -u <username> System username to add to server."
echo " -p <password> Password for username. Optional, random password generated if none specified." echo " -p <password> Password for username. Optional, random password generated if none specified."
echo " -i <uid> Numberic User ID to assign to user. Optional, next available uid set if none specified." echo " -i <uid> Numberic User ID to assign to user. Optional, next available uid set if none specified."
echo " -x <fpmmax> PHP-FPM pm.max_children. Optional, defaults to 4, recommended range 2-12 on Shared Server." echo " -x <fpmmax> 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 exit
} }
@ -37,6 +37,11 @@ if [ -z "$password" ]; then
password=`/usr/bin/pwgen 12 1` password=`/usr/bin/pwgen 12 1`
fi fi
# check for and set write option
if [[ -z $write ]]; then
write=$WRITE_INFO
fi
# get next UID if none specified # get next UID if none specified
if [ -z "$uid" ]; then 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` 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 install -o $username -g $username -m 640 /etc/skel/.profile /home/$username
fi fi
if [[ -n $write ]]; then if [[ $write == 1 ]]; then
vhost::set-opensslpass vhost::set-opensslpass
encryptedpass=`echo -n "$password" | openssl aes-256-cbc -a -salt -pass pass:$opensslpass -pbkdf2` encryptedpass=`echo -n "$password" | openssl aes-256-cbc -a -salt -pass pass:$opensslpass -pbkdf2`
userpasswdinfo="$username:$encryptedpass:$uid:$uid::/home/$username:/bin/bash" 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 install -o $username -g $username -m 640 /dev/null /home/$username/.passwd
echo "$userpasswdinfo" > /home/$username/.passwd
fi fi
echo "$userpasswdinfo" > /home/$username/.passwd
fi fi
# php-fpm pool # php-fpm pool

View File

@ -14,6 +14,7 @@ fi
FPM_MAX=4 FPM_MAX=4
JAIL_USER=1 JAIL_USER=1
WRITE_INFO=1
# functions # functions
@ -102,7 +103,11 @@ function vhost:getoptions () {
verbose=true verbose=true
;; ;;
w ) # write - store data in file 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 x ) # php-fpm pm.max_children
fpmmax=${OPTARG} fpmmax=${OPTARG}