6 ## Copyright (c) 2005 David B. Harris <dbharris@eelf.ddts.net>
7 ## Copyright (c) 2005 Peter Palfrader <peter@palfrader.org>
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.
12 if [ "$#" != 1 ]; then
13 echo "Usage: $0 <recvconf.files>" >&2
19 printf "\nrecvconf on %s processing:\n" "$(hostname -s)"
26 tempscript="$(mktemp)"
27 chmod 0600 "$tempscript"
29 tempdir="$(mktemp -d)"
31 # Read tarball from STDIN
37 copy_and_runcommands() {
39 local file perms user group precommand postcommand
40 file="$1"; perms="$2"; user="$3"; group="$4"; precommand="$5"; postcommand="$6"
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
48 if ! [ "$file" -nt "/$file" ]; then
53 if [ -n "$precommand" ]; then
54 printf "Running precommand \`%s' for %s\n" "$precommand" "$file" >&2
55 eval -- $precommand >&2
58 if [ -n "$perms" ]; then
59 chmod -- "$perms" "$file"
61 printf "Warning, no perms defined for \`%s', assuming 0640.\n" "$file" >&2
64 if [ -n "$user" ]; then
65 chown -- "$user" "$file"
67 printf "Warning, no user defined for \`%s', assuming root.\n" "$file" >&2
70 if [ -n "$group" ]; then
71 chgrp -- "$group" "$file"
73 printf "Warning, no group defined for \`%s', assuming root.\n" "$file" >&2
77 if [ ! -d "/$(dirname "$file")" ]; then
78 printf "Directory \`%s' does not exist, aborting.\n" "$(dirname "$file")" >&2
82 cp -a -- "$file" "/$(dirname "$file")" >&2
83 ls -l "/$(dirname "$file")/$(basename "$file")" >&2
85 if [ -n "$postcommand" ]; then
86 if ! grep -F -- "$postcommand" "$tempscript" > /dev/null 2>&1; then
87 printf "%s\n" "$postcommand" >> "$tempscript"
99 linenum="$(($linenum + 1))"
101 if printf "%s\n" "$line" | grep -E '^[[:space:]]*$' > /dev/null 2>&1; then
102 ## This line is an empty line; skip it
104 elif printf "%s" "$line" | grep -E '^[[:space:]]*#' > /dev/null 2>&1; then
105 ## This line is a comment; skip it
109 ## IN=0, so we're out of a stanza: better get a file declaration next
110 if [ "$IN" = "0" ] && ! printf "%s" "$line" | grep -E '^[[:space:]]*file[[:space:]]' > /dev/null 2>&1; then
111 printf "Error on line %s, file declaration expected. Got\n\t%s\n" "$linenum" "$line" >&2
113 elif [ "$IN" = 0 ] && printf "%s" "$line" | grep -E '^[[:space:]]*file[[:space:]]' > /dev/null 2>&1; then
114 ## Okay, we're just starting out; set $file and move on
115 file="$(printf "%s" "$line" | sed -e 's/[[:space:]]*file[[:space:]]\+\([^[:space:]#]*\).*/\1/')"
118 elif [ "$IN" = 1 ] && printf "%s" "$line" | grep -E '^[[:space:]]*file[[:space:]]' > /dev/null 2>&1; then
119 ## Okay, not only are we at a file declaration, but this isn't our first one. Run the commands to process
120 ## the file, then set a $file to the new value and continue parsing.
121 [ -n "$file" ] && copy_and_runcommands "$file" "$perms" "$user" "$group" "$precommand" "$postcommand"
122 file="$(printf "%s" "$line" | sed -e 's/[[:space:]]*file[[:space:]]\+\([^[:space:]#]*\).*/\1/')"
123 perms=""; user=""; group=""; precommand=""; postcommand=""
127 ## 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
128 if printf "%s" "$line" | grep -E '^[[:space:]]*perms[[:space:]]' > /dev/null 2>&1; then
129 perms="$(printf "%s" "$line" | sed -e 's/[[:space:]]*perms[[:space:]]\+\([^[:space:]#]*\).*/\1/')"
131 elif printf "%s" "$line" | grep -E '^[[:space:]]*user[[:space:]]' > /dev/null 2>&1; then
132 user="$(printf "%s" "$line" | sed -e 's/[[:space:]]*user[[:space:]]\+\([^[:space:]#]*\).*/\1/')"
134 elif printf "%s" "$line" | grep -E '^[[:space:]]*group[[:space:]]' > /dev/null 2>&1; then
135 group="$(printf "%s" "$line" | sed -e 's/[[:space:]]*group[[:space:]]\+\([^[:space:]#]*\).*/\1/')"
137 elif printf "%s" "$line" | grep -E '^[[:space:]]*precommand[[:space:]]' > /dev/null 2>&1; then
138 precommand="$(printf "%s" "$line" | sed -e 's/[[:space:]]*precommand[[:space:]]\+\([^[:space:]#]*\)/\1/')"
140 elif printf "%s" "$line" | grep -E '^[[:space:]]*postcommand[[:space:]]' > /dev/null 2>&1; then
141 postcommand="$(printf "%s" "$line" | sed -e 's/[[:space:]]*postcommand[[:space:]]\+\([^[:space:]#]*\)/\1/')"
144 printf "Unknown token at line %s:\n\t%s\n" "$linenum" "$line"
149 ## This is the last stanza and the above loop has set the variables, but hasn't yet processed the file
150 [ -n "$file" ] && copy_and_runcommands "$file" "$perms" "$user" "$group" "$precommand" "$postcommand"
152 if [ -s "$tempscript" ]; then
153 tempoutput="$(mktemp)"
154 ## Post-copying commands to be run, run them here. Only display output if they exit with $? > 0
155 while read command; do
156 printf "Running postcommand \`%s' on %s.\n" "$command" "$(hostname -s)" >&2
157 if ! eval -- "(cd / && env -i $command)" > "$tempoutput" 2>&1; then
158 printf "Error, postcommand \`%s' on %s failed. Output follows:\n" "$command" "$(hostname -s)" >&2
159 cat -- "$tempoutput" >&2
165 # Check for any leftover files here; if there are any, exit with an error and print the list
166 if [ ! -z "$(find . -type f)" ]; then
167 printf "The following files were not listed in $FILELIST:\n%s\n" "$(find . -type f)" >&2
172 rm -f -- "$tempoutput"
173 rm -f -- "$tempscript"
177 printf "recvconf on %s finished.\n" "$(hostname -s)"
179 ## Redistribution and use in source and binary forms, with or without
180 ## modification, are permitted provided that the following conditions are
183 ## * Redistributions of source code must retain the above copyright
184 ## notice, this list of conditions and the following disclaimer.
186 ## * Redistributions in binary form must reproduce the above
187 ## copyright notice, this list of conditions and the following disclaimer
188 ## in the documentation and/or other materials provided with the
191 ## * Neither the names of the copyright owners nor the names of its
192 ## contributors may be used to endorse or promote products derived from
193 ## this software without specific prior written permission.
195 ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
196 ## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
197 ## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
198 ## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
199 ## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
200 ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
201 ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
202 ## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
203 ## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
204 ## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
205 ## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.