~ubuntu-branches/ubuntu/trusty/aide/trusty

« back to all changes in this revision

Viewing changes to debian/cron.daily/aide

  • Committer: Bazaar Package Importer
  • Author(s): Marc Haber
  • Date: 2007-03-06 12:03:24 UTC
  • Revision ID: james.westby@ubuntu.com-20070306120324-02usb8ekj2dg07ui
Tags: 0.13.1-5
* 31_aide_apt:
  * add rule generation for deb-src lines
  * fix ARCHIVESDIR/lock for sarge
* 31_aide_aide: Apply patch by Tim Stoop. Closes: #412320
* cron.daily:
  * handle broken hostname
  * fix language issue regarding return code
  * remove last references to former CHANGES variable.
    Thanks to P.M. van Aalten. Closes: #411823
* zg2.d/31_local_apt_settings: set IGNORE_FRQCHG yes by default
* 31_aide_dovecot: add /var/lib/dovecot
* 31_aide_exim4:
  * zap obsolete rule for dh-params
  * add /root/.rnd (side effect of openssl gendh)

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
set -e
4
4
set -C
5
5
 
 
6
# trap handler
 
7
 
 
8
FQDN="$(hostname -f)"
 
9
if [ -z "$FQDN" ]; then
 
10
  echo >&2 "error determining FQDN: hostname -f does not give output"
 
11
  hostname -f >&2
 
12
  exit 1
 
13
fi
 
14
 
 
15
traphandler() {
 
16
        trap - INT ERR
 
17
        if [ -n "${LOCKED:-}" ]; then
 
18
          # we have the lock, 
 
19
          pidof aide | xargs --no-run-if-empty kill -9
 
20
        fi
 
21
        onexit signal $1
 
22
        return 0
 
23
}
 
24
trap ' traphandler INT; trap - INT ERR' INT
 
25
trap ' traphandler ERR; trap - INT ERR' ERR
 
26
 
6
27
# bail if no aide binary found
7
28
 
8
29
[ -f "/usr/bin/aide" ] || exit 0
11
32
 
12
33
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
13
34
LOGDIR="/var/log/aide"
 
35
# LOGFILE: /var/log/aide/aide.log - all logs untruncated (not temp)
14
36
LOGFILE="$LOGDIR/aide.log"
15
37
CONFFILE="/var/lib/aide/aide.conf.autogenerated"
16
38
PREFIX="aide"
17
39
TMPBASE="/var/run/aide"
18
40
LOCKFILE="$TMPBASE/cron.daily.lock"
19
 
TMPDIR="$TMPBASE/cron.daily"
 
41
TMPDIRIN="$TMPBASE/cron.daily"
20
42
 
21
43
AIDEARGS="-V4"
22
 
FQDN="$(hostname -f)"
23
44
MAILSUBJ="Daily AIDE report for $FQDN"
24
45
 
25
46
DATE="$(date +"%Y-%m-%d %H:%M")"
 
47
BEGINSTAMP="$(date +"%Y-%m-%d %H:%M:%S")"
26
48
 
27
49
# have /etc/default/aide override variables
28
50
 
54
76
QUIETREPORTS="${QUIETREPORTS:-no}"
55
77
ONEXIT=""
56
78
 
57
 
traphandler() {
58
 
        trap - INT ERR
59
 
        printf >> "$ERRORLOG" "SIG %s received!\n\n" "$1"
60
 
        pidof aide | xargs --no-run-if-empty kill -9
61
 
        onexit signal $1
62
 
        return 0
63
 
}
64
 
trap ' traphandler INT; trap - INT ERR' INT
65
 
trap ' traphandler ERR; trap - INT ERR' ERR
66
 
 
67
 
frame() {
68
 
WIDTH=78
69
 
STARS="*******************************************************************************"
70
 
SPACES="                                                                               "
71
 
printf "%s\n" "${STARS:1:$WIDTH}"
72
 
while read line ; do
73
 
  HALF="${SPACES:1:$((($WIDTH-${#line})/2))}"
74
 
  LINE="$HALF$line$SPACES"
75
 
  printf "*%s*\n" "${LINE:1:$(($WIDTH-2))}"
76
 
done
77
 
printf "%s\n" "${STARS:1:$WIDTH}"
78
 
}
79
 
 
80
 
