]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_namesilo.sh
Merge pull request #1142 from maomihz/dev
[mirror_acme.sh.git] / dnsapi / dns_namesilo.sh
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
7 Namesilo_API="https://www.namesilo.com/api"
8
9 ######## Public functions #####################
10
11 #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
12 dns_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.
49 dns_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
62 _record_id=$(printf "%s\n" "$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)
63 _debug record_id "$_record_id"
64 _info "Successfully retrieved the record id for ACME challenge."
65 else
66 _err "Unable to retrieve the record id."
67 return 1
68 fi
69 fi
70
71 # Remove the DNS record using record id.
72 if _namesilo_rest GET "dnsDeleteRecord?version=1&type=xml&key=$Namesilo_Key&domain=$_domain&rrid=$_record_id"; then
73 retcode=$(printf "%s\n" "$response" | _egrep_o "<code>300")
74 if [ "$retcode" ]; then
75 _info "Successfully removed the TXT record."
76 return 0
77 else
78 _err "Unable to remove the DNS record."
79 return 1
80 fi
81 fi
82 }
83
84 #################### Private functions below ##################################
85
86 # _acme-challenge.www.domain.com
87 # returns
88 # _sub_domain=_acme-challenge.www
89 # _domain=domain.com
90 _get_root() {
91 domain=$1
92 i=2
93 p=1
94
95 if ! _namesilo_rest GET "listDomains?version=1&type=xml&key=$Namesilo_Key"; then
96 return 1
97 fi
98
99 # Need to exclude the last field (tld)
100 numfields=$(echo "$domain" | _egrep_o "\." | wc -l)
101 while [ $i -le "$numfields" ]; do
102 host=$(printf "%s" "$domain" | cut -d . -f $i-100)
103 _debug host "$host"
104 if [ -z "$host" ]; then
105 return 1
106 fi
107
108 if _contains "$response" "$host"; then
109 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
110 _domain="$host"
111 return 0
112 fi
113 p=$i
114 i=$(_math "$i" + 1)
115 done
116 return 1
117 }
118
119 _namesilo_rest() {
120 method=$1
121 param=$2
122 data=$3
123
124 if [ "$method" != "GET" ]; then
125 response="$(_post "$data" "$Namesilo_API/$param" "" "$method")"
126 else
127 response="$(_get "$Namesilo_API/$param")"
128 fi
129
130 if [ "$?" != "0" ]; then
131 _err "error $param"
132 return 1
133 fi
134
135 _debug2 response "$response"
136 return 0
137 }