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