HEX
Server: nginx/1.29.3
System: Linux 11979.bigscoots-wpo.com 6.8.0-88-generic #89-Ubuntu SMP PREEMPT_DYNAMIC Sat Oct 11 01:02:46 UTC 2025 x86_64
User: nginx (1068)
PHP: 7.4.33
Disabled: exec,system,passthru,shell_exec,proc_open,proc_close,popen,show_source,cmd# Do not modify this line # 1684243876
Upload Files
File: //bigscoots/wpo/cloudflare/cfent_remove.sh
#!/bin/bash

source /bigscoots/includes/common.sh
source /root/.bigscoots/.cf

# Usage information
usage() {
    echo "Usage: $0 --domain DOMAIN --ip IP [--email EMAIL] [--apikey APIKEY] [--zoneid ZONEID]"
    echo "Mandatory options:"
    echo "  --domain DOMAIN    Domain name to update DNS records for"
    echo "  --ip IP            IP address to set for the DNS records"
    echo "Optional options:"
    echo "  --email EMAIL      Cloudflare email address"
    echo "  --apikey APIKEY    Cloudflare API key"
    echo "  --zoneid ZONEID    Cloudflare zone ID"
    exit 0
}

DOMAIN=""
IP=""
DOMAIN_CF_EMAIL=""
DOMAIN_CF_APIKEY=""
DOMAIN_CF_ZONEID=""

# Parse command line arguments
while [[ $# -gt 0 ]]
do
    key="$1"
    case $key in
        --domain)
            DOMAIN="$2"
            shift 2
            ;;
        --ip)
            IP="$2"
            shift 2
            ;;
        --email)
            DOMAIN_CF_EMAIL="$2"
            shift 2
            ;;
        --apikey)
            DOMAIN_CF_APIKEY="$2"
            shift 2
            ;;
        --zoneid)
            DOMAIN_CF_ZONEID="$2"
            shift 2
            ;;
        *) 
            usage
            ;;
    esac
done

ENTZONE=c2d79b78db1f915729e6a48f8bd6b0c3
CFAPIURL=https://api.cloudflare.com/client/v4

# Check that mandatory options are present
if [ -z "$DOMAIN" ]
then
    usage
    exit 0
fi

if [ -z "$DOMAIN_CF_EMAIL" ] || [ -z "$DOMAIN_CF_APIKEY" ]
then
    send_slack_alert "#wpo-alerts" ":warning:" "Remove Cloudflare Enterprise: cfent_remove.sh" "$hostname" "Cloudflare Email and API was not given so we are not removing / adjusting DNS records in customers Cloudflare account, only our CLoudflare Enterprise account."
fi

function get_pkey() {
    local domain=$1
    local file_path="/root/cfplugin/${domain}.info"

    if [ ! -f "$file_path" ]
    then
    	send_slack_alert "#wpo-alerts" ":warning:" "Function: \`get_pkey\`" "$hostname" "Domain info file does not exist, unable to get pkey. \`\`\`file: $file_path\`\`\`"
        return 1
    fi

    local json_content=$(awk '/{/,/}/' "$file_path")

    if ! pkey=$(echo "$json_content" | jq -r '.bs_p_key')
    then
    	send_slack_alert "#wpo-alerts" ":warning:" "Function: \`get_pkey\`" "$hostname" "Failed to parse JSON content, unable to get pkey. \`\`\`file: $file_path\`\`\`"
    fi

    echo "$pkey"
}

function get_bs_site_id() {
    local domain=$1
    local file_path="/root/cfplugin/${domain}.info"

    [[ $enable_get_cfe_vars == "false" ]] && return 1

    if [ ! -f "$file_path" ]
    then
    	send_slack_alert "#wpo-alerts" ":warning:" "Function: \`get_bs_site_id\`" "$hostname" "Domain info file does not exist, unable to get bs_site_id. \`\`\`file: $file_path\`\`\`"
    fi

    local json_content=$(awk '/{/,/}/' "$file_path")

    if ! bs_site_id=$(echo "$json_content" | jq -r '.bs_site_id')
    then
    	send_slack_alert "#wpo-alerts" ":warning:" "Function: \`get_bs_site_id\`" "$hostname" "Failed to parse JSON content, unable to get bs_site_id. \`\`\`file: $file_path\`\`\`"
    fi

    echo "$bs_site_id"
}

function remove_bscache() {
	local PKEY=$(get_pkey $DOMAIN)

	if [ -z "$DOMAIN_CF_EMAIL" ] || [ -z "$DOMAIN_CF_APIKEY" ]
	then
	    return 1
	fi

    # Check if get_pkey returned an error (JSON string containing "fail")
    if [[ $PKEY == *'"status":"fail"'* ]]
    then
        ssh "${SSH_OPTIONS[@]}" -p 2222 "$IP" "bash /bigscoots/wpo/cloudflare/bscache.sh remove_cfe --domain '$DOMAIN' --cfuser '$DOMAIN_CF_EMAIL' --cfapikey '$DOMAIN_CF_APIKEY' --cfzoneid '$DOMAIN_CF_ZONEID'"
    else
		ssh "${SSH_OPTIONS[@]}" -p 2222 "$IP" "bash /bigscoots/wpo/cloudflare/bscache.sh remove_cfe --domain '$DOMAIN' --cfuser '$DOMAIN_CF_EMAIL' --cfapikey '$DOMAIN_CF_APIKEY' --cfzoneid '$DOMAIN_CF_ZONEID' --pkey '$PKEY'"
    fi
}

function remove_cfe_custom_hostname() {
    local api_url="https://main.bigscoots.com/cf-custom-hostnames/"
    local content_type="Content-Type: application/json"
    local security_header="x-bigscoots-user: webmaster"
    local step3_file_path="/root/cfplugin/${DOMAIN}.step3.api"
    local hostname="$CFAPI_CANONICALDOMAIN"
    local custom_host_name_id

    if [ ! -f "$step3_file_path" ]
    then
        send_slack_alert "#wpo-alerts" ":warning:" "Function: \`remove_cfe_custom_hostname\`" "$hostname" "File does not exist, unable to remove custom hostname. \`\`\`file: $step3_file_path\`\`\`"
        return 1
    fi

    custom_host_name_id=$(cat "$step3_file_path" | jq -r ".result.id")

    if [ -z "$custom_host_name_id" ] || [ "$custom_host_name_id" == "null" ] 
    then
        send_slack_alert "#wpo-alerts" ":warning:" "Function: \`remove_cfe_custom_hostname\`" "$hostname" "Custom Hostname ID is empty, unable to remove custom hostname. \`\`\`file: $step3_file_path\`\`\`"
        return 1
    fi

    if [ -z "$hostname" ] || [ "$hostname" == "null" ] 
    then
        send_slack_alert "#wpo-alerts" ":warning:" "Function: \`remove_cfe_custom_hostname\`" "$hostname" "Unable to get canonical domain. \`\`\`file: $step3_file_path\`\`\`"
        return 1
    fi

  local data='{
    "action": "remove_record",
    "hostname": "'"$hostname"'"
  }'
  
  local response=$(curl -s --header "$content_type" --header "$security_header" -X POST --data "$data" "$api_url")

  if [[ $response == *"\"success\": false"* ]]
  then
    send_slack_alert "#wpo-alerts" ":warning:" "Function: \`update_master_db\`" "$hostname" "$response"
  fi


    curl -s --request DELETE \
    --url "${CFAPIURL}/zones/${ENTZONE}/custom_hostnames/${custom_host_name_id}" \
    -H "Authorization: Bearer ${CFAPITOKEN}" \
    -H "Content-Type: application/json"

}

function remove_cfe_custom_hostname_dnsrecord() {
    local record_id=$(get_cfe_dns_record_id)

	if [ -z "$record_id" ] || [ "$record_id" == "null" ]
	then
	    send_slack_alert "#wpo-alerts" ":warning:" "Function: \`remove_cfe_custom_hostname_dnsrecord\`" "$hostname" "Unable to get the DNS Record ID from \`get_cf_dns_record_id\`"
	    return 1
	fi

    curl -s --request DELETE \
    --url "${CFAPIURL}/zones/${ENTZONE}/dns_records/${record_id}" \
    -H "Authorization: Bearer ${CFAPITOKEN}" \
    -H 'Content-Type: application/json'

}

get_cf_zone_id() {
    local response
    response=$(curl -s -X GET \
		       --url "${CFAPIURL}/zones?name=${DOMAIN}" \
		       -H "X-Auth-Email: $DOMAIN_CF_EMAIL" \
		       -H "X-Auth-Key: $DOMAIN_CF_APIKEY" \
		       -H "Content-Type: application/json")

    if [[ $? -ne 0 ]] || [[ -z "$response" ]] || [ "$response" == "null" ]
    then
        send_slack_alert "#wpo-alerts" ":warning:" "Function: \`get_cf_zone_id\`" "$hostname" "Failed to retrieve data from Cloudflare API for domain: ${DOMAIN}"
        return 1
    fi

    local zone_id
    zone_id=$(echo "$response" | jq -r '.result[0].id')

    if [[ -z "$zone_id" ]] || [[ "$zone_id" == "null" ]]
    then
        send_slack_alert "#wpo-alerts" ":warning:" "Function: \`get_cf_zone_id\`" "$hostname" "Unable to parse zone ID for domain: ${DOMAIN}"
        return 1
    fi

    echo "$zone_id"
}

function get_cfe_vars() {
    local domain_info_file="/root/cfplugin/${DOMAIN}.info"
    enable_get_cfe_vars=false

    if [ ! -f "$domain_info_file" ]
    then
        send_slack_alert "#wpo-alerts" ":warning:" "Function: \`get_cfe_custom_hostname\`" "$hostname" "Info file does not exist, unable to gather CFAPI details. \`\`\`file: $domain_info_file\`\`\`"
        return 1
    fi

    CFAPI_CANONICALDOMAIN=$(grep 'CFAPI Canonical Domain' "$domain_info_file" | awk '{print $4}')
    CFAPI_CUSTOMHOSTNAME=$(grep 'CFAPI Custom Hostname' "$domain_info_file" | awk '{print $4}')

    if [ -z "$CFAPI_CANONICALDOMAIN" ]
    then
        send_slack_alert "#wpo-alerts" ":warning:" "Function: \`get_cfe_custom_hostname\`" "$hostname" "One or more CFAPI details are missing in the info file. \`\`\`file: $domain_info_file\`\`\`"
        return 1
    fi

    enable_get_cfe_vars=true
}


function update_cf_dns_record() {
	local domain_info_file="/root/cfplugin/${DOMAIN}.info"

	if [ -z "$DOMAIN_CF_EMAIL" ] || [ -z "$DOMAIN_CF_APIKEY" ] || [ -z "$IP" ] 
	then
	    return 1
	fi

    if [ ! -f "$domain_info_file" ]
    then
        send_slack_alert "#wpo-alerts" ":warning:" "Function: \`update_cf_dns_record\`" "$hostname" "Info file does not exist, unable to gather CFAPI details. \`\`\`file: $domain_info_file\`\`\`"
        return 1
    fi

    if [ -z "$CFAPI_CANONICALDOMAIN" ]
    then
        send_slack_alert "#wpo-alerts" ":warning:" "Function: \`update_cf_dns_record\`" "$hostname" "One or more CFAPI details are missing in the info file. \`\`\`file: $domain_info_file\`\`\`"
        return 1
    fi

  response=$(curl --silent  --location "${CFAPIURL}/zones/$DOMAIN_CF_ZONEID/dns_records?name=$CFAPI_CANONICALDOMAIN" \
			 --header "X-Auth-Email: $DOMAIN_CF_EMAIL" \
			 --header "X-Auth-Key: $DOMAIN_CF_APIKEY" \
			 --header 'Content-Type: application/json')

  record_id=$(echo "$response" | jq -r '.result[0].id')
  record_type=$(echo "$response" | jq -r '.result[0].type')

  if [ "$record_type" == "CNAME" ]
  then
    # Delete the CNAME record
    curl --silent --location --request DELETE "${CFAPIURL}/zones/$DOMAIN_CF_ZONEID/dns_records/$record_id" \
    --header "X-Auth-Email: $DOMAIN_CF_EMAIL" \
    --header "X-Auth-Key: $DOMAIN_CF_APIKEY" \
    --header 'Content-Type: application/json'

    # Add a new A record with the IP from the $IP variable
    curl --silent  --location --request POST "${CFAPIURL}/zones/$DOMAIN_CF_ZONEID/dns_records" \
    --header "X-Auth-Email: $DOMAIN_CF_EMAIL" \
    --header "X-Auth-Key: $DOMAIN_CF_APIKEY" \
    --header 'Content-Type: application/json' \
    --data '{
      "type": "A",
      "name": "'"$CFAPI_CANONICALDOMAIN"'",
      "content": "'"$IP"'",
      "ttl": 120,
      "proxied": true
    }'
  fi
}

function get_cf_dns_record_id() {

    [[ $enable_get_cfe_vars == "false" ]] && return 1

    curl -s --location \
    --url "${CFAPIURL}/zones/$DOMAIN_CF_ZONEID/dns_records?name=$CFAPI_CANONICALDOMAIN" \
    -H "X-Auth-Key: $DOMAIN_CF_APIKEY" \
    -H "X-Auth-Email: $DOMAIN_CF_EMAIL" \
    -H "Content-Type: application/json" | jq -r '.result[0].id'
}

function get_cfe_dns_record_id() {

    [[ $enable_get_cfe_vars == "false" ]] && return 1

    curl -s --location \
    --url https://api.cloudflare.com/client/v4/zones/${ENTZONE}/dns_records?name=${CFAPI_CUSTOMHOSTNAME} \
    -H "Authorization: Bearer ${CFAPITOKEN}" \
    -H "Content-Type: application/json" | jq -r '.result[0].id'
}

function remove_site_id_json() {
	local bs_site_id=$(get_bs_site_id $DOMAIN)

    if [ -z "$CFAPI_CANONICALDOMAIN" ]
    then
        send_slack_alert "#wpo-alerts" ":warning:" "Function: \`get_cfe_custom_hostname\`" "$hostname" "One or more CFAPI details are missing in the info file. \`\`\`file: $domain_info_file\`\`\`"
        return 1
    else
        ssh "${SSH_OPTIONS[@]}" -p 2222 [email protected] "rm -f /var/www/main.bigscoots.com/site-id/$bs_site_id"
    fi

    cfdir="/root/cfplugin/"
    cffiles=("$DOMAIN.info" "$DOMAIN.step1.api" "$DOMAIN.step2.api" "$DOMAIN.step3.api")

    for cffile in "${cffiles[@]}"
    do
        if [ -e "$cfdir$cffile" ]
        then
            rm -f "$cfdir$cffile"
        fi
    done
}

disable_performance_plan() {
  local api_url="https://main.bigscoots.com/bscache-usage-log/api/"
  local content_type="Content-Type: application/json"
  local security_header="x-bigscoots-user: webmaster"
  local hostname="$CFAPI_CANONICALDOMAIN"
  local server_hostname="$(hostname)"
  local data='{
    "action": "remove_plan",
    "hostname": "'"$hostname"'",
    "plan": "performance+"
  }'

    local response=$(curl -s --header "$content_type" --header "$security_header" --data "$data" "$api_url")

  if [[ $response == *"\"success\": false"* ]]
  then
    send_slack_alert "#wpo-alerts" ":warning:" "Function: \`disable_performance_plan\`" "$hostname" "$response"
  fi
}

if [[ -z "$DOMAIN_CF_ZONEID" && -n "$DOMAIN_CF_EMAIL" && -n "$DOMAIN_CF_APIKEY" ]]
then
    DOMAIN_CF_ZONEID=$(get_cf_zone_id)
fi

validate_domain "$DOMAIN"
[ -n "$IP" ] && remove_bscache "$DOMAIN"
get_cfe_vars
update_cf_dns_record
remove_cfe_custom_hostname
remove_cfe_custom_hostname_dnsrecord
remove_site_id_json
disable_performance_plan