4 # https://www.namecheap.com/support/api/intro.aspx
6 # Requires Namecheap API key set in
10 # Due to Namecheap's API limitation all the records of your domain will be read and re applied, make sure to have a backup of your records you could apply if any issue would arise.
12 ######## Public functions #####################
14 NAMECHEAP_API
="https://api.namecheap.com/xml.response"
16 #Usage: dns_namecheap_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
21 if ! _namecheap_check_config
; then
26 if ! _namecheap_set_publicip
; then
30 _debug
"First detect the root zone"
31 if ! _get_root
"$fulldomain"; then
36 _debug fulldomain
"$fulldomain"
37 _debug txtvalue
"$txtvalue"
38 _debug domain
"$_domain"
39 _debug sub_domain
"$_sub_domain"
41 _set_namecheap_TXT
"$_domain" "$_sub_domain" "$txtvalue"
44 #Usage: fulldomain txtvalue
45 #Remove the txt record after validation.
50 if ! _namecheap_set_publicip
; then
54 if ! _namecheap_check_config
; then
59 _debug
"First detect the root zone"
60 if ! _get_root
"$fulldomain"; then
65 _debug fulldomain
"$fulldomain"
66 _debug txtvalue
"$txtvalue"
67 _debug domain
"$_domain"
68 _debug sub_domain
"$_sub_domain"
70 _del_namecheap_TXT
"$_domain" "$_sub_domain" "$txtvalue"
73 #################### Private functions below ##################################
74 #_acme-challenge.www.domain.com
76 # _sub_domain=_acme-challenge.www
81 if ! _namecheap_post
"namecheap.domains.getList"; then
91 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
98 if ! _contains
"$response" "$h"; then
101 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)
111 _namecheap_set_publicip
() {
113 if [ -z "$NAMECHEAP_SOURCEIP" ]; then
114 _err
"No Source IP specified for Namecheap API."
115 _err
"Use your public ip address or an url to retrieve it (e.g. https://ipconfig.co/ip) and export it as NAMECHEAP_SOURCEIP"
118 _saveaccountconf NAMECHEAP_SOURCEIP
"$NAMECHEAP_SOURCEIP"
119 _debug sourceip
"$NAMECHEAP_SOURCEIP"
121 ip
=$
(echo "$NAMECHEAP_SOURCEIP" | _egrep_o
'[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
122 addr
=$
(echo "$NAMECHEAP_SOURCEIP" | _egrep_o
'(http|https)://.*')
127 if [ -n "$ip" ]; then
129 elif [ -n "$addr" ]; then
130 _publicip
=$
(_get
"$addr")
132 _err
"No Source IP specified for Namecheap API."
133 _err
"Use your public ip address or an url to retrieve it (e.g. https://ipconfig.co/ip) and export it as NAMECHEAP_SOURCEIP"
138 _debug publicip
"$_publicip"
145 data
="ApiUser=${NAMECHEAP_USERNAME}&ApiKey=${NAMECHEAP_API_KEY}&ClientIp=${_publicip}&UserName=${NAMECHEAP_USERNAME}&Command=${command}"
146 _debug2
"_namecheap_post data" "$data"
147 response
="$(_post "$data" "$NAMECHEAP_API" "" "POST
")"
148 _debug2 response
"$response"
150 if _contains
"$response" "Status=\"ERROR\"" >/dev
/null
; then
151 error
=$
(echo "$response" | _egrep_o
">.*<\\/Error>" | cut
-d '<' -f 1 |
tr -d '>')
159 _namecheap_parse_host
() {
161 _debug _host
"$_host"
163 _hostid
=$
(echo "$_host" | _egrep_o
' HostId="[^"]*' | cut
-d '"' -f 2)
164 _hostname
=$
(echo "$_host" | _egrep_o
' Name="[^"]*' | cut
-d '"' -f 2)
165 _hosttype
=$
(echo "$_host" | _egrep_o
' Type="[^"]*' | cut
-d '"' -f 2)
166 _hostaddress
=$
(echo "$_host" | _egrep_o
' Address="[^"]*' | cut
-d '"' -f 2)
167 _hostmxpref
=$
(echo "$_host" | _egrep_o
' MXPref="[^"]*' | cut
-d '"' -f 2)
168 _hostttl
=$
(echo "$_host" | _egrep_o
' TTL="[^"]*' | cut
-d '"' -f 2)
170 _debug hostid
"$_hostid"
171 _debug hostname
"$_hostname"
172 _debug hosttype
"$_hosttype"
173 _debug hostaddress
"$_hostaddress"
174 _debug hostmxpref
"$_hostmxpref"
175 _debug hostttl
"$_hostttl"
178 _namecheap_check_config
() {
180 if [ -z "$NAMECHEAP_API_KEY" ]; then
181 _err
"No API key specified for Namecheap API."
182 _err
"Create your key and export it as NAMECHEAP_API_KEY"
186 if [ -z "$NAMECHEAP_USERNAME" ]; then
187 _err
"No username key specified for Namecheap API."
188 _err
"Create your key and export it as NAMECHEAP_USERNAME"
192 _saveaccountconf NAMECHEAP_API_KEY
"$NAMECHEAP_API_KEY"
193 _saveaccountconf NAMECHEAP_USERNAME
"$NAMECHEAP_USERNAME"
198 _set_namecheap_TXT
() {
202 if ! _namecheap_set_tld_sld
"$1"; then
206 request
="namecheap.domains.dns.getHosts&SLD=${_sld}&TLD=${_tld}"
208 if ! _namecheap_post
"$request"; then
213 hosts
=$
(echo "$response" | _egrep_o
'<host[^>]*')
214 _debug hosts
"$hosts"
216 if [ -z "$hosts" ]; then
217 _error
"Hosts not found"
221 _namecheap_reset_hostList
223 while read -r host; do
224 if _contains
"$host" "<host"; then
225 _namecheap_parse_host
"$host"
226 _debug2 _hostname
"_hostname"
227 _debug2 _hosttype
"_hosttype"
228 _debug2 _hostaddress
"_hostaddress"
229 _debug2 _hostmxpref
"_hostmxpref"
230 _hostaddress
="$(printf "%s
" "$_hostaddress" | _url_encode)"
231 _debug2
"encoded _hostaddress" "_hostaddress"
232 _namecheap_add_host
"$_hostname" "$_hosttype" "$_hostaddress" "$_hostmxpref" "$_hostttl"
238 _namecheap_add_host
"$subdomain" "TXT" "$txt" 10 120
240 _debug hostrequestfinal
"$_hostrequest"
242 request
="namecheap.domains.dns.setHosts&SLD=${_sld}&TLD=${_tld}${_hostrequest}"
244 if ! _namecheap_post
"$request"; then
252 _del_namecheap_TXT
() {
256 if ! _namecheap_set_tld_sld
"$1"; then
260 request
="namecheap.domains.dns.getHosts&SLD=${_sld}&TLD=${_tld}"
262 if ! _namecheap_post
"$request"; then
267 hosts
=$
(echo "$response" | _egrep_o
'<host[^>]*')
268 _debug hosts
"$hosts"
270 if [ -z "$hosts" ]; then
271 _error
"Hosts not found"
275 _namecheap_reset_hostList
279 while read -r host; do
280 if _contains
"$host" "<host"; then
281 _namecheap_parse_host
"$host"
282 if [ "$_hosttype" = "TXT" ] && [ "$_hostname" = "$subdomain" ] && [ "$_hostaddress" = "$txt" ]; then
283 _debug
"TXT entry found"
286 _hostaddress
="$(printf "%s
" "$_hostaddress" | _url_encode)"
287 _namecheap_add_host
"$_hostname" "$_hosttype" "$_hostaddress" "$_hostmxpref" "$_hostttl"
294 if [ $found -eq 0 ]; then
295 _debug
"TXT entry not found"
299 _debug hostrequestfinal
"$_hostrequest"
301 request
="namecheap.domains.dns.setHosts&SLD=${_sld}&TLD=${_tld}${_hostrequest}"
303 if ! _namecheap_post
"$request"; then
311 _namecheap_reset_hostList
() {
316 #Usage: _namecheap_add_host HostName RecordType Address MxPref TTL
317 _namecheap_add_host
() {
318 _hostindex
=$
(_math
"$_hostindex" + 1)
319 _hostrequest
=$
(printf '%s&HostName%d=%s&RecordType%d=%s&Address%d=%s&MXPref%d=%d&TTL%d=%d' "$_hostrequest" "$_hostindex" "$1" "$_hostindex" "$2" "$_hostindex" "$3" "$_hostindex" "$4" "$_hostindex" "$5")
322 _namecheap_set_tld_sld
() {
331 _tld
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
334 if [ -z "$_tld" ]; then
341 _sld
=$
(printf "%s" "$domain" | cut
-d .
-f 1-"$j")
344 if [ -z "$_sld" ]; then
349 request
="namecheap.domains.dns.getHosts&SLD=$_sld&TLD=$_tld"
351 if ! _namecheap_post
"$request"; then
352 _debug
"sld($_sld)/tld($_tld) not found"
354 _debug
"sld($_sld)/tld($_tld) found"