]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_mydnsjp.sh
Merge pull request #4542 from alexleigh/master
[mirror_acme.sh.git] / dnsapi / dns_mydnsjp.sh
1 #!/usr/bin/env sh
2
3 #Here is a api script for MyDNS.JP.
4 #This file name is "dns_mydnsjp.sh"
5 #So, here must be a method dns_mydnsjp_add()
6 #Which will be called by acme.sh to add the txt record to your api system.
7 #returns 0 means success, otherwise error.
8 #
9 #Author: epgdatacapbon
10 #Report Bugs here: https://github.com/epgdatacapbon/acme.sh
11 #
12 ######## Public functions #####################
13
14 # Export MyDNS.JP MasterID and Password in following variables...
15 # MYDNSJP_MasterID=MasterID
16 # MYDNSJP_Password=Password
17
18 MYDNSJP_API="https://www.mydns.jp"
19
20 #Usage: dns_mydnsjp_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
21 dns_mydnsjp_add() {
22 fulldomain=$1
23 txtvalue=$2
24
25 _info "Using mydnsjp"
26 _debug fulldomain "$fulldomain"
27 _debug txtvalue "$txtvalue"
28
29 # Load the credentials from the account conf file
30 MYDNSJP_MasterID="${MYDNSJP_MasterID:-$(_readaccountconf_mutable MYDNSJP_MasterID)}"
31 MYDNSJP_Password="${MYDNSJP_Password:-$(_readaccountconf_mutable MYDNSJP_Password)}"
32 if [ -z "$MYDNSJP_MasterID" ] || [ -z "$MYDNSJP_Password" ]; then
33 MYDNSJP_MasterID=""
34 MYDNSJP_Password=""
35 _err "You don't specify mydnsjp api MasterID and Password yet."
36 _err "Please export as MYDNSJP_MasterID / MYDNSJP_Password and try again."
37 return 1
38 fi
39
40 # Save the credentials to the account conf file
41 _saveaccountconf_mutable MYDNSJP_MasterID "$MYDNSJP_MasterID"
42 _saveaccountconf_mutable MYDNSJP_Password "$MYDNSJP_Password"
43
44 _debug "First detect the root zone."
45 if ! _get_root "$fulldomain"; then
46 _err "invalid domain"
47 return 1
48 fi
49
50 _debug _sub_domain "$_sub_domain"
51 _debug _domain "$_domain"
52
53 if _mydnsjp_api "REGIST" "$_domain" "$txtvalue"; then
54 if printf -- "%s" "$response" | grep "OK." >/dev/null; then
55 _info "Added, OK"
56 return 0
57 else
58 _err "Add txt record error."
59 return 1
60 fi
61 fi
62 _err "Add txt record error."
63
64 return 1
65 }
66
67 #Usage: fulldomain txtvalue
68 #Remove the txt record after validation.
69 dns_mydnsjp_rm() {
70 fulldomain=$1
71 txtvalue=$2
72
73 _info "Removing TXT record"
74 _debug fulldomain "$fulldomain"
75 _debug txtvalue "$txtvalue"
76
77 # Load the credentials from the account conf file
78 MYDNSJP_MasterID="${MYDNSJP_MasterID:-$(_readaccountconf_mutable MYDNSJP_MasterID)}"
79 MYDNSJP_Password="${MYDNSJP_Password:-$(_readaccountconf_mutable MYDNSJP_Password)}"
80 if [ -z "$MYDNSJP_MasterID" ] || [ -z "$MYDNSJP_Password" ]; then
81 MYDNSJP_MasterID=""
82 MYDNSJP_Password=""
83 _err "You don't specify mydnsjp api MasterID and Password yet."
84 _err "Please export as MYDNSJP_MasterID / MYDNSJP_Password and try again."
85 return 1
86 fi
87
88 _debug "First detect the root zone"
89 if ! _get_root "$fulldomain"; then
90 _err "invalid domain"
91 return 1
92 fi
93
94 _debug _sub_domain "$_sub_domain"
95 _debug _domain "$_domain"
96
97 if _mydnsjp_api "DELETE" "$_domain" "$txtvalue"; then
98 if printf -- "%s" "$response" | grep "OK." >/dev/null; then
99 _info "Deleted, OK"
100 return 0
101 else
102 _err "Delete txt record error."
103 return 1
104 fi
105 fi
106 _err "Delete txt record error."
107
108 return 1
109 }
110
111 #################### Private functions below ##################################
112 # _acme-challenge.www.domain.com
113 # returns
114 # _sub_domain=_acme-challenge.www
115 # _domain=domain.com
116 _get_root() {
117 fulldomain=$1
118 i=2
119 p=1
120
121 # Get the root domain
122 _mydnsjp_retrieve_domain
123 if [ "$?" != "0" ]; then
124 # not valid
125 return 1
126 fi
127
128 while true; do
129 _domain=$(printf "%s" "$fulldomain" | cut -d . -f $i-100)
130
131 if [ -z "$_domain" ]; then
132 # not valid
133 return 1
134 fi
135
136 if [ "$_domain" = "$_root_domain" ]; then
137 _sub_domain=$(printf "%s" "$fulldomain" | cut -d . -f 1-$p)
138 return 0
139 fi
140
141 p=$i
142 i=$(_math "$i" + 1)
143 done
144
145 return 1
146 }
147
148 # Retrieve the root domain
149 # returns 0 success
150 _mydnsjp_retrieve_domain() {
151 _debug "Login to MyDNS.JP"
152
153 response="$(_post "MENU=100&masterid=$MYDNSJP_MasterID&masterpwd=$MYDNSJP_Password" "$MYDNSJP_API/members/")"
154 cookie="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2)"
155
156 # If cookies is not empty then logon successful
157 if [ -z "$cookie" ]; then
158 _err "Fail to get a cookie."
159 return 1
160 fi
161
162 _root_domain=$(echo "$response" | grep "DNSINFO\[domainname\]" | sed 's/^.*value="\([^"]*\)".*/\1/')
163
164 _debug _root_domain "$_root_domain"
165
166 if [ -z "$_root_domain" ]; then
167 _err "Fail to get the root domain."
168 return 1
169 fi
170
171 return 0
172 }
173
174 _mydnsjp_api() {
175 cmd=$1
176 domain=$2
177 txtvalue=$3
178
179 # Base64 encode the credentials
180 credentials=$(printf "%s:%s" "$MYDNSJP_MasterID" "$MYDNSJP_Password" | _base64)
181
182 # Construct the HTTP Authorization header
183 export _H1="Content-Type: application/x-www-form-urlencoded"
184 export _H2="Authorization: Basic ${credentials}"
185
186 response="$(_post "CERTBOT_DOMAIN=$domain&CERTBOT_VALIDATION=$txtvalue&EDIT_CMD=$cmd" "$MYDNSJP_API/directedit.html")"
187
188 if [ "$?" != "0" ]; then
189 _err "error $domain"
190 return 1
191 fi
192
193 _debug2 response "$response"
194
195 return 0
196 }