]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_namesilo.sh
Merge pull request #4658 from Justman10000/master
[mirror_acme.sh.git] / dnsapi / dns_namesilo.sh
CommitLineData
eb207322
M
1#!/usr/bin/env sh
2
3#Author: meowthink
4#Created 01/14/2017
5#Utilize namesilo.com API to finish dns-01 verifications.
6
7Namesilo_API="https://www.namesilo.com/api"
8
9######## Public functions #####################
10
11#Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
12dns_namesilo_add() {
13 fulldomain=$1
14 txtvalue=$2
15
16 if [ -z "$Namesilo_Key" ]; then
17 Namesilo_Key=""
18 _err "API token for namesilo.com is missing."
19 _err "Please specify that in your environment variable."
20 return 1
21 fi
22
23 #save the api key and email to the account conf file.
24 _saveaccountconf Namesilo_Key "$Namesilo_Key"
25
26 if ! _get_root "$fulldomain"; then
27 _err "Unable to find domain specified."
28 return 1
29 fi
30
31 _debug _sub_domain "$_sub_domain"
32 _debug _domain "$_domain"
33
34 _debug txtvalue "$txtvalue"
35 if _namesilo_rest GET "dnsAddRecord?version=1&type=xml&key=$Namesilo_Key&domain=$_domain&rrtype=TXT&rrhost=$_sub_domain&rrvalue=$txtvalue"; then
36 retcode=$(printf "%s\n" "$response" | _egrep_o "<code>300")
37 if [ "$retcode" ]; then
38 _info "Successfully added TXT record, ready for validation."
39 return 0
40 else
41 _err "Unable to add the DNS record."
42 return 1
43 fi
44 fi
45}
46
47#Usage: fulldomain txtvalue
48#Remove the txt record after validation.
49dns_namesilo_rm() {
50 fulldomain=$1
51 txtvalue=$2
52
53 if ! _get_root "$fulldomain"; then
54 _err "Unable to find domain specified."
55 return 1
56 fi
57
58 # Get the record id.
59 if _namesilo_rest GET "dnsListRecords?version=1&type=xml&key=$Namesilo_Key&domain=$_domain"; then
60 retcode=$(printf "%s\n" "$response" | _egrep_o "<code>300")
61 if [ "$retcode" ]; then
28cadc5e 62 _record_id=$(echo "$response" | _egrep_o "<record_id>([^<]*)</record_id><type>TXT</type><host>$fulldomain</host>" | _egrep_o "<record_id>([^<]*)</record_id>" | sed -r "s/<record_id>([^<]*)<\/record_id>/\1/" | tail -n 1)
5c09788e 63 _debug _record_id "$_record_id"
64 if [ "$_record_id" ]; then
28cadc5e 65 _info "Successfully retrieved the record id for ACME challenge."
66 else
67 _info "Empty record id, it seems no such record."
68 return 0
69 fi
eb207322
M
70 else
71 _err "Unable to retrieve the record id."
72 return 1
73 fi
74 fi
75
76 # Remove the DNS record using record id.
77 if _namesilo_rest GET "dnsDeleteRecord?version=1&type=xml&key=$Namesilo_Key&domain=$_domain&rrid=$_record_id"; then
78 retcode=$(printf "%s\n" "$response" | _egrep_o "<code>300")
79 if [ "$retcode" ]; then
80 _info "Successfully removed the TXT record."
81 return 0
82 else
83 _err "Unable to remove the DNS record."
84 return 1
85 fi
86 fi
87}
88
89#################### Private functions below ##################################
90
91# _acme-challenge.www.domain.com
92# returns
93# _sub_domain=_acme-challenge.www
94# _domain=domain.com
95_get_root() {
96 domain=$1
97 i=2
98 p=1
99
100 if ! _namesilo_rest GET "listDomains?version=1&type=xml&key=$Namesilo_Key"; then
101 return 1
102 fi
103
104 # Need to exclude the last field (tld)
105 numfields=$(echo "$domain" | _egrep_o "\." | wc -l)
106 while [ $i -le "$numfields" ]; do
107 host=$(printf "%s" "$domain" | cut -d . -f $i-100)
108 _debug host "$host"
109 if [ -z "$host" ]; then
110 return 1
111 fi
112
3dde83d8 113 if _contains "$response" ">$host</domain>"; then
eb207322
M
114 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
115 _domain="$host"
116 return 0
117 fi
118 p=$i
119 i=$(_math "$i" + 1)
120 done
121 return 1
122}
123
124_namesilo_rest() {
125 method=$1
126 param=$2
127 data=$3
128
129 if [ "$method" != "GET" ]; then
130 response="$(_post "$data" "$Namesilo_API/$param" "" "$method")"
131 else
132 response="$(_get "$Namesilo_API/$param")"
133 fi
134
135 if [ "$?" != "0" ]; then
136 _err "error $param"
137 return 1
138 fi
139
140 _debug2 response "$response"
141 return 0
142}