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
() {
203 if ! _namecheap_set_tld_sld
"$1"; then
207 request
="namecheap.domains.dns.getHosts&SLD=${_sld}&TLD=${_tld}"
209 if ! _namecheap_post
"$request"; then
214 hosts
=$
(echo "$response" | _egrep_o
'<host[^>]*')
215 _debug hosts
"$hosts"
217 if [ -z "$hosts" ]; then
218 _error
"Hosts not found"
222 _namecheap_reset_hostList
224 while read -r host; do
225 if _contains
"$host" "<host"; then
226 _namecheap_parse_host
"$host"
227 _namecheap_add_host
"$_hostname" "$_hosttype" "$_hostaddress" "$_hostmxpref" "$_hostttl"
233 _namecheap_add_host
"$subdomain" "TXT" "$txt" 10 120
235 _debug hostrequestfinal
"$_hostrequest"
237 request
="namecheap.domains.dns.setHosts&SLD=${_sld}&TLD=${_tld}${_hostrequest}"
239 if ! _namecheap_post
"$request"; then
247 _del_namecheap_TXT
() {
251 if ! _namecheap_set_tld_sld
"$1"; then
255 request
="namecheap.domains.dns.getHosts&SLD=${_sld}&TLD=${_tld}"
257 if ! _namecheap_post
"$request"; then
262 hosts
=$
(echo "$response" | _egrep_o
'<host[^>]*')
263 _debug hosts
"$hosts"
265 if [ -z "$hosts" ]; then
266 _error
"Hosts not found"
270 _namecheap_reset_hostList
274 while read -r host; do
275 if _contains
"$host" "<host"; then
276 _namecheap_parse_host
"$host"
277 if [ "$_hosttype" = "TXT" ] && [ "$_hostname" = "$subdomain" ] && [ "$_hostaddress" = "$txt" ]; then
278 _debug
"TXT entry found"
281 _namecheap_add_host
"$_hostname" "$_hosttype" "$_hostaddress" "$_hostmxpref" "$_hostttl"
288 if [ $found -eq 0 ]; then
289 _debug
"TXT entry not found"
293 _debug hostrequestfinal
"$_hostrequest"
295 request
="namecheap.domains.dns.setHosts&SLD=${_sld}&TLD=${_tld}${_hostrequest}"
297 if ! _namecheap_post
"$request"; then
305 _namecheap_reset_hostList
() {
310 #Usage: _namecheap_add_host HostName RecordType Address MxPref TTL
311 _namecheap_add_host
() {
312 _hostindex
=$
(_math
"$_hostindex" + 1)
313 _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")
316 _namecheap_set_tld_sld
() {
325 _tld
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
328 if [ -z "$_tld" ]; then
335 _sld
=$
(printf "%s" "$domain" | cut
-d .
-f 1-"$j")
338 if [ -z "$_sld" ]; then
343 request
="namecheap.domains.dns.getHosts&SLD=$_sld&TLD=$_tld"
345 if ! _namecheap_post
"$request"; then
346 _debug
"sld($_sld)/tld($_tld) not found"
348 _debug
"sld($_sld)/tld($_tld) found"