blob: 3b88342f4ea153795856f6a0d2345500de97f712 (
plain)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
#!/bin/bash
CONFIGFILE="/etc/default/dnscrypt-wrapper"
DAEMON="/usr/sbin/dnscrypt-wrapper"
. $CONFIGFILE
start_instance() {
if [ -z ${PIDFILE[$1]} ]; then
echo "No configuration for instance $1 found!"
return
fi
if [ -r ${PIDFILE[$1]} ]; then
PID=$(cat ${PIDFILE[$1]})
if [ -z "$PID" ] || ! kill -0 $PID ; then
echo "Removing stale PID file..."
rm -f ${PIDFILE[$1]}
else
echo "dnscrypt-wrapper (instance $1) already running!"
return
fi
fi
# dnscrypt-wrapper will NOT work without this. /dev/urandom is required in
# the chroot.
if [ -n "${CHROOTDIR[$1]}" ]; then
if [ "$(readlink -f ${CHROOTDIR[$1]})" != "/" ]; then
if [ ! -d ${CHROOTDIR[$1]} ]; then
mkdir -p ${CHROOTDIR[$1]}
chmod 755 ${CHROOTDIR[$1]}
fi
if [ ! -d ${CHROOTDIR[$1]}/dev ]; then
mkdir -p ${CHROOTDIR[$1]}/dev
chmod 755 ${CHROOTDIR[$1]}/dev
fi
if [ ! -c ${CHROOTDIR[$1]}/dev/urandom ]; then
mknod -m 666 ${CHROOTDIR[$1]}/dev/urandom c 1 9
fi
fi
fi
mkdir -p $(dirname ${PIDFILE[$1]})
# The child (unprivileged) process needs write access or the PID will not
# be written.
chmod 0700 $(dirname ${PIDFILE[$1]})
chown ${USER[$1]} $(dirname ${PIDFILE[$1]})
OPTIONS="-d"
if [ -n "${LISTENADDRESS[$1]}" ]; then
OPTIONS="${OPTIONS} --listen-address=${LISTENADDRESS[$1]}"
fi
if [ -n "${PIDFILE[$1]}" ]; then
OPTIONS="${OPTIONS} --pidfile=${PIDFILE[$1]}"
fi
if [ -n "${USER[$1]}" ]; then
OPTIONS="${OPTIONS} --user=${USER[$1]}"
fi
if [ -n "${DNSCRYPTDIR[$1]}" ]; then
if [ -n "$2" ] && [ "$2" == "rotate" ]; then
OPTIONS="${OPTIONS} --crypt-secretkey-file=${DNSCRYPTDIR[$1]}/crypt_secret.key,${DNSCRYPTDIR[$1]}/crypt_secret.key_prev"
else
OPTIONS="${OPTIONS} --crypt-secretkey-file=${DNSCRYPTDIR[$1]}/crypt_secret.key"
fi
OPTIONS="${OPTIONS} --provider-publickey-file=${DNSCRYPTDIR[$1]}/public.key"
OPTIONS="${OPTIONS} --provider-secretkey-file=${DNSCRYPTDIR[$1]}/secret.key"
fi
if [ -z "${DNSCRYPTDIR[$1]}" ] && [ -n "${CRYPTSECRETKEYFILE[$1]}" ]; then
OPTIONS="${OPTIONS} --crypt-secretkey-file=${CRYPTSECRETKEYFILE[$1]}"
fi
if [ -z "${DNSCRYPTDIR[$1]}" ] && [ -n "${PROVIDERPUBLICKEYFILE[$1]}" ]; then
OPTIONS="${OPTIONS} --provider-publickey-file=${PROVIDERPUBLICKEYFILE[$1]}"
fi
if [ -z "${DNSCRYPTDIR[$1]}" ] && [ -n "${PROVIDERSECRETKEYFILE[$1]}" ]; then
OPTIONS="${OPTIONS} --provider-secretkey-file=${PROVIDERSECRETKEYFILE[$1]}"
fi
if [ -n "${RESOLVERADDRESS[$1]}" ]; then
OPTIONS="${OPTIONS} --resolver-address=${RESOLVERADDRESS[$1]}"
fi
if [ -n "${PROVIDERNAME[$1]}" ]; then
OPTIONS="${OPTIONS} --provider-name=${PROVIDERNAME[$1]}"
fi
if [ -n "${PROVIDERCERTFILE[$1]}" ]; then
if [ -n "$2" ] && [ "$2" == "rotate" ]; then
OPTIONS="${OPTIONS} --provider-cert-file=${PROVIDERCERTFILE[$1]},${PROVIDERCERTFILE[$1]}_prev"
else
OPTIONS="${OPTIONS} --provider-cert-file=${PROVIDERCERTFILE[$1]}"
fi
fi
if [ "${UNAUTHENTICATED[$1]}" == "yes" ]; then
OPTIONS="${OPTIONS} --unauthenticated"
fi
if [ -n "${LOGFILE[$1]}" ]; then
OPTIONS="${OPTIONS} --logfile=${LOGFILE[$1]}"
fi
$DAEMON $OPTIONS
}
stop_instance() {
if [ -z ${PIDFILE[$1]} ]; then
echo "No configuration for instance $1 found!"
return
fi
if [ ! -r ${PIDFILE[$1]} ]; then
echo "dnscrypt-wrapper (instance $1) is not running!"
return
fi
echo "Stopping dnscrypt-wrapper (instance $1)..."
kill $(cat ${PIDFILE[$1]})
rm -f ${PIDFILE[$1]}
}
status_instance() {
if [ -z ${PIDFILE[$1]} ]; then
echo "No configuration for instance $1 found!"
return
fi
if [ ! -r ${PIDFILE[$1]} ]; then
echo "dnscrypt-wrapper (instance $1) is not running."
return
fi
PID=$(cat ${PIDFILE[$1]})
if [ -z "$PID" ]; then
echo "PID file is empty! dnscrypt-wrapper (instance $1) does not appear to be running, but there is a stale PID file."
elif kill -0 $PID ; then
echo "dnscrypt-wrapper (instance $1) is running."
else
echo "dnscrypt-wrapper (instance $1) is not running, but there is a stale PID file."
fi
}
generate-keys_instance() {
if [ -z ${PIDFILE[$1]} ]; then
echo "No configuration for instance $1 found!"
return
fi
if [ -z ${DNSCRYPTDIR[$1]} ]; then
echo "DNSCRYPTDIR not set for instance $1! Either set DNSCRYPTDIR or generate keys manually."
return
fi
OPTIONS=""
if [ "${NOLOG[$1]}" == "yes" ]; then
OPTIONS="${OPTIONS} --nolog"
fi
if [ "${DNSSEC[$1]}" == "yes" ]; then
OPTIONS="${OPTIONS} --dnssec"
fi
(
echo "Generating keys for instance $1. You should record the fingerprint, since this will be used by clients."
cd ${DNSCRYPTDIR[$1]}
rm -f public.key secret.key
$DAEMON $OPTIONS --gen-provider-keypair \
--provider-name=${PROVIDERNAME[$1]} \
--ext-address=${EXTADDRESS[$1]}
chmod 0600 public.key secret.key
)
}
generate-cryptkeys_instance() {
if [ -z ${PIDFILE[$1]} ]; then
echo "No configuration for instance $1 found!"
return
fi
if [ -z ${DNSCRYPTDIR[$1]} ]; then
echo "DNSCRYPTDIR not set for instance $1! Either set DNSCRYPTDIR or generate keys manually."
return
fi
(
echo "Generating cryptkeys for instance $1."
cd ${DNSCRYPTDIR[$1]}
rm -f crypt_secret.key
$DAEMON --gen-crypt-keypair
chmod 0600 crypt_secret.key
)
}
generate-cert_instance() {
if [ -z ${PIDFILE[$1]} ]; then
echo "No configuration for instance $1 found!"
return
fi
if [ -z ${PROVIDERCERTFILE[$1]} ]; then
echo "PROVIDERCERTFILE for instance $1 not set! Set PROVIDERCERTFILE before generating a certificate."
return
fi
OPTIONS=""
if [ -n "${DNSCRYPTDIR[$1]}" ]; then
OPTIONS="${OPTIONS} --crypt-secretkey-file=${DNSCRYPTDIR[$1]}/crypt_secret.key"
OPTIONS="${OPTIONS} --provider-publickey-file=${DNSCRYPTDIR[$1]}/public.key"
OPTIONS="${OPTIONS} --provider-secretkey-file=${DNSCRYPTDIR[$1]}/secret.key"
fi
if [ -z "${DNSCRYPTDIR[$1]}" ] && [ -n "${CRYPTSECRETKEYFILE[$1]}" ]; then
OPTIONS="${OPTIONS} --crypt-secretkey-file=${CRYPTSECRETKEYFILE[$1]}"
fi
if [ -z "${DNSCRYPTDIR[$1]}" ] && [ -n "${PROVIDERPUBLICKEYFILE[$1]}" ]; then
OPTIONS="${OPTIONS} --provider-publickey-file=${PROVIDERPUBLICKEYFILE[$1]}"
fi
if [ -z "${DNSCRYPTDIR[$1]}" ] && [ -n "${PROVIDERSECRETKEYFILE[$1]}" ]; then
OPTIONS="${OPTIONS} --provider-secretkey-file=${PROVIDERSECRETKEYFILE[$1]}"
fi
if [ -n "${CERTEXPIRATION[$1]}" ]; then
OPTIONS="${OPTIONS} --cert-file-expire-days=${CERTEXPIRATION[$1]}"
fi
(
echo "Generating certificate for instance $1."
mkdir /tmp/dnscrypt-wrapper-$$
cd /tmp/dnscrypt-wrapper-$$
$DAEMON $OPTIONS --gen-cert-file
chmod 0600 dnscrypt.cert
mv -f dnscrypt.cert ${PROVIDERCERTFILE[$1]}
cd /
rmdir /tmp/dnscrypt-wrapper-$$
)
}
rotate-keys_instance() {
if [ -z ${PIDFILE[$1]} ]; then
echo "No configuration for instance $1 found!"
return
fi
if [ -z ${DNSCRYPTDIR[$1]} ]; then
echo "DNSCRYPTDIR not set for instance $1! Either set DNSCRYPTDIR or rotate keys manually."
return
fi
if [ -z ${PROVIDERCERTFILE[$1]} ]; then
echo "PROVIDERCERTFILE for instance $1 not set! Set PROVIDERCERTFILE before rotating keys."
return
fi
(
echo "Backing up existing keys for instance $1."
cd ${DNSCRYPTDIR[$1]}
cp -f crypt_secret.key crypt_secret.key_prev
cp -f ${PROVIDERCERTFILE[$1]} ${PROVIDERCERTFILE[$1]}_prev
chmod 0600 crypt_secret.key_prev ${PROVIDERCERTFILE[$1]}_prev
generate-cryptkeys_instance $1
generate-cert_instance $1
stop_instance $1
sleep 1
start_instance $1 "rotate"
)
}
start() {
for i in `/usr/bin/seq 0 $((${#PIDFILE[@]}-1))`
do
start_instance $i
done
}
stop() {
for i in `/usr/bin/seq 0 $((${#PIDFILE[@]}-1))`
do
stop_instance $i
done
}
status() {
for i in `/usr/bin/seq 0 $((${#PIDFILE[@]}-1))`
do
status_instance $i
done
}
generate-keys() {
for i in `/usr/bin/seq 0 $((${#PIDFILE[@]}-1))`
do
generate-keys_instance $i
done
}
generate-cryptkeys() {
for i in `/usr/bin/seq 0 $((${#PIDFILE[@]}-1))`
do
generate-cryptkeys_instance $i
done
}
generate-cert() {
for i in `/usr/bin/seq 0 $((${#PIDFILE[@]}-1))`
do
generate-cert_instance $i
done
}
rotate-keys() {
for i in `/usr/bin/seq 0 $((${#PIDFILE[@]}-1))`
do
rotate-keys_instance $i
done
}
case "$1" in
'start')
start
;;
'stop')
stop
;;
'restart')
stop
start
;;
'status')
status
;;
'generate-keys')
generate-keys
;;
'generate-cryptkeys')
generate-cryptkeys
;;
'generate-cert')
generate-cert
;;
'rotate-keys')
rotate-keys
;;
*_start)
INSTANCE=`echo $1 | /bin/cut -d '_' -f 1`
start_instance $INSTANCE
;;
*_stop)
INSTANCE=`echo $1 | /bin/cut -d '_' -f 1`
stop_instance $INSTANCE
;;
*_restart)
INSTANCE=`echo $1 | /bin/cut -d '_' -f 1`
stop_instance $INSTANCE
sleep 1
start_instance $INSTANCE
;;
*_status)
INSTANCE=`echo $1 | /bin/cut -d '_' -f 1`
status_instance $INSTANCE
;;
*_generate-keys)
INSTANCE=`echo $1 | /bin/cut -d '_' -f 1`
generate-keys_instance $INSTANCE
;;
*_generate-cryptkeys)
INSTANCE=`echo $1 | /bin/cut -d '_' -f 1`
generate-cryptkeys_instance $INSTANCE
;;
*_generate-cert)
INSTANCE=`echo $1 | /bin/cut -d '_' -f 1`
generate-cert_instance $INSTANCE
;;
*_rotate-keys)
INSTANCE=`echo $1 | /bin/cut -d '_' -f 1`
rotate-keys_instance $INSTANCE
;;
*)
echo "Usage: $0 {start|stop|restart|status|generate-keys|generate-cryptkeys|generate-cert|rotate-keys|#_start|#_stop|#_restart|#_status|#_generate-keys|#_generate-cryptkeys|#_generate-cert|#_rotate-keys}"
exit 1
;;
esac
|