4 # https://www.namecheap.com/support/api/intro.aspx
6 # Requires Namecheap API key set in NAMECHEAP_API_KEY and NAMECHEAP_USERNAME set as environment variable
8 ######## Public functions #####################
10 if [ "$STAGE" -eq 1 ]; then
11 NAMECHEAP_API
="https://api.sandbox.namecheap.com/xml.response"
13 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"
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 '>')
161 _namecheap_parse_host
() {
163 _debug _host
"$_host"
165 _hostid
=$
(echo "$_host" | _egrep_o
'HostId=".*"' | cut
-d '"' -f 2)
166 _hostname
=$
(echo "$_host" | _egrep_o
'Name=".*"' | cut
-d '"' -f 2)
167 _hosttype
=$
(echo "$_host" | _egrep_o
'Type=".*"' | cut
-d '"' -f 2)
168 _hostaddress
=$
(echo "$_host" | _egrep_o
'Address=".*"' | cut
-d '"' -f 2)
169 _hostmxpref
=$
(echo "$_host" | _egrep_o
'MXPref=".*"' | cut
-d '"' -f 2)
170 _hostttl
=$
(echo "$_host" | _egrep_o
'TTL=".*"' | cut
-d '"' -f 2)
172 _debug hostid
"$_hostid"
173 _debug hostname
"$_hostname"
174 _debug hosttype
"$_hosttype"
175 _debug hostaddress
"$_hostaddress"
176 _debug hostmxpref
"$_hostmxpref"
177 _debug hostttl
"$_hostttl"
181 _namecheap_check_config
() {
183 if [ -z "$NAMECHEAP_API_KEY" ]; then
184 _err
"No API key specified for Namecheap API."
185 _err
"Create your key and export it as NAMECHEAP_API_KEY"
189 if [ -z "$NAMECHEAP_USERNAME" ]; then
190 _err
"No username key specified for Namecheap API."
191 _err
"Create your key and export it as NAMECHEAP_USERNAME"
195 _saveaccountconf NAMECHEAP_API_KEY
"$NAMECHEAP_API_KEY"
196 _saveaccountconf NAMECHEAP_USERNAME
"$NAMECHEAP_USERNAME"
201 _set_namecheap_TXT
() {
204 tld
=$
(echo "$1" | cut
-d '.' -f 2)
205 sld
=$
(echo "$1" | cut
-d '.' -f 1)
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
224 while read -r host; do
226 if _contains
"$host" "<host"; then
227 _namecheap_parse_host
"$host"
229 if [ "$_hosttype" = "TXT" ] && [ "$_hostname" = "$subdomain" ]; then
230 _namecheap_add_host
"$_hostname" "$_hosttype" "$txt" "$_hostmxpref" "$_hostttl"
233 _namecheap_add_host
"$_hostname" "$_hosttype" "$_hostaddress" "$_hostmxpref" "$_hostttl"
242 if [ $found -eq 0 ]; then
243 _namecheap_add_host
"$subdomain" "TXT" "$txt" 10 120
247 _debug hostrequestfinal
"$_hostrequest"
249 request
="namecheap.domains.dns.setHosts&SLD=${sld}&TLD=${tld}${_hostrequest}"
251 if ! _namecheap_post
"$request"; then
260 _namecheap_reset_hostList
() {
265 #Usage: _namecheap_add_host HostName RecordType Address MxPref TTL
266 _namecheap_add_host
() {
267 _hostindex
=$
(_math
"$_hostindex" + 1)
268 _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")