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