]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_mydnsjp.sh
Merge pull request #3734 from acmesh-official/dev
[mirror_acme.sh.git] / dnsapi / dns_mydnsjp.sh
CommitLineData
14ad5955 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
be5085f2 18MYDNSJP_API="https://www.mydns.jp"
14ad5955 19
20#Usage: dns_mydnsjp_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
21dns_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.
69dns_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 "masterid=$MYDNSJP_MasterID&masterpwd=$MYDNSJP_Password" "$MYDNSJP_API/?MENU=100")"
be5085f2 154 cookie="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2)"
14ad5955 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 _debug "Retrieve DOMAIN INFO page"
163
164 export _H1="Cookie:${cookie}"
165
166 response="$(_get "$MYDNSJP_API/?MENU=300")"
167
168 if [ "$?" != "0" ]; then
169 _err "Fail to retrieve DOMAIN INFO."
170 return 1
171 fi
172
173 _root_domain=$(echo "$response" | grep "DNSINFO\[domainname\]" | sed 's/^.*value="\([^"]*\)".*/\1/')
174
175 # Logout
176 response="$(_get "$MYDNSJP_API/?MENU=090")"
177
178 _debug _root_domain "$_root_domain"
179
180 if [ -z "$_root_domain" ]; then
181 _err "Fail to get the root domain."
182 return 1
183 fi
184
185 return 0
186}
187
188_mydnsjp_api() {
189 cmd=$1
190 domain=$2
191 txtvalue=$3
192
193 # Base64 encode the credentials
194 credentials=$(printf "%s:%s" "$MYDNSJP_MasterID" "$MYDNSJP_Password" | _base64)
195
196 # Construct the HTTP Authorization header
197 export _H1="Content-Type: application/x-www-form-urlencoded"
198 export _H2="Authorization: Basic ${credentials}"
199
200 response="$(_post "CERTBOT_DOMAIN=$domain&CERTBOT_VALIDATION=$txtvalue&EDIT_CMD=$cmd" "$MYDNSJP_API/directedit.html")"
201
202 if [ "$?" != "0" ]; then
203 _err "error $domain"
204 return 1
205 fi
206
207 _debug2 response "$response"
208
209 return 0
210}