]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_namecom.sh
Merge pull request #4542 from alexleigh/master
[mirror_acme.sh.git] / dnsapi / dns_namecom.sh
1 #!/usr/bin/env sh
2
3 #Author: RaidenII
4 #Created 06/28/2017
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 #####################
8
9 Namecom_API="https://api.name.com/v4"
10
11 #Usage: dns_namecom_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
12 dns_namecom_add() {
13 fulldomain=$1
14 txtvalue=$2
15
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
20 Namecom_Username=""
21 _err "Username for name.com is missing."
22 _err "Please specify that in your environment variable."
23 return 1
24 fi
25
26 if [ -z "$Namecom_Token" ]; then
27 Namecom_Token=""
28 _err "API token for name.com is missing."
29 _err "Please specify that in your environment variable."
30 return 1
31 fi
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"
37
38 # Login in using API
39 if ! _namecom_login; then
40 return 1
41 fi
42
43 # Find domain in domain list.
44 if ! _namecom_get_root "$fulldomain"; then
45 _err "Unable to find domain specified."
46 return 1
47 fi
48
49 # Add TXT record.
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."
55 return 0
56 else
57 _err "Unable to add the DNS record."
58 return 1
59 fi
60 fi
61 }
62
63 #Usage: fulldomain txtvalue
64 #Remove the txt record after validation.
65 dns_namecom_rm() {
66 fulldomain=$1
67 txtvalue=$2
68
69 Namecom_Username="${Namecom_Username:-$(_readaccountconf_mutable Namecom_Username)}"
70 Namecom_Token="${Namecom_Token:-$(_readaccountconf_mutable Namecom_Token)}"
71 if ! _namecom_login; then
72 return 1
73 fi
74
75 # Find domain in domain list.
76 if ! _namecom_get_root "$fulldomain"; then
77 _err "Unable to find domain specified."
78 return 1
79 fi
80
81 # Get the record id.
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."
87 else
88 _err "Unable to retrieve the record id."
89 return 1
90 fi
91 fi
92
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."
96 return 0
97 else
98 _err "Unable to delete record id."
99 return 1
100 fi
101 }
102
103 #################### Private functions below ##################################
104 _namecom_rest() {
105 method=$1
106 param=$2
107 data=$3
108
109 export _H1="Authorization: Basic $_namecom_auth"
110 export _H2="Content-Type: application/json"
111
112 if [ "$method" != "GET" ]; then
113 response="$(_post "$data" "$Namecom_API/$param" "" "$method")"
114 else
115 response="$(_get "$Namecom_API/$param")"
116 fi
117
118 if [ "$?" != "0" ]; then
119 _err "error $param"
120 return 1
121 fi
122
123 _debug2 response "$response"
124 return 0
125 }
126
127 _namecom_login() {
128 # Auth string
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)
132
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."
137 else
138 _err "$response"
139 _err "Please add your ip to api whitelist"
140 _err "Logging in failed."
141 return 1
142 fi
143 fi
144 }
145
146 _namecom_get_root() {
147 domain=$1
148 i=2
149 p=1
150
151 if ! _namecom_rest GET "domains"; then
152 return 1
153 fi
154
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)
159 _debug host "$host"
160 if [ -z "$host" ]; then
161 return 1
162 fi
163
164 if _contains "$response" "$host"; then
165 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
166 _domain="$host"
167 return 0
168 fi
169 p=$i
170 i=$(_math "$i" + 1)
171 done
172 return 1
173 }