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