xref: /illumos-gate/usr/src/cmd/gss/gsscred_clean/gsscred_clean.ksh (revision 4283d10e18fc3904736c7c067fb29de9bb67d25d)
1#!/bin/ksh
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 (c) 1998 by Sun Microsystems, Inc.
25# All rights reserved.
26#
27# gsscred_db clean up script
28#
29# This file is used to remove duplicate entries from
30# the gsscred_db file. It is activated as a root cron
31# job once a day. It only performs cleanup when
32# the gsscred_db file has changed since last operation.
33
34FILE_TO_CLEAN=/etc/gss/gsscred_db
35CLEAN_TIME=/etc/gss/.gsscred_clean
36TMP_FILE=/etc/gss/gsscred_clean$$
37
38trap "rm -f $TMP_FILE; exit" 0 1 2 3 13 15
39
40
41if [ -s $FILE_TO_CLEAN ] && [ $FILE_TO_CLEAN -nt $CLEAN_TIME ]
42then
43
44#
45#	The file being sorted has the following format:
46#		name	uid	comment
47#
48#	We are trying to remove duplicate entries for the name
49#	which may have different uids. Entries lower in the file
50#	are newer since addition performs an append. We use cat -n
51#	in order to preserve the order of the duplicate entries and
52#	only keep the latest. We then sort on the name, and line
53#	number (line number in reverse). The line numbers are then
54#	removed and duplicate entries are cut out.
55#
56	cat -n $FILE_TO_CLEAN | sort -k 2,2 -k 1,1nr 2> /dev/null \
57		| cut -f2- | \
58		awk ' (NR > 1 && $1 != key) || NR == 1 {
59				key = $1;
60				print $0;
61			}
62		' > $TMP_FILE
63
64	if [ $? -eq 0 ] && mv $TMP_FILE $FILE_TO_CLEAN; then
65#
66#		update time stamp for this sort
67#
68		touch -r $FILE_TO_CLEAN $CLEAN_TIME
69	else
70		rm -f $TMP_FILE
71	fi
72fi
73