4 # https://www.namecheap.com/support/api/intro.aspx
6 # Requires Namecheap API key set in NAMECHEAP_API_KEY, NAMECHEAP_SOURCEIP and NAMECHEAP_USERNAME set as environment variable
7 # 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.
9 ######## Public functions #####################
11 if [ "$STAGE" -eq 1 ]; then
12 NAMECHEAP_API
="https://api.sandbox.namecheap.com/xml.response"
14 NAMECHEAP_API
="https://api.namecheap.com/xml.response"
17 #Usage: dns_namecheap_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
22 if ! _namecheap_check_config
; then
27 if ! _namecheap_set_publicip
; then
31 _debug
"First detect the root zone"
32 if ! _get_root
"$fulldomain"; then
37 _debug fulldomain
"$fulldomain"
38 _debug txtvalue
"$txtvalue"
39 _debug domain
"$_domain"
40 _debug sub_domain
"$_sub_domain"
42 _set_namecheap_TXT
"$_domain" "$_sub_domain" "$txtvalue"
45 #Usage: fulldomain txtvalue
46 #Remove the txt record after validation.
51 if ! _namecheap_set_publicip
; then
55 if ! _namecheap_check_config
; then
60 _debug
"First detect the root zone"
61 if ! _get_root
"$fulldomain"; then
66 _debug fulldomain
"$fulldomain"
67 _debug txtvalue
"$txtvalue"
68 _debug domain
"$_domain"
69 _debug sub_domain
"$_sub_domain"
71 _del_namecheap_TXT
"$_domain" "$_sub_domain" "$txtvalue"
74 #################### Private functions below ##################################
75 #_acme-challenge.www.domain.com
77 # _sub_domain=_acme-challenge.www
82 if ! _namecheap_post
"namecheap.domains.getList"; then
92 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
99 if ! _contains
"$response" "$h"; then
100 _debug
"$h not found"
102 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)
112 _namecheap_set_publicip
() {
114 if [ -z "$NAMECHEAP_SOURCEIP" ]; then
115 _err
"No Source IP specified for Namecheap API."
116 _err
"Use your public ip address or an url to retrieve it (e.g. https://ipconfig.co/ip) and export it as NAMECHEAP_SOURCEIP"
119 _saveaccountconf NAMECHEAP_SOURCEIP
"$NAMECHEAP_SOURCEIP"
120 _debug sourceip
"$NAMECHEAP_SOURCEIP"
122 ip
=$
(echo "$NAMECHEAP_SOURCEIP" | _egrep_o
'[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
123 addr
=$
(echo "$NAMECHEAP_SOURCEIP" | _egrep_o
'(http|https)://.*')
128 if [ -n "$ip" ]; then
130 elif [ -n "$addr" ]; then
131 _publicip
=$
(_get
"$addr")
133 _err
"No Source IP specified for Namecheap API."
134 _err
"Use your public ip address or an url to retrieve it (e.g. https://ipconfig.co/ip) and export it as NAMECHEAP_SOURCEIP"
139 _debug publicip
"$_publicip"
146 data
="ApiUser=${NAMECHEAP_USERNAME}&ApiKey=${NAMECHEAP_API_KEY}&ClientIp=${_publicip}&UserName=${NAMECHEAP_USERNAME}&Command=${command}"
148 response
="$(_post "$data" "$NAMECHEAP_API" "" "POST
")"
149 _debug2 response
"$response"
151 if _contains
"$response" "Status=\"ERROR\"" >/dev
/null
; then
152 error
=$
(echo "$response" | _egrep_o
">.*<\\/Error>" | cut
-d '<' -f 1 |
tr -d '>')
160 _namecheap_parse_host
() {
162 _debug _host
"$_host"
164 _hostid
=$
(echo "$_host" | _egrep_o
'\sHostId="[^"]*' | cut
-d '"' -f 2)
165 _hostname
=$
(echo "$_host" | _egrep_o
'\sName="[^"]*' | cut
-d '"' -f 2)
166 _hosttype
=$
(echo "$_host" | _egrep_o
'\sType="[^"]*' | cut
-d '"' -f 2)
167 _hostaddress
=$
(echo "$_host" | _egrep_o
'\sAddress="[^"]*' | cut
-d '"' -f 2)
168 _hostmxpref
=$
(echo "$_host" | _egrep_o
'\sMXPref="[^"]*' | cut
-d '"' -f 2)
169 _hostttl
=$
(echo "$_host" | _egrep_o
'\sTTL="[^"]*' | cut
-d '"' -f 2)
171 _debug hostid
"$_hostid"
172 _debug hostname
"$_hostname"
173 _debug hosttype
"$_hosttype"
174 _debug hostaddress
"$_hostaddress"
175 _debug hostmxpref
"$_hostmxpref"
176 _debug hostttl
"$_hostttl"
179 _namecheap_check_config
() {
181 if [ -z "$NAMECHEAP_API_KEY" ]; then
182 _err
"No API key specified for Namecheap API."
183 _err
"Create your key and export it as NAMECHEAP_API_KEY"
187 if [ -z "$NAMECHEAP_USERNAME" ]; then
188 _err
"No username key specified for Namecheap API."
189 _err
"Create your key and export it as NAMECHEAP_USERNAME"
193 _saveaccountconf NAMECHEAP_API_KEY
"$NAMECHEAP_API_KEY"
194 _saveaccountconf NAMECHEAP_USERNAME
"$NAMECHEAP_USERNAME"
199 _set_namecheap_TXT
() {
202 tld
=$
(echo "$1" | cut
-d '.' -f 2)
203 sld
=$
(echo "$1" | cut
-d '.' -f 1)
204 request
="namecheap.domains.dns.getHosts&SLD=$sld&TLD=$tld"
206 if ! _namecheap_post
"$request"; then
211 hosts
=$
(echo "$response" | _egrep_o
'<host[^>]*')
212 _debug hosts
"$hosts"
214 if [ -z "$hosts" ]; then
215 _error
"Hosts not found"
219 _namecheap_reset_hostList
221 while read -r host; do
222 if _contains
"$host" "<host"; then
223 _namecheap_parse_host
"$host"
224 _namecheap_add_host
"$_hostname" "$_hosttype" "$_hostaddress" "$_hostmxpref" "$_hostttl"
230 _namecheap_add_host
"$subdomain" "TXT" "$txt" 10 120
232 _debug hostrequestfinal
"$_hostrequest"
234 request
="namecheap.domains.dns.setHosts&SLD=${sld}&TLD=${tld}${_hostrequest}"
236 if ! _namecheap_post
"$request"; then
244 _del_namecheap_TXT
() {
247 tld
=$
(echo "$1" | cut
-d '.' -f 2)
248 sld
=$
(echo "$1" | cut
-d '.' -f 1)
249 request
="namecheap.domains.dns.getHosts&SLD=$sld&TLD=$tld"
251 if ! _namecheap_post
"$request"; then
256 hosts
=$
(echo "$response" | _egrep_o
'<host[^>]*')
257 _debug hosts
"$hosts"
259 if [ -z "$hosts" ]; then
260 _error
"Hosts not found"
264 _namecheap_reset_hostList
268 while read -r host; do
269 if _contains
"$host" "<host"; then
270 _namecheap_parse_host
"$host"
271 if [ "$_hosttype" = "TXT" ] && [ "$_hostname" = "$subdomain" ] && [ "$_hostaddress" = "$txt" ]; then
272 _debug
"TXT entry found"
275 _namecheap_add_host
"$_hostname" "$_hosttype" "$_hostaddress" "$_hostmxpref" "$_hostttl"
282 if [ $found -eq 0 ]; then
283 _debug
"TXT entry not found"
287 _debug hostrequestfinal
"$_hostrequest"
289 request
="namecheap.domains.dns.setHosts&SLD=${sld}&TLD=${tld}${_hostrequest}"
291 if ! _namecheap_post
"$request"; then
299 _namecheap_reset_hostList
() {
304 #Usage: _namecheap_add_host HostName RecordType Address MxPref TTL
305 _namecheap_add_host
() {
306 _hostindex
=$
(_math
"$_hostindex" + 1)
307 _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")