]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_namecom.sh
Move code to fit DNS API dev guide.
[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 # First we need name.com credentials.
17 if [ -z "$Namecom_Username" ]; then
18 Namecom_Username=""
19 _err "Username for name.com is missing."
20 _err "Please specify that in your environment variable."
21 return 1
22 fi
23
24 if [ -z "$Namecom_Token" ]; then
25 Namecom_Token=""
26 _err "API token for name.com is missing."
27 _err "Please specify that in your environment variable."
28 return 1
29 fi
30
31 # Save them in configuration.
32 _saveaccountconf Namecom_Username "$Namecom_Username"
33 _saveaccountconf Namecom_Token "$Namecom_Token"
34
35 # Login in using API
36 if ! _namecom_login; then
37 return 1
38 fi
39
40 # Find domain in domain list.
41 if ! _namecom_get_root "$fulldomain"; then
42 _err "Unable to find domain specified."
43 return 1
44 fi
45
46 # Add TXT record.
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."
52 return 0
53 else
54 _err "Unable to add the DNS record."
55 return 1
56 fi
57 fi
58 }
59
60 #Usage: fulldomain txtvalue
61 #Remove the txt record after validation.
62 dns_namecom_rm() {
63 fulldomain=$1
64 txtvalue=$2
65
66 if ! _namecom_login; then
67 return 1
68 fi
69
70 # Find domain in domain list.
71 if ! _namecom_get_root "$fulldomain"; then
72 _err "Unable to find domain specified."
73 return 1
74 fi
75
76 # Get the record id.
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."
82 else
83 _err "Unable to retrieve the record id."
84 return 1
85 fi
86 fi
87
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."
91 return 0
92 else
93 _err "Unable to delete record id."
94 return 1
95 fi
96 }
97
98 #################### Private functions below ##################################
99 _namecom_rest() {
100 method=$1
101 param=$2
102 data=$3
103
104 export _H1="Authorization: Basic $_namecom_auth"
105 export _H2="Content-Type: application/json"
106
107 if [ "$method" != "GET" ]; then
108 response="$(_post "$data" "$Namecom_API/$param" "" "$method")"
109 else
110 response="$(_get "$Namecom_API/$param")"
111 fi
112
113 if [ "$?" != "0" ]; then
114 _err "error $param"
115 return 1
116 fi
117
118 _debug response "$response"
119 return 0
120 }
121
122 _namecom_login() {
123 # Auth string
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)
127
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."
132 else
133 _err "Logging in failed."
134 return 1
135 fi
136 fi
137 }
138
139 _namecom_get_root() {
140 domain=$1
141 i=2
142 p=1
143
144 if ! _namecom_rest GET "domains"; then
145 return 1
146 fi
147
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)
152 _debug host "$host"
153 if [ -z "$host" ]; then
154 return 1
155 fi
156
157 if _contains "$response" "$host"; then
158 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
159 _domain="$host"
160 return 0
161 fi
162 p=$i
163 i=$(_math "$i" + 1)
164 done
165 return 1
166 }