]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_mydevil.sh
Merge pull request #4646 from Eagle3386/patch-1
[mirror_acme.sh.git] / dnsapi / dns_mydevil.sh
1 #!/usr/bin/env sh
2
3 # MyDevil.net API (2019-02-03)
4 #
5 # MyDevil.net already supports automatic Let's Encrypt certificates,
6 # except for wildcard domains.
7 #
8 # This script depends on `devil` command that MyDevil.net provides,
9 # which means that it works only on server side.
10 #
11 # Author: Marcin Konicki <https://ahwayakchih.neoni.net>
12 #
13 ######## Public functions #####################
14
15 #Usage: dns_mydevil_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
16 dns_mydevil_add() {
17 fulldomain=$1
18 txtvalue=$2
19 domain=""
20
21 if ! _exists "devil"; then
22 _err "Could not find 'devil' command."
23 return 1
24 fi
25
26 _info "Using mydevil"
27
28 domain=$(mydevil_get_domain "$fulldomain")
29 if [ -z "$domain" ]; then
30 _err "Invalid domain name: could not find root domain of $fulldomain."
31 return 1
32 fi
33
34 # No need to check if record name exists, `devil` always adds new record.
35 # In worst case scenario, we end up with multiple identical records.
36
37 _info "Adding $fulldomain record for domain $domain"
38 if devil dns add "$domain" "$fulldomain" TXT "$txtvalue"; then
39 _info "Successfully added TXT record, ready for validation."
40 return 0
41 else
42 _err "Unable to add DNS record."
43 return 1
44 fi
45 }
46
47 #Usage: fulldomain txtvalue
48 #Remove the txt record after validation.
49 dns_mydevil_rm() {
50 fulldomain=$1
51 txtvalue=$2
52 domain=""
53
54 if ! _exists "devil"; then
55 _err "Could not find 'devil' command."
56 return 1
57 fi
58
59 _info "Using mydevil"
60
61 domain=$(mydevil_get_domain "$fulldomain")
62 if [ -z "$domain" ]; then
63 _err "Invalid domain name: could not find root domain of $fulldomain."
64 return 1
65 fi
66
67 # catch one or more numbers
68 num='[0-9][0-9]*'
69 # catch one or more whitespace
70 w=$(printf '[\t ][\t ]*')
71 # catch anything, except newline
72 any='.*'
73 # filter to make sure we do not delete other records
74 validRecords="^${num}${w}${fulldomain}${w}TXT${w}${any}${txtvalue}$"
75 for id in $(devil dns list "$domain" | tail -n+2 | grep "${validRecords}" | cut -w -s -f 1); do
76 _info "Removing record $id from domain $domain"
77 echo "y" | devil dns del "$domain" "$id" || _err "Could not remove DNS record."
78 done
79 }
80
81 #################### Private functions below ##################################
82
83 # Usage: domain=$(mydevil_get_domain "_acme-challenge.www.domain.com" || _err "Invalid domain name")
84 # echo $domain
85 mydevil_get_domain() {
86 fulldomain=$1
87 domain=""
88
89 for domain in $(devil dns list | cut -w -s -f 1 | tail -n+2); do
90 _debug "Checking domain: $domain"
91 if _endswith "$fulldomain" "$domain"; then
92 _debug "Fulldomain '$fulldomain' matches '$domain'"
93 printf -- "%s" "$domain"
94 return 0
95 fi
96 done
97
98 return 1
99 }