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