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