]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_namecom.sh
Yet another fix.
[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 # First we need name.com credentials.
12 if [ -z "$Namecom_Username" ]; then
13 Namecom_Username=""
14 _err "Username for name.com is missing."
15 _err "Please specify that in your environment variable."
16 return 1
17 fi
18
19 if [ -z "$Namecom_Token" ]; then
20 Namecom_Token=""
21 _err "API token for name.com is missing."
22 _err "Please specify that in your environment variable."
23 return 1
24 fi
25
26 # Save them in configuration.
27 _saveaccountconf Namecom_Username "$Namecom_Username"
28 _saveaccountconf Namecom_Token "$Namecom_Token"
29
30 # Auth string
31 # Name.com API v4 uses http basic auth to authenticate
32 # need to convert the token for http auth
33 _namecom_auth=$(printf "%s:%s" "$Namecom_Username" "$Namecom_Token" | base64)
34
35 #Usage: dns_namecom_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
36 dns_namecom_add() {
37 fulldomain=$1
38 txtvalue=$2
39
40 # Login in using API
41 if ! _namecom_login; then
42 return 1
43 fi
44
45 # Find domain in domain list.
46 if ! _namecom_get_root "$fulldomain"; then
47 _err "Unable to find domain specified."
48 return 1
49 fi
50
51 # Add TXT record.
52 _namecom_addtxt_json="{\"host\":\"$_sub_domain\",\"type\":\"TXT\",\"answer\":\"$txtvalue\",\"ttl\":\"300\"}"
53 if _namecom_rest POST "domains/$_domain/records" "$_namecom_addtxt_json"; then
54 _retvalue=$(printf "%s\n" "$response" | _egrep_o "\"$_sub_domain\"")
55 if [ "$_retvalue" ]; then
56 _info "Successfully added TXT record, ready for validation."
57 return 0
58 else
59 _err "Unable to add the DNS record."
60 return 1
61 fi
62 fi
63 }
64
65 #Usage: fulldomain txtvalue
66 #Remove the txt record after validation.
67 dns_namecom_rm() {
68 fulldomain=$1
69 txtvalue=$2
70
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=$(printf "%s\n" "$response" | _egrep_o "\"id\":[0-9]+,\"domainName\":\"$_domain\",\"host\":\"$_sub_domain\"" | 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 _debug response "$response"
124 return 0
125 }
126
127 _namecom_login() {
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 }
167