]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_transip.sh
Remove default key file (leave it to the user to explicitly specify)
[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=$(openssl rand -hex 12)
97
98 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}\"}"
99 _debug data "$data"
100
101 #_signature=$(printf "%s" "$data" | openssl dgst -sha512 -sign "$TRANSIP_Key_File" | _base64)
102 _signature=$(printf "%s" "$data" | _sign "$TRANSIP_Key_File" "sha512")
103 _debug2 _signature "$_signature"
104
105 export _H1="Signature: $_signature"
106 export _H2="Content-Type: application/json"
107
108 response="$(_post "$data" "$TRANSIP_Api_Url/auth" "" "POST")"
109 retcode=$?
110 _debug2 response "$response"
111 if [ "$retcode" != "0" ]; then
112 _err "Authentication failed."
113 return 1
114 fi
115 if _contains "$response" "token"; then
116 _token="$(echo "$response" | _normalizeJson | sed -n 's/^{"token":"\(.*\)"}/\1/p')"
117 _debug _token "$_token"
118 return 0
119 fi
120 return 1
121 }
122
123 _transip_setup() {
124 fulldomain=$1
125
126 # retrieve the transip creds
127 TRANSIP_Username="${TRANSIP_Username:-$(_readaccountconf_mutable TRANSIP_Username)}"
128 TRANSIP_Key_File="${TRANSIP_Key_File:-$(_readaccountconf_mutable TRANSIP_Key_File)}"
129 # check their vals for null
130 if [ -z "$TRANSIP_Username" ] || [ -z "$TRANSIP_Key_File" ]; then
131 TRANSIP_Username=""
132 TRANSIP_Key_File=""
133 _err "You didn't specify a TransIP username and api key file location"
134 _err "Please set those values and try again."
135 return 1
136 fi
137 # save the username and api key to the account conf file.
138 _saveaccountconf_mutable TRANSIP_Username "$TRANSIP_Username"
139 _saveaccountconf_mutable TRANSIP_Key_File "$TRANSIP_Key_File"
140
141 if [ -f "$TRANSIP_Key_File" ]; then
142 if ! grep "BEGIN PRIVATE KEY" "$TRANSIP_Key_File" >/dev/null 2>&1; then
143 _err "Key file doesn't seem to be a valid key: ${TRANSIP_Key_File}"
144 return 1
145 fi
146 else
147 _err "Can't read private key file: ${TRANSIP_Key_File}"
148 return 1
149 fi
150
151 if [ -z "$_token" ]; then
152 if ! _transip_get_token; then
153 _err "Can not get token."
154 return 1
155 fi
156 fi
157
158 _get_root "$fulldomain" || return 1
159
160 return 0
161 }