]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_cpanel.sh
Merge pull request #4777 from acmesh-official/dev
[mirror_acme.sh.git] / dnsapi / dns_cpanel.sh
CommitLineData
d2d023cc
BS
1#!/usr/bin/env sh
2#
7f9b8d68 3#Author: Bjarne Saltbaek
d2d023cc 4#Report Bugs here: https://github.com/acmesh-official/acme.sh/issues/3732
e11d0d37 5#
1d2af0f2 6#
7f9b8d68 7######## Public functions #####################
8339b881 8#
7f9b8d68
BS
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
d2d023cc 14#
d2d023cc 15# Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
2fb9c923 16
d2d023cc 17# Used to add txt record
7f9b8d68
BS
18dns_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"
be827be7
BS
25 _debug cPanel_Username "$cPanel_Username"
26 _debug cPanel_Apitoken "$cPanel_Apitoken"
27 _debug cPanel_Hostname "$cPanel_Hostname"
7f9b8d68
BS
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"
d2d023cc 35 if ! _get_root "$fulldomain"; then
7f9b8d68
BS
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
d2d023cc
BS
49# Usage: fulldomain txtvalue
50# Used to remove the txt record after validation
7f9b8d68
BS
51dns_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
d2d023cc 64 if ! _get_root; then
7f9b8d68
BS
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
86daaf4b 77 _debug "_result is: $_result"
7f9b8d68
BS
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)}"
d2d023cc 89 cPanel_Hostname="${cPanel_Hostname:-$(_readaccountconf_mutable cPanel_Hostname)}"
7f9b8d68 90
d2d023cc 91 if [ -z "$cPanel_Username" ] || [ -z "$cPanel_Apitoken" ] || [ -z "$cPanel_Hostname" ]; then
7f9b8d68
BS
92 cPanel_Username=""
93 cPanel_Apitoken=""
d2d023cc
BS
94 cPanel_Hostname=""
95 _err "You haven't specified cPanel username, apitoken and hostname yet."
7f9b8d68
BS
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"
d2d023cc 102 _saveaccountconf_mutable cPanel_Hostname "$cPanel_Hostname"
7f9b8d68
BS
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
d2d023cc 122_get_root() {
7f9b8d68 123 _myget 'json-api/cpanel?cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=fetchzones'
c485011e 124 _domains=$(echo "$_result" | _egrep_o '"[a-z0-9\.\-]*":\["; cPanel first' | cut -d':' -f1 | sed 's/"//g' | sed 's/{//g')
7f9b8d68
BS
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() {
45090fc8 142 if (echo "$_result" | _egrep_o 'data":\[[^]]*]' | grep -q '"newserial":null'); then return 1; fi
9feeba8d 143 return 0
7f9b8d68
BS
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"
c485011e 150 _id=$(echo "$_result" | sed -e "s/},{/},\n{/g" | grep "$fulldomain" | grep "$txtvalue" | _egrep_o 'line":[0-9]+' | cut -d ':' -f 2)
7f9b8d68
BS
151 _debug "_result is: $_result"
152 _debug "fulldomain. is $fulldomain."
a95e83ab 153 _debug "txtvalue is $txtvalue"
7f9b8d68
BS
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}