]>
Commit | Line | Data |
---|---|---|
e64ad517 R |
1 | #!/usr/bin/env sh |
2 | ||
50a91453 | 3 | #Author: RaidenII |
e64ad517 | 4 | #Created 06/28/2017 |
50a91453 | 5 | #Updated 03/01/2018, rewrote to support name.com API v4 |
e64ad517 R |
6 | #Utilize name.com API to finish dns-01 verifications. |
7 | ######## Public functions ##################### | |
8 | ||
50a91453 | 9 | Namecom_API="https://api.name.com/v4" |
10 | ||
e64ad517 R |
11 | #Usage: dns_namecom_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" |
12 | dns_namecom_add() { | |
13 | fulldomain=$1 | |
14 | txtvalue=$2 | |
15 | ||
2b36f4f5 | 16 | Namecom_Username="${Namecom_Username:-$(_readaccountconf_mutable Namecom_Username)}" |
17 | Namecom_Token="${Namecom_Token:-$(_readaccountconf_mutable Namecom_Token)}" | |
9fa207e6 | 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 | |
653c77e8 | 32 | _debug Namecom_Username "$Namecom_Username" |
33 | _secure_debug Namecom_Token "$Namecom_Token" | |
9fa207e6 | 34 | # Save them in configuration. |
2b36f4f5 | 35 | _saveaccountconf_mutable Namecom_Username "$Namecom_Username" |
36 | _saveaccountconf_mutable Namecom_Token "$Namecom_Token" | |
9fa207e6 | 37 | |
e64ad517 | 38 | # Login in using API |
2e602ef6 R |
39 | if ! _namecom_login; then |
40 | return 1 | |
41 | fi | |
e64ad517 R |
42 | |
43 | # Find domain in domain list. | |
44 | if ! _namecom_get_root "$fulldomain"; then | |
45 | _err "Unable to find domain specified." | |
e64ad517 R |
46 | return 1 |
47 | fi | |
48 | ||
49 | # Add TXT record. | |
50a91453 | 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 | |
2b36f4f5 | 52 | _retvalue=$(echo "$response" | _egrep_o "\"$_sub_domain\"") |
50a91453 | 53 | if [ "$_retvalue" ]; then |
eeda3062 | 54 | _info "Successfully added TXT record, ready for validation." |
eeda3062 R |
55 | return 0 |
56 | else | |
57 | _err "Unable to add the DNS record." | |
eeda3062 R |
58 | return 1 |
59 | fi | |
e64ad517 R |
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 | ||
2b36f4f5 | 69 | Namecom_Username="${Namecom_Username:-$(_readaccountconf_mutable Namecom_Username)}" |
70 | Namecom_Token="${Namecom_Token:-$(_readaccountconf_mutable Namecom_Token)}" | |
2e602ef6 R |
71 | if ! _namecom_login; then |
72 | return 1 | |
73 | fi | |
e64ad517 R |
74 | |
75 | # Find domain in domain list. | |
76 | if ! _namecom_get_root "$fulldomain"; then | |
77 | _err "Unable to find domain specified." | |
e64ad517 R |
78 | return 1 |
79 | fi | |
80 | ||
81 | # Get the record id. | |
50a91453 | 82 | if _namecom_rest GET "domains/$_domain/records"; then |
2b36f4f5 | 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]+) |
50a91453 | 84 | _debug record_id "$_record_id" |
85 | if [ "$_record_id" ]; then | |
eeda3062 R |
86 | _info "Successfully retrieved the record id for ACME challenge." |
87 | else | |
88 | _err "Unable to retrieve the record id." | |
eeda3062 R |
89 | return 1 |
90 | fi | |
e64ad517 R |
91 | fi |
92 | ||
93 | # Remove the DNS record using record id. | |
50a91453 | 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 | |
e64ad517 R |
100 | fi |
101 | } | |
102 | ||
103 | #################### Private functions below ################################## | |
104 | _namecom_rest() { | |
105 | method=$1 | |
106 | param=$2 | |
107 | data=$3 | |
108 | ||
50a91453 | 109 | export _H1="Authorization: Basic $_namecom_auth" |
110 | export _H2="Content-Type: application/json" | |
111 | ||
e64ad517 | 112 | if [ "$method" != "GET" ]; then |
63c6ed3f | 113 | response="$(_post "$data" "$Namecom_API/$param" "" "$method")" |
e64ad517 | 114 | else |
63c6ed3f | 115 | response="$(_get "$Namecom_API/$param")" |
e64ad517 R |
116 | fi |
117 | ||
118 | if [ "$?" != "0" ]; then | |
119 | _err "error $param" | |
120 | return 1 | |
121 | fi | |
122 | ||
50801234 | 123 | _debug2 response "$response" |
e64ad517 R |
124 | return 0 |
125 | } | |
126 | ||
127 | _namecom_login() { | |
9fa207e6 | 128 | # Auth string |
129 | # Name.com API v4 uses http basic auth to authenticate | |
130 | # need to convert the token for http auth | |
9c88971b | 131 | _namecom_auth=$(printf "%s:%s" "$Namecom_Username" "$Namecom_Token" | _base64) |
9fa207e6 | 132 | |
50a91453 | 133 | if _namecom_rest GET "hello"; then |
2b36f4f5 | 134 | retcode=$(echo "$response" | _egrep_o "\"username\"\:\"$Namecom_Username\"") |
63c6ed3f | 135 | if [ "$retcode" ]; then |
50a91453 | 136 | _info "Successfully logged in." |
eeda3062 | 137 | else |
3f35006c | 138 | _err "$response" |
139 | _err "Please add your ip to api whitelist" | |
eeda3062 R |
140 | _err "Logging in failed." |
141 | return 1 | |
142 | fi | |
143 | fi | |
e64ad517 R |
144 | } |
145 | ||
e64ad517 R |
146 | _namecom_get_root() { |
147 | domain=$1 | |
148 | i=2 | |
149 | p=1 | |
150 | ||
50a91453 | 151 | if ! _namecom_rest GET "domains"; then |
6963f388 | 152 | return 1 |
e64ad517 | 153 | fi |
6963f388 R |
154 | |
155 | # Need to exclude the last field (tld) | |
156 | numfields=$(echo "$domain" | _egrep_o "\." | wc -l) | |
7b32bbfc | 157 | while [ $i -le "$numfields" ]; do |
6963f388 R |
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 | |
e64ad517 R |
172 | return 1 |
173 | } |