]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_knot.sh
Merge pull request #4542 from alexleigh/master
[mirror_acme.sh.git] / dnsapi / dns_knot.sh
1 #!/usr/bin/env sh
2
3 ######## Public functions #####################
4
5 #Usage: dns_knot_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
6 dns_knot_add() {
7 fulldomain=$1
8 txtvalue=$2
9 _checkKey || return 1
10 [ -n "${KNOT_SERVER}" ] || KNOT_SERVER="localhost"
11 # save the dns server and key to the account.conf file.
12 _saveaccountconf KNOT_SERVER "${KNOT_SERVER}"
13 _saveaccountconf KNOT_KEY "${KNOT_KEY}"
14
15 if ! _get_root "$fulldomain"; then
16 _err "Domain does not exist."
17 return 1
18 fi
19
20 _info "Adding ${fulldomain}. 60 TXT \"${txtvalue}\""
21
22 knsupdate <<EOF
23 server ${KNOT_SERVER}
24 key ${KNOT_KEY}
25 zone ${_domain}.
26 update add ${fulldomain}. 60 TXT "${txtvalue}"
27 send
28 quit
29 EOF
30
31 if [ $? -ne 0 ]; then
32 _err "Error updating domain."
33 return 1
34 fi
35
36 _info "Domain TXT record successfully added."
37 return 0
38 }
39
40 #Usage: dns_knot_rm _acme-challenge.www.domain.com
41 dns_knot_rm() {
42 fulldomain=$1
43 _checkKey || return 1
44 [ -n "${KNOT_SERVER}" ] || KNOT_SERVER="localhost"
45
46 if ! _get_root "$fulldomain"; then
47 _err "Domain does not exist."
48 return 1
49 fi
50
51 _info "Removing ${fulldomain}. TXT"
52
53 knsupdate <<EOF
54 server ${KNOT_SERVER}
55 key ${KNOT_KEY}
56 zone ${_domain}.
57 update del ${fulldomain}. TXT
58 send
59 quit
60 EOF
61
62 if [ $? -ne 0 ]; then
63 _err "error updating domain"
64 return 1
65 fi
66
67 _info "Domain TXT record successfully deleted."
68 return 0
69 }
70
71 #################### Private functions below ##################################
72 # _acme-challenge.www.domain.com
73 # returns
74 # _domain=domain.com
75 _get_root() {
76 domain=$1
77 i="$(echo "$fulldomain" | tr '.' ' ' | wc -w)"
78 i=$(_math "$i" - 1)
79
80 while true; do
81 h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
82 if [ -z "$h" ]; then
83 return 1
84 fi
85 _domain="$h"
86 return 0
87 done
88 _debug "$domain not found"
89 return 1
90 }
91
92 _checkKey() {
93 if [ -z "${KNOT_KEY}" ]; then
94 _err "You must specify a TSIG key to authenticate the request."
95 return 1
96 fi
97 }