]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_transip.sh
Remove extra newline
[mirror_acme.sh.git] / dnsapi / dns_transip.sh
1 #!/usr/bin/env sh
2 TRANSIP_Api_Url="https://api.transip.nl/v6"
3 TRANSIP_Key_File="transip2.key"
4 TRANSIP_Token_Read_Only="false"
5 TRANSIP_Token_Global_Key="false"
6 TRANSIP_Token_Expiration="30 minutes"
7 # You can't reuse a label token, so we leave this empty normally
8 TRANSIP_Token_Label=""
9
10 ######## Public functions #####################
11 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
12 dns_transip_add() {
13 fulldomain="$1"
14 _debug fulldomain="$fulldomain"
15 txtvalue="$2"
16 _debug txtvalue="$txtvalue"
17 _transip_setup "$fulldomain" || return 1
18 _info "Creating TXT record."
19 if ! _transip_rest POST "domains/$_domain/dns" "{\"dnsEntry\":{\"name\":\"$_sub_domain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"expire\":300}}"; then
20 _err "Could not add TXT record."
21 return 1
22 fi
23 return 0
24 }
25
26 dns_transip_rm() {
27 fulldomain=$1
28 _debug fulldomain="$fulldomain"
29 txtvalue=$2
30 _debug txtvalue="$txtvalue"
31 _transip_setup "$fulldomain" || return 1
32 _info "Removing TXT record."
33 if ! _transip_rest DELETE "domains/$_domain/dns" "{\"dnsEntry\":{\"name\":\"$_sub_domain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"expire\":300}}"; then
34 _err "Could not remove TXT record $_sub_domain for $domain"
35 return 1
36 fi
37 return 0
38 }
39
40 #################### Private functions below ##################################
41 #_acme-challenge.www.domain.com
42 #returns
43 # _sub_domain=_acme-challenge.www
44 # _domain=domain.com
45 _get_root() {
46 domain="$1"
47 i=2
48 p=1
49 while true; do
50 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
51
52 if [ -z "$h" ]; then
53 #not valid
54 return 1
55 fi
56
57 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
58 _domain="$h"
59
60 if _transip_rest GET "domains/$h/dns" && _contains "$response" "dnsEntries"; then
61 return 0
62 fi
63
64 p=$i
65 i=$(_math "$i" + 1)
66 done
67 _err "Unable to parse this domain"
68 return 1
69 }
70
71 _transip_rest() {
72 m="$1"
73 ep="$2"
74 data="$3"
75 _debug ep "$ep"
76 export _H1="Accept: application/json"
77 export _H2="Authorization: Bearer $_token"
78 export _H4="Content-Type: application/json"
79 if [ "$m" != "GET" ]; then
80 _debug data "$data"
81 response="$(_post "$data" "$TRANSIP_Api_Url/$ep" "" "$m")"
82 retcode=$?
83 else
84 response="$(_get "$TRANSIP_Api_Url/$ep")"
85 retcode=$?
86 fi
87
88 if [ "$retcode" != "0" ]; then
89 _err "error $ep"
90 return 1
91 fi
92 _debug2 response "$response"
93 return 0
94 }
95
96 _transip_get_token() {
97 nonce=$(openssl rand -hex 12)
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 }