mirror of
https://github.com/myvesta/vesta
synced 2025-07-30 19:40:56 -07:00
201 lines
7.4 KiB
Bash
201 lines
7.4 KiB
Bash
#!/bin/bash
|
|
# info: interactively delete or change WordPress admin passwords for a given domain
|
|
# options: DOMAIN
|
|
#
|
|
# d → delete user (with content reassignment)
|
|
# c → change password (random 10-char alnum)
|
|
# s → skip
|
|
# x → exit
|
|
|
|
#----------------------------------------------------------#
|
|
# Variable & Function #
|
|
#----------------------------------------------------------#
|
|
|
|
[ "$(whoami)" != "root" ] && { echo "You must be root to run this command."; exit 1; }
|
|
source /etc/profile
|
|
|
|
DOMAIN="$1"
|
|
[ -z "$DOMAIN" ] && { echo "Usage: v-change-wp-admins-pass DOMAIN"; exit 1; }
|
|
|
|
USER="$(/usr/local/vesta/bin/v-search-domain-owner "$DOMAIN")"
|
|
[ -z "$USER" ] && { echo "Domain $DOMAIN does not exist."; exit 1; }
|
|
|
|
WP_PATH="/home/$USER/web/$DOMAIN/public_html"
|
|
[ ! -f "$WP_PATH/wp-config.php" ] && { echo "WordPress is not installed on this domain."; exit 1; }
|
|
|
|
# WP-CLI wrapper
|
|
if [ ! -z "$PHP" ]; then
|
|
WP_RUN="PHP=$PHP /usr/local/vesta/bin/v-run-wp-cli $DOMAIN --skip-plugins --skip-themes"
|
|
else
|
|
WP_RUN="/usr/local/vesta/bin/v-run-wp-cli $DOMAIN --skip-plugins --skip-themes"
|
|
fi
|
|
|
|
# random 10-char password
|
|
gen_pass() { tr -dc 'A-Za-z0-9' </dev/urandom | head -c 10; }
|
|
|
|
#----------------------------------------------------------#
|
|
# Action #
|
|
#----------------------------------------------------------#
|
|
|
|
cd "$WP_PATH" || exit 1
|
|
echo
|
|
echo "WordPress administrators for $DOMAIN:"
|
|
echo "-------------------------------------"
|
|
|
|
if [ -f /home/$USER/web/$DOMAIN/wp-admin-password-change.txt ]; then
|
|
rm /home/$USER/web/$DOMAIN/wp-admin-password-change.txt
|
|
fi
|
|
|
|
RUN="$WP_RUN user list --role=administrator --fields=ID,user_login,user_email --format=csv --skip-plugins --skip-themes"
|
|
ADMIN_LIST_CSV=$(eval "$RUN")
|
|
|
|
return_code=$?
|
|
|
|
if [ $return_code -ne 0 ]; then
|
|
echo "WP-CLI error:"
|
|
echo "return code: $return_code"
|
|
cat /home/$USER/web/$DOMAIN/wp-cli-error.log
|
|
exit $return_code
|
|
fi
|
|
|
|
ADMIN_LIST_CSV=$(echo "$ADMIN_LIST_CSV" | tail -n +2)
|
|
|
|
[ -z "$ADMIN_LIST_CSV" ] && { echo "No administrator accounts found."; exit 0; }
|
|
|
|
DEFAULT_USER=""
|
|
|
|
printf "%-6s %-20s %s\n" "ID" "Username" "Email"
|
|
while IFS=',' read -r PID PLOGIN PEMAIL; do
|
|
printf "%-6s %-20s %s\n" "$PID" "$PLOGIN" "$PEMAIL"
|
|
if [ "$PID" = "1" ]; then
|
|
DEFAULT_USER="$PLOGIN"
|
|
fi
|
|
done <<< "$ADMIN_LIST_CSV"
|
|
|
|
echo
|
|
echo "For each admin choose: (d) delete, (c) change password, (s) skip, (x) exit."
|
|
|
|
# interactive loop
|
|
while IFS=',' read -r ID LOGIN EMAIL; do
|
|
[ -n "$EMAIL" ] && TARGET="$LOGIN <$EMAIL>" || TARGET="$LOGIN"
|
|
while true; do
|
|
echo "-------------------------------------"
|
|
read -r -p "Action for \"$TARGET\" [d/c/s/x]? " ACT < /dev/tty
|
|
skip=0;
|
|
case "$ACT" in
|
|
[Dd]* )
|
|
# read -r -p "Really DELETE \"$TARGET\" ? (y/n, default: y) " CONF < /dev/tty
|
|
CONF="y"
|
|
if [[ ! "$CONF" =~ ^[Nn]$ ]]; then
|
|
# build an array of OTHER admin usernames
|
|
mapfile -t OTHER_USERS < <(echo "$ADMIN_LIST_CSV" | awk -F',' -v cur="$ID" '$1!=cur {print $2}')
|
|
if [ "${#OTHER_USERS[@]}" -eq 0 ]; then
|
|
echo "Cannot delete the only administrator account."
|
|
break
|
|
fi
|
|
if [ "$DEFAULT_USER" = "" ]; then
|
|
DEFAULT_USER="${OTHER_USERS[0]}"
|
|
fi
|
|
echo "Available admin usernames for reassignment: ${OTHER_USERS[*]}"
|
|
while true; do
|
|
read -r -p "Reassign content to which username? [default: $DEFAULT_USER, s: skip] " REASSIGN < /dev/tty
|
|
REASSIGN=${REASSIGN:-$DEFAULT_USER}
|
|
DEFAULT_USER=$REASSIGN
|
|
if printf '%s\n' "${OTHER_USERS[@]}" | grep -qx "$REASSIGN"; then
|
|
break
|
|
fi
|
|
if [[ "$REASSIGN" =~ ^[Ss]$ ]]; then
|
|
echo "Skipping reassignment."
|
|
skip=1;
|
|
break
|
|
fi
|
|
if [[ "$REASSIGN" =~ ^[0-9]+$ ]]; then
|
|
break
|
|
fi
|
|
echo "Invalid username. Please choose one of: ${OTHER_USERS[*]}"
|
|
done
|
|
if [ $skip -eq 1 ]; then
|
|
break
|
|
fi
|
|
# delete by username, reassign by username
|
|
RUN="$WP_RUN user delete $ID --reassign=$REASSIGN --yes --skip-plugins --skip-themes"
|
|
eval "$RUN"
|
|
if [ $? -eq 0 ]; then
|
|
echo "$TARGET deleted (content reassigned to $REASSIGN)."
|
|
else
|
|
cat /home/$USER/web/$DOMAIN/wp-cli-error.log
|
|
echo "Failed to delete $TARGET."
|
|
fi
|
|
else
|
|
echo "Deletion cancelled."
|
|
fi
|
|
break
|
|
;;
|
|
[Cc]* )
|
|
NEW_PASS=$(gen_pass)
|
|
RUN="$WP_RUN user update $ID --user_pass=$NEW_PASS --skip-plugins --skip-themes"
|
|
eval "$RUN"
|
|
if [ $? -eq 0 ]; then
|
|
echo "Password for username '$TARGET' changed to: $NEW_PASS"
|
|
echo "Password for username '$TARGET' changed to: $NEW_PASS" >> /home/$USER/web/$DOMAIN/wp-admin-password-change.txt
|
|
chown $USER:$USER /home/$USER/web/$DOMAIN/wp-admin-password-change.txt
|
|
chmod 600 /home/$USER/web/$DOMAIN/wp-admin-password-change.txt
|
|
else
|
|
cat /home/$USER/web/$DOMAIN/wp-cli-error.log
|
|
echo "Failed to change password for $TARGET."
|
|
fi
|
|
break
|
|
;;
|
|
[Ss]* )
|
|
echo "Skipping $TARGET."
|
|
break
|
|
;;
|
|
[Xx]* )
|
|
echo "Exiting."
|
|
exit 0
|
|
;;
|
|
* ) echo "Please answer d, c, s, or x." ;;
|
|
esac
|
|
done
|
|
done <<< "$ADMIN_LIST_CSV"
|
|
|
|
if [ -f /home/$USER/web/$DOMAIN/wp-admin-password-change.txt ]; then
|
|
echo ""
|
|
echo ""
|
|
echo "-------------------------------------"
|
|
echo "For website $DOMAIN - new wp-admin passwords have been set."
|
|
echo "-------------------------------------"
|
|
cat /home/$USER/web/$DOMAIN/wp-admin-password-change.txt
|
|
echo "-------------------------------------"
|
|
echo ""
|
|
echo ""
|
|
read -r -p "Do you want to save the new passwords to a file /home/$USER/web/$DOMAIN/wp-admin-password-change.txt ? (y/n, default: n) " SAVE_PASSWORDS < /dev/tty
|
|
if [ -z "$SAVE_PASSWORDS" ]; then
|
|
SAVE_PASSWORDS="n"
|
|
fi
|
|
if [[ $SAVE_PASSWORDS =~ ^[Nn]$ ]]; then
|
|
rm /home/$USER/web/$DOMAIN/wp-admin-password-change.txt
|
|
fi
|
|
fi
|
|
|
|
#----------------------------------------------------------#
|
|
# flush cache and refresh all security salts #
|
|
#----------------------------------------------------------#
|
|
|
|
echo "-------------------------------------"
|
|
echo
|
|
echo "Flushing cache and refreshing salts..."
|
|
|
|
RUN="$WP_RUN cache flush"
|
|
eval "$RUN"
|
|
RUN="$WP_RUN config shuffle-salts WP_CACHE_KEY_SALT --force"
|
|
eval "$RUN"
|
|
RUN="$WP_RUN config shuffle-salts"
|
|
eval "$RUN"
|
|
|
|
echo "Cache flushed and salts refreshed."
|
|
|
|
echo
|
|
echo "Done."
|
|
|
|
exit 0
|