Add an openssl_options main configuration option, to allow administrators to
[exim.git] / src / src / exicyclog.src
CommitLineData
059ec3d9 1#! /bin/sh
0a49a7a4 2# $Cambridge: exim/src/src/exicyclog.src,v 1.11 2009/11/16 19:50:36 nm4 Exp $
059ec3d9 3
0a49a7a4 4# Copyright (c) University of Cambridge, 1995 - 2007
059ec3d9
PH
5# See the file NOTICE for conditions of use and distribution.
6
3d240ff7
PH
7# This script takes the following command line arguments:
8# -l dir Log file directory
9# -k days Number of days to keep the log files
10
059ec3d9
PH
11# Except when they appear in comments, the following placeholders in this
12# source are replaced when it is turned into a runnable script:
13#
14# CONFIGURE_FILE_USE_NODE
15# CONFIGURE_FILE_USE_EUID
16# CONFIGURE_FILE
17# BIN_DIRECTORY
18# EXICYCLOG_MAX
19# COMPRESS_COMMAND
20# COMPRESS_SUFFIX
21# CHOWN_COMMAND
22# CHGRP_COMMAND
dae9d94e
NM
23# CHMOD_COMMAND
24# TOUCH_COMMAND
059ec3d9
PH
25# MV_COMMAND
26# RM_COMMAND
27
28# PROCESSED_FLAG
29
30# This is a shell script for cycling exim main and reject log files. Each time
31# it is run, the files get "shuffled down" by one, the current one (e.g.
32# mainlog) becoming mainlog.01, the previous mainlog.01 becoming mainlog.02,
8e669ac1
PH
33# and so on, up to the limit configured here. When the number to keep is
34# greater than 99 (not common, but some people do it), three digits are used
35# (e.g. mainlog.001). The same shuffling happens to the reject logs. All
18ce445d 36# renamed files with numbers greater than 1 are compressed.
059ec3d9
PH
37
38# This script should be called regularly (e.g. daily) by a root crontab
39# entry of the form
40
41# 1 0 * * * /opt/exim/bin/exicyclog
42
43# The following lines are generated from Exim's configuration file when
44# this source is built into a script, but you can subsequently edit them
45# without rebuilding things, as long are you are careful not to overwrite
46# the script in the next Exim rebuild/install. "Keep" is the number of old log
3d240ff7
PH
47# files that are required to be kept. Its value can be overridden by the -k
48# command line option. "Compress" and "suffix" define your chosen compression
49# method. The others are provided because the location of certain commands
50# varies from OS to OS. Sigh.
059ec3d9
PH
51
52keep=EXICYCLOG_MAX
53compress=COMPRESS_COMMAND
54suffix=COMPRESS_SUFFIX
55
059ec3d9 56chgrp=CHGRP_COMMAND
dae9d94e 57chmod=CHMOD_COMMAND
5c3a378f 58chown=CHOWN_COMMAND
059ec3d9
PH
59mv=MV_COMMAND
60rm=RM_COMMAND
5c3a378f 61touch=TOUCH_COMMAND
059ec3d9
PH
62
63# End of editable lines
64#########################################################################
65
3d240ff7
PH
66# Sort out command line options.
67
68while [ $# -gt 0 ] ; do
69 case "$1" in
70 -l) log_file_path=$2
71 shift
72 ;;
73 -k) keep=$2
74 shift
75 ;;
76 *) echo "** exicyclog: unknown option $1"
77 exit 1
78 ;;
79 esac
80 shift
81done
82
059ec3d9
PH
83# Some operating systems have different versions in which the commands live
84# in different places. We have a fudge that will search the usual suspects if
85# requested.
86
5c3a378f 87for cmd in chgrp chmod chown mv rm touch; do
059ec3d9
PH
88 eval "oldcmd=\$$cmd"
89 if [ "$oldcmd" != "look_for_it" ] ; then continue ; fi
90 newcmd=$cmd
91 for dir in /bin /usr/bin /usr/sbin /usr/etc ; do
92 if [ -f $dir/$cmd ] ; then
93 newcmd=$dir/$cmd
94 break
95 fi
96 done
97 eval $cmd=$newcmd
98done
99
100# See if this installation is using the esoteric "USE_EUID" feature of Exim,
101# in which it uses the effective user id as a suffix for the configuration file
102# name. In order for this to work, exicyclog must be run under the appropriate
103# euid.
104
105if [ "CONFIGURE_FILE_USE_EUID" = "yes" ]; then
106 euid=.`id -u`
107fi
108
109# See if this installation is using the esoteric "USE_NODE" feature of Exim,
110# in which it uses the host's name as a suffix for the configuration file name.
111
112if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then
113 hostsuffix=.`uname -n`
114fi
115
116# Now find the configuration file name. This has got complicated because the
117# CONFIGURE_FILE value may now be a list of files. The one that is used is the
118# first one that exists. Mimic the code in readconf.c by testing first for the
119# suffixed file in each case.
120
121set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' <<End
122CONFIGURE_FILE
123End
124`
125while [ "$config" = "" -a $# -gt 0 ] ; do
126 if [ -f "$1$euid$hostsuffix" ] ; then
127 config="$1$euid$hostsuffix"
128 elif [ -f "$1$euid" ] ; then
129 config="$1$euid"
130 elif [ -f "$1$hostsuffix" ] ; then
131 config="$1$hostsuffix"
132 elif [ -f "$1" ] ; then
133 config="$1"
134 fi
135 shift
136done
137
138# Determine if the log file path is set, and where the spool directory is.
139# Search for an exim_path setting in the configure file; otherwise use the bin
140# directory. Call that version of Exim to find the spool directory and log file
3d240ff7
PH
141# path, unless log_file_path was set above by a command line option. BEWARE: a
142# tab character is needed in the command below. It has had a nasty tendency to
143# get lost in the past. Use a variable to hold a space and a tab to keep the
144# tab in one place.
059ec3d9
PH
145
146st=' '
147exim_path=`grep "^[$st]*exim_path" $config | sed "s/.*=[$st]*//"`
148if test "$exim_path" = ""; then exim_path=BIN_DIRECTORY/exim; fi
149
150spool_directory=`$exim_path -C $config -bP spool_directory | sed 's/.*=[ ]*//'`
3d240ff7
PH
151
152if [ "$log_file_path" = "" ] ; then
153 log_file_path=`$exim_path -C $config -bP log_file_path | sed 's/.*=[ ]*//'`
154fi
059ec3d9
PH
155
156# If log_file_path contains only "syslog" then no Exim log files are in use.
157# We can't cycle anything. Complain and give up.
158
159if [ "$log_file_path" = "syslog" ] ; then
160 echo "*** Exim is logging to syslog - no log files to cycle ***"
161 exit 1
162fi
163
164# Otherwise, remove ":syslog" or "syslog:" (some spaces allowed) and inspect
165# what remains. The simplistic regex originally used failed when a filename
166# contained "syslog", so we have to use three less general ones, because sed
167# doesn't have much power in its regexs.
168
169log_file_path=`echo "$log_file_path" | \
170 sed 's/^ *:\{0,1\} *syslog *:\{0,1\} *//;s/: *syslog *:/:/;s/: *syslog *$//'`
171
4aac9b49
PH
172# If log_file_path is empty, try and get the compiled in default by using
173# /dev/null as the configuration file.
174
175if [ "$log_file_path" = "" ]; then
176 log_file_path=`$exim_path -C /dev/null -bP log_file_path | sed 's/.*=[ ]*//'`
177 log_file_path=`echo "$log_file_path" | \
178 sed 's/^ *:\{0,1\} *syslog *:\{0,1\} *//;s/: *syslog *:/:/;s/: *syslog *$//'`
179fi
180
181# If log_file_path is still empty, the logs we are interested in are probably
182# called "mainlog" and "rejectlog" in the directory called "log" in the spool
183# directory. Otherwise we fish out the directory from the given path, and also
184# the names of the logs.
059ec3d9
PH
185
186if [ "$log_file_path" = "" ]; then
187 logdir=$spool_directory/log
188 mainlog=mainlog
189 rejectlog=rejectlog
dae9d94e 190 paniclog=paniclog
059ec3d9
PH
191else
192 logdir=`echo $log_file_path | sed 's?/[^/]*$??'`
193 logbase=`echo $log_file_path | sed 's?^.*/??'`
194 mainlog=`echo $logbase | sed 's/%s/main/'`
195 rejectlog=`echo $logbase | sed 's/%s/reject/'`
dae9d94e 196 paniclog=`echo $logbase | sed 's/%s/panic/'`
059ec3d9
PH
197fi
198
199# Get into the log directory to do the business.
200
3d240ff7 201cd $logdir || exit 1
059ec3d9
PH
202
203# If there is no main log file, do nothing.
204
205if [ ! -f $mainlog ]; then exit; fi
206
207# Find out the owner and group of the main log file so that we can re-instate
208# this on moved and compressed files, since some operating systems may change
209# things. This is a tedious bit of code, but it should work both in operating
210# systems where the -l option of ls gives the user and group, and those in which
211# you need -lg. The condition is that, if the fifth field of the output from
212# ls consists entirely of digits, then the third and fourth fields are the user
213# and group.
214
215a=`ls -lg $mainlog`
216b=`ls -l $mainlog`
217
218# These statements work fine in the Bourne or Korn shells, but not in Bash.
219# So for the benefit of systems whose /bin/sh is really Bash, they have been
220# changed to a messier form.
221
222# user=`echo "$a\n$b\n" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) print $3; }'`
223# group=`echo "$a\n$b\n" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) print $4; }'`
224
225user=`echo "$a
226$b
227" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $3; exit; } }'`
228
229group=`echo "$a
230$b
231" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $4; exit; } }'`
232
233# Now do the job. First remove the files that have "fallen off the bottom".
234# Look for both the compressed and uncompressed forms.
235
236if [ $keep -lt 10 ]; then keept=0$keep; else keept=$keep; fi;
237
238if [ -f $mainlog.$keept ]; then $rm $mainlog.$keept; fi;
239if [ -f $mainlog.$keept.$suffix ]; then $rm $mainlog.$keept.$suffix; fi;
240
241if [ -f $rejectlog.$keept ]; then $rm $rejectlog.$keept; fi;
242if [ -f $rejectlog.$keept.$suffix ]; then $rm $rejectlog.$keept.$suffix; fi;
243
df40aca4
NM
244if [ -f $paniclog.$keept ]; then $rm $paniclog.$keept; fi;
245if [ -f $paniclog.$keept.$suffix ]; then $rm $paniclog.$keept.$suffix; fi;
dae9d94e 246
059ec3d9
PH
247# Now rename all the previous old files by increasing their numbers by 1.
248# When the number is less than 10, insert a leading zero.
249
250count=$keep
18ce445d 251if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi
059ec3d9
PH
252
253while [ $count -gt 1 ]; do
17792b53 254 old=`expr -- $count - 1`
18ce445d 255 if [ $keep -gt 99 ]; then
8e669ac1 256 if [ $old -lt 10 ]; then oldt=00$old
18ce445d
PH
257 elif [ $old -lt 100 ]; then oldt=0$old
258 else oldt=$old
8e669ac1
PH
259 fi
260 else
18ce445d 261 if [ $old -lt 10 ]; then oldt=0$old; else oldt=$old; fi;
8e669ac1 262 fi
059ec3d9
PH
263 if [ -f $mainlog.$oldt ]; then
264 $mv $mainlog.$oldt $mainlog.$countt
265 elif [ -f $mainlog.$oldt.$suffix ]; then
266 $mv $mainlog.$oldt.$suffix $mainlog.$countt.$suffix
267 fi
268 if [ -f $rejectlog.$oldt ]; then
269 $mv $rejectlog.$oldt $rejectlog.$countt
270 elif [ -f $rejectlog.$oldt.$suffix ]; then
271 $mv $rejectlog.$oldt.$suffix $rejectlog.$countt.$suffix
272 fi
dae9d94e
NM
273 if [ -f $paniclog.$oldt ]; then
274 $mv $paniclog.$oldt $paniclog.$countt
275 elif [ -f $paniclog.$oldt.$suffix ]; then
276 $mv $paniclog.$oldt.$suffix $paniclog.$countt.$suffix
277 fi
059ec3d9
PH
278 count=$old
279 countt=$oldt
280done
281
18ce445d
PH
282# Now rename the current files as 01 or 001 if keeping more than 99
283
284if [ $keep -gt 99 ]; then first=001; else first=01; fi
059ec3d9
PH
285
286if [ -f $mainlog ]; then
18ce445d
PH
287 $mv $mainlog $mainlog.$first
288 $chown $user:$group $mainlog.$first
dae9d94e
NM
289 $touch $mainlog
290 $chown $user:$group $mainlog
291 $chmod 640 $mainlog
059ec3d9
PH
292fi
293
294if [ -f $rejectlog ]; then
18ce445d
PH
295 $mv $rejectlog $rejectlog.$first
296 $chown $user:$group $rejectlog.$first
dae9d94e
NM
297 $touch $rejectlog
298 $chown $user:$group $rejectlog
299 $chmod 640 $rejectlog
300fi
301
302if [ -f $paniclog ]; then
303 $mv $paniclog $paniclog.$first
df40aca4 304 $chown $user:$group $paniclog.$first
dae9d94e
NM
305 $touch $paniclog
306 $chown $user:$group $paniclog
307 $chmod 640 $paniclog
059ec3d9
PH
308fi
309
18ce445d 310# Now scan the (0)02 and later files, compressing where necessary, and
059ec3d9
PH
311# ensuring that their owners and groups are correct.
312
313count=2;
314
315while [ $count -le $keep ]; do
18ce445d
PH
316 if [ $keep -gt 99 ]; then
317 if [ $count -lt 10 ]; then countt=00$count
318 elif [ $count -lt 100 ]; then countt=0$count
319 else countt=$count
8e669ac1
PH
320 fi
321 else
18ce445d 322 if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi
8e669ac1 323 fi
059ec3d9
PH
324 if [ -f $mainlog.$countt ]; then $compress $mainlog.$countt; fi
325 if [ -f $mainlog.$countt.$suffix ]; then
326 $chown $user:$group $mainlog.$countt.$suffix
327 fi
328 if [ -f $rejectlog.$countt ]; then $compress $rejectlog.$countt; fi
329 if [ -f $rejectlog.$countt.$suffix ]; then
330 $chown $user:$group $rejectlog.$countt.$suffix
331 fi
dae9d94e
NM
332 if [ -f $paniclog.$countt ]; then $compress $paniclog.$countt; fi
333 if [ -f $paniclog.$countt.$suffix ]; then
334 $chown $user:$group $paniclog.$countt.$suffix
335 fi
336
17792b53 337 count=`expr -- $count + 1`
059ec3d9
PH
338done
339
340# End of exicyclog