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