54
76
QUIETREPORTS="${QUIETREPORTS:-no}"
59
printf >> "$ERRORLOG" "SIG %s received!\n\n" "$1"
60
pidof aide | xargs --no-run-if-empty kill -9
64
trap ' traphandler INT; trap - INT ERR' INT
65
trap ' traphandler ERR; trap - INT ERR' ERR
69
STARS="*******************************************************************************"
71
printf "%s\n" "${STARS:1:$WIDTH}"
73
HALF="${SPACES:1:$((($WIDTH-${#line})/2))}"
74
LINE="$HALF$line$SPACES"
75
printf "*%s*\n" "${LINE:1:$(($WIDTH-2))}"
77
printf "%s\n" "${STARS:1:$WIDTH}"
81
if [ -e "$TMPDIR" ]; then
82
if ! NEWNAME="$(mktemp -d $TMPBASE/cron.daily.old.XXXXXXXXXX)"; then
83
printf >&2 "Fatal error: Cannot move away %s.\n" "$TMPDIR"
86
mv "$TMPDIR" "$NEWNAME"
90
if ! mkdir -p $TMPDIR; then
91
printf >&2 "Fatal error: Cannot create temporary directory %s.\n" "$TMPDIR"
95
# we can now directly use file names inside $TMPDIR: It is only
96
# writeable for us (umask 077), so we're safe against symlink attacks.
97
# We use invariant file names here since our work files need to be
101
83
echo "$TMPDIR/$NAME"
102
84
touch "$TMPDIR/$NAME"
105
# LOGFILE: /var/log/aide/aide.log - all logs untruncated (not temp)
106
# ERRORLOG: Error messages from script. Gets written to $LOGFILE first
107
ERRORLOG="$(mytempfile errorlog)"
109
# MAILFILE: Contents gets mailed. Built and handled from inside onexit()
110
MAILFILE="$(mytempfile mailfile)"
114
if [ ! -f "$DATABASE" ]; then
115
printf >> "$ERRORLOG" "Fatal error: The AIDE database does not exist!\n"
116
printf >> "$ERRORLOG" "This may mean you haven't created it, or it may mean that someone has removed it.\n"
121
BEGINTIME="$(date +%s)"
123
if command -v dotlockfile >/dev/null 2>&1; then
124
if ! dotlockfile -p -l "$LOCKFILE"; then
125
printf >> "$ERRORLOG" "cannot obtain lock %s, stale lock?\n" "$LOCKFILE"
130
printf >> "$ERRORLOG" "no dotlockfile binary in path, not checking for already running aide cron job\n"
89
STARS="*******************************************************************************"
91
printf "%s\n" "${STARS:1:$WIDTH}"
93
HALF="${SPACES:1:$((($WIDTH-${#line})/2))}"
94
LINE="$HALF$line$SPACES"
95
printf "*%s*\n" "${LINE:1:$(($WIDTH-2))}"
97
printf "%s\n" "${STARS:1:$WIDTH}"
136
if [ "$ONEXIT" = "running" ]; then
142
[ -f "$LOGFILE" ] && savelog -t -g adm -m 640 -u root -c 7 "$LOGFILE" > /dev/null
144
printf >> "$MAILFILE" \
145
"This is an automated report generated by the Advanced Intrusion Detection
146
Environment on %s started at %s.\n\n" "$FQDN" "$BEGINSTAMP"
148
printf >> "$LOGFILE" \
149
"aide run on %s started at %s.\n" "$FQDN" "$BEGINSTAMP"
101
if [ "$ONEXIT" = "running" ]; then
153
printf >> "$LOGFILE" "terminated with signal %s\n" "$2"
154
printf >> "$MAILFILE" "The cron job was terminated with signal %s\n\n" "$2"
112
LOGHEAD="$(printf "terminated with signal %s" "$2")"
113
MAILHEAD="$(printf "The cron job was terminated with signal %s" "$2")"
157
printf >> "$LOGFILE" "terminated by fatal error.\n"
158
printf >> "$MAILFILE" "The cron job was terminated by a fatal error.\n\n"
116
LOGHEAD="$(printf "terminated by fatal error.")"
117
MAILHEAD="$(printf "The cron job was terminated by a fatal error.")"
120
LOGHEAD="$(printf "terminated because lock %s could not be obtaiend." "$LOCKFILE")"
121
MAILHEAD="$(printf "The cron job was terminated because lock %s could not be obtained." "$LOCKFILE")"
124
LOGHEAD="$(printf "terminated: Cannot move away %s." "$TMPDIRIN")"
125
MAILHEAD="$(printf "The cron job was terminated: Cannot move away %s." "$TMPDIRIN")"
128
LOGHEAD="$(printf "terminated: Cannot create temporary directory %s." "$TMPDIRIN")"
129
MAILHEAD="$(printf "The cron job was terminated: Cannot create temporary directory %s." "$TMPDIRIN")"
163
printf >> "$LOGFILE" "wrong parameter (\"%s\") to onexit.\n" "$1"
164
printf >> "$MAILFILE" "The cron job was terminated for unknown reasons, and a wrong parameter (\"%s\")was given to onexit.\n\n" "$1"
134
LOGHEAD="$(printf "wrong parameter (\"%s\") to onexit." "$1")"
135
MAILHEAD="$(printf "The cron job was terminated for unknown reasons, and a wrong parameter (\"%s\")was given to onexit." "$1")"
170
if [ -s "$ERRORLOG" ]; then
171
printf "script errors\n" | frame >> "$MAILFILE"
172
< "$ERRORLOG" cat >> "$MAILFILE"
173
printf "End of script errors\n\n" >> "$MAILFILE"
175
printf "script errors\n" | frame >> "$LOGFILE"
176
< "$ERRORLOG" cat >> "$LOGFILE"
177
printf "End of script errors\n" >> "$LOGFILE"
181
# aide post run information
183
if [ -s "$POSTRUNLOG" ]; then
184
printf "AIDE post run information\n" >> "$MAILFILE"
185
< "$POSTRUNLOG" cat >> "$MAILFILE"
186
printf "End of AIDE post run information\n\n" >> "$MAILFILE"
188
printf "AIDE post run information\n" >> "$LOGFILE"
189
< "$POSTRUNLOG" cat >> "$LOGFILE"
190
printf "End of AIDE post run information\n" >> "$LOGFILE"
194
# include error log in daily report e-mail
196
if [ "$ARETVAL" != "0" ]; then
197
printf "AIDE returned a non-zero exit value\nexit value is %d\n\n" "$ARETVAL" | frame >> "$MAILFILE"
198
printf "AIDE returned a non-zero exit value\nexit value is %d\n" "$ARETVAL" | frame >> "$LOGFILE"
201
if [ -s "$AERRLOG" ]; then
139
if [ -z "${TMPDIR:-}" ] || [ -z "${MAILFILE:-}" ]; then
140
# we are being called so early that we are not yet fully initialized
141
# LOGHEAD goes to syslog instead of LOGFILE since we do not know
142
# what's up with LOGFILE
143
logger -t aide-cron-daily "$LOGHEAD"
144
echo "$MAILHEAD" | /usr/bin/mail -s "premature termination - $MAILSUBJ" "$MAILTO"
146
# we are being called after the cron job was properly set up.
149
[ -f "$LOGFILE" ] && savelog -t -g adm -m 640 -u root -c 7 "$LOGFILE" > /dev/null
151
printf >> "$MAILFILE" \
152
"This is an automated report generated by the Advanced Intrusion Detection
153
Environment on %s started at %s.\n\n" "$FQDN" "$BEGINSTAMP"
155
printf >> "$LOGFILE" \
156
"aide run on %s started at %s.\n" "$FQDN" "$BEGINSTAMP"
158
if [ -n "$LOGHEAD" ]; then
159
printf "$LOGHEAD\n" | frame >> "$LOGFILE"
160
printf "\n" >> "$LOGFILE"
162
if [ -n "$MAILHEAD" ]; then
163
printf "$MAILHEAD\n" | frame >> "$MAILFILE"
164
printf "\n\n" >> "$MAILFILE"
167
# report about AIDE's return value
169
if [ -n "${ARETVAL:-}" ]; then
172
PREFIX="$(printf "AIDE returned with exit code %d. " "$ARETVAL")"
176
ARETERR="the cron job was interrupted before AIDE could return an exit code.";;
178
PREFIX="AIDE returned with a zero exit code. "
179
ARETEXPL="No changes detected!";;
181
ARETEXPL="New files detected!";;
183
ARETEXPL="Removed files detected!";;
185
ARETEXPL="New and removed files detected!";;
187
ARETEXPL="Changed files detected!";;
189
ARETEXPL="New and changed files detected!";;
191
ARETEXPL="New and deleted files detected!";;
193
ARETEXPL="New, deleted and changed files detected!";;
195
ARETERR="Error writing!";;
197
ARETERR="Invalid Argument!";;
199
ARETERR="Unimplemented function!";;
201
ARETERR="Invalid configuration!";;
203
ARETERR="Input/Output error!";;
205
ARETERR="$(printf "AIDE returned an unknown non-zero exit value\nexit value is %d\n\n" "$ARETVAL")";;
207
if [ -n "$ARETEXPL" ]; then
208
echo "$PREFIX $ARETEXPL" >> "$MAILFILE"
209
echo "$PREFIX $ARETEXPL" >> "$LOGFILE"
211
if [ -n "$ARETERR" ]; then
212
echo "$PREFIX $ARETERR" | frame >> "$MAILFILE"
213
echo "$PREFIX $ARETERR" | frame >> "$LOGFILE"
219
ARETEXPL="ARETVAL not initialized. cron job was aborted prematurely."
221
echo $ARETEXPL | frame >> "$LOGFILE"
222
echo $ARETEXPL | frame >> "$MAILFILE"
224
printf "\n" >> "$LOGFILE"
225
printf "\n\n" >> "$MAILFILE"
230
if [ -n "${ERRORLOG:-}" ] && [ -s "$ERRORLOG" ]; then
231
printf "script errors\n" | frame >> "$MAILFILE"
232
< "$ERRORLOG" cat >> "$MAILFILE"
233
printf "End of script errors\n\n" >> "$MAILFILE"
235
printf "script errors\n" | frame >> "$LOGFILE"
236
< "$ERRORLOG" cat >> "$LOGFILE"
237
printf "End of script errors\n" >> "$LOGFILE"
240
# aide post run information
242
if [ -n "${POSTRUNLOG:-}" ] && [ -s "$POSTRUNLOG" ]; then
243
printf "AIDE post run information\n" >> "$MAILFILE"
244
< "$POSTRUNLOG" cat >> "$MAILFILE"
245
printf "End of AIDE post run information\n\n" >> "$MAILFILE"
247
printf "AIDE post run information\n" >> "$LOGFILE"
248
< "$POSTRUNLOG" cat >> "$LOGFILE"
249
printf "End of AIDE post run information\n" >> "$LOGFILE"
252
# include error log in daily report e-mail
254
if [ -n "${AERRLOG:-}" ] && [ -s "$AERRLOG" ]; then
202
255
errorlines="$(wc -l "$AERRLOG" | awk '{ print $1 }')"
203
256
if [ "${errorlines:=0}" -gt "$LINES" ]; then
204
257
printf "AIDE has returned many errors.\nthe error log output has been truncated in this mail\n" | \
272
327
printf >> "$LOGFILE" "AIDE output (%d lines):\n" "$loglines"
273
328
< "$ARUNLOG" cat >> "$LOGFILE"
274
329
printf >> "$LOGFILE" "End of AIDE output.\n\n"
276
331
printf >> "$MAILFILE" "AIDE detected no changes.\n\n"
277
332
printf >> "$LOGFILE" "AIDE detected no changes.\n\n"
335
printf >> "$MAILFILE" "funny, AIDE did not leave a log.\n\n"
336
printf >> "$LOGFILE" "funny, AIDE did not leave a log.\n"
280
if [ -s "$DBCHECKLOG" ]; then
339
if [ -n "${DBCHECKLOG:-}" ] && [ -s "$DBCHECKLOG" ]; then
281
340
< "$DBCHECKLOG" cat >> "$MAILFILE"
282
341
printf >> "$MAILFILE" "\n"
283
342
< "$DBCHECKLOG" cat >> "$LOGFILE"
286
printf >> "$MAILFILE" "End of AIDE daily cron job at %s, run time %d seconds\n" "$(date +"at %Y-%m-%d %H:%M")" "$(( $(date +%s) - $BEGINTIME ))"
287
printf >> "$LOGFILE" "End of AIDE daily cron job at %s, run time %d seconds\n" "$(date +"at %Y-%m-%d %H:%M")" "$(( $(date +%s) - $BEGINTIME ))"
289
# send mail if changes or errors were detected or quiet reports not requested
290
if [ "$QUIETREPORTS" = "no" ] || [ "$CHANGES" != "0" ] || [ $(< "$ERRORLOG" wc -l) -ne 0 ]; then
291
< "$MAILFILE" /usr/bin/mail -s "$MAILSUBJ" "$MAILTO"
294
# clean up temp files
345
printf >> "$MAILFILE" "End of AIDE daily cron job at %s, run time %d seconds\n" "$(date +"at %Y-%m-%d %H:%M")" "$(( $(date +%s) - $BEGINTIME ))"
346
printf >> "$LOGFILE" "End of AIDE daily cron job at %s, run time %d seconds\n" "$(date +"at %Y-%m-%d %H:%M")" "$(( $(date +%s) - $BEGINTIME ))"
348
# send mail if changes or errors were detected or quiet reports not requested
349
if [ "$QUIETREPORTS" = "no" ] || [ "$ARETVAL" != "0" ] || [ $(< "$ERRORLOG" wc -l) -ne 0 ]; then
350
< "$MAILFILE" /usr/bin/mail -s "$MAILSUBJ" "$MAILTO"
353
# clean up temp files
358
if [ -n "${LOCKED:-}" ] && command -v dotlockfile >/dev/null 2>&1; then
359
dotlockfile -u "$LOCKFILE" || true
366
BEGINTIME="$(date +%s)"
298
368
if command -v dotlockfile >/dev/null 2>&1; then
299
dotlockfile -u "$LOCKFILE" || true
369
if ! dotlockfile -p -l "$LOCKFILE"; then
374
PREERRLOG="no dotlockfile binary in path, not checking for already running aide cron job\n"
379
if [ -e "$TMPDIRIN" ]; then
380
if ! NEWNAME="$(mktemp -d $TMPBASE/cron.daily.old.XXXXXXXXXX)"; then
384
mv "$TMPDIRIN" "$NEWNAME"
389
if ! mkdir -p $TMPDIRIN; then
394
# we can now directly use file names inside $TMPDIR: It is only
395
# writeable for us (umask 077), so we're safe against symlink attacks.
396
# We use invariant file names here since our work files need to be
397
# excluded from aide.
400
# now, with $TMPDIR having been created, we can use onexit.
402
# ERRORLOG: Error messages from script. Gets written to $LOGFILE first
403
ERRORLOG="$(mytempfile errorlog)"
405
if [ -n "${PREERRORLOG:-}" ]; then
406
printf >> "$ERRORLOG" "$PREERRORLOG"
410
# MAILFILE: Contents gets mailed. Built and handled from inside onexit()
411
MAILFILE="$(mytempfile mailfile)"
416
if [ ! -f "$DATABASE" ]; then
417
printf >> "$ERRORLOG" "Fatal error: The AIDE database does not exist!\n"
418
printf >> "$ERRORLOG" "This may mean you haven't created it, or it may mean that someone has removed it.\n"
425
# re-assign current time to be more accurate about aide's real start time
307
426
BEGINSTAMP="$(date +"%Y-%m-%d %H:%M:%S")"
309
428
# ARUNLOG: standard output of aide run