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
11 ## Copyright (c) 2005 David B. Harris <dbharris@eelf.ddts.net>
12 ## Copyright (c) 2005,2009 Peter Palfrader <peter@palfrader.org>
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.
17 if [ "$#" != 1 ]; then
18 echo "Usage: $0 <recvconf.files>" >&2
24 printf "\nrecvconf on %s processing:\n" "$(hostname -s)"
31 tempscript="$(mktemp)"
32 chmod 0600 "$tempscript"
34 tempdir="$(mktemp -d)"
36 # Read tarball from STDIN
42 copy_and_runcommands() {
44 local file perms user group precommand postcommand
45 file="$1"; perms="$2"; user="$3"; group="$4"; precommand="$5"; postcommand="$6"
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
53 if ! [ "$file" -nt "/$file" ]; then
58 if [ -n "$precommand" ]; then
59 printf "Running precommand \`%s' for %s\n" "$precommand" "$file" >&2
60 eval -- $precommand >&2
63 if [ -n "$perms" ]; then
64 chmod -- "$perms" "$file"
66 printf "Warning, no perms defined for \`%s', assuming 0640.\n" "$file" >&2
69 if [ -n "$user" ]; then
70 chown -- "$user" "$file"
72 printf "Warning, no user defined for \`%s', assuming root.\n" "$file" >&2
75 if [ -n "$group" ]; then
76 chgrp -- "$group" "$file"
78 printf "Warning, no group defined for \`%s', assuming root.\n" "$file" >&2
82 if [ ! -d "/$(dirname "$file")" ]; then
83 printf "Directory \`%s' does not exist, aborting.\n" "$(dirname "$file")" >&2
87 cp -a -- "$file" "/$(dirname "$file")" >&2
88 ls -l "/$(dirname "$file")/$(basename "$file")" >&2
90 if [ -n "$postcommand" ]; then
91 if ! grep -F -- "$postcommand" "$tempscript" > /dev/null 2>&1; then
92 printf "%s\n" "$postcommand" >> "$tempscript"
106 perms=""; user=""; group=""; precommand=""; postcommand=""
111 linenum="$(($linenum + 1))"
113 if printf "%s\n" "$line" | grep -E '^[[:space:]]*$' > /dev/null 2>&1; then
114 ## This line is an empty line; skip it
116 elif printf "%s" "$line" | grep -E '^[[:space:]]*#' > /dev/null 2>&1; then
117 ## This line is a comment; skip it
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
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/')"
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/')"
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/')"
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/')"
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/')"
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/')"
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/')"
156 printf "Unknown token at line %s:\n\t%s\n" "$linenum" "$line"
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"
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
175 rm -f -- "$tempoutput"
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
185 rm -f -- "$tempscript"
189 printf "recvconf on %s finished.\n" "$(hostname -s)"
191 ## Redistribution and use in source and binary forms, with or without
192 ## modification, are permitted provided that the following conditions are
195 ## * Redistributions of source code must retain the above copyright
196 ## notice, this list of conditions and the following disclaimer.
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
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.
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.