# prepare temp dir
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"
84
 
                exit 1
85
 
        fi
86
 
        mv "$TMPDIR" "$NEWNAME"
87
 
        OLDTMPDIRFOUND="yes"
88
 
fi
89
 
 
90
 
if ! mkdir -p $TMPDIR; then
91
 
        printf >&2 "Fatal error: Cannot create temporary directory %s.\n" "$TMPDIR"
92
 
        exit 1
93
 
fi
94
 
 
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
98
 
# excluded from aide.
 
79
# functions
 
80
 
99
81
mytempfile() {
100
82
  NAME="$1"
101
83
  echo "$TMPDIR/$NAME"
102
84
  touch "$TMPDIR/$NAME"
103
85
}
104
86
 
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)"
108
 
  
109
 
# MAILFILE: Contents gets mailed. Built and handled from inside onexit()
110
 
MAILFILE="$(mytempfile mailfile)"
111
 
 
112
 
ARETVAL=0
113
 
 
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"
117
 
        onexit fatal
118
 
        exit 0
119
 
fi
120
 
 
121
 
BEGINTIME="$(date +%s)"
122
 
 
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"
126
 
                onexit fatal
127
 
                exit 1
128
 
        fi
129
 
else
130
 
  printf >> "$ERRORLOG" "no dotlockfile binary in path, not checking for already running aide cron job\n"
131
 
fi
132
 
 
133
 
# functions
 
87
frame() {
 
88
  WIDTH=78
 
89
  STARS="*******************************************************************************"
 
90
  SPACES="                                                                               "
 
91
  printf "%s\n" "${STARS:1:$WIDTH}"
 
92
  while read line ; do
 
93
    HALF="${SPACES:1:$((($WIDTH-${#line})/2))}"
 
94
    LINE="$HALF$line$SPACES"
 
95
    printf "*%s*\n" "${LINE:1:$(($WIDTH-2))}"
 
96
  done
 
97
  printf "%s\n" "${STARS:1:$WIDTH}"
 
98
}
134
99
 
135
100
onexit() {
136
 
if [ "$ONEXIT" = "running" ]; then
137
 
  return 1
138
 
fi
139
 
 
140
 
ONEXIT="running"
141
 
 
142
 
[ -f "$LOGFILE" ] && savelog -t -g adm -m 640 -u root -c 7 "$LOGFILE" > /dev/null
143
 
 
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"
147
 
 
148
 
printf >> "$LOGFILE" \
149
 
"aide run on %s started at %s.\n" "$FQDN" "$BEGINSTAMP"
150
 
 
151
 
case "$1" in
 
101
  if [ "$ONEXIT" = "running" ]; then
 
102
    return 1
 
103
  fi
 
104
 
 
105
  ONEXIT="running"
 
106
 
 
107
  local LOGHEAD
 
108
  local MAILHEAD
 
109
 
 
110
  case "$1" in
152
111
        signal)
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")"
155
114
                ;;
156
115
        fatal)
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.")"
 
118
                ;;
 
119
        nolock)
 
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")"
 
122
                ;;
 
123
        cantmovetmp)
 
124
                LOGHEAD="$(printf "terminated: Cannot move away %s." "$TMPDIRIN")"
 
125
                MAILHEAD="$(printf "The cron job was terminated: Cannot move away %s." "$TMPDIRIN")"
 
126
                ;;
 
127
        cantcreatetmp)
 
128
                LOGHEAD="$(printf "terminated: Cannot create temporary directory %s." "$TMPDIRIN")"
 
129
                MAILHEAD="$(printf "The cron job was terminated: Cannot create temporary directory %s." "$TMPDIRIN")"
159
130
                ;;
160
131
        success)
161
132
                ;;
162
133
        *)
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")"
165
136
                ;;
166
 
esac
167
 
 
168
 
# script errors
169
 
 
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"
174
 
 
175
 
  printf "script errors\n" | frame >> "$LOGFILE"
176
 
  < "$ERRORLOG" cat >> "$LOGFILE"
177
 
  printf "End of script errors\n" >> "$LOGFILE"
178
 
 
179
 
fi
180
 
 
181
 
# aide post run information
182
 
 
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"
187
 
 
188
 
  printf "AIDE post run information\n" >> "$LOGFILE"
189
 
  < "$POSTRUNLOG" cat >> "$LOGFILE"
190
 
  printf "End of AIDE post run information\n" >> "$LOGFILE"
191
 
 
192
 
fi
193
 
 
194
 
# include error log in daily report e-mail
195
 
 
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"
199
 
fi
200
 
 
201
 
if [ -s "$AERRLOG" ]; then
 
137
  esac
 
138
  
 
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"
 
145
  else
 
146
    # we are being called after the cron job was properly set up.
 
147
    # To the full works.
 
148
 
 
149
    [ -f "$LOGFILE" ] && savelog -t -g adm -m 640 -u root -c 7 "$LOGFILE" > /dev/null
 
150
 
 
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"
 
154
 
 
155
    printf >> "$LOGFILE" \
 
156
"aide run on %s started at %s.\n" "$FQDN" "$BEGINSTAMP"
 
157
 
 
158
    if [ -n "$LOGHEAD" ]; then
 
159
      printf "$LOGHEAD\n" | frame >> "$LOGFILE"
 
160
      printf "\n" >> "$LOGFILE"
 
161
    fi
 
162
    if [ -n "$MAILHEAD" ]; then
 
163
      printf "$MAILHEAD\n" | frame >> "$MAILFILE"
 
164
      printf "\n\n" >> "$MAILFILE"
 
165
    fi
 
166
 
 
167
    # report about AIDE's return value
 
168
 
 
169
    if [ -n "${ARETVAL:-}" ]; then
 
170
      ARETEXPL=""
 
171
      ARETERR=""
 
172
      PREFIX="$(printf "AIDE returned with exit code %d. " "$ARETVAL")"
 
173
      case "$ARETVAL" in
 
174
        -1)
 
175
          PREFIX=""
 
176
          ARETERR="the cron job was interrupted before AIDE could return an exit code.";;
 
177
        0)
 
178
          PREFIX="AIDE returned with a zero exit code. "
 
179
          ARETEXPL="No changes detected!";;
 
180
        1)
 
181
          ARETEXPL="New files detected!";;
 
182
        2)
 
183
          ARETEXPL="Removed files detected!";;
 
184
        3)
 
185
          ARETEXPL="New and removed files detected!";;
 
186
        4)
 
187
          ARETEXPL="Changed files detected!";;
 
188
        5)
 
189
          ARETEXPL="New and changed files detected!";;
 
190
        6)
 
191
          ARETEXPL="New and deleted files detected!";;
 
192
        7)
 
193
          ARETEXPL="New, deleted and changed files detected!";;
 
194
        14)
 
195
          ARETERR="Error writing!";;
 
196
        15)
 
197
          ARETERR="Invalid Argument!";;
 
198
        16)
 
199
          ARETERR="Unimplemented function!";;
 
200
        17)
 
201
          ARETERR="Invalid configuration!";;
 
202
        18)
 
203
          ARETERR="Input/Output error!";;
 
204
        default)
 
205
          ARETERR="$(printf "AIDE returned an unknown non-zero exit value\nexit value is %d\n\n" "$ARETVAL")";;
 
206
      esac
 
207
      if [ -n "$ARETEXPL" ]; then
 
208
        echo "$PREFIX $ARETEXPL" >> "$MAILFILE"
 
209
        echo "$PREFIX $ARETEXPL" >> "$LOGFILE"
 
210
      fi
 
211
      if [ -n "$ARETERR" ]; then
 
212
        echo "$PREFIX $ARETERR" | frame >> "$MAILFILE"
 
213
        echo "$PREFIX $ARETERR" | frame >> "$LOGFILE"
 
214
      fi
 
215
      unset ARETEXPL
 
216
      unset ARETERR
 
217
      unset PREFIX
 
218
    else
 
219
      ARETEXPL="ARETVAL not initialized. cron job was aborted prematurely."
 
220
      ARETVAL=255
 
221
      echo $ARETEXPL | frame >> "$LOGFILE"
 
222
      echo $ARETEXPL | frame >> "$MAILFILE"
 
223
      unset ARETEXPL
 
224
      printf "\n" >> "$LOGFILE"
 
225
      printf "\n\n" >> "$MAILFILE"
 
226
    fi
 
227
 
 
228
    # script errors
 
229
 
 
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"
 
234
 
 
235
      printf "script errors\n" | frame >> "$LOGFILE"
 
236
      < "$ERRORLOG" cat >> "$LOGFILE"
 
237
      printf "End of script errors\n" >> "$LOGFILE"
 
238
    fi
 
239
 
 
240
    # aide post run information
 
241
 
 
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"
 
246
 
 
247
      printf "AIDE post run information\n" >> "$LOGFILE"
 
248
      < "$POSTRUNLOG" cat >> "$LOGFILE"
 
249
      printf "End of AIDE post run information\n" >> "$LOGFILE"
 
250
    fi
 
251
 
 
252
    # include error log in daily report e-mail
 
253
    
 
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" | \
214
267
        printf >> "$LOGFILE" "AIDE error output (%d lines):\n" "$errorlines"
215
268
        < "$AERRLOG" cat >> "$LOGFILE"
216
269
        printf >> "$LOGFILE" "End of AIDE error output\n"
217
 
else
 
270
    else
218
271
        printf >> "$MAILFILE" "AIDE produced no errors.\n\n"
219
272
        printf >> "$LOGFILE" "AIDE produced no errors.\n"
220
 
fi
221
 
 
222
 
# include de-noised log into mail
223
 
 
224
 
if [ -n "$NOISE" ]; then
 
273
    fi
 
274
 
 
275
    # include de-noised log into mail
 
276
 
 
277
    if [ -n "${ARUNLOG:-}" ] && [ -s "$ARUNLOG" ]; then
 
278
 
 
279
      if [ -n "${NOISE:-}" ]; then
225
280
        NOISETMP="$(tempfile --directory "/tmp" --prefix "aidenoise")"
226
281
        NOISETMP2="$(tempfile --directory "/tmp" --prefix "aidenoise")"
227
282
        < "$ARUNLOG" sed -n '1,/^Detailed information about changes:/p' | \
251
306
                printf >> "$MAILFILE" "AIDE detected no changes after removing noise.\n\n"
252
307
        fi
253
308
        printf >> "$MAILFILE" "============================================================================\n"
254
 
fi
255
 
 
256
 
# include non-de-noised log into mail
257
 
 
258
 
if [ -s "$ARUNLOG" ]; then
 
309
      fi
 
310
 
 
311
      # include non-de-noised log into mail
 
312
 
 
313
      if [ -n "${ARUNLOG:-}" ] && [ -s "$ARUNLOG" ]; then
259
314
        loglines="$(wc -l "$ARUNLOG" | awk '{ print $1 }')"
260
315
        if [ "${loglines:=0}" -gt "$LINES" ]; then
261
316
                printf "AIDE has returned long output which 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"
275
 
else
 
330
      else
276
331
        printf >> "$MAILFILE" "AIDE detected no changes.\n\n"
277
332
        printf >> "$LOGFILE" "AIDE detected no changes.\n\n"
278
 
fi
 
333
      fi
 
334
    else
 
335
        printf >> "$MAILFILE" "funny, AIDE did not leave a log.\n\n"
 
336
        printf >> "$LOGFILE" "funny, AIDE did not leave a log.\n"
 
337
    fi
279
338
 
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"
284
 
fi
285
 
 
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 ))"
288
 
 
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"
292
 
fi
293
 
 
294
 
# clean up temp files
295
 
rm -rf $TMPDIR
296
 
 
297
 
# clear lock
 
343
    fi
 
344
 
 
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 ))"
 
347
 
 
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"
 
351
    fi
 
352
 
 
353
    # clean up temp files
 
354
    rm -rf $TMPDIR
 
355
  fi
 
356
 
 
357
  # clear lock
 
358
  if [ -n "${LOCKED:-}" ] && command -v dotlockfile >/dev/null 2>&1; then
 
359
    dotlockfile -u "$LOCKFILE" || true
 
360
  fi
 
361
  unset LOCKED
 
362
 
 
363
  return 0
 
364
}
 
365
 
 
366
BEGINTIME="$(date +%s)"
 
367
 
298
368
if command -v dotlockfile >/dev/null 2>&1; then
299
 
  dotlockfile -u "$LOCKFILE" || true
300
 
fi
301
 
 
302
 
return 0
303
 
}
 
369
        if ! dotlockfile -p -l "$LOCKFILE"; then
 
370
                onexit nolock
 
371
                exit 1
 
372
        fi
 
373
else
 
374
  PREERRLOG="no dotlockfile binary in path, not checking for already running aide cron job\n"
 
375
fi
 
376
LOCKED=yes
 
377
 
 
378
# prepare temp dir
 
379
if [ -e "$TMPDIRIN" ]; then
 
380
        if ! NEWNAME="$(mktemp -d $TMPBASE/cron.daily.old.XXXXXXXXXX)"; then
 
381
                onexit cantmovetmp
 
382
                exit 1
 
383
        fi
 
384
        mv "$TMPDIRIN" "$NEWNAME"
 
385
        unset NEWNAME
 
386
        OLDTMPDIRFOUND="yes"
 
387
fi
 
388
 
 
389
if ! mkdir -p $TMPDIRIN; then
 
390
        onexit cantcreatetmp
 
391
        exit 1
 
392
fi
 
393
 
 
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.
 
398
TMPDIR="$TMPDIRIN"
 
399
 
 
400
# now, with $TMPDIR having been created, we can use onexit.
 
401
 
 
402
# ERRORLOG: Error messages from script. Gets written to $LOGFILE first
 
403
ERRORLOG="$(mytempfile errorlog)"
 
404
 
 
405
if [ -n "${PREERRORLOG:-}" ]; then
 
406
  printf >> "$ERRORLOG" "$PREERRORLOG"
 
407
fi
 
408
unset PREERRORLOG
 
409
 
 
410
# MAILFILE: Contents gets mailed. Built and handled from inside onexit()
 
411
MAILFILE="$(mytempfile mailfile)"
 
412
 
 
413
# aide return value
 
414
ARETVAL=-1
 
415
 
 
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"
 
419
        onexit fatal
 
420
        exit 0
 
421
fi
304
422
 
305
423
# code
306
424
 
 
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")"
308
427
 
309
428
# ARUNLOG: standard output of aide run
315
434
printf "begin timestamp %s\n" "$BEGINSTAMP" >> "$ARUNLOG"
316
435
 
317
436
update-aide.conf
318
 
aide.wrapper $AIDEARGS "--$COMMAND" >|"$ARUNLOG" 2>|"$AERRLOG"
 
437
aide.wrapper $AIDEARGS "--$COMMAND" >|"$ARUNLOG" 2>|"$AERRLOG" && ARETVAL="$?"
319
438
ARETVAL="$?"
320
439
 
321
440
# POSTRUNLOG: summary of aide execution and cron job log
370
489
# find out whether we neeed to copy the new database over the old one
371
490
 
372
491
COPYDB="0"
373
 
CHANGES="1"
374
 
if < "$ARUNLOG" grep '^###' | head -n 1 | grep -q '### All files match AIDE database. Looks okay!' && \
375
 
   [ "$(< $ARUNLOG wc -l)" -lt 20 ]; then
376
 
        CHANGES="0"
377
 
fi
378
 
 
379
 
if [ "$COPYNEWDB" = "ifnochange" ] && [ "$CHANGES" = "0" ]; then
 
492
if [ "$COPYNEWDB" = "ifnochange" ] && [ "$ARETVAL" = "0" ]; then
380
493
        COPYDB="1"
381
494
fi
382
495