-
Notifications
You must be signed in to change notification settings - Fork 1
/
loopia_hook.sh
248 lines (216 loc) · 5.7 KB
/
loopia_hook.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env bash
# This is built upon https://gist.github.com/jreinert/49aca3b5f3bf2c5d73d8 by Joakim Reinert, and edited to match the Loopia API
#
# Copyright (c) Anton Andersson. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
API_URL='https://api.loopia.se/RPCSERV'
TMPDIR='/tmp/loopia-acme'
source "$BASEDIR/hooks/loopia/loopia-api-user.auth" # contains user and pass variables
USER=$user
PASS=$pass
build_method_call() {
local method_name=$1
shift
echo -n "<?xml version=\"1.0\"?>"
echo -n "<methodCall>"
echo -n "<methodName>$method_name</methodName><params>"
for param in "$@"; do
echo -n "$param"
done
echo -n "</params></methodCall>"
}
build_param() {
local value=$1
local type=${2:-string}
echo -n "<param><value><$type>"
echo -n "$value"
echo -n "</$type></value></param>"
}
build_get_records_call() {
local subdomain=$1
local domain=$2
build_method_call 'getZoneRecords' \
"$(build_param $USER)" \
"$(build_param $PASS)" \
"$(build_param $domain)" \
"$(build_param $subdomain)"
}
build_array_update() {
local token=$1
local record_id=${2:-0}
echo -n '<param>'
echo -n '<value><struct>'
echo -n '<member>'
echo -n "<name>type</name>"
echo -n "<value><string>TXT</string></value>"
echo -n "</member>"
echo -n '<member>'
echo -n "<name>ttl</name>"
echo -n "<value><int>300</int></value>"
echo -n "</member>"
echo -n '<member>'
echo -n "<name>rdata</name>"
echo -n "<value><string>$token</string></value>"
echo -n "</member>"
echo -n '<member>'
echo -n "<name>record_id</name>"
echo -n "<value><int>$record_id</int></value>"
echo -n "</member>"
echo -n '<member>'
echo -n "<name>priority</name>"
echo -n "<value><int>1</int></value>"
echo -n "</member>"
echo -n "</struct></value>"
echo -n "</param>"
}
build_update_record_call() {
local subdomain=$1
local domain=$2
local token=$3
local record_id=${4:-0}
build_method_call 'updateZoneRecord' \
"$(build_param $USER)" \
"$(build_param $PASS)" \
"$(build_param $domain)" \
"$(build_param $subdomain)" \
"$(build_array_update $token $record_id)"
}
build_array_add() {
local token=$1
echo -n '<param>'
echo -n '<value><struct>'
echo -n '<member>'
echo -n "<name>type</name>"
echo -n "<value><string>TXT</string></value>"
echo -n "</member>"
echo -n '<member>'
echo -n "<name>ttl</name>"
echo -n "<value><int>300</int></value>"
echo -n "</member>"
echo -n '<member>'
echo -n "<name>rdata</name>"
echo -n "<value><string>$token</string></value>"
echo -n "</member>"
echo -n '<member>'
echo -n "<name>priority</name>"
echo -n "<value><int>10</int></value>"
echo -n "</member>"
echo -n "</struct></value>"
echo -n "</param>"
}
build_add_record_call() {
local subdomain=$1
local domain=$2
local token=$3
build_method_call 'addZoneRecord' \
"$(build_param $USER)" \
"$(build_param $PASS)" \
"$(build_param $domain)" \
"$(build_param $subdomain)" \
"$(build_array_add $token)"
}
build_remove_record_call() {
local subdomain=$1
local domain=$2
local record_id=$3
build_method_call 'removeZoneRecord' \
"$(build_param $USER)" \
"$(build_param $PASS)" \
"$(build_param $domain)" \
"$(build_param $subdomain)" \
"$(build_param $record_id "int")"
}
method_call() {
local call=$1
local result=$(curl -s -c "$TMPDIR/cookies" -d "$call" -H 'Content-Type: text/xml' "$API_URL")
echo "$result"
echo >&2
return 1
}
deploy_challenge() {
local domain=$1
local token=$2
echo "$token"
local result=$(method_call \
"$(build_add_record_call "_acme-challenge" "$domain" "$token")")
echo "$result"
}
clean_challenge() {
local domain=$1
local result=$(method_call \
"$(build_get_records_call "_acme-challenge" "$domain")")
local xpath='//methodResponse//params//member/name[text()="record_id"]/../value/int'
result=$(xmllint --xpath "$xpath" - <<< $result)
result=${result//<\/int><int>/" "}
result=${result//<int>/""}
result=${result//<\/int>/""}
local array=($result)
for i in ${array[@]}
do
$(method_call \
"$(build_remove_record_call "_acme-challenge" "$domain" "$i")")
done
}
deploy_cert() {
# you don't really need to do this, and could link directly to
# the symlinks in the dehydrated cert folder, but I like nginx
# and to keep my nginx-config-files clean
local DOMAIN=$1
local KEYFILE=$2
local FULLCHAINFILE=$3
ln -sf "$KEYFILE" "/etc/nginx/ssl/$DOMAIN/privkey.pem"
ln -sf "$FULLCHAINFILE" "/etc/nginx/ssl/$DOMAIN/fullchain.pem"
service nginx reload
}
invalid_challenge() {
local RESPONSE=$2
echo "$RESPONSE"
}
unchanged_cert() {
local DOMAIN=$1
local KEYFILE=$2
local FULLCHAINFILE=$3
echo "$DOMAIN"
echo "$KEYFILE"
echo "$FULLCHAINFILE"
}
mkdir -p "$TMPDIR"
case $1 in
'deploy_challenge')
deploy_challenge "$2" "$4"
sleep 305
;;
'clean_challenge')
# this seems to clear the challenge txt before letsencypt have had a chance to validate
# so I moved the cleaning to when the cert is deployed instead
# clean_challenge "$2"
;;
'deploy_cert')
deploy_cert "$2" "$3" "$5"
clean_challenge "$2"
# used to keep track of when a cert was last deployed
echo "deployed $2" > "$TMPDIR/$2.deploycert"
;;
'unchanged_cert')
# used to keep track of when a cert was last requested - to see that cron works as it should
unchanged_cert "$2" "$3" "$5" > "$TMPDIR/$2.unchanged"
;;
'invalid_challenge')
# used to keep track of fails
invalid_challenge "$2" > "$TMPDIR/$2.fail"
;;
'request_failure')
# do something
;;
'generate_csr')
# do something
;;
'startup_hook')
# do something
;;
'exit_hook')
# do something
;;
esac