Annotation of src/usr.bin/skey/skeyprune.pl, Revision 1.3
1.2 millert 1: #!/usr/bin/perl -w
1.1 millert 2: #
1.3 ! millert 3: # Copyright (c) 1996, 2001, 2002 Todd C. Miller <Todd.Miller@courtesan.com>
1.2 millert 4: # All rights reserved.
5: #
6: # Redistribution and use in source and binary forms, with or without
7: # modification, are permitted provided that the following conditions
8: # are met:
9: # 1. Redistributions of source code must retain the above copyright
10: # notice, this list of conditions and the following disclaimer.
11: # 2. Redistributions in binary form must reproduce the above copyright
12: # notice, this list of conditions and the following disclaimer in the
13: # documentation and/or other materials provided with the distribution.
14: # 3. The name of the author may not be used to endorse or promote products
15: # derived from this software without specific prior written permission.
16: #
17: # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18: # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19: # AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20: # THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21: # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22: # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23: # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24: # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25: # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26: # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27: #
28: # Prune commented out, bogus, and crufty entries from /etc/skeykeys
1.1 millert 29: # Usage: skeyprune [days]
30: #
1.3 ! millert 31: # $OpenBSD: skeyprune.pl,v 1.2 2001/06/20 22:19:58 millert Exp $
1.2 millert 32: #
1.1 millert 33:
1.3 ! millert 34: use POSIX qw(S_ISREG);
1.2 millert 35: use Fcntl qw(:DEFAULT :flock);
1.1 millert 36:
37: # Keep out the stupid
38: die "Only root may run $0.\n" if $>;
1.2 millert 39: die "Usage: $0 [days]\n" if $#ARGV > 0;
1.1 millert 40:
41: # Pathnames
1.3 ! millert 42: $skeydir = '/etc/skey';
1.1 millert 43:
44: # Remove entries that haven't been modified in this many days.
45: $days_old = $ARGV[0] || -1;
46:
1.2 millert 47: # Safe umask
48: umask(077);
49:
1.3 ! millert 50: # Current time
! 51: $now = time();
! 52:
! 53: # Slurp mode
! 54: undef $/;
! 55:
! 56: chdir($skeydir) || die "$0: Can't cd to $skeydir: $!\n";
! 57: opendir(SKEYDIR, ".") || die "$0: Can't open $skeydir: $!\n";
! 58: while (defined($user = readdir(SKEYDIR))) {
! 59: next if $user =~ /^\./;
! 60: if (!sysopen(SKEY, $user, 0, O_RDWR | O_NONBLOCK | O_NOFOLLOW)) {
! 61: warn "$0: Can't open $user: $!\n";
! 62: next;
! 63: }
! 64: if (!flock(SKEY, LOCK_EX)) {
! 65: warn "$0: Can't lock $user: $!\n";
! 66: close(SKEY);
! 67: next;
! 68: }
! 69:
! 70: if (!stat(SKEY)) {
! 71: warn "$0: Can't stat $user: $!\n";
! 72: close(SKEY);
! 73: next;
! 74: }
! 75:
! 76: # Sanity checks.
! 77: if (!S_ISREG((stat(_))[2])) {
! 78: warn "$0: $user is not a regular file\n";
! 79: close(SKEY);
! 80: next;
! 81: }
! 82: if (((stat(_))[2] & 07777) != 0600) {
! 83: printf STDERR ("%s: Bad mode for %s: 0%o\n", $0, $user,
! 84: (stat(_))[2]);
! 85: close(SKEY);
! 86: next;
! 87: }
! 88: if ((stat(_))[3] != 1) {
! 89: printf STDERR ("%s: Bad link count for %s: %d\n", $0, $user,
! 90: (stat(_))[3]);
! 91: close(SKEY);
! 92: next;
! 93: }
! 94:
! 95: # Remove zero size entries
! 96: if (-z _) {
! 97: unlink($user) || warn "$0: Can't unlink $user: $!\n";
! 98: close(SKEY);
! 99: next;
! 100: }
1.2 millert 101:
1.3 ! millert 102: # Prune out old entries if asked to
! 103: if ($days_old > 0) {
! 104: $then = (stat(_))[9];
! 105: if (($now - $then) / (60 * 60 * 24) - 1 > $days_old) {
! 106: unlink($user) || warn "$0: Can't unlink $user: $!\n";
! 107: close(SKEY);
! 108: next;
1.2 millert 109: }
1.3 ! millert 110: }
1.2 millert 111:
1.3 ! millert 112: # Read in the entry and check its contents.
! 113: $entry = <SKEY>;
! 114: if ($entry !~ /^\S+[\r\n]+\S+[\r\n]+\d+[\r\n]+[A-z0-9]+[\r\n]+[a-f0-9]+[\r\n]+$/) {
! 115: warn "$0: Invalid entry for $user:\n$entry";
1.1 millert 116: }
117:
1.3 ! millert 118: close(SKEY);
1.2 millert 119: }
1.1 millert 120: exit(0);