]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_transip.sh
fix format
[mirror_acme.sh.git] / dnsapi / dns_transip.sh
1 #!/usr/bin/env sh
2 TRANSIP_Api_Url="https://api.transip.nl/v6"
3 TRANSIP_Token_Read_Only="false"
4 TRANSIP_Token_Global_Key="false"
5 TRANSIP_Token_Expiration="30 minutes"
6 # You can't reuse a label token, so we leave this empty normally
7 TRANSIP_Token_Label=""
8
9 ######## Public functions #####################
10 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
11 dns_transip_add() {
12 fulldomain="$1"
13 _debug fulldomain="$fulldomain"
14 txtvalue="$2"
15 _debug txtvalue="$txtvalue"
16 _transip_setup "$fulldomain" || return 1
17 _info "Creating TXT record."
18 if ! _transip_rest POST "domains/$_domain/dns" "{\"dnsEntry\":{\"name\":\"$_sub_domain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"expire\":300}}"; then
19 _err "Could not add TXT record."
20 return 1
21 fi
22 return 0
23 }
24
25 dns_transip_rm() {
26 fulldomain=$1
27 _debug fulldomain="$fulldomain"
28 txtvalue=$2
29 _debug txtvalue="$txtvalue"
30 _transip_setup "$fulldomain" || return 1
31 _info "Removing TXT record."
32 if ! _transip_rest DELETE "domains/$_domain/dns" "{\"dnsEntry\":{\"name\":\"$_sub_domain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"expire\":300}}"; then
33 _err "Could not remove TXT record $_sub_domain for $domain"
34 return 1
35 fi
36 return 0
37 }
38
39 #################### Private functions below ##################################
40 #_acme-challenge.www.domain.com
41 #returns
42 # _sub_domain=_acme-challenge.www
43 # _domain=domain.com
44 _get_root() {
45 domain="$1"
46 i=2
47 p=1
48 while true; do
49 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
50
51 if [ -z "$h" ]; then
52 #not valid
53 return 1
54 fi
55
56 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
57 _domain="$h"
58
59 if _transip_rest GET "domains/$h/dns" && _contains "$response" "dnsEntries"; then
60 return 0
61 fi
62
63 p=$i
64 i=$(_math "$i" + 1)
65 done
66 _err "Unable to parse this domain"
67 return 1
68 }
69
70 _transip_rest() {
71 m="$1"
72 ep="$2"
73 data="$3"
74 _debug ep "$ep"
75 export _H1="Accept: application/json"
76 export _H2="Authorization: Bearer $_token"
77 export _H4="Content-Type: application/json"
78 if [ "$m" != "GET" ]; then
79 _debug data "$data"
80 response="$(_post "$data" "$TRANSIP_Api_Url/$ep" "" "$m")"
81 retcode=$?
82 else
83 response="$(_get "$TRANSIP_Api_Url/$ep")"
84 retcode=$?
85 fi
86
87 if [ "$retcode" != "0" ]; then
88 _err "error $ep"
89 return 1
90 fi
91 _debug2 response "$response"
92 return 0
93 }
94
95 _transip_get_token() {
96 nonce=$(echo "TRANSIP$(_time)" | _digest sha1 hex | cut -c 1-32)
97 _debug nonce "$nonce"
98
99 data="{\"login\":\"${TRANSIP_Username}\",\"nonce\":\"${nonce}\",\"read_only\":\"${TRANSIP_Token_Read_Only}\",\"expiration_time\":\"${TRANSIP_Token_Expiration}\",\"label\":\"${TRANSIP_Token_Label}\",\"global_key\":\"${TRANSIP_Token_Global_Key}\"}"
100 _debug data "$data"
101
102 #_signature=$(printf "%s" "$data" | openssl dgst -sha512 -sign "$TRANSIP_Key_File" | _base64)
103 _signature=$(printf "%s" "$data" | _sign "$TRANSIP_Key_File" "sha512")
104 _debug2 _signature "$_signature"
105
106 export _H1="Signature: $_signature"
107 export _H2="Content-Type: application/json"
108
109 response="$(_post "$data" "$TRANSIP_Api_Url/auth" "" "POST")"
110 retcode=$?
111 _debug2 response "$response"
112 if [ "$retcode" != "0" ]; then
113 _err "Authentication failed."
114 return 1
115 fi
116 if _contains "$response" "token"; then
117 _token="$(echo "$response" | _normalizeJson | sed -n 's/^{"token":"\(.*\)"}/\1/p')"
118 _debug _token "$_token"
119 return 0
120 fi
121 return 1
122 }
123
124 _transip_setup() {
125 fulldomain=$1
126
127 # retrieve the transip creds
128 TRANSIP_Username="${TRANSIP_Username:-$(_readaccountconf_mutable TRANSIP_Username)}"
129 TRANSIP_Key_File="${TRANSIP_Key_File:-$(_readaccountconf_mutable TRANSIP_Key_File)}"
130 # check their vals for null
131 if [ -z "$TRANSIP_Username" ] || [ -z "$TRANSIP_Key_File" ]; then
132 TRANSIP_Username=""
133 TRANSIP_Key_File=""
134 _err "You didn't specify a TransIP username and api key file location"
135 _err "Please set those values and try again."
136 return 1
137 fi
138 # save the username and api key to the account conf file.
139 _saveaccountconf_mutable TRANSIP_Username "$TRANSIP_Username"
140 _saveaccountconf_mutable TRANSIP_Key_File "$TRANSIP_Key_File"
141
142 if [ -f "$TRANSIP_Key_File" ]; then
143 if ! grep "BEGIN PRIVATE KEY" "$TRANSIP_Key_File" >/dev/null 2>&1; then
144 _err "Key file doesn't seem to be a valid key: ${TRANSIP_Key_File}"
145 return 1
146 fi
147 else
148 _err "Can't read private key file: ${TRANSIP_Key_File}"
149 return 1
150 fi
151
152 if [ -z "$_token" ]; then
153 if ! _transip_get_token; then
154 _err "Can not get token."
155 return 1
156 fi
157 fi
158
159 _get_root "$fulldomain" || return 1
160
161 return 0
162 }