[BACK]Return to skeyprune.pl CVS log [TXT][DIR] Up to [local] / src / usr.bin / skey

Annotation of src/usr.bin/skey/skeyprune.pl, Revision 1.4

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: #
1.4     ! millert     5: # Permission to use, copy, modify, and distribute this software for any
        !             6: # purpose with or without fee is hereby granted, provided that the above
        !             7: # copyright notice and this permission notice appear in all copies.
1.2       millert     8: #
1.4     ! millert     9: # THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
        !            10: # WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
        !            11: # OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
        !            12: # FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        !            13: # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
        !            14: # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
        !            15: # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.2       millert    16: #
                     17: # Prune commented out, bogus, and crufty entries from /etc/skeykeys
1.1       millert    18: # Usage: skeyprune [days]
                     19: #
1.4     ! millert    20: # $OpenBSD: skeyprune.pl,v 1.3 2002/05/16 18:27:34 millert Exp $
1.2       millert    21: #
1.1       millert    22:
1.3       millert    23: use POSIX qw(S_ISREG);
1.2       millert    24: use Fcntl qw(:DEFAULT :flock);
1.1       millert    25:
                     26: # Keep out the stupid
                     27: die "Only root may run $0.\n" if $>;
1.2       millert    28: die "Usage: $0 [days]\n" if $#ARGV > 0;
1.1       millert    29:
                     30: # Pathnames
1.3       millert    31: $skeydir = '/etc/skey';
1.1       millert    32:
                     33: # Remove entries that haven't been modified in this many days.
                     34: $days_old = $ARGV[0] || -1;
                     35:
1.2       millert    36: # Safe umask
                     37: umask(077);
                     38:
1.3       millert    39: # Current time
                     40: $now = time();
                     41:
                     42: # Slurp mode
                     43: undef $/;
                     44:
                     45: chdir($skeydir) || die "$0: Can't cd to $skeydir: $!\n";
                     46: opendir(SKEYDIR, ".") || die "$0: Can't open $skeydir: $!\n";
                     47: while (defined($user = readdir(SKEYDIR))) {
                     48:        next if $user =~ /^\./;
                     49:        if (!sysopen(SKEY, $user, 0, O_RDWR | O_NONBLOCK | O_NOFOLLOW)) {
                     50:            warn "$0: Can't open $user: $!\n";
                     51:            next;
                     52:        }
                     53:        if (!flock(SKEY, LOCK_EX)) {
                     54:                warn "$0: Can't lock $user: $!\n";
                     55:                close(SKEY);
                     56:                next;
                     57:        }
                     58:
                     59:        if (!stat(SKEY)) {
                     60:                warn "$0: Can't stat $user: $!\n";
                     61:                close(SKEY);
                     62:                next;
                     63:        }
                     64:
                     65:        # Sanity checks.
                     66:        if (!S_ISREG((stat(_))[2])) {
                     67:                warn "$0: $user is not a regular file\n";
                     68:                close(SKEY);
                     69:                next;
                     70:        }
                     71:        if (((stat(_))[2] & 07777) != 0600) {
                     72:                printf STDERR ("%s: Bad mode for %s: 0%o\n", $0, $user,
                     73:                    (stat(_))[2]);
                     74:                close(SKEY);
                     75:                next;
                     76:        }
                     77:        if ((stat(_))[3] != 1) {
                     78:                printf STDERR ("%s: Bad link count for %s: %d\n", $0, $user,
                     79:                    (stat(_))[3]);
                     80:                close(SKEY);
                     81:                next;
                     82:        }
                     83:
                     84:        # Remove zero size entries
                     85:        if (-z _) {
                     86:                unlink($user) || warn "$0: Can't unlink $user: $!\n";
                     87:                close(SKEY);
                     88:                next;
                     89:        }
1.2       millert    90:
1.3       millert    91:        # Prune out old entries if asked to
                     92:        if ($days_old > 0) {
                     93:                $then = (stat(_))[9];
                     94:                if (($now - $then) / (60 * 60 * 24) - 1 > $days_old) {
                     95:                        unlink($user) || warn "$0: Can't unlink $user: $!\n";
                     96:                        close(SKEY);
                     97:                        next;
1.2       millert    98:                }
1.3       millert    99:        }
1.2       millert   100:
1.3       millert   101:        # Read in the entry and check its contents.
                    102:        $entry = <SKEY>;
                    103:        if ($entry !~ /^\S+[\r\n]+\S+[\r\n]+\d+[\r\n]+[A-z0-9]+[\r\n]+[a-f0-9]+[\r\n]+$/) {
                    104:                warn "$0: Invalid entry for $user:\n$entry";
1.1       millert   105:        }
                    106:
1.3       millert   107:        close(SKEY);
1.2       millert   108: }
1.1       millert   109: exit(0);