Get filelist from the commandline instead of hardcoding it
[mirror/dsa-puppet.git] / modules / geodns / files / common / recvconf
1 #!/bin/bash
2
3 set -e
4 set -u
5
6 ## Copyright (c) 2005 David B. Harris <dbharris@eelf.ddts.net>
7 ## Copyright (c) 2005 Peter Palfrader <peter@palfrader.org>
8
9 ## This text is released under the "three-clause BSD license".
10 ## The full text of the license is available at the end of this file.
11
12 if [ "$#" != 1 ]; then
13         echo "Usage: $0 <recvconf.files>" >&2
14         exit 1
15 fi
16
17 FILELIST="$1"
18
19 printf "\nrecvconf on %s processing:\n" "$(hostname -s)"
20
21 temptar="$(mktemp)"
22 chmod 0600 "$temptar"
23
24 tempscript="$(mktemp)"
25 chmod 0600 "$tempscript"
26
27 tempdir="$(mktemp -d)"
28
29 # Read tarball from STDIN
30 gzip -dc > "$temptar"
31
32 cd "$tempdir"
33 tar xf "$temptar"
34
35 copy_and_runcommands() {
36
37     local file perms user group precommand postcommand
38     file="$1"; perms="$2"; user="$3"; group="$4"; precommand="$5"; postcommand="$6"
39
40     if [ -f "$file" ]; then
41         if [ -h "$file" ]; then # File should NOT be a symlink
42             printf "\`%s' is a symlink, aborting.\n" "$file" >&2
43             return 1
44         fi
45
46         if ! [ "$file" -nt "/$file" ]; then
47             rm -f "$file"
48             return 0
49         fi
50
51         if [ -n "$precommand" ]; then
52             printf "Running precommand \`%s' for %s\n" "$precommand" "$file" >&2
53             eval -- $precommand >&2
54         fi
55
56         if [ -n "$perms" ]; then
57             chmod -- "$perms" "$file"
58         else
59             printf "Warning, no perms defined for \`%s', assuming 0640.\n" "$file" >&2
60             chmod 0640 "$file"
61         fi
62         if [ -n "$user" ]; then
63             chown -- "$user" "$file"
64         else
65             printf "Warning, no user defined for \`%s', assuming root.\n" "$file" >&2
66             chown root "$file"
67         fi
68         if [ -n "$group" ]; then
69             chgrp -- "$group" "$file"
70         else
71             printf "Warning, no group defined for \`%s', assuming root.\n" "$file" >&2
72             chgrp root "$file"
73         fi
74
75         if [ ! -d "/$(dirname "$file")" ]; then
76             printf "Directory \`%s' does not exist, aborting.\n" "$(dirname "$file")" >&2
77             exit 1
78         fi
79
80         cp -a -- "$file" "/$(dirname "$file")" >&2
81         ls -l "/$(dirname "$file")/$(basename "$file")" >&2
82
83         if [ -n "$postcommand" ]; then
84             if ! grep -F -- "$postcommand" "$tempscript" > /dev/null 2>&1; then
85                 printf "%s\n" "$postcommand" >> "$tempscript"
86             fi
87         fi
88
89         rm -f -- "$file"
90     fi
91 }
92
93 IN=0
94 linenum=0
95 nextfile=""
96 while read line; do
97     linenum="$(($linenum + 1))"
98
99     if printf "%s\n" "$line" | grep -E '^[[:space:]]*$' > /dev/null 2>&1; then
100         ## This line is an empty line; skip it
101         continue
102     elif printf "%s" "$line" | grep -E '^[[:space:]]*#' > /dev/null 2>&1; then
103         ## This line is a comment; skip it
104         continue
105     fi
106
107     ## IN=0, so we're out of a stanza: better get a file declaration next
108     if [ "$IN" = "0" ] && ! printf "%s" "$line" | grep -E '^[[:space:]]*file[[:space:]]' > /dev/null 2>&1; then
109         printf "Error on line %s, file declaration expected. Got\n\t%s\n" "$linenum" "$line" >&2
110         exit 1
111     elif [ "$IN" = 0 ] && printf "%s" "$line" | grep -E '^[[:space:]]*file[[:space:]]' > /dev/null 2>&1; then
112         ## Okay, we're just starting out; set $file and move on
113         file="$(printf "%s" "$line" | sed -e 's/[[:space:]]*file[[:space:]]\+\([^[:space:]#]*\).*/\1/')"
114         IN=1
115         continue
116     elif [ "$IN" = 1 ] && printf "%s" "$line" | grep -E '^[[:space:]]*file[[:space:]]' > /dev/null 2>&1; then
117         ## Okay, not only are we at a file declaration, but this isn't our first one. Run the commands to process
118         ## the file, then set a $file to the new value and continue parsing.
119         [ -n "$file" ] && copy_and_runcommands "$file" "$perms" "$user" "$group" "$precommand" "$postcommand"
120         file="$(printf "%s" "$line" | sed -e 's/[[:space:]]*file[[:space:]]\+\([^[:space:]#]*\).*/\1/')"
121         perms=""; user=""; group=""; precommand=""; postcommand=""
122         continue
123     fi
124
125     ## The last two if blocks weren't processed; thus this isn't a comment, a blank line, and we're in the middle of a stanza
126     if printf "%s" "$line" | grep -E '^[[:space:]]*perms[[:space:]]' > /dev/null 2>&1; then
127         perms="$(printf "%s" "$line" | sed -e 's/[[:space:]]*perms[[:space:]]\+\([^[:space:]#]*\).*/\1/')"
128         continue
129     elif printf "%s" "$line" | grep -E '^[[:space:]]*user[[:space:]]' > /dev/null 2>&1; then
130         user="$(printf "%s" "$line" | sed -e 's/[[:space:]]*user[[:space:]]\+\([^[:space:]#]*\).*/\1/')"
131         continue
132     elif printf "%s" "$line" | grep -E '^[[:space:]]*group[[:space:]]' > /dev/null 2>&1; then
133         group="$(printf "%s" "$line" | sed -e 's/[[:space:]]*group[[:space:]]\+\([^[:space:]#]*\).*/\1/')"
134         continue
135     elif printf "%s" "$line" | grep -E '^[[:space:]]*precommand[[:space:]]' > /dev/null 2>&1; then
136         precommand="$(printf "%s" "$line" | sed -e 's/[[:space:]]*precommand[[:space:]]\+\([^[:space:]#]*\)/\1/')"
137         continue
138     elif printf "%s" "$line" | grep -E '^[[:space:]]*postcommand[[:space:]]' > /dev/null 2>&1; then
139         postcommand="$(printf "%s" "$line" | sed -e 's/[[:space:]]*postcommand[[:space:]]\+\([^[:space:]#]*\)/\1/')"
140         continue
141     else
142         printf "Unknown token at line %s:\n\t%s\n" "$linenum" "$line"
143     fi
144
145 done < "$FILELIST"
146
147 ## This is the last stanza and the above loop has set the variables, but hasn't yet processed the file
148 [ -n "$file" ] && copy_and_runcommands "$file" "$perms" "$user" "$group" "$precommand" "$postcommand"
149
150 if [ -s "$tempscript" ]; then
151     tempoutput="$(mktemp)"
152     ## Post-copying commands to be run, run them here. Only display output if they exit with $? > 0
153     while read command; do
154         printf "Running postcommand \`%s' on %s.\n" "$command" "$(hostname -s)" >&2
155         if ! eval -- "(cd / && env -i $command)" > "$tempoutput" 2>&1; then
156             printf "Error, postcommand \`%s' on %s failed. Output follows:\n" "$command" "$(hostname -s)" >&2
157             cat -- "$tempoutput" >&2
158             exit 1
159         fi
160     done < "$tempscript"
161 fi
162
163 # Check for any leftover files here; if there are any, exit with an error and print the list
164 if [ ! -z "$(find . -type f)" ]; then
165     printf "The following files were not listed in $FILELIST:\n%s\n" "$(find . -type f)" >&2
166     exit 1
167 fi
168
169 rm -f -- "$temptar"
170 rm -f -- "$tempoutput"
171 rm -f -- "$tempscript"
172 cd
173 rm -rf -- "$tempdir"
174
175 printf "recvconf on %s finished.\n" "$(hostname -s)"
176
177 ## Redistribution and use in source and binary forms, with or without
178 ## modification, are permitted provided that the following conditions are
179 ## met:
180 ## 
181 ##     * Redistributions of source code must retain the above copyright
182 ## notice, this list of conditions and the following disclaimer.
183 ## 
184 ##     * Redistributions in binary form must reproduce the above
185 ## copyright notice, this list of conditions and the following disclaimer
186 ## in the documentation and/or other materials provided with the
187 ## distribution.
188 ## 
189 ##     * Neither the names of the copyright owners nor the names of its
190 ## contributors may be used to endorse or promote products derived from
191 ## this software without specific prior written permission.
192 ## 
193 ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
194 ## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
195 ## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196 ## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
197 ## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
198 ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
199 ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
200 ## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
201 ## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
202 ## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
203 ## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.