]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_cpanel.sh
Merge pull request #4531 from NCDGHA/bugfix/issue_4530_fix_http_status_503
[mirror_acme.sh.git] / dnsapi / dns_cpanel.sh
1 #!/usr/bin/env sh
2 #
3 #Author: Bjarne Saltbaek
4 #Report Bugs here: https://github.com/acmesh-official/acme.sh/issues/3732
5 #
6 #
7 ######## Public functions #####################
8 #
9 # Export CPANEL username,api token and hostname in the following variables
10 #
11 # cPanel_Username=username
12 # cPanel_Apitoken=apitoken
13 # cPanel_Hostname=hostname
14 #
15 # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
16
17 # Used to add txt record
18 dns_cpanel_add() {
19 fulldomain=$1
20 txtvalue=$2
21
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"
28
29 if ! _cpanel_login; then
30 _err "cPanel Login failed for user $cPanel_Username. Check $HTTP_HEADER file"
31 return 1
32 fi
33
34 _debug "First detect the root zone"
35 if ! _get_root "$fulldomain"; then
36 _err "No matching root domain for $fulldomain found"
37 return 1
38 fi
39 # adding entry
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!"
46 return 1
47 }
48
49 # Usage: fulldomain txtvalue
50 # Used to remove the txt record after validation
51 dns_cpanel_rm() {
52 fulldomain=$1
53 txtvalue=$2
54
55 _info "Using cPanel based system"
56 _debug fulldomain "$fulldomain"
57 _debug txtvalue "$txtvalue"
58
59 if ! _cpanel_login; then
60 _err "cPanel Login failed for user $cPanel_Username. Check $HTTP_HEADER file"
61 return 1
62 fi
63
64 if ! _get_root; then
65 _err "No matching root domain for $fulldomain found"
66 return 1
67 fi
68
69 _findentry "$fulldomain" "$txtvalue"
70 if [ -z "$_id" ]; then
71 _info "Entry doesn't exist, nothing to delete"
72 return 0
73 fi
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"
76 # removing entry
77 _debug "_result is: $_result"
78
79 if _successful_update; then return 0; fi
80 _err "Couldn't delete entry!"
81 return 1
82 }
83
84 #################### Private functions below ##################################
85
86 _checkcredentials() {
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)}"
90
91 if [ -z "$cPanel_Username" ] || [ -z "$cPanel_Apitoken" ] || [ -z "$cPanel_Hostname" ]; then
92 cPanel_Username=""
93 cPanel_Apitoken=""
94 cPanel_Hostname=""
95 _err "You haven't specified cPanel username, apitoken and hostname yet."
96 _err "Please add credentials and try again."
97 return 1
98 fi
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"
103 return 0
104 }
105
106 _cpanel_login() {
107 if ! _checkcredentials; then return 1; fi
108
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."
111 return 1
112 fi
113 return 0
114 }
115
116 _myget() {
117 #Adds auth header to request
118 export _H1="Authorization: cpanel $cPanel_Username:$cPanel_Apitoken"
119 _result=$(_get "$cPanel_Hostname/$1")
120 }
121
122 _get_root() {
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!"
129 return 1
130 fi
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"
135 return 0
136 fi
137 done
138 return 1
139 }
140
141 _successful_update() {
142 if (echo "$_result" | _egrep_o 'data":\[[^]]*]' | grep -q '"newserial":null'); then return 1; fi
143 return 0
144 }
145
146 _findentry() {
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"
157 return 0
158 fi
159 return 1
160 }