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 Namecom_Username
="${Namecom_Username:-$(_readaccountconf_mutable Namecom_Username)}"
17 Namecom_Token
="${Namecom_Token:-$(_readaccountconf_mutable Namecom_Token)}"
18 # First we need name.com credentials.
19 if [ -z "$Namecom_Username" ]; then
21 _err
"Username for name.com is missing."
22 _err
"Please specify that in your environment variable."
26 if [ -z "$Namecom_Token" ]; then
28 _err
"API token for name.com is missing."
29 _err
"Please specify that in your environment variable."
32 _debug Namecom_Username
"$Namecom_Username"
33 _secure_debug Namecom_Token
"$Namecom_Token"
34 # Save them in configuration.
35 _saveaccountconf_mutable Namecom_Username
"$Namecom_Username"
36 _saveaccountconf_mutable Namecom_Token
"$Namecom_Token"
39 if ! _namecom_login
; then
43 # Find domain in domain list.
44 if ! _namecom_get_root
"$fulldomain"; then
45 _err
"Unable to find domain specified."
50 _namecom_addtxt_json
="{\"host\":\"$_sub_domain\",\"type\":\"TXT\",\"answer\":\"$txtvalue\",\"ttl\":\"300\"}"
51 if _namecom_rest POST
"domains/$_domain/records" "$_namecom_addtxt_json"; then
52 _retvalue
=$
(echo "$response" | _egrep_o
"\"$_sub_domain\"")
53 if [ "$_retvalue" ]; then
54 _info
"Successfully added TXT record, ready for validation."
57 _err
"Unable to add the DNS record."
63 #Usage: fulldomain txtvalue
64 #Remove the txt record after validation.
69 Namecom_Username
="${Namecom_Username:-$(_readaccountconf_mutable Namecom_Username)}"
70 Namecom_Token
="${Namecom_Token:-$(_readaccountconf_mutable Namecom_Token)}"
71 if ! _namecom_login
; then
75 # Find domain in domain list.
76 if ! _namecom_get_root
"$fulldomain"; then
77 _err
"Unable to find domain specified."
82 if _namecom_rest GET
"domains/$_domain/records"; then
83 _record_id
=$
(echo "$response" | _egrep_o
"\"id\":[0-9]+,\"domainName\":\"$_domain\",\"host\":\"$_sub_domain\",\"fqdn\":\"$fulldomain.\",\"type\":\"TXT\",\"answer\":\"$txtvalue\"" | cut
-d \" -f 3 | _egrep_o
[0-9]+)
84 _debug record_id
"$_record_id"
85 if [ "$_record_id" ]; then
86 _info
"Successfully retrieved the record id for ACME challenge."
88 _err
"Unable to retrieve the record id."
93 # Remove the DNS record using record id.
94 if _namecom_rest DELETE
"domains/$_domain/records/$_record_id"; then
95 _info
"Successfully removed the TXT record."
98 _err
"Unable to delete record id."
103 #################### Private functions below ##################################
109 export _H1
="Authorization: Basic $_namecom_auth"
110 export _H2
="Content-Type: application/json"
112 if [ "$method" != "GET" ]; then
113 response
="$(_post "$data" "$Namecom_API/$param" "" "$method")"
115 response
="$(_get "$Namecom_API/$param")"
118 if [ "$?" != "0" ]; then
123 _debug2 response
"$response"
129 # Name.com API v4 uses http basic auth to authenticate
130 # need to convert the token for http auth
131 _namecom_auth
=$
(printf "%s:%s" "$Namecom_Username" "$Namecom_Token" | _base64
)
133 if _namecom_rest GET
"hello"; then
134 retcode
=$
(echo "$response" | _egrep_o
"\"username\"\:\"$Namecom_Username\"")
135 if [ "$retcode" ]; then
136 _info
"Successfully logged in."
139 _err
"Please add your ip to api whitelist"
140 _err
"Logging in failed."
146 _namecom_get_root
() {
151 if ! _namecom_rest GET
"domains"; then
155 # Need to exclude the last field (tld)
156 numfields
=$
(echo "$domain" | _egrep_o
"\." |
wc -l)
157 while [ $i -le "$numfields" ]; do
158 host=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
160 if [ -z "$host" ]; then
164 if _contains
"$response" "$host"; then
165 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)