summaryrefslogtreecommitdiff
path: root/recvconf
blob: b4157797350f00b349eab19fc92ea125855cfd7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/bash -e

## Copyright (c) 2005 David B. Harris <dbharris@eelf.ddts.net>
## Copyright (c) 2005,2016 Peter Palfrader <peter@palfrader.org>

## This text is released under the "three-clause BSD license".
## The full text of the license is available at the end of this file.

set -e
set -u

printf "\nrecvconf on %s processing:\n" "$(hostname -s)"

FILELIST=/etc/NOREPLY/recvconf.files
while [ "$#" -gt 0 ]; do
    case "$1" in
        -f)
            shift;
            FILELIST="$1"; shift
            ;;
        *)
            echo >&2 "Usage: $0 [-f <recvconf.files>"
            exit 1
            ;;
    esac
done

temptar=""
tempscript=""
tempoutput=""
tempdir=""

cleanup() {
    cd /
    if [ -n "$temptar" ]; then
        rm -f "$temptar"
    fi
    if [ -n "$tempscript" ]; then
        rm -f "$tempscript"
    fi
    if [ -n "$tempoutput" ]; then
        rm -f "$tempoutput"
    fi
    if [ -n "$tempdir" ]; then
        rm -rf "$tempdir"
    fi
}
trap 'cleanup' EXIT

temptar="$(mktemp)"
chmod 0600 "$temptar"

tempscript="$(mktemp)"
chmod 0600 "$tempscript"

tempdir="$(mktemp -d)"

# Read tarball from STDIN
gzip -dc > "$temptar"

cd "$tempdir"
tar xf "$temptar"

copy_and_runcommands() {

    local file perms user group precommand postcommand
    file="$1"; perms="$2"; user="$3"; group="$4"; precommand="$5"; postcommand="$6"

    if [ -f "$file" ]; then
        if [ -h "$file" ]; then # File should NOT be a symlink
            printf "\`%s' is a symlink, aborting.\n" "$file" >&2
            return 1
        fi

        if ! [ "$file" -nt "/$file" ]; then
            rm -f "$file"
            return 0
        fi

        if [ -n "$precommand" ]; then
            printf "Running precommand \`%s' for %s\n" "$precommand" "$file" >&2
            eval -- $precommand >&2
        fi

        if [ -n "$perms" ]; then
            chmod -- "$perms" "$file"
        else
            printf "Warning, no perms defined for \`%s', assuming 0640.\n" "$file" >&2
            chmod 0640 "$file"
        fi
        if [ -n "$user" ]; then
            chown -- "$user" "$file"
        else
            printf "Warning, no user defined for \`%s', assuming root.\n" "$file" >&2
            chown root "$file"
        fi
        if [ -n "$group" ]; then
            chgrp -- "$group" "$file"
        else
            printf "Warning, no group defined for \`%s', assuming root.\n" "$file" >&2
            chgrp root "$file"
        fi

        if [ ! -d "/$(dirname "$file")" ]; then
            printf "Directory \`%s' does not exist, aborting.\n" "$(dirname "$file")" >&2
            exit 1
        fi

        cp -a -- "$file" "/$(dirname "$file")" >&2
        ls -l "/$(dirname "$file")/$(basename "$file")" >&2

        if [ -n "$postcommand" ]; then
            if ! grep -F -- "$postcommand" "$tempscript" > /dev/null 2>&1; then
                printf "%s\n" "$postcommand" >> "$tempscript"
            fi
        fi

        rm -f -- "$file"
    fi
}

IN=0
linenum=0
nextfile=""
while read line; do
    linenum="$(($linenum + 1))"

    if printf "%s\n" "$line" | grep -E '^[[:space:]]*$' > /dev/null 2>&1; then
        ## This line is an empty line; skip it
        continue
    elif printf "%s" "$line" | grep -E '^[[:space:]]*#' > /dev/null 2>&1; then
        ## This line is a comment; skip it
        continue
    fi

    ## IN=0, so we're out of a stanza: better get a file declaration next
    if [ "$IN" = "0" ] && ! printf "%s" "$line" | grep -E '^[[:space:]]*file[[:space:]]' > /dev/null 2>&1; then
        printf "Error on line %s, file declaration expected. Got\n\t%s\n" "$linenum" "$line" >&2
        exit 1
    elif [ "$IN" = 0 ] && printf "%s" "$line" | grep -E '^[[:space:]]*file[[:space:]]' > /dev/null 2>&1; then
        ## Okay, we're just starting out; set $file and move on
        file="$(printf "%s" "$line" | sed -e 's/[[:space:]]*file[[:space:]]\+\([^[:space:]#]*\).*/\1/')"
        IN=1
        continue
    elif [ "$IN" = 1 ] && printf "%s" "$line" | grep -E '^[[:space:]]*file[[:space:]]' > /dev/null 2>&1; then
        ## Okay, not only are we at a file declaration, but this isn't our first one. Run the commands to process
        ## the file, then set a $file to the new value and continue parsing.
        [ -n "$file" ] && copy_and_runcommands "$file" "$perms" "$user" "$group" "$precommand" "$postcommand"
        file="$(printf "%s" "$line" | sed -e 's/[[:space:]]*file[[:space:]]\+\([^[:space:]#]*\).*/\1/')"
        perms=""; user=""; group=""; precommand=""; postcommand=""
        continue
    fi

    ## 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
    if printf "%s" "$line" | grep -E '^[[:space:]]*perms[[:space:]]' > /dev/null 2>&1; then
        perms="$(printf "%s" "$line" | sed -e 's/[[:space:]]*perms[[:space:]]\+\([^[:space:]#]*\).*/\1/')"
        continue
    elif printf "%s" "$line" | grep -E '^[[:space:]]*user[[:space:]]' > /dev/null 2>&1; then
        user="$(printf "%s" "$line" | sed -e 's/[[:space:]]*user[[:space:]]\+\([^[:space:]#]*\).*/\1/')"
        continue
    elif printf "%s" "$line" | grep -E '^[[:space:]]*group[[:space:]]' > /dev/null 2>&1; then
        group="$(printf "%s" "$line" | sed -e 's/[[:space:]]*group[[:space:]]\+\([^[:space:]#]*\).*/\1/')"
        continue
    elif printf "%s" "$line" | grep -E '^[[:space:]]*precommand[[:space:]]' > /dev/null 2>&1; then
        precommand="$(printf "%s" "$line" | sed -e 's/[[:space:]]*precommand[[:space:]]\+\([^[:space:]#]*\)/\1/')"
        continue
    elif printf "%s" "$line" | grep -E '^[[:space:]]*postcommand[[:space:]]' > /dev/null 2>&1; then
        postcommand="$(printf "%s" "$line" | sed -e 's/[[:space:]]*postcommand[[:space:]]\+\([^[:space:]#]*\)/\1/')"
        continue
    else
        printf "Unknown token at line %s:\n\t%s\n" "$linenum" "$line"
    fi

done < "$FILELIST"

## This is the last stanza and the above loop has set the variables, but hasn't yet processed the file
[ -n "$file" ] && copy_and_runcommands "$file" "$perms" "$user" "$group" "$precommand" "$postcommand"

if [ -s "$tempscript" ]; then
    tempoutput="$(mktemp)"
    ## Post-copying commands to be run, run them here. Only display output if they exit with $? > 0
    while read command; do
        printf "Running postcommand \`%s' on %s.\n" "$command" "$(hostname -s)" >&2
        if ! eval -- "(cd / && env -i $command)" > "$tempoutput" 2>&1; then
            printf "Error, postcommand \`%s' on %s failed. Output follows:\n" "$command" "$(hostname -s)" >&2
            cat -- "$tempoutput" >&2
            exit 1
        fi
    done < "$tempscript"
fi

# Check for any leftover files here; if there are any, exit with an error and print the list
if [ ! -z "$(find . -type f)" ]; then
    printf "The following files were not listed in $FILELIST:\n%s\n" "$(find . -type f)" >&2
    exit 1
fi

printf "recvconf on %s finished.\n" "$(hostname -s)"

## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are
## met:
## 
##     * Redistributions of source code must retain the above copyright
## notice, this list of conditions and the following disclaimer.
## 
##     * Redistributions in binary form must reproduce the above
## copyright notice, this list of conditions and the following disclaimer
## in the documentation and/or other materials provided with the
## distribution.
## 
##     * Neither the names of the copyright owners nor the names of its
## contributors may be used to endorse or promote products derived from
## this software without specific prior written permission.
## 
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.