5 #Updated 03/01/2018, rewrote to support name.com API v4
6 #Utilize name.com API to finish dns-01 verifications.
7 ######## Public functions #####################
9 Namecom_API
="https://api.name.com/v4"
11 #Usage: dns_namecom_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
16 # First we need name.com credentials.
17 if [ -z "$Namecom_Username" ]; then
19 _err
"Username for name.com is missing."
20 _err
"Please specify that in your environment variable."
24 if [ -z "$Namecom_Token" ]; then
26 _err
"API token for name.com is missing."
27 _err
"Please specify that in your environment variable."
31 # Save them in configuration.
32 _saveaccountconf Namecom_Username
"$Namecom_Username"
33 _saveaccountconf Namecom_Token
"$Namecom_Token"
36 if ! _namecom_login
; then
40 # Find domain in domain list.
41 if ! _namecom_get_root
"$fulldomain"; then
42 _err
"Unable to find domain specified."
47 _namecom_addtxt_json
="{\"host\":\"$_sub_domain\",\"type\":\"TXT\",\"answer\":\"$txtvalue\",\"ttl\":\"300\"}"
48 if _namecom_rest POST
"domains/$_domain/records" "$_namecom_addtxt_json"; then
49 _retvalue
=$
(printf "%s\n" "$response" | _egrep_o
"\"$_sub_domain\"")
50 if [ "$_retvalue" ]; then
51 _info
"Successfully added TXT record, ready for validation."
54 _err
"Unable to add the DNS record."
60 #Usage: fulldomain txtvalue
61 #Remove the txt record after validation.
66 if ! _namecom_login
; then
70 # Find domain in domain list.
71 if ! _namecom_get_root
"$fulldomain"; then
72 _err
"Unable to find domain specified."
77 if _namecom_rest GET
"domains/$_domain/records"; then
78 _record_id
=$
(printf "%s\n" "$response" | _egrep_o
"\"id\":[0-9]+,\"domainName\":\"$_domain\",\"host\":\"$_sub_domain\"" | cut
-d \" -f 3 | _egrep_o
[0-9]+)
79 _debug record_id
"$_record_id"
80 if [ "$_record_id" ]; then
81 _info
"Successfully retrieved the record id for ACME challenge."
83 _err
"Unable to retrieve the record id."
88 # Remove the DNS record using record id.
89 if _namecom_rest DELETE
"domains/$_domain/records/$_record_id"; then
90 _info
"Successfully removed the TXT record."
93 _err
"Unable to delete record id."
98 #################### Private functions below ##################################
104 export _H1
="Authorization: Basic $_namecom_auth"
105 export _H2
="Content-Type: application/json"
107 if [ "$method" != "GET" ]; then
108 response
="$(_post "$data" "$Namecom_API/$param" "" "$method")"
110 response
="$(_get "$Namecom_API/$param")"
113 if [ "$?" != "0" ]; then
118 _debug response
"$response"
124 # Name.com API v4 uses http basic auth to authenticate
125 # need to convert the token for http auth
126 _namecom_auth
=$
(printf "%s:%s" "$Namecom_Username" "$Namecom_Token" | base64
)
128 if _namecom_rest GET
"hello"; then
129 retcode
=$
(printf "%s\n" "$response" | _egrep_o
"\"username\"\:\"$Namecom_Username\"")
130 if [ "$retcode" ]; then
131 _info
"Successfully logged in."
133 _err
"Logging in failed."
139 _namecom_get_root
() {
144 if ! _namecom_rest GET
"domains"; then
148 # Need to exclude the last field (tld)
149 numfields
=$
(echo "$domain" | _egrep_o
"\." |
wc -l)
150 while [ $i -le "$numfields" ]; do
151 host=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
153 if [ -z "$host" ]; then
157 if _contains
"$response" "$host"; then
158 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)