rsync-ssh-wrap: add allowed_rsyncs for buster
[mirror/dsa-puppet.git] / modules / roles / files / ssh_upload / rsync-ssh-wrap
1 #!/bin/bash
2
3 # Copyright (c) 2009, 2010, 2012 Peter Palfrader
4 # Copyright (c) 2015 Aurelien Jarno
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining
7 # a copy of this software and associated documentation files (the
8 # "Software"), to deal in the Software without restriction, including
9 # without limitation the rights to use, copy, modify, merge, publish,
10 # distribute, sublicense, and/or sell copies of the Software, and to
11 # permit persons to whom the Software is furnished to do so, subject to
12 # the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25 set -e
26 set -u
27
28 MYLOGNAME="`basename "$0"`[$$]"
29 RSYNCDIRS=(/srv/upload.debian.org/UploadQueue/ /srv/security.upload.debian.org/SecurityUploadQueue/)
30
31 usage() {
32         echo "local Usage: $0 <host>"
33         echo "via ssh orig command: rsync <stuff>"
34 }
35
36 one_more_arg() {
37         if [ "$#" -lt 1 ]; then
38                 usage >&2
39                 exit 1
40         fi
41 }
42
43 info() {
44         logger -p daemon.info -t "$MYLOGNAME" "$1"
45 }
46
47 croak() {
48         logger -s -p daemon.warn -t "$MYLOGNAME" "$1"
49         exit 1
50 }
51
52 do_rsync() {
53         local remote_host="$1"
54         shift
55
56         local allowed_rsyncs
57         allowed_rsyncs=()
58
59         local rsyncdir
60         for rsyncdir in "${RSYNCDIRS[@]}"; do
61                 if [ -d "$rsyncdir" ]; then
62                         allowed_rsyncs+=("--server -vlogDtprxze.iLsf --partial . $rsyncdir") # wheezy
63                         allowed_rsyncs+=("--server -vlogDtprxze.iLsfx --partial . $rsyncdir") # jessie
64                         allowed_rsyncs+=("--server -vlogDtprxze.iLsfxC --partial . $rsyncdir") # stretch
65                         allowed_rsyncs+=("--server -vtxze.LsfxC --partial . $rsyncdir") # buster
66                 fi
67         done
68         for cmd_idx in ${!allowed_rsyncs[*]}; do
69                 allowed="${allowed_rsyncs[$cmd_idx]}"
70                 if [ "$*" = "$allowed" ]; then
71                         info "Running for host $remote_host: rsync $*"
72                         exec rsync --chmod=F640 "$@"
73                         croak "Exec failed"
74                 fi
75         done
76
77         info "NOT allowed for $remote_host: rsync $*"
78         echo >&2 "This rsync command ($@) not allowed."
79         exit 1
80 }
81
82 if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
83         usage
84         exit 0
85 fi
86
87 one_more_arg "$@"
88 remote_host="$1"
89 shift
90
91
92 # check/parse remote command line
93 if [ -z "${SSH_ORIGINAL_COMMAND:-}" ] ; then
94         croak "Did not find SSH_ORIGINAL_COMMAND"
95 fi
96 set "dummy" ${SSH_ORIGINAL_COMMAND}
97 shift
98
99 info "host $remote_host called with $*"
100
101 one_more_arg "$@"
102 action="$1"
103 shift
104
105 case "$action" in
106         # rsync command to upload packages
107         rsync)
108                 do_rsync "$remote_host" "$@"
109                 ;;
110         # just ignore any other commands (e.g. chmod)
111         *)
112                 info "Ignored operation '$action'"
113                 ;;
114 esac