forked from srvrco/getssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns_del_cpanel
executable file
·69 lines (61 loc) · 2.2 KB
/
dns_del_cpanel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env bash
# Need to add your email address and API key to cpanel below or set as env variables
user=${CPANEL_USERNAME:-''}
password=${CPANEL_PASSWORD:-''}
url=${CPANEL_URL:-''} # e.g. https://www.cpanel-host.test:2083
apitoken=${CPANEL_APITOKEN:-''}
fulldomain="${1}"
# Check initial parameters
if [[ -z "$fulldomain" ]]; then
echo "DNS script requires full domain name as first parameter"
exit 1
fi
if [[ -z "$user" ]]; then
echo "CPANEL_USERNAME (username) parameter not set"
exit 1
fi
if [[ -z "$apitoken" ]] && [[ -z "$password" ]]; then
echo "Must set either CPANEL_APITOKEN or CPANEL_PASSWORD in dns script, environment variable or getssl.cfg"
exit 1
fi
if [[ -z "$url" ]]; then
echo "CPANEL_URL (url) parameter not set"
exit 1
fi
# Setup
request_func="${url}/json-api/cpanel?cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=ZoneEdit"
if [[ -n $apitoken ]]; then
curl_params=( -H "Authorization: cpanel $user:$apitoken" )
else
auth_string=$(echo -ne "$user:$password" | base64 --wrap 0)
curl_params=( -H "Authorization: Basic $auth_string" )
fi
# Check if domain is a CNAME
res=$(dig CNAME "$fulldomain")
domain=$(echo "$res"| awk '$4 ~ "CNAME" {print $5}' |sed 's/\.$//g')
if [[ -n "$domain" ]]; then
name=".${fulldomain%.$domain}"
else
domain=$fulldomain
name=""
fi
# Find line number of existing record
request_params="&cpanel_jsonapi_func=fetchzone_records&domain=${domain}&type=TXT&name=_acme-challenge.${fulldomain}."
resp=$(curl --silent "${curl_params[@]}" "$request_func$request_params")
if [[ "$resp" = *\"error\":* ]]; then
echo -n "cpanel fetchzone records failed: "
echo "$resp" | awk -F"error" '{ print $2 }' | awk -F\" '{ print $3 }'
exit 1
fi
# shellcheck disable=SC2001
line=$(echo "$resp" | sed -e 's/.*line":\([0-9]*\),.*/\1/')
if [[ "$line" != "" ]]; then
# Delete the challenge token
request_params="&cpanel_jsonapi_func=remove_zone_record&domain=$domain&type=TXT&name=_acme-challenge$name&line=$line"
resp=$(curl --silent "${curl_params[@]}" "$request_func$request_params")
fi
if [[ "$resp" = *\"status\":0* ]]; then
echo -n "cpanel remove zone record failed: "
echo "$resp" | awk -F"statusmsg" '{ print $2 }' | awk -F\" '{ print $3 }'
exit 1
fi