]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_knot.sh
Merge pull request #3734 from acmesh-official/dev
[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 -y "${KNOT_KEY}" <<EOF
23 server ${KNOT_SERVER}
24 zone ${_domain}.
25 update add ${fulldomain}. 60 TXT "${txtvalue}"
26 send
27 quit
28 EOF
29
30 if [ $? -ne 0 ]; then
31 _err "Error updating domain."
32 return 1
33 fi
34
35 _info "Domain TXT record successfully added."
36 return 0
37 }
38
39 #Usage: dns_knot_rm _acme-challenge.www.domain.com
40 dns_knot_rm() {
41 fulldomain=$1
42 _checkKey || return 1
43 [ -n "${KNOT_SERVER}" ] || KNOT_SERVER="localhost"
44
45 if ! _get_root "$fulldomain"; then
46 _err "Domain does not exist."
47 return 1
48 fi
49
50 _info "Removing ${fulldomain}. TXT"
51
52 knsupdate -y "${KNOT_KEY}" <<EOF
53 server ${KNOT_SERVER}
54 zone ${_domain}.
55 update del ${fulldomain}. TXT
56 send
57 quit
58 EOF
59
60 if [ $? -ne 0 ]; then
61 _err "error updating domain"
62 return 1
63 fi
64
65 _info "Domain TXT record successfully deleted."
66 return 0
67 }
68
69 #################### Private functions below ##################################
70 # _acme-challenge.www.domain.com
71 # returns
72 # _domain=domain.com
73 _get_root() {
74 domain=$1
75 i="$(echo "$fulldomain" | tr '.' ' ' | wc -w)"
76 i=$(_math "$i" - 1)
77
78 while true; do
79 h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
80 if [ -z "$h" ]; then
81 return 1
82 fi
83 _domain="$h"
84 return 0
85 done
86 _debug "$domain not found"
87 return 1
88 }
89
90 _checkKey() {
91 if [ -z "${KNOT_KEY}" ]; then
92 _err "You must specify a TSIG key to authenticate the request."
93 return 1
94 fi
95 }