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