]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_cn.sh
Merge pull request #3208 from cusae/dev
[mirror_acme.sh.git] / dnsapi / dns_cn.sh
CommitLineData
110a41d1 1#!/usr/bin/env sh
2
3# DNS API for acme.sh for Core-Networks (https://beta.api.core-networks.de/doc/).
4# created by 5ll and francis
5
6CN_API="https://beta.api.core-networks.de"
7
8######## Public functions #####################
9
f5850d0c 10dns_cn_add() {
110a41d1 11 fulldomain=$1
12 txtvalue=$2
13
14 if ! _cn_login; then
15 _err "login failed"
16 return 1
17 fi
18
19 _debug "First detect the root zone"
20 if ! _cn_get_root "$fulldomain"; then
21 _err "invalid domain"
22 return 1
23 fi
f5850d0c 24
110a41d1 25 _debug "_sub_domain $_sub_domain"
26 _debug "_domain $_domain"
f5850d0c 27
110a41d1 28 _info "Adding record"
29 curData="{\"name\":\"$_sub_domain\",\"ttl\":120,\"type\":\"TXT\",\"data\":\"$txtvalue\"}"
30 curResult="$(_post "${curData}" "${CN_API}/dnszones/${_domain}/records/")"
31
32 _debug "curData $curData"
33 _debug "curResult $curResult"
f5850d0c 34
110a41d1 35 if _contains "$curResult" ""; then
36 _info "Added, OK"
37
38 if ! _cn_commit; then
39 _err "commiting changes failed"
40 return 1
41 fi
42 return 0
f5850d0c 43
110a41d1 44 else
45 _err "Add txt record error."
46 _debug "curData is $curData"
47 _debug "curResult is $curResult"
48 _err "error adding text record, response was $curResult"
49 return 1
50 fi
51}
52
f5850d0c 53dns_cn_rm() {
110a41d1 54 fulldomain=$1
55 txtvalue=$2
56
57 if ! _cn_login; then
58 _err "login failed"
59 return 1
60 fi
61
62 _debug "First detect the root zone"
63 if ! _cn_get_root "$fulldomain"; then
64 _err "invalid domain"
65 return 1
66 fi
f5850d0c 67
110a41d1 68 _info "Deleting record"
69 curData="{\"name\":\"$_sub_domain\",\"data\":\"$txtvalue\"}"
70 curResult="$(_post "${curData}" "${CN_API}/dnszones/${_domain}/records/delete")"
71 _debug curData is "$curData"
725addaf 72
110a41d1 73 _info "commiting changes"
f5850d0c 74 if ! _cn_commit; then
110a41d1 75 _err "commiting changes failed"
76 return 1
77 fi
78
79 _info "Deletet txt record"
80 return 0
81}
82
110a41d1 83################### Private functions below ##################################
84_cn_login() {
85 CN_User="${CN_User:-$(_readaccountconf_mutable CN_User)}"
86 CN_Password="${CN_Password:-$(_readaccountconf_mutable CN_Password)}"
87 if [ -z "$CN_User" ] || [ -z "$CN_Password" ]; then
88 CN_User=""
89 CN_Password=""
90 _err "You must export variables: CN_User and CN_Password"
91 return 1
92 fi
93
94 #save the config variables to the account conf file.
95 _saveaccountconf_mutable CN_User "$CN_User"
96 _saveaccountconf_mutable CN_Password "$CN_Password"
97
98 _info "Getting an AUTH-Token"
99 curData="{\"login\":\"${CN_User}\",\"password\":\"${CN_Password}\"}"
100 curResult="$(_post "${curData}" "${CN_API}/auth/token")"
101 _debug "Calling _CN_login: '${curData}' '${CN_API}/auth/token'"
f5850d0c 102
110a41d1 103 if _contains "${curResult}" '"token":"'; then
104 authToken=$(echo "${curResult}" | cut -d ":" -f2 | cut -d "," -f1 | sed 's/^.\(.*\).$/\1/')
105 export _H1="Authorization: Bearer $authToken"
106 _info "Successfully acquired AUTH-Token"
107 _debug "AUTH-Token: '${authToken}'"
108 _debug "_H1 '${_H1}'"
109 else
110 _err "Couldn't acquire an AUTH-Token"
111 return 1
112 fi
113}
114
115# Commit changes
f5850d0c 116_cn_commit() {
110a41d1 117 _info "Commiting changes"
118 _post "" "${CN_API}/dnszones/$h/records/commit"
119}
120
f5850d0c 121_cn_get_root() {
110a41d1 122 domain=$1
123 i=2
124 p=1
125 while true; do
126
127 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
128 _debug h "$h"
129 _debug _H1 "${_H1}"
130
131 if [ -z "$h" ]; then
132 #not valid
133 return 1
134 fi
725addaf 135
110a41d1 136 _cn_zonelist="$(_get ${CN_API}/dnszones/)"
137 _debug _cn_zonelist "${_cn_zonelist}"
138
139 if [ "$?" != "0" ]; then
140 _err "something went wrong while getting the zone list"
141 return 1
142 fi
143
144 if _contains "$_cn_zonelist" "\"name\":\"$h\"" >/dev/null; then
145 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
146 _domain=$h
147 return 0
148 else
149 _debug "Zonelist does not contain domain - iterating "
150 fi
151 p=$i
152 i=$(_math "$i" + 1)
153
154 done
155 _err "Zonelist does not contain domain - exiting"
156 return 1
157}