xref: /onnv-gate/usr/src/cmd/bnu/uudemon.cleanup (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!/usr/bin/sh
2*0Sstevel@tonic-gate#
3*0Sstevel@tonic-gate# CDDL HEADER START
4*0Sstevel@tonic-gate#
5*0Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*0Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only
7*0Sstevel@tonic-gate# (the "License").  You may not use this file except in compliance
8*0Sstevel@tonic-gate# with the License.
9*0Sstevel@tonic-gate#
10*0Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11*0Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
12*0Sstevel@tonic-gate# See the License for the specific language governing permissions
13*0Sstevel@tonic-gate# and limitations under the License.
14*0Sstevel@tonic-gate#
15*0Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
16*0Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17*0Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
18*0Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
19*0Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
20*0Sstevel@tonic-gate#
21*0Sstevel@tonic-gate# CDDL HEADER END
22*0Sstevel@tonic-gate#
23*0Sstevel@tonic-gate#
24*0Sstevel@tonic-gate# Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
25*0Sstevel@tonic-gate# Use is subject to license terms.
26*0Sstevel@tonic-gate#
27*0Sstevel@tonic-gate#ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate#
29*0Sstevel@tonic-gate#	This demon cleans up uucp directories.
30*0Sstevel@tonic-gate#	It is started by /var/spool/cron/crontabs/uucp;
31*0Sstevel@tonic-gate#	it can be run daily, weekly, whatever depending on the system
32*0Sstevel@tonic-gate#	  uucp load.
33*0Sstevel@tonic-gate#
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate# return a list of systems defined in /etc/uucp/Systems
36*0Sstevel@tonic-gategetsystems() {
37*0Sstevel@tonic-gateif [ ! -f /etc/uucp/Systems ]; then
38*0Sstevel@tonic-gate  return
39*0Sstevel@tonic-gateelse
40*0Sstevel@tonic-gate  awk '$1 !~ /^#/ {print $1}' /etc/uucp/Systems
41*0Sstevel@tonic-gatefi
42*0Sstevel@tonic-gate}
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate# return a list of systems defined in /etc/asppp.cf
45*0Sstevel@tonic-gategetpppsystems() {
46*0Sstevel@tonic-gateif [ ! -f /etc/asppp.cf ]; then
47*0Sstevel@tonic-gate  return
48*0Sstevel@tonic-gateelse
49*0Sstevel@tonic-gate  X=`sed -e 's/#.*$//' /etc/asppp.cf`
50*0Sstevel@tonic-gate  set -- $X
51*0Sstevel@tonic-gate  while [ $# -ne 0 ];
52*0Sstevel@tonic-gate  do
53*0Sstevel@tonic-gate      if [ "$1" = "peer_system_name" ]; then
54*0Sstevel@tonic-gate          PPPSYSTEMS="$PPPSYSTEMS $2"
55*0Sstevel@tonic-gate      fi
56*0Sstevel@tonic-gate      shift
57*0Sstevel@tonic-gate  done
58*0Sstevel@tonic-gate  echo "$PPPSYSTEMS"
59*0Sstevel@tonic-gatefi
60*0Sstevel@tonic-gate}
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gatenouucp() {
63*0Sstevel@tonic-gate# run through the systems list, deleting ppp systems
64*0Sstevel@tonic-gateoutstr=""
65*0Sstevel@tonic-gatefor i in `getsystems`
66*0Sstevel@tonic-gatedo
67*0Sstevel@tonic-gate    del=0
68*0Sstevel@tonic-gate    for j in `getpppsystems`
69*0Sstevel@tonic-gate    do
70*0Sstevel@tonic-gate        if [ "$j" = "$i" ]; then
71*0Sstevel@tonic-gate            del=1
72*0Sstevel@tonic-gate        fi
73*0Sstevel@tonic-gate    done
74*0Sstevel@tonic-gate    if [ $del -ne 1 ]; then
75*0Sstevel@tonic-gate        outstr="$outstr $i"
76*0Sstevel@tonic-gate    fi
77*0Sstevel@tonic-gatedone
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate# if any names are in $outstr, assume uucp is configured
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gateif [ -n "$outstr" ]; then
82*0Sstevel@tonic-gate	return 1
83*0Sstevel@tonic-gateelse
84*0Sstevel@tonic-gate	return 0
85*0Sstevel@tonic-gatefi
86*0Sstevel@tonic-gate}
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate# Start of actual processing. For energystar compatibility,
89*0Sstevel@tonic-gate# we attempt to do as little I/O as possible, so first check
90*0Sstevel@tonic-gate# to see if uucp is configured before doing all this work.
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gateif nouucp; then
93*0Sstevel@tonic-gate	exit 0
94*0Sstevel@tonic-gatefi
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gateMAILTO=uucp
97*0Sstevel@tonic-gateMAILDIR=/var/mail
98*0Sstevel@tonic-gateexport PATH
99*0Sstevel@tonic-gatePATH=/usr/bin:/usr/lib/uucp
100*0Sstevel@tonic-gateTMP=/tmp/uu$$
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gate#	Running as uucp, take care to protect things
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gateumask 0022
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate#
107*0Sstevel@tonic-gate#	These are taken from the Makefile.  If changed in Makefile
108*0Sstevel@tonic-gate#	they must be changed here also.
109*0Sstevel@tonic-gate#
110*0Sstevel@tonic-gatePUBDIR=/var/spool/uucppublic
111*0Sstevel@tonic-gateSPOOL=/var/spool/uucp
112*0Sstevel@tonic-gateVAR=/var/uucp
113*0Sstevel@tonic-gateLOCKS=/var/spool/locks	# needs a comment in parms.h on USRSPOOLOCKS
114*0Sstevel@tonic-gateXQTDIR=$VAR/.Xqtdir
115*0Sstevel@tonic-gateCORRUPT=$SPOOL/.Corrupt
116*0Sstevel@tonic-gateLOGDIR=$VAR/.Log
117*0Sstevel@tonic-gateSEQDIR=$VAR/.Sequence
118*0Sstevel@tonic-gateSTATDIR=$VAR/.Status
119*0Sstevel@tonic-gateWORKDIR=$SPOOL/.Workspace
120*0Sstevel@tonic-gateADMIN=$VAR/.Admin
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gate#	OLD is the directory for archiving old admin/log files
123*0Sstevel@tonic-gateOLD=$VAR/.Old
124*0Sstevel@tonic-gateO_LOGS=$OLD/Old-Log
125*0Sstevel@tonic-gateACCT_LOGS=$OLD/Old-acct
126*0Sstevel@tonic-gateSEC_LOGS=$OLD/Old-sec
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gate[ -f $ADMIN/xferstats ] && mv $ADMIN/xferstats $OLD/xferstats
129*0Sstevel@tonic-gate[ -f $ADMIN/audit ] && mv $ADMIN/audit $OLD/audit
130*0Sstevel@tonic-gate[ -f $ADMIN/command ] &&mv $ADMIN/command $OLD/command
131*0Sstevel@tonic-gate[ -f $ADMIN/errors ] && mv $ADMIN/errors $OLD/errors
132*0Sstevel@tonic-gate[ -f $ADMIN/Foreign ] && mv $ADMIN/Foreign $OLD/Foreign
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate> $ADMIN/xferstats
135*0Sstevel@tonic-gate> $ADMIN/audit
136*0Sstevel@tonic-gate> $ADMIN/command
137*0Sstevel@tonic-gate> $ADMIN/errors
138*0Sstevel@tonic-gate> $ADMIN/Foreign
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gate#
141*0Sstevel@tonic-gate#	If performance log exists, save it and create a new one
142*0Sstevel@tonic-gate#
143*0Sstevel@tonic-gateif [ -f $ADMIN/perflog ]
144*0Sstevel@tonic-gatethen
145*0Sstevel@tonic-gate	mv $ADMIN/perflog $OLD/perflog
146*0Sstevel@tonic-gate	> $ADMIN/perflog
147*0Sstevel@tonic-gatefi
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate#
150*0Sstevel@tonic-gate#	The list in the for controls how many old Log and security logs
151*0Sstevel@tonic-gate#	are retained: 2 -> 3, 1 -> 2, current -> 1.
152*0Sstevel@tonic-gate#
153*0Sstevel@tonic-gatefor i in  2 1
154*0Sstevel@tonic-gatedo
155*0Sstevel@tonic-gate	j=`expr $i + 1`
156*0Sstevel@tonic-gate	[ -f ${O_LOGS}-$i ] && mv ${O_LOGS}-$i ${O_LOGS}-$j
157*0Sstevel@tonic-gate	[ -f ${SEC_LOGS}-$i ] && mv ${SEC_LOGS}-$i ${SEC_LOGS}-$j
158*0Sstevel@tonic-gatedone
159*0Sstevel@tonic-gate
160*0Sstevel@tonic-gate[ -f $ADMIN/security ] && mv $ADMIN/security ${SEC_LOGS}-1
161*0Sstevel@tonic-gate> $ADMIN/security
162*0Sstevel@tonic-gate
163*0Sstevel@tonic-gate#
164*0Sstevel@tonic-gate#	Combine all log files into O_LOGS-1.
165*0Sstevel@tonic-gate#	Add a name separator between each system.
166*0Sstevel@tonic-gate#
167*0Sstevel@tonic-gate> ${O_LOGS}-1
168*0Sstevel@tonic-gatefor i in uucico uucp uux uuxqt
169*0Sstevel@tonic-gatedo
170*0Sstevel@tonic-gate	if [ ! -d $LOGDIR/$i ]
171*0Sstevel@tonic-gate	then
172*0Sstevel@tonic-gate	      (echo "uudemon.cleanup: $LOGDIR/$i directory does not exist, remove if file"
173*0Sstevel@tonic-gate	       echo "uudemon.cleanup: making a directory $LOGDIR/$i"
174*0Sstevel@tonic-gate	      ) | mail $MAILTO
175*0Sstevel@tonic-gate		rm -f $LOGDIR/$i
176*0Sstevel@tonic-gate		mkdir $LOGDIR/$i
177*0Sstevel@tonic-gate		continue
178*0Sstevel@tonic-gate	fi
179*0Sstevel@tonic-gate	cd $LOGDIR/$i
180*0Sstevel@tonic-gate
181*0Sstevel@tonic-gate#	can't compare exactly because of symlinks
182*0Sstevel@tonic-gate	case `pwd` in
183*0Sstevel@tonic-gate	*`basename $LOGDIR`/$i)
184*0Sstevel@tonic-gate		;;
185*0Sstevel@tonic-gate	*)
186*0Sstevel@tonic-gate	 	 (echo "uudemon.cleanup: unable to chdir to $LOGDIR/$i") | mail $MAILTO
187*0Sstevel@tonic-gate		continue
188*0Sstevel@tonic-gate		;;
189*0Sstevel@tonic-gate	esac
190*0Sstevel@tonic-gate	for j in *
191*0Sstevel@tonic-gate	do
192*0Sstevel@tonic-gate		if [ "$j" = "*" ]
193*0Sstevel@tonic-gate		then
194*0Sstevel@tonic-gate			break
195*0Sstevel@tonic-gate		fi
196*0Sstevel@tonic-gate		echo "********** $j ********** ($i)" >> ${O_LOGS}-1
197*0Sstevel@tonic-gate		cat $j >> ${O_LOGS}-1
198*0Sstevel@tonic-gate		rm -f $j
199*0Sstevel@tonic-gate	done
200*0Sstevel@tonic-gatedone
201*0Sstevel@tonic-gate
202*0Sstevel@tonic-gate#
203*0Sstevel@tonic-gate#	If the accounting log exists, save it and create a new one.
204*0Sstevel@tonic-gate#	The list in the for controls how many old accounting logs
205*0Sstevel@tonic-gate#	are retained: 2 -> 3, 1 -> 2, current -> 1.
206*0Sstevel@tonic-gate#
207*0Sstevel@tonic-gateif [ -f $ADMIN/account ]
208*0Sstevel@tonic-gatethen
209*0Sstevel@tonic-gate	for i in  2 1
210*0Sstevel@tonic-gate	do
211*0Sstevel@tonic-gate		j=`expr $i + 1`
212*0Sstevel@tonic-gate		[ -f ${ACCT_LOGS}-$i ] && mv ${ACCT_LOGS}-$i ${ACCT_LOGS}-$j
213*0Sstevel@tonic-gate	done
214*0Sstevel@tonic-gate	[ -f $ADMIN/account ] && mv $ADMIN/account ${ACCT_LOGS}-1
215*0Sstevel@tonic-gate	> $ADMIN/account
216*0Sstevel@tonic-gatefi
217*0Sstevel@tonic-gate
218*0Sstevel@tonic-gate#	Execute the system directory cleanup program
219*0Sstevel@tonic-gate#	See uucleanup.1m for details.
220*0Sstevel@tonic-gateuucleanup -D7 -C7 -X2 -o2 -W1
221*0Sstevel@tonic-gate
222*0Sstevel@tonic-gate#	Use the grep instead of the mv to ignore warnings to uucp
223*0Sstevel@tonic-gate# grep -v 'warning message sent to uucp' $ADMIN/uucleanup > $OLD/uucleanup
224*0Sstevel@tonic-gate[ -f $ADMIN/uucleanup ] && mv $ADMIN/uucleanup $OLD/uucleanup
225*0Sstevel@tonic-gateif [ -s $OLD/uucleanup ]
226*0Sstevel@tonic-gatethen
227*0Sstevel@tonic-gate	(echo "Subject: cleanup"; echo; cat $OLD/uucleanup) | mail $MAILTO
228*0Sstevel@tonic-gatefi
229*0Sstevel@tonic-gate>$ADMIN/uucleanup
230*0Sstevel@tonic-gate
231*0Sstevel@tonic-gate#  cleanup funny directories that may have been created in the spool areas
232*0Sstevel@tonic-gatefor d in $SPOOL/[0-9A-Za-z]*
233*0Sstevel@tonic-gatedo
234*0Sstevel@tonic-gate	if [ -f $d ]
235*0Sstevel@tonic-gate	then
236*0Sstevel@tonic-gate		# skip any regular files, like lockfiles
237*0Sstevel@tonic-gate		# and mail.log and so forth
238*0Sstevel@tonic-gate		continue
239*0Sstevel@tonic-gate	fi
240*0Sstevel@tonic-gate	if [ -z "`ls $d`" ]
241*0Sstevel@tonic-gate	then
242*0Sstevel@tonic-gate		# empty directory
243*0Sstevel@tonic-gate		continue
244*0Sstevel@tonic-gate	fi
245*0Sstevel@tonic-gate	cd $d
246*0Sstevel@tonic-gate	# we'd check that we were in the correct directory
247*0Sstevel@tonic-gate	if [ "`basename \`pwd\``" != "`basename $d`" ]
248*0Sstevel@tonic-gate	then
249*0Sstevel@tonic-gate		(echo "uudemon.cleanup: unable to chdir to $d") | mail $MAILTO
250*0Sstevel@tonic-gate		continue
251*0Sstevel@tonic-gate	fi
252*0Sstevel@tonic-gate	for s in */*
253*0Sstevel@tonic-gate	do
254*0Sstevel@tonic-gate		if [ "$s" = "*/*" ]
255*0Sstevel@tonic-gate		then
256*0Sstevel@tonic-gate			break
257*0Sstevel@tonic-gate		fi
258*0Sstevel@tonic-gate		if [ -d $s ]
259*0Sstevel@tonic-gate		then
260*0Sstevel@tonic-gate			# Remove subdirs of subdirs
261*0Sstevel@tonic-gate			rm -fr $s
262*0Sstevel@tonic-gate		fi
263*0Sstevel@tonic-gate	done
264*0Sstevel@tonic-gate
265*0Sstevel@tonic-gate	# if it is now empty, remove it.
266*0Sstevel@tonic-gate	cd ..
267*0Sstevel@tonic-gate	rmdir $d/* $d
268*0Sstevel@tonic-gatedone >/dev/null 2>&1
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate#
271*0Sstevel@tonic-gate#	Find old cores
272*0Sstevel@tonic-gate#
273*0Sstevel@tonic-gatefind $SPOOL -name core -print > $TMP
274*0Sstevel@tonic-gateif [ -s $TMP ]
275*0Sstevel@tonic-gatethen
276*0Sstevel@tonic-gate	(echo "Subject: cores"; echo; cat $TMP) | mail $MAILTO
277*0Sstevel@tonic-gatefi
278*0Sstevel@tonic-gate
279*0Sstevel@tonic-gate#
280*0Sstevel@tonic-gate#	Remove old files and directories
281*0Sstevel@tonic-gate#
282*0Sstevel@tonic-gate#find $PUBDIR -type f -mtime +30 -exec rm -f {} \;
283*0Sstevel@tonic-gatefind $PUBDIR/* -depth -type d -exec rmdir {} \; >/dev/null 2>&1
284*0Sstevel@tonic-gatefind $SPOOL/* -depth -type d -exec rmdir {} \; >/dev/null 2>&1
285*0Sstevel@tonic-gatefind $SEQDIR -type f -mtime +30 -exec rm -f {} \;
286*0Sstevel@tonic-gatefind $WORKDIR -type f -mtime +1 -exec rm -f {} \;
287*0Sstevel@tonic-gatefind $STATDIR -type f -mtime +2 -exec rm -f {} \;
288*0Sstevel@tonic-gatefind $CORRUPT -type f -mtime +10 -exec rm -f {} \;
289*0Sstevel@tonic-gate
290*0Sstevel@tonic-gaterm -f $LOCKS/LTMP*
291*0Sstevel@tonic-gatermdir $SPOOL/[0-9A-Za-z]* >/dev/null 2>&1
292*0Sstevel@tonic-gate
293*0Sstevel@tonic-gate#
294*0Sstevel@tonic-gate#	Mail a daily summary of status
295*0Sstevel@tonic-gate#
296*0Sstevel@tonic-gategrep passwd ${O_LOGS}-1 > $TMP
297*0Sstevel@tonic-gategrep "REQUEST.*/" ${O_LOGS}-1 >> $TMP
298*0Sstevel@tonic-gateif [ -s $TMP ]
299*0Sstevel@tonic-gatethen
300*0Sstevel@tonic-gate	(echo "Subject: uucp requests"; echo; cat $TMP) | mail $MAILTO
301*0Sstevel@tonic-gatefi
302*0Sstevel@tonic-gate
303*0Sstevel@tonic-gate
304*0Sstevel@tonic-gateawk '/(DENIED)/	{print prev}
305*0Sstevel@tonic-gate		{prev = $0}' ${O_LOGS}-1 > $TMP
306*0Sstevel@tonic-gateif [ -s $TMP ]
307*0Sstevel@tonic-gatethen
308*0Sstevel@tonic-gate	(echo "Subject: uucp DENIED"; echo; cat $TMP) | mail $MAILTO
309*0Sstevel@tonic-gatefi
310*0Sstevel@tonic-gate
311*0Sstevel@tonic-gateuustat -q > $TMP
312*0Sstevel@tonic-gateif [ -s $TMP ]
313*0Sstevel@tonic-gatethen
314*0Sstevel@tonic-gate	(echo "Subject: uu-status"; echo; cat $TMP) | mail $MAILTO
315*0Sstevel@tonic-gatefi
316*0Sstevel@tonic-gate
317*0Sstevel@tonic-gatels $CORRUPT > $TMP
318*0Sstevel@tonic-gateif [ -s $TMP ]
319*0Sstevel@tonic-gatethen
320*0Sstevel@tonic-gate	(echo "Subject: $CORRUPT"; echo; cat $TMP) | mail $MAILTO
321*0Sstevel@tonic-gatefi
322*0Sstevel@tonic-gate
323*0Sstevel@tonic-gateif [ -s $OLD/errors -o -s $OLD/Foreign ]
324*0Sstevel@tonic-gatethen
325*0Sstevel@tonic-gate	(echo "Subject: uucp Admin"; \
326*0Sstevel@tonic-gate	echo; echo tail errors; tail $OLD/errors; \
327*0Sstevel@tonic-gate	echo; echo tail Foreign; tail $OLD/Foreign; \
328*0Sstevel@tonic-gate	) | mail $MAILTO
329*0Sstevel@tonic-gatefi
330*0Sstevel@tonic-gate# don't run if no system directories exist
331*0Sstevel@tonic-gateif [ "`echo $SPOOL/*`" != "$SPOOL/*" ]
332*0Sstevel@tonic-gatethen
333*0Sstevel@tonic-gate	(echo "Subject: uucleanup ran; $SPOOL du"; echo; du $SPOOL) | \
334*0Sstevel@tonic-gate		mail $MAILTO
335*0Sstevel@tonic-gatefi
336*0Sstevel@tonic-gate
337*0Sstevel@tonic-gate#
338*0Sstevel@tonic-gate#	Dispose of mail to nuucp
339*0Sstevel@tonic-gate#
340*0Sstevel@tonic-gaterm -f $MAILDIR/nuucp $TMP
341