3 ## Copyright (c) 2005 David B. Harris <dbharris@eelf.ddts.net>
4 ## Copyright (c) 2005 Peter Palfrader <peter@palfrader.org>
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.
9 printf "\nrecvconf on %s processing:\n" "$(hostname -s)"
10 FILELIST=/etc/NOREPLY/recvconf.files
15 tempscript="$(mktemp)"
16 chmod 0600 "$tempscript"
18 tempdir="$(mktemp -d)"
20 # Read tarball from STDIN
26 copy_and_runcommands() {
28 local file perms user group precommand postcommand
29 file="$1"; perms="$2"; user="$3"; group="$4"; precommand="$5"; postcommand="$6"
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
37 if ! [ "$file" -nt "/$file" ]; then
42 if [ -n "$precommand" ]; then
43 printf "Running precommand \`%s' for %s\n" "$precommand" "$file" >&2
44 eval -- $precommand >&2
47 if [ -n "$perms" ]; then
48 chmod -- "$perms" "$file"
50 printf "Warning, no perms defined for \`%s', assuming 0640.\n" "$file" >&2
53 if [ -n "$user" ]; then
54 chown -- "$user" "$file"
56 printf "Warning, no user defined for \`%s', assuming root.\n" "$file" >&2
59 if [ -n "$group" ]; then
60 chgrp -- "$group" "$file"
62 printf "Warning, no group defined for \`%s', assuming root.\n" "$file" >&2
66 if [ ! -d "/$(dirname "$file")" ]; then
67 printf "Directory \`%s' does not exist, aborting.\n" "$(dirname "$file")" >&2
71 cp -a -- "$file" "/$(dirname "$file")" >&2
72 ls -l "/$(dirname "$file")/$(basename "$file")" >&2
74 if [ -n "$postcommand" ]; then
75 if ! grep -F -- "$postcommand" "$tempscript" > /dev/null 2>&1; then
76 printf "%s\n" "$postcommand" >> "$tempscript"
88 linenum="$(($linenum + 1))"
90 if printf "%s\n" "$line" | grep -E '^[[:space:]]*$' > /dev/null 2>&1; then
91 ## This line is an empty line; skip it
93 elif printf "%s" "$line" | grep -E '^[[:space:]]*#' > /dev/null 2>&1; then
94 ## This line is a comment; skip it
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
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/')"
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=""
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/')"
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/')"
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/')"
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/')"
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/')"
133 printf "Unknown token at line %s:\n\t%s\n" "$linenum" "$line"
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"
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
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
161 rm -f -- "$tempoutput"
162 rm -f -- "$tempscript"
166 printf "recvconf on %s finished.\n" "$(hostname -s)"
168 ## Redistribution and use in source and binary forms, with or without
169 ## modification, are permitted provided that the following conditions are
172 ## * Redistributions of source code must retain the above copyright
173 ## notice, this list of conditions and the following disclaimer.
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
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.
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.