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