3 #Author: Bjarne Saltbaek
4 #Report Bugs here: https://github.com/acmesh-official/acme.sh/issues/3732
7 ######## Public functions #####################
9 # Export CPANEL username,api token and hostname in the following variables
11 # cPanel_Username=username
12 # cPanel_Apitoken=apitoken
13 # cPanel_Hostname=hostname
15 # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
17 # Used to add txt record
22 _info
"Adding TXT record to cPanel based system"
23 _debug fulldomain
"$fulldomain"
24 _debug txtvalue
"$txtvalue"
25 _debug cPanel_Username
"$cPanel_Username"
26 _debug cPanel_Apitoken
"$cPanel_Apitoken"
27 _debug cPanel_Hostname
"$cPanel_Hostname"
29 if ! _cpanel_login
; then
30 _err
"cPanel Login failed for user $cPanel_Username. Check $HTTP_HEADER file"
34 _debug
"First detect the root zone"
35 if ! _get_root
"$fulldomain"; then
36 _err
"No matching root domain for $fulldomain found"
40 _info
"Adding the entry"
41 stripped_fulldomain
=$
(echo "$fulldomain" |
sed "s/.$_domain//")
42 _debug
"Adding $stripped_fulldomain to $_domain zone"
43 _myget
"json-api/cpanel?cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=add_zone_record&domain=$_domain&name=$stripped_fulldomain&type=TXT&txtdata=$txtvalue&ttl=1"
44 if _successful_update
; then return 0; fi
45 _err
"Couldn't create entry!"
49 # Usage: fulldomain txtvalue
50 # Used to remove the txt record after validation
55 _info
"Using cPanel based system"
56 _debug fulldomain
"$fulldomain"
57 _debug txtvalue
"$txtvalue"
59 if ! _cpanel_login
; then
60 _err
"cPanel Login failed for user $cPanel_Username. Check $HTTP_HEADER file"
65 _err
"No matching root domain for $fulldomain found"
69 _findentry
"$fulldomain" "$txtvalue"
70 if [ -z "$_id" ]; then
71 _info
"Entry doesn't exist, nothing to delete"
74 _debug
"Deleting record..."
75 _myget
"json-api/cpanel?cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=remove_zone_record&domain=$_domain&line=$_id"
77 _debug
"_result is: $_result"
79 if _successful_update
; then return 0; fi
80 _err
"Couldn't delete entry!"
84 #################### Private functions below ##################################
87 cPanel_Username
="${cPanel_Username:-$(_readaccountconf_mutable cPanel_Username)}"
88 cPanel_Apitoken
="${cPanel_Apitoken:-$(_readaccountconf_mutable cPanel_Apitoken)}"
89 cPanel_Hostname
="${cPanel_Hostname:-$(_readaccountconf_mutable cPanel_Hostname)}"
91 if [ -z "$cPanel_Username" ] ||
[ -z "$cPanel_Apitoken" ] ||
[ -z "$cPanel_Hostname" ]; then
95 _err
"You haven't specified cPanel username, apitoken and hostname yet."
96 _err
"Please add credentials and try again."
99 #save the credentials to the account conf file.
100 _saveaccountconf_mutable cPanel_Username
"$cPanel_Username"
101 _saveaccountconf_mutable cPanel_Apitoken
"$cPanel_Apitoken"
102 _saveaccountconf_mutable cPanel_Hostname
"$cPanel_Hostname"
107 if ! _checkcredentials
; then return 1; fi
109 if ! _myget
"json-api/cpanel?cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=CustInfo&cpanel_jsonapi_func=displaycontactinfo"; then
110 _err
"cPanel login failed for user $cPanel_Username."
117 #Adds auth header to request
118 export _H1
="Authorization: cpanel $cPanel_Username:$cPanel_Apitoken"
119 _result
=$
(_get
"$cPanel_Hostname/$1")
123 _myget
'json-api/cpanel?cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=fetchzones'
124 _domains
=$
(echo "$_result" | _egrep_o
'"[a-z0-9\.\-]*":\["; cPanel first' | cut
-d':' -f1 |
sed 's/"//g' |
sed 's/{//g')
125 _debug
"_result is: $_result"
126 _debug
"_domains is: $_domains"
127 if [ -z "$_domains" ]; then
128 _err
"Primary domain list not found!"
131 for _domain
in $_domains; do
132 _debug
"Checking if $fulldomain ends with $_domain"
133 if (_endswith
"$fulldomain" "$_domain"); then
134 _debug
"Root domain: $_domain"
141 _successful_update
() {
142 if (echo "$_result" |
grep -q 'newserial'); then return 0; fi
147 _debug
"In _findentry"
148 #returns id of dns entry, if it exists
149 _myget
"json-api/cpanel?cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=fetchzone_records&domain=$_domain"
150 _id
=$
(echo "$_result" |
sed -e "s/},{/},\n{/g" |
grep "$fulldomain" |
grep "$txtvalue" | _egrep_o
'line":[0-9]+' | cut
-d ':' -f 2)
151 _debug
"_result is: $_result"
152 _debug
"fulldomain. is $fulldomain."
153 _debug
"txtvalue is $txtvalue"
154 _debug
"_id is: $_id"
155 if [ -n "$_id" ]; then
156 _debug
"Entry found with _id=$_id"