-
Notifications
You must be signed in to change notification settings - Fork 0
/
xtlbackup-receive.sh
executable file
·73 lines (65 loc) · 1.61 KB
/
xtlbackup-receive.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
#!/bin/sh
#
# This file sanitizes commands received for remote backups with xtlbackup.
# It is to be used as a shell for an authorized SSH key.
#
# To use it, prefix the relevant SSH key in /root/.ssh/authorized_keys with
# command="SNAPSHOTS_PATH='<some path regex>' /usr/sbin/xtlbackup-receive",no-port-forwarding,no-agent-forwarding,no-X11-forwarding,no-pty
#
# The regex match is attempted on the canonicalized path (only if it exists).
# Here are some examples:
# * To match /remote_backups, SNAPSHOTS_PATH='/remote_backups'
# * To match any given subdirectory of /remote_backups, SNAPSHOTS_PATH='/remote_backups/[^/]\+$'
#
# The following commands are allowed:
# * ls (grep $SNAPSHOTS_PATH)
# * btrfs receive (grep $SNAPSHOTS_PATH)
#
# Check if a given path is authorized.
validate_path() {
if [ -z "$SNAPSHOTS_PATH" ]
then
>&2 echo "Error: SNAPSHOTS_PATH not set."
exit 1
fi
# Extract an absolute path from $1
CMD_PATH=$(cd "$1" 2> /dev/null && pwd)
if ! echo "$CMD_PATH" | grep "$SNAPSHOTS_PATH" > /dev/null
then
>&2 echo "Error: illegal path '$1'."
exit 1
fi
}
# Retrieve SSH command
if [ -n "$SSH_ORIGINAL_COMMAND" ]
then
set -- $SSH_ORIGINAL_COMMAND
fi
if [ "$1" = 'btrfs' ]
then
if [ "$2" = 'receive' ]
then
if [ "$#" -ne 3 ]
then
>&2 echo "Error: invalid number of arguments."
exit 1
fi
validate_path "$3"
exec $@
else
>&2 echo "Error: unauthorized btrfs operation '$2'."
exit 1
fi
elif [ "$1" = 'ls' ]
then
if [ "$#" -ne 2 ]
then
>&2 echo "Error: invalid number of arguments."
exit 1
fi
validate_path "$2"
exec $@
else
>&2 echo "Error: unauthorized command '$1'."
exit 1
fi