4ee9bbd0007b4af31514e631b1a2abe10b0327c4
[mirror/dsa-puppet.git] / modules / roles / files / static-mirroring / static-mirror-ssh-wrap
1 #!/bin/bash
2
3 # This is a wrapper script for ssh access on Debian's static mirroring infrastructure.
4 #
5 # It limits the commands the master can run on static-mirroring mirrors (i.e.
6 # the things running apache) on one hand, and also on static-mirroring sources,
7 # that is the things that create the data.
8
9 # Copyright (c) 2009, 2010, 2012 Peter Palfrader
10 #
11 # Permission is hereby granted, free of charge, to any person obtaining
12 # a copy of this software and associated documentation files (the
13 # "Software"), to deal in the Software without restriction, including
14 # without limitation the rights to use, copy, modify, merge, publish,
15 # distribute, sublicense, and/or sell copies of the Software, and to
16 # permit persons to whom the Software is furnished to do so, subject to
17 # the following conditions:
18 #
19 # The above copyright notice and this permission notice shall be
20 # included in all copies or substantial portions of the Software.
21 #
22 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 set -e
31 set -u
32
33 MYLOGNAME="`basename "$0"`[$$]"
34 COMPONENTLIST=/etc/static-components.conf
35
36 usage() {
37         echo "local Usage: $0 <basedir> <host>"
38         echo "via ssh orig command:"
39         echo "                      mirror <component> <serial>"
40         echo "                      rsync ..."
41                 do_rsync "$remote_host" "$@"
42 }
43
44 one_more_arg() {
45         if [ "$#" -lt 1 ]; then
46                 usage >&2
47                 exit 1
48         fi
49 }
50
51 info() {
52         logger -p daemon.info -t "$MYLOGNAME" "$1"
53 }
54
55 croak() {
56         logger -s -p daemon.warn -t "$MYLOGNAME" "$1"
57         exit 1
58 }
59
60 do_mirror() {
61         local basedir="$1"; shift
62         local remote_host="$1"; shift
63         one_more_arg "$@"
64         local component="$1"; shift
65         one_more_arg "$@"
66         local serial="$1"; shift
67
68         info "Host $remote_host triggered a mirror run for $component, serial $serial"
69         exec /usr/local/bin/static-mirror-run "$basedir/$component" "$remote_host:$component/-new-" "$serial"
70         echo >&2 "Exec failed"
71         croak "exec failed"
72 }
73
74 do_rsync() {
75         local remote_host="$1"
76         shift
77
78         local allowed_rsyncs
79         allowed_rsyncs=()
80
81         if [ -e "$COMPONENTLIST" ]; then
82                 for path in $(awk -v host="$(hostname -f)" '$3 == host {print $4}' $COMPONENTLIST); do
83                         allowed_rsyncs+=("--server --sender -lHtrze.iLsf --safe-links . $path/.")
84                 done
85         fi
86         for cmd_idx in ${!allowed_rsyncs[*]}; do
87                 allowed="${allowed_rsyncs[$cmd_idx]}"
88                 if [ "$*" = "$allowed" ]; then
89                         info "Running for host $remote_host: rsync $*"
90                         exec rsync "$@"
91                         echo >&2 "Exec failed"
92                         exit 1
93                 fi
94         done
95
96         info "NOT allowed for $remote_host: rsync $*"
97         echo >&2 "This rsync command ($*) not allowed."
98         exit 1
99 }
100
101
102 if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
103         usage
104         exit 0
105 fi
106
107 one_more_arg "$@"
108 basedir="$1"
109 shift
110
111 one_more_arg "$@"
112 remote_host="$1"
113 shift
114
115
116 # check/parse remote command line
117 if [ -z "${SSH_ORIGINAL_COMMAND:-}" ] ; then
118         croak "Did not find SSH_ORIGINAL_COMMAND"
119 fi
120 set "dummy" ${SSH_ORIGINAL_COMMAND}
121 shift
122
123 one_more_arg "$@"
124 action="$1"
125 shift
126
127 case "$action" in
128         mirror)
129                 do_mirror "$basedir" "$remote_host" "$@"
130                 ;;
131         rsync)
132                 do_rsync "$remote_host" "$@"
133                 ;;
134         *)
135                 croak "Invalid operation '$action'"
136                 ;;
137 esac