Debian Codegraph @

debian/bin/dailyaidecheck

raw symbols SCIP
1 #!/bin/bash
2 #
3 # Copyright (C) 2000-2005 Mike Markley
4 # Copyright (C) 2006-2023 Marc Haber
5 # Copyright (C) 2010-2021 Hannes von Haugwitz
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; either version 2 of the
9 # License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20
21 set -e
22 set -C
23
24 shopt -u checkwinsize
25
26 CALLER=""
27 if [ -n "$1" ]; then
28 #shellcheck disable=SC2034
29 CALLER="$1"
30 fi
31
32 # trap handler
33
34 FQDN="$(hostname --fqdn 2>/dev/null || true)"
35 if [ -z "$FQDN" ]; then
36 FQDN="$(hostname 2>/dev/null || true)"
37 fi
38 if [ -z "$FQDN" ]; then
39 echo >&2 "error determining FQDN: neither hostname nor hostname --fqdn does give output"
40 hostname >&2
41 hostname --fqdn >&2
42 exit 1
43 fi
44
45 traphandler() {
46 trap - INT ERR
47 if [ -n "${LOCKED:-}" ]; then
48 # we have the lock,
49 pidof aide | xargs --no-run-if-empty kill -9
50 fi
51 if type -t onexit >/dev/null; then
52 onexit signal "$1"
53 else
54 echo "${MAILHEAD:-}" | fold --bytes --width="${MAILWIDTH:-990}" | send_mail "${MAILTO}" "early termination - ${MAILSUBJ}"
55 fi
56 return 0
57 }
58 trap ' traphandler INT; trap - INT ERR' INT
59 trap ' traphandler ERR; trap - INT ERR' ERR
60
61 # bail if no aide binary found
62
63 if ! [ -f "/usr/bin/aide" ] && ! [ -f "/usr/sbin/aide" ]; then
64 exit 0
65 fi
66
67 # default variables
68
69 PATH="/sbin:/usr/sbin:/bin:/usr/bin"
70 LOGDIR="/var/log/aide"
71 # LOGFILE: /var/log/aide/aide.log - all logs untruncated (not temp)
72 LOGFILE="$LOGDIR/aide.log"
73 PREFIX="aide"
74 LOCKBASE="/run/aide"
75 LOCKFILE="$LOCKBASE/dailyaidecheck.lock"
76
77 TMPBASE="/var/lib/aide"
78 if ! [ -d "${TMPBASE}" ]; then
79 TMPBASE="/run/aide"
80 fi
81 TMPDIRIN="$TMPBASE/dailyaidecheck"
82
83 USE_SAVELOG=""
84 if command -v savelog > /dev/null; then
85 USE_SAVELOG="1"
86 fi
87
88 AIDEARGS=""
89 MAILSUBJ="Daily AIDE report for $FQDN"
90
91 BEGINSTAMP="$(date +"%Y-%m-%d %H:%M:%S")"
92
93 # insist that $LOCKBASE exists
94 if ! [ -d "$LOCKBASE" ]; then
95 printf "ERR: %s does not exist. It must exist and be only writable for %s\n" "${LOCKBASE}" "${AIDEUSER}"
96 fi
97
98 # have /etc/default/aide override variables
99 if [ -f "/etc/default/aide" ]; then
100 #shellcheck disable=1091
101 . "/etc/default/aide"
102 fi
103
104 # from here on, we're going to bail on unbound variables
105
106 set -u
107
108 # umask
109
110 umask 027
111
112 MAILTO="${MAILTO:-root}"
113 eval MAILTO="$MAILTO"
114
115 send_mail() {
116 local MAILTO
117 local MAILSUBJ
118 local MAILNAME
119 local S_NAIL_MTA
120 local RET
121 MAILTO="$1"
122 shift
123 MAILSUBJ="$@"
124 RET=""
125 if [ -n "${MAILCMD:-}" ]; then
126 eval "${MAILCMD} -s \"${MAILSUBJ}\" \"${MAILTO}\"" || RET=$?
127 elif command -v s-nail >/dev/null; then
128 S_NAIL_MTA="smtp://_aide@localhost"
129 if [ "${CALLER}" == "--autopkgtest" ]; then
130 S_NAIL_MTA="test:///tmp/dailyaidecheck"
131 fi
132 if echo "${MAILTO}" | grep -qv @; then
133 # unqualified MAILTO, add /etc/mailname
134 MAILNAME="$(cat /etc/mailname 2>/dev/null || true)"
135 MAILTO="${MAILTO}@${MAILNAME:-localhost}"
136 fi
137 s-nail -Smta=${S_NAIL_MTA} -Ssmtp-auth=none -Sv15-compat -Sfrom="AIDE on ${FQDN} <root@${FQDN}>" -s "${MAILSUBJ}" "${MAILTO}" || RET=$?
138 elif command -v mail >/dev/null; then
139 if [ "${CALLER}" = "--crondaily" ] || [ "$(id -u)" -eq 0 ]; then
140 # We are called from cron via capsh or have root and thus
141 # mail(1) is useable, even if sendmail is set-uid-root.
142 mail -s "${MAILSUBJ}" "${MAILTO}" || RET=$?
143 else
144 printf >&2 "WARN: it is not possible to use mail(1) unless aide is run as root or as non-root with added capabilities (either via systemd or via capsh). See /usr/share/doc/aide-common/README.Debian.gz for more information.\\n"
145 fi
146 else
147 printf >&2 "WARN: mail or s-nail not installed, cannot send mail. See /usr/share/doc/aide-common/README.Debian.gz for more information.\\n"
148 fi
149 if [ -n "${RET}" ]; then
150 printf >&2 "ERR: dailyaidejob was not able to send mail. The invoked mail command returned a non-zero exit code.\\n"
151 fi
152 }
153
154 # grep aide configuration data from aide config
155
156 DATABASE="$(< "${CONFIG}" grep "^database_in[[:space:]]*=[[:space:]]*file:/" | head -n 1 | cut --delimiter=: --fields=2)"
157 DATABASE_OUT="$(< "${CONFIG}" grep "^database_out[[:space:]]*=[[:space:]]*file:/" | head -n 1 | cut --delimiter=: --fields=2)"
158
159 < "${CONFIG}" grep -qE "^(report_)?grouped[[:space:]]*=[[:space:]]*(no|false)[[:space:]]*$" && GROUPED="false" || GROUPED="true"
160
161 # default values
162
163 CRON_DAILY_RUN="${CRON_DAILY_RUN:-yes}"
164 MAILWIDTH="${MAILWIDTH:-990}"
165 FIGLET="${FIGLET:-yes}"
166 DATABASE="${DATABASE:-/var/lib/aide/aide.db}"
167 LINES="${LINES:-1000}"
168 COMMAND="${COMMAND:-check}"
169 COPYNEWDB="${COPYNEWDB:-no}"
170 QUIETREPORTS="${QUIETREPORTS:-no}"
171 SILENTREPORTS="${SILENTREPORTS:-no}"
172 TRUNCATEDETAILS="${TRUNCATEDETAILS:-no}"
173 FILTERUPDATES="${FILTERUPDATES:-no}"
174 FILTERINSTALLATIONS="${FILTERINSTALLATIONS:-no}"
175 CRONEXITHOOK="${CRONEXITHOOK:-}"
176 ONEXIT=""
177 AIDEUSER="${AIDEUSER:-$(id --user --name)}"
178 AIDEUSERUID="$(id --user "${AIDEUSER}")"
179
180 # silent implies quiet
181 if [ "$SILENTREPORTS" = "yes" ]; then
182 QUIETREPORTS="yes"
183 fi
184
185 # Get the database's date
186 DATABASEDATE=""
187 if [ -f "$DATABASE" ]; then
188 DATABASEDATE="$(stat -c %y "$DATABASE" | sed -e "s/\\..*//")"
189 fi
190
191 # Force TRUNCATEDETAILS when filter updates/installations
192 if [ "$FILTERUPDATES" = "yes" ] || [ "$FILTERINSTALLATIONS" = "yes" ] ; then
193 TRUNCATEDETAILS="yes"
194 fi
195
196 # functions
197
198 mytempfile() {
199 NAME="$1"
200 echo "$TMPDIR/$NAME"
201 touch "$TMPDIR/$NAME"
202 }
203
204 frame() {
205 WIDTH=78
206 STARS="*******************************************************************************"
207 SPACES=" "
208 printf "%s\\n" "${STARS:1:$WIDTH}"
209 while read -r line ; do
210 HALF="${SPACES:1:$(((WIDTH-${#line})/2))}"
211 LINE="$HALF$line$SPACES"
212 printf "*%s*\\n" "${LINE:1:$((WIDTH-2))}"
213 done
214 printf "%s\\n" "${STARS:1:$WIDTH}"
215 }
216
217 onexit() {
218 if [ "$ONEXIT" = "running" ]; then
219 return 1
220 fi
221
222 ONEXIT="running"
223
224 local LOGHEAD
225 local MAILHEAD
226 local CRONJOBERR
227
228 CRONEXITHOOKPARM="$1"
229 case "$1" in
230 signal)
231 LOGHEAD="$(printf "terminated with signal %s" "$2")"
232 MAILHEAD="$(printf "The daily aide check was terminated with signal %s" "$2")"
233 CRONJOBERR="$2"
234 ;;
235 nodb)
236 LOGHEAD="$(printf "database not present.")"
237 MAILHEAD="$(printf "The daily aide check was terminated because no AIDE database is present")"
238 CRONJOBERR="no DB"
239 ;;
240 fatal)
241 LOGHEAD="$(printf "terminated by fatal error.")"
242 MAILHEAD="$(printf "The daily aide check was terminated by a fatal error.")"
243 CRONJOBERR="fatal"
244 ;;
245 nolock)
246 LOGHEAD="$(printf "terminated because lock %s could not be obtained." "$LOCKFILE")"
247 MAILHEAD="$(printf "The daily aide check was terminated because lock %s could not be obtained." "$LOCKFILE")"
248 CRONJOBERR="no lock"
249 ;;
250 cantmovetmp)
251 LOGHEAD="$(printf "terminated: Cannot move away %s." "$TMPDIRIN")"
252 MAILHEAD="$(printf "The daily aide check was terminated: Cannot move away %s." "$TMPDIRIN")"
253 CRONJOBERR="temp dir"
254 ;;
255 nohook)
256 LOGHEAD="$(printf "terminated: CRONEXITHOOK set to %s which is not executeable." "$CRONEXITHOOK")"
257 MAILHEAD="$(printf "The daily aide check was terminated: CRONEXITHOOK set to %s which is not executeable." "$CRONEXITHOOK")"
258 CRONJOBERR="no hook"
259 ;;
260 cantcreatetmp)
261 LOGHEAD="$(printf "terminated: Cannot create temporary directory %s." "$TMPDIRIN")"
262 MAILHEAD="$(printf "The daily aide check was terminated: Cannot create temporary directory %s." "$TMPDIRIN")"
263 CRONJOBERR="temp dir"
264 ;;
265 cantchmodtmp)
266 LOGHEAD="$(printf "terminated: Cannot chmod temporary directory %s." "$TMPDIRIN")"
267 MAILHEAD="$(printf "The daily aide check was terminated: Cannot chmod temporary directory %s." "$TMPDIRIN")"
268 CRONJOBERR="temp dir"
269 ;;
270 tmpnotours)
271 LOGHEAD="$(printf "terminated: Temporary directory %s has wrong owner/mode." "$TMPDIRIN")"
272 MAILHEAD="$(printf "The daily aide check was terminated: Temporary directory %s has wrong owner/mode." "$TMPDIRIN")"
273 CRONJOBERR="temp dir"
274 ;;
275 success)
276 ;;
277 *)
278 LOGHEAD="$(printf "wrong parameter (\"%s\") to onexit." "$1")"
279 MAILHEAD="$(printf "The daily aide check was terminated for unknown reasons, and a wrong parameter (\"%s\")was given to onexit." "$1")"
280 CRONJOBERR="unknown"
281 CRONEXITHOOKPARM="unknown"
282 ;;
283 esac
284
285 if [ -z "${TMPDIR:-}" ] || [ -z "${MAILFILE:-}" ]; then
286 # we are being called so early that we are not yet fully initialized
287 # LOGHEAD goes to syslog instead of LOGFILE since we do not know
288 # what's up with LOGFILE
289 logger -t dailyaidecheck "$LOGHEAD"
290 if [ "$SILENTREPORTS" != "yes" ]; then
291 echo "$MAILHEAD" | fold --bytes --width="${MAILWIDTH:-990}" | send_mail "${MAILTO}" "premature termination - ${MAILSUBJ}"
292 fi
293 CRONEXITHOOKPARM="early-$CRONEXITHOOKPARM"
294 else
295 # we are being called after the daily aide check was properly set up.
296 # Do the full works.
297
298 if [ "$USE_SAVELOG" = "1" ] || [ "$USE_SAVELOG" = "yes" ]; then
299 savelog -t -m 640 -c 7 "$LOGFILE" > /dev/null
300 else
301 LOGFILEWDATE="${LOGFILE}-$(date +%Y%m%d-%H%M%S)"
302 ln -sf "$LOGFILEWDATE" "$LOGFILE"
303 LOGFILE="${LOGFILEWDATE}"
304 fi
305
306 printf >> "$MAILFILE" \
307 "This is an automated report generated by the Advanced Intrusion Detection environment on %s started at %s.\\n\\n" "$FQDN" "$BEGINSTAMP"
308
309 printf >> "$LOGFILE" \
310 "aide run on %s started at %s.\\n" "$FQDN" "$BEGINSTAMP"
311
312 if [ -n "${LOGHEAD:-}" ]; then
313 printf "%s\\n" "$LOGHEAD" | frame >> "$LOGFILE"
314 printf "\\n" >> "$LOGFILE"
315 fi
316 if [ -n "${MAILHEAD:-}" ]; then
317 printf "%s\\n" "$MAILHEAD" | frame >> "$MAILFILE"
318 printf "\\n\\n" >> "$MAILFILE"
319 fi
320
321 # report about AIDE's return value
322
323 PRINTED=""
324 FIGLETTEXT=""
325 SCRIPTRETVAL="0"
326 if [ -n "${ARETVAL:-}" ]; then
327 ARETEXPL=""
328 ARETERR=""
329 PREFIX="$(printf "AIDE returned with exit code %d." "$ARETVAL")"
330 case "$ARETVAL" in
331 -1)
332 PREFIX=""
333 ARETERR="the daily aide check was interrupted before AIDE could return an exit code."
334 FIGLETTEXT="${CRONJOBERR:-interrupt}"
335 SCRIPTRETVAL="1"
336 ;;
337 0)
338 PREFIX="AIDE returned with a zero exit code."
339 ARETEXPL="No changes detected!"
340 FIGLETTEXT="unchanged"
341 ;;
342 1)
343 ARETEXPL="Added entries detected!"
344 FIGLETTEXT="add"
345 ;;
346 2)
347 ARETEXPL="Removed entries detected!"
348 FIGLETTEXT="rem"
349 ;;
350 3)
351 ARETEXPL="Added and removed entries detected!"
352 FIGLETTEXT="add rem"
353 ;;
354 4)
355 ARETEXPL="Changed entries detected!"
356 FIGLETTEXT="chg"
357 ;;
358 5)
359 ARETEXPL="Added and changed entries detected!"
360 FIGLETTEXT="add chg"
361 ;;
362 6)
363 ARETEXPL="Removed and changed entries detected!"
364 FIGLETTEXT="rem chg"
365 ;;
366 7)
367 ARETEXPL="Added, removed and changed entries detected!"
368 FIGLETTEXT="add rem chg"
369 ;;
370 14)
371 ARETERR="Error writing!"
372 FIGLETTEXT="$ARETERR"
373 SCRIPTRETVAL="1"
374 ;;
375 15)
376 ARETERR="Invalid Argument!"
377 FIGLETTEXT="EINVAL"
378 SCRIPTRETVAL="1"
379 ;;
380 16)
381 ARETERR="Unimplemented function!"
382 FIGLETTEXT="unimplemented"
383 SCRIPTRETVAL="1"
384 ;;
385 17|255)
386 ARETERR="Invalid configuration!"
387 FIGLETTEXT="invalid config"
388 SCRIPTRETVAL="1"
389 ;;
390 18)
391 ARETERR="Input/Output error!"
392 FIGLETTEXT="EIO"
393 SCRIPTRETVAL="1"
394 ;;
395 21)
396 ARETERR="cannot obtain lock"
397 FIGLETTEXT="no lock"
398 SCRIPTRETVAL="1"
399 ;;
400 250)
401 ARETERR="executable aide not found"
402 FIGLETTEXT="no executable"
403 SCRIPTRETVAL="1"
404 ;;
405 *)
406 ARETERR="$(printf "unknown non-zero exit value %d\\n\\n" "$ARETVAL")"
407 FIGLETTEXT="unknown error"
408 SCRIPTRETVAL="1"
409 ;;
410 esac
411 if [ -n "$ARETEXPL" ]; then
412 printf "%s %s\\n" "$PREFIX" "$ARETEXPL" >> "$MAILFILE"
413 printf "%s %s\\n" "$PREFIX" "$ARETEXPL" >> "$LOGFILE"
414 PRINTED=1
415 fi
416 if [ -n "$ARETERR" ]; then
417 printf "%s %s\\n" "$PREFIX" "$ARETERR" | frame >> "$MAILFILE"
418 printf "%s %s\\n" "$PREFIX" "$ARETERR" | frame >> "$LOGFILE"
419 PRINTED=1
420 fi
421 unset ARETEXPL
422 unset ARETERR
423 unset PREFIX
424 else
425 ARETEXPL="ARETVAL not initialized. daily aide check was aborted prematurely."
426 ARETVAL=255
427 FIGLETTEXT="abort"
428 printf "%s\\n" "$ARETEXPL" | frame >> "$MAILFILE"
429 printf "%s\\n" "$ARETEXPL" | frame >> "$LOGFILE"
430 PRINTED=1
431 unset ARETEXPL
432 fi
433 if [ "${FIGLET}" = "yes" ] && [ -x "$(command -v figlet)" ] && [ -n "$FIGLETTEXT" ]; then
434 printf "\\n%s\\n\\n" "$(figlet $FIGLETTEXT)" >> "$MAILFILE"
435 PRINTED=1
436 fi
437 if [ -n "$PRINTED" ]; then
438 printf "\\n" >> "$LOGFILE"
439 printf "\\n\\n" >> "$MAILFILE"
440 fi
441 unset PRINTED
442
443 # script errors
444
445 if [ -n "${ERRORLOG:-}" ] && [ -s "$ERRORLOG" ]; then
446 {
447 printf "script errors\\n" | frame
448 cat "$ERRORLOG"
449 printf "End of script errors\\n\\n"
450 } >> "$MAILFILE"
451
452 {
453 printf "script errors\\n" | frame
454 cat "$ERRORLOG"
455 printf "End of script errors\\n"
456 } >> "$LOGFILE"
457 fi
458
459 # aide post run information
460
461 if [ -n "${POSTRUNLOG:-}" ] && [ -s "$POSTRUNLOG" ]; then
462 {
463 printf "AIDE post run information\\n"
464 cat "$POSTRUNLOG"
465 printf "End of AIDE post run information\\n\\n"
466 } >> "$MAILFILE"
467
468 {
469 printf "AIDE post run information\\n"
470 cat "$POSTRUNLOG"
471 printf "End of AIDE post run information\\n"
472 } >> "$LOGFILE"
473 fi
474
475 # include error log in daily report e-mail
476
477 if [ -n "${AERRLOG:-}" ] && [ -s "$AERRLOG" ]; then
478 errorlines="$(wc -l "$AERRLOG" | awk '{ print $1 }')"
479 {
480 if [ "$LINES" -gt "0" ] && [ "${errorlines:=0}" -gt "$LINES" ]; then
481 printf "AIDE has returned many errors.\\nthe error log output has been truncated in this mail\\n" | \
482 frame
483 printf "Error output is %d lines, truncated to %d.\\n" "$errorlines" "$LINES"
484 head -n "$LINES" "$AERRLOG"
485 printf "\\nEnd of truncated AIDE error output. The full output can be found in %s.\\n\\n" "$LOGFILE"
486 else
487 printf "Errors produced (%d lines):\\n" "$errorlines"
488 cat "$AERRLOG"
489 printf "\\nEnd of AIDE error output.\\n\\n"
490 fi
491 } >> "$MAILFILE"
492 {
493 printf "AIDE error output (%d lines):\\n" "$errorlines"
494 cat "$AERRLOG"
495 printf "End of AIDE error output\\n"
496 } >> "$LOGFILE"
497 else
498 printf >> "$MAILFILE" "AIDE produced no errors.\\n\\n"
499 printf >> "$LOGFILE" "AIDE produced no errors.\\n"
500 fi
501
502 # finish log file
503 {
504 if [ -n "${ARUNLOG:-}" ] && [ -s "$ARUNLOG" ]; then
505 printf "AIDE output (%d lines):\\n" "$(wc -l "$ARUNLOG" | awk '{ print $1 }')"
506 cat "$ARUNLOG"
507 printf "End of AIDE output.\\n\\n"
508 else
509 printf "AIDE detected no changes.\\n\\n"
510 fi
511
512 if [ -n "${DBCHECKLOG:-}" ] && [ -s "$DBCHECKLOG" ]; then
513 cat "$DBCHECKLOG"
514 fi
515
516 ENDTIME="$(date +%s)"
517
518 printf "End of AIDE daily aide check at %s, run time %d seconds\\n" "$(date +"%Y-%m-%d %H:%M" -d@"$ENDTIME")" "$(( ENDTIME - BEGINTIME ))"
519 } >> "$LOGFILE"
520
521 LOGFILE_CHECKSUM="$(sha256sum "$LOGFILE")"
522
523 # include de-noised log into mail
524
525 if [ -n "${ARUNLOG:-}" ] && [ -s "$ARUNLOG" ]; then
526
527 MAIL_MODE=0
528
529 # truncate details
530 if [ "$TRUNCATEDETAILS" = "yes" ] ; then
531 case "$ARETVAL" in
532 4|5|6|7)
533 MAILTMP="$(mytempfile aidemail)"
534 < "$ARUNLOG" sed '/^Detailed information about changes:$/,/^The attributes of the (uncompressed) database(s):$/{/^The attributes of the (uncompressed) database(s):$/!d}' >> "$MAILTMP"
535 MAIL_MODE=1
536 ;;
537 *)
538 MAILTMP="$ARUNLOG"
539 ;;
540 esac
541
542 # Filter package upgrades/installations
543
544 # Figure out where the dpkg log file is
545 DPKGLOG="$(< /etc/dpkg/dpkg.cfg grep "^log" | head -n 1 | cut -d ' ' -f 2)"
546
547 if { [ "$FILTERUPDATES" = "yes" ] || [ "$FILTERINSTALLATIONS" = "yes" ] ; } && [ -s "$DPKGLOG" ]; then
548
549 # Create a list of files modified by system updates
550 if [ "$FILTERUPDATES" = "yes" ] && [ "$FILTERINSTALLATIONS" = "yes" ] ; then
551 FILTER="install|upgrade"
552 elif [ "$FILTERUPDATES" = "yes" ]; then
553 FILTER="upgrade"
554 else
555 FILTER="install"
556 fi
557 PKG_FILE_LIST="$(mytempfile pkg_file_list)"
558 REGEX="^([^ ]+ [^ ]+) ($FILTER) ([^ ]+) [^ ]+ [^ ]+$"
559 PKGS=()
560 while read -r line; do
561 if [[ $line =~ $REGEX ]] && [[ "$DATABASEDATE" < ${BASH_REMATCH[1]} ]]; then
562 if dpkg-query -L "${BASH_REMATCH[3]}" > /dev/null 2>&1; then
563 PKGS+=("${BASH_REMATCH[3]} (${BASH_REMATCH[2]})")
564 dpkg-query -L "${BASH_REMATCH[3]}" | sed -e "/^$/d" -e "/\\/\\./d" >> "$PKG_FILE_LIST"
565 if ! ls "/var/lib/dpkg/info/${BASH_REMATCH[3]}."* >> "$PKG_FILE_LIST" 2>/dev/null; then
566 ls "/var/lib/dpkg/info/${BASH_REMATCH[3]%:*}."* >> "$PKG_FILE_LIST"
567 fi
568 fi
569 fi
570 done < "$DPKGLOG"
571
572 if [ ${#PKGS[@]} -gt 0 ]; then
573 FILTEREDMAIL=$(mytempfile filteredmail)
574 MAIL_MODE=$(( MAIL_MODE + 2 ))
575 ADD=0; REM=0; CHG=0
576 N_ADD=0; N_REM=0; N_CHG=0
577 declare -a NF_ADD NF_REM NF_CHG
578 NF_ADD=()
579 NF_REM=()
580 NF_CHG=()
581 REGEX="^(changed|removed|added|[fdLDBFs?!][ :l<>=bpugamcinHAXSEC.+-]{17}): (.*)"
582 BACKUPIFS="$IFS"
583 IFS=""
584 while read -r line; do
585 if [[ $line =~ $REGEX ]] ; then
586 #shellcheck disable=SC2143
587 [ -z "$(grep -xF "${BASH_REMATCH[2]}" "$PKG_FILE_LIST")" ] && DONTFILTER_FILE=true || DONTFILTER_FILE=false
588 case "${BASH_REMATCH[1]}" in
589 added|[fdLDBFs?]+++++++++++++++++)
590 ((ADD++)) || true
591 if $DONTFILTER_FILE; then
592 ((N_ADD++)) || true
593 if $GROUPED; then
594 NF_ADD[${#NF_ADD[*]}]="$line"
595 else
596 NF_CHG[${#NF_CHG[*]}]="$line"
597 fi
598 fi
599 ;;
600 removed|[fdLDBFs?]-----------------)
601 ((REM++)) || true
602 if $DONTFILTER_FILE; then
603 ((N_REM++)) || true
604 if $GROUPED; then
605 NF_REM[${#NF_REM[*]}]="$line"
606 else
607 NF_CHG[${#NF_CHG[*]}]="$line"
608 fi
609 fi
610 ;;
611 changed|[fdLDBFs?!]*)
612 ((CHG++)) || true
613 if $DONTFILTER_FILE; then
614 ((N_CHG++)) || true
615 NF_CHG[${#NF_CHG[*]}]="$line"
616 fi
617 ;;
618 *)
619 printf >> "$FILTEREDMAIL" "error: '%s' could not be matched, mail report is incomplete (full output can be found in %s)!! Please file a bug report against the aide-common package and include this error message.\\n" "${BASH_REMATCH[1]}" "$LOGFILE"
620 ;;
621 esac
622 fi
623 done < "$MAILTMP"
624 IFS=$BACKUPIFS
625 F_ADD=$(( ADD-N_ADD )) || true
626 F_REM=$(( REM-N_REM )) || true
627 F_CHG=$(( CHG-N_CHG )) || true
628 {
629 < "$MAILTMP" sed -n '0,/^ Total number of entries:/{p;}'
630 #shellcheck disable=SC2059
631 {
632 SEPERATOR_TEMPLATE="\\n---------------------------------------------------\\n%s entries (filtered: %s):\\n---------------------------------------------------\\n\\n"
633 NUM_FILES_TEMPLATE=" %s entries:\\t\\t%s\\t(filtered: %s)\\n"
634 printf "$NUM_FILES_TEMPLATE" "Added" "$N_ADD" "$F_ADD"
635 printf "$NUM_FILES_TEMPLATE" "Removed" "$N_REM" "$F_REM"
636 printf "$NUM_FILES_TEMPLATE" "Changed" "$N_CHG" "$F_CHG"
637 printf "\\nThe following package changes were detected and were filtered from this mail:\\n"
638 printf '%s\n' "${PKGS[@]}"
639 if [ "$N_ADD" -eq "0" ] && [ "$N_REM" -eq "0" ] && [ "$N_CHG" -eq "0" ] ; then
640 printf "\\nAIDE detected no changes after filtering package changes.\\n\\n"
641 else
642 if [ "${#NF_ADD[@]}" -gt "0" ]; then
643 printf "$SEPERATOR_TEMPLATE" "Added" "$F_ADD"
644 for ((i=0;i<${#NF_ADD[@]};i++)); do printf "%s\\n" "${NF_ADD[$i]}"; done
645 fi
646 if [ "${#NF_REM[@]}" -gt "0" ]; then
647 printf "$SEPERATOR_TEMPLATE" "Removed" "$F_REM"
648 for ((i=0;i<${#NF_REM[@]};i++)); do printf "%s\\n" "${NF_REM[$i]}"; done
649 fi
650 if [ "${#NF_CHG[@]}" -gt "0" ]; then
651 if $GROUPED; then
652 printf "$SEPERATOR_TEMPLATE" "Changed" "$F_CHG"
653 else
654 if [ "$N_ADD" -gt "0" ] && [ "$N_REM" -gt "0" ] && [ "$N_CHG" -gt "0" ]; then
655 HEAD="Added, removed and changed"
656 elif [ "$N_ADD" -gt "0" ] && [ "$N_REM" -gt "0" ]; then
657 HEAD="Added and removed"
658 elif [ "$N_ADD" -gt "0" ] && [ "$N_CHG" -gt "0" ]; then
659 HEAD="Added and changed"
660 elif [ "$N_REM" -gt "0" ] && [ "$N_CHG" -gt "0" ]; then
661 HEAD="Removed and changed"
662 elif [ "$N_ADD" -gt "0" ]; then
663 HEAD="Added"
664 elif [ "$N_REM" -gt "0" ]; then
665 HEAD="Removed"
666 elif [ "$N_CHG" -gt "0" ]; then
667 HEAD="Changed"
668 fi
669 printf "$SEPERATOR_TEMPLATE" "$HEAD" "$((F_ADD+F_REM+F_CHG))"
670 fi
671 for ((i=0;i<${#NF_CHG[@]};i++)); do printf "%s\\n" "${NF_CHG[$i]}"; done
672 fi
673 fi
674 }
675 printf "\\n---------------------------------------------------\\n"
676 < "$MAILTMP" sed -n '/^The attributes of the (uncompressed) database(s):$/,$ {p;}'
677 } >> "$FILTEREDMAIL"
678 MAILTMP="$FILTEREDMAIL"
679 fi
680 fi
681 else
682 MAILTMP="$ARUNLOG"
683 fi
684
685 if [ -n "${NOISE:-}" ]; then
686 NOISETMP="$(mytempfile aidenoise1)"
687 NOISETMP2="$(mytempfile aidenoise2)"
688 < "$MAILTMP" sed -n '1,/^Detailed information about changes:/p' | \
689 grep '^\(changed\|removed\|added\|[fdLDBFs?!][ :l<>=bpugamcinCAXSE.+-]\{16\}\):' | \
690 grep -v "^added: THERE WERE ALSO [0-9]\\+ FILES ADDED UNDER THIS DIRECTORY" >> "$NOISETMP2"
691
692 {
693 if [ -n "$NOISE" ]; then
694 ##+# leaning toothpick syndrome, consider grep -E
695 < "$NOISETMP2" grep -v "^\\(changed\\|removed\\|added\\|[fdLDBFs?!][ :l<>=bpugamcinCAXSE.+-]\\{16\\}\\): $NOISE" >> "$NOISETMP" || true
696 printf "De-Noised output removes everything matching %s.\\n" "$NOISE"
697 fi
698
699 if [ -s "$NOISETMP" ]; then
700 loglines="$(< "$NOISETMP" wc -l | awk '{ print $1 }')"
701 if [ "$LINES" -gt "0" ] && [ "${loglines:=0}" -gt "$LINES" ]; then
702 printf "AIDE has returned long output which has been truncated in this mail\\n" | \
703 frame
704 printf "De-Noised output is %d lines, truncated to %d.\\n" "$loglines" "$LINES"
705 head -n "$LINES" "$NOISETMP"
706 printf "\\nEnd of truncated De-Noised AIDE output. The full output can be found in %s.\\nsha256sum: %s\\n\\n" "$LOGFILE" "$LOGFILE_CHECKSUM"
707 else
708 printf "De-Noised output of the daily AIDE check (%d lines):\\n" "$loglines"
709 cat "$NOISETMP"
710 printf "\\nEnd of De-Noised AIDE output.\\n\\n"
711 fi
712 else
713 printf "AIDE detected no changes after removing noise.\\n\\n"
714 fi
715 printf "============================================================================\\n"
716 }
717 fi
718
719 # include non-de-noised log into mail
720
721 {
722 if [ -n "${MAILTMP:-}" ] && [ -s "$MAILTMP" ]; then
723 loglines="$(wc -l "$MAILTMP" | awk '{ print $1 }')"
724 if [ "$LINES" -gt "0" ] && [ "${loglines:=0}" -gt "$LINES" ]; then
725 printf "AIDE has returned long output which has been truncated in this mail\\n" | \
726 frame
727 printf "Output is %d lines, truncated to %d.\\n" "$loglines" "$LINES"
728 head -n "$LINES" "$MAILTMP"
729 printf "\\nEnd of truncated AIDE output. The full output can be found in %s.\\nsha256sum: %s\\n\\n" "$LOGFILE" "$LOGFILE_CHECKSUM"
730 else
731 printf "Output of the daily AIDE check (%d lines):\\n" "$loglines"
732 cat "$MAILTMP"
733 if [ "$MAIL_MODE" -gt "0" ] ; then
734 case "$MAIL_MODE" in
735 1) AIDE_OUTPUT="truncated" ;;
736 2) AIDE_OUTPUT="filtered" ;;
737 3) AIDE_OUTPUT="truncated and filtered" ;;
738 esac
739 printf "\\nEnd of %s AIDE output.\\n\\nThe full output can be found in %s.\\nsha256sum: %s\\n\\n" "$AIDE_OUTPUT" "$LOGFILE" "$LOGFILE_CHECKSUM"
740 else
741 printf "\\nEnd of AIDE output.\\n\\n"
742 fi
743 fi
744 else
745 printf "AIDE detected no changes.\\n\\n"
746 fi
747 } >> "$MAILFILE"
748 else
749 printf >> "$MAILFILE" "funny, AIDE did not leave a log.\\n\\n"
750 printf >> "$LOGFILE" "funny, AIDE did not leave a log.\\n"
751 fi
752
753 if [ -n "${DBCHECKLOG:-}" ] && [ -s "$DBCHECKLOG" ]; then
754 < "$DBCHECKLOG" cat >> "$MAILFILE"
755 printf >> "$MAILFILE" "\\n"
756 fi
757
758 printf >> "$MAILFILE" "End of AIDE daily aide check at %s, run time %d seconds\\n" "$(date +"%Y-%m-%d %H:%M" -d@"$ENDTIME")" "$(( ENDTIME - BEGINTIME ))"
759
760 # send mail if changes or errors were detected or quiet reports not requested
761 if [ "$QUIETREPORTS" != "yes" ] || [ "$ARETVAL" != "0" ] || [ "$(< "$ERRORLOG" wc -l)" -ne 0 ]; then
762 # do not send anything (not even error messages) if silence is requested
763 if [ "$SILENTREPORTS" != "yes" ]; then
764 < "$MAILFILE" fold --bytes --width="${MAILWIDTH:-990}" | send_mail "${MAILTO}" "${MAILSUBJ}"
765 fi
766 fi
767
768 # clean up tmpfiles
769 rm -rf "$TMPDIRIN"
770 fi
771
772 if [ -n "$CRONEXITHOOK" ] && [ -x "$CRONEXITHOOK" ]; then
773 $CRONEXITHOOK $CRONEXITHOOKPARM
774 fi
775
776 # clear lock
777 if [ -n "${LOCKED:-}" ] && command -v dotlockfile >/dev/null 2>&1; then
778 cd "${LOCKBASE}"
779 dotlockfile -u "$LOCKFILE" || true
780 fi
781 unset LOCKED
782
783 return ${SCRIPTRETVAL}
784 }
785
786 BEGINTIME="$(date +%s)"
787
788 if [ "$CRON_DAILY_RUN" != "yes" ] && ! tty -s; then
789 exit 0
790 fi
791
792 if command -v dotlockfile >/dev/null 2>&1; then
793 cd "${LOCKBASE}"
794 if ! dotlockfile -p -l "$LOCKFILE"; then
795 onexit nolock
796 exit 1
797 fi
798 else
799 PREERRORLOG="no dotlockfile binary in path, not checking for already running daily aide check\\n"
800 fi
801 LOCKED=yes
802
803 # prepare temp dir
804 # We use invariant file names here since our work files need to be
805 # excluded from aide.
806 if [ -e "$TMPDIRIN" ]; then
807 # $TMPDIRIN already exists (from an aborted run, maybe?). Move
808 # it away. This does not cause the script to abort, but aide should
809 # have rules to report the new directrory appearing in $TMPBASE.
810 if ! NEWNAME="$(mktemp -d $TMPBASE/dailyaidecheck.old.XXXXXXXXXX)"; then
811 onexit cantmovetmp
812 exit 1
813 fi
814 mv "$TMPDIRIN" "$NEWNAME"
815 unset NEWNAME
816 fi
817
818 if ! mkdir -p $TMPDIRIN; then
819 onexit cantcreatetmp
820 exit 1
821 fi
822
823 if [ "$(stat --format='%a %u' "${TMPDIRIN}")" != "750 ${AIDEUSERUID}" ]; then
824 onexit tmpnotours
825 exit 1
826 fi
827
828 if ! chmod 0700 "${TMPDIRIN}"; then
829 onexit cantchmodtmp
830 exit 1
831 fi
832
833 # handle the case that CRONEXITHOOK does not exist or is not executeable
834 if [ -n "$CRONEXITHOOK" ]; then
835 if ! [ -x "$CRONEXITHOOK" ]; then
836 onexit nohook
837 exit 1
838 fi
839 fi
840
841 # we can now directly use file names inside $TMPDIR: It is only
842 # writeable for us (umask 027), so we're safe against symlink attacks.
843 TMPDIR="$TMPDIRIN"
844
845 # now, with $TMPDIR having been created, we can use the full power of onexit.
846
847 # ERRORLOG: Error messages from script. Gets written to $LOGFILE first
848 ERRORLOG="$(mytempfile errorlog)"
849
850 if [ -n "${PREERRORLOG:-}" ]; then
851 printf >> "$ERRORLOG" "%s" "$PREERRORLOG"
852 fi
853 unset PREERRORLOG
854
855 # MAILFILE: Contents gets mailed. Built and handled from inside onexit()
856 MAILFILE="$(mytempfile mailfile)"
857
858 # aide return value
859 ARETVAL=-1
860
861 if [ ! -f "$DATABASE" ]; then
862 printf >> "$ERRORLOG" "Fatal error: The AIDE database '%s' does not exist!\\n" "$DATABASE"
863 printf >> "$ERRORLOG" "This may mean you haven't created it or that the initialization process is still running, or it may mean that someone has removed it.\\n"
864 onexit nodb
865 exit 1
866 fi
867
868 # code
869
870 # re-assign current time to be more accurate about aide's real start time
871 BEGINSTAMP="$(date +"%Y-%m-%d %H:%M:%S")"
872
873 # ARUNLOG: standard output of aide run
874 ARUNLOG="$(mytempfile arunlog)"
875
876 # AERRLOG: standard error of aide run
877 AERRLOG="$(mytempfile aerrlog)"
878
879 printf "begin timestamp %s\\n" "$BEGINSTAMP" >> "$ARUNLOG"
880
881 # shellcheck disable=SC2086
882 aide --config="${CONFIG}" ${AIDEARGS} "--${COMMAND}" --after="report_url=file:${ARUNLOG}" 2>|"${AERRLOG}" && ARETVAL="$?"
883 ARETVAL="$?"
884
885 # POSTRUNLOG: summary of aide execution and daily aide check log
886 POSTRUNLOG="$(mytempfile postrunlog)"
887
888 # DBCHECKLOG: Output of the database checksums
889 DBCHECKLOG="$(mytempfile dbchecklog)"
890
891 # NOISETMP: completely de-noised log
892 # NOISETMP2: pre-filtered ARUNLOG, containing only changed, removed and added lines
893 NOISETMP="$(mytempfile noisetmp)"
894 NOISETMP2="$(mytempfile noisetmp2)"
895
896 # find out whether we neeed to copy the new database over the old one
897
898 COPYDB="0"
899 if [ "$COPYNEWDB" = "ifnochange" ] && [ "$ARETVAL" = "0" ]; then
900 COPYDB="1"
901 printf >> "$POSTRUNLOG" "no significant changes detected.\\n"
902 fi
903
904 if [ "$COPYNEWDB" = "yes" ]; then
905 COPYDB=1
906 fi
907
908 if [ "$COPYDB" = "1" ] && [ "$COMMAND" = "update" ]; then
909 cp -f "$DATABASE_OUT" "$DATABASE"
910 printf >> "$POSTRUNLOG" "output database %s was copied to %s as requested by daily aide check configuration\\n" "$DATABASE_OUT" "$DATABASE"
911 fi
912
913 onexit success
914 exit 0
915
916 # vim: tabstop=4 shiftwidth=4 expandtab
917 # end of file