aboutsummaryrefslogtreecommitdiff
path: root/contrib/wallet-summary
blob: 4e761191ae05707f2bb2a31cc4297dd4f1c0d999 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/perl -w
#
# Summarize keytabs in the wallet database.

##############################################################################
# Site configuration
##############################################################################

# Path to the infrastructure reports directory.
$REPORTS = '/afs/ir/dept/itss/infrastructure/reports';

# Address to which to mail the report.
$ADDRESS = 'nobody@example.com';

# The various classification patterns for srvtabs.
@PATTERNS
    = ([qr(/cgi\z),       '*/cgi',        'CGI users'],
       [qr(^(?i)http/),   'HTTP/*',       'HTTP Negotiate-Auth'],
       [qr(^cifs/),       'cifs/*',       'CIFS'],
       [qr(^host/),       'host/*',       'Host login'],
       [qr(^ident/),      'ident/*',      'S/Ident'],
       [qr(^imap/),       'imap/*',       'IMAP'],
       [qr(^ldap/),       'ldap/*',       'LDAP'],
       [qr(^nfs/),        'nfs/*',        'NFS'],
       [qr(^pop/),        'pop/*',        'Kerberized POP'],
       [qr(^sieve/),      'sieve/*',      'Sieve mail sorting'],
       [qr(^smtp/),       'smtp/*',       'SMTP'],
       [qr(^webauth/),    'webauth/*',    'WebAuth v3'],
       [qr(^service/),    'service/*',    'Service principals']);

##############################################################################
# Modules and declarations
##############################################################################

require 5.005;

use strict;
use vars qw($ADDRESS $DUMPFILE @PATTERNS $REPORTS);

use Getopt::Long qw(GetOptions);
use File::Path qw(mkpath);
use POSIX qw(strftime);
use Wallet::Report ();

##############################################################################
# Database queries
##############################################################################

# Return a list of keytab objects in the wallet database.  Currently, we only
# report on keytab objects; reports for other objects will be added later.
sub list_keytabs {
    my $report = Wallet::Report->new;
    my @objects = $report->objects;
    if (!@objects and $report->error) {
        die $report->error;
    }
    return map { $$_[1] } grep { $$_[0] eq 'keytab' } @objects;
}

##############################################################################
# Reporting
##############################################################################

# Used to make heredocs look pretty.
sub unquote { my ($string) = @_; $string =~ s/^:( {0,7}|\t)//gm; $string }

# Given an array of principal names, classify them into various interesting
# groups and then report on the total number of principals, broken down by the
# individual groups.
sub report_principals {
    my @principals = @_;
    my (%count, $found);

    # Count the principals in each category.
    for (@principals) {
        $found = 0;
        for my $mapping (@PATTERNS) {
            if (/$$mapping[0]/) {
                $count{$$mapping[1]}++;
                $found = 1;
                last;
            }
        }
        $count{OTHER}++ unless $found;
    }
    my $total = scalar @principals;

    # Find the longest label for any principal type.
    my ($taglen, $desclen) = (0, 0);
    for (@PATTERNS) {
        next unless $count{$$_[1]};
        $taglen = length ($$_[1]) if length ($$_[1]) > $taglen;
        $desclen = length ($$_[2]) if length ($$_[2]) > $desclen;
    }
    $taglen = 6 if $taglen < 6;

    # Print the report.
    print unquote (<<"EOM");
:       This is a summary of the current keytab entries in the wallet database,
:       which contain entries for every principal that is managed by our
:       Kerberos keytab management system.  Not all of these principals may
:       necessarily be in active use.  Principals corresponding to hosts which
:       are no longer registered in NetDB are purged periodically.
:
EOM
    printf ("%-${taglen}s  Count  %-${desclen}s\n", 'Type', 'Description');
    print '-' x $taglen, '  -----  ', '-' x $desclen, "\n";
    for (@PATTERNS) {
        next unless $count{$$_[1]};
        printf ("%-${taglen}s  %5d  %s\n", $$_[1], $count{$$_[1]}, $$_[2]);
    }
    if ($count{OTHER}) {
        print "\n";
        printf ("%-${taglen}s  %5d  %s\n", '', $count{OTHER}, 'Other');
    }
    print ' ' x $taglen, '  ', '=====', "\n";
    printf ("%${taglen}s  %5d\n", 'Total:', $total);
}

##############################################################################
# Main routine
##############################################################################

# Read in command-line options.
my ($help, $mail);
Getopt::Long::config ('no_ignore_case', 'bundling');
GetOptions ('help|h' => \$help,
            'mail|m' => \$mail) or exit 1;
if ($help) {
    print "Feeding myself to perldoc, please wait....\n";
    exec ('perldoc', '-t', $0);
}

# Clean up $0 for error reporting.
$0 =~ s%.*/%%;

# If -m was given, save the report into the infrastructure area.
if ($mail) {
    my $date = strftime ('%Y/%m', localtime);
    mkpath ("$REPORTS/$date/kerberos");
    open (REPORT, "+> $REPORTS/$date/kerberos/wallet")
        or die "$0: cannot create $REPORTS/$date/kerberos/wallet: $!\n";
    select REPORT;
}

# Run the report.
my @principals = read_dump;
report_principals (@principals);

# If -m was given, take the saved report and mail it as well.
if ($mail) {
    seek (REPORT, 0, 0)
        or die "$0: cannot rewind generated report: $!\n";
    my $date = strftime ('%Y-%m-%d', localtime);
    open (MAIL, '| /usr/lib/sendmail -t -oi -oem')
        or die "$0: cannot fork sendmail: $!\n";
    print MAIL "From: root\n";
    print MAIL "To: $ADDRESS\n";
    print MAIL "Subject: wallet keytab report ($date)\n\n";
    print MAIL <REPORT>;
    close MAIL;
    if ($? != 0) {
        warn "$0: sendmail exited with status ", ($? >> 8), "\n";
    }
}
close REPORT;

##############################################################################
# Documentation
##############################################################################

=for stopwords
-hm keytab keytabs MERCHANTABILITY NONINFRINGEMENT sublicense

=head1 NAME

wallet-summary - Report on keytabs in the wallet database

=head1 SYNOPSIS

B<wallet-summary> [B<-hm>]

=head1 DESCRIPTION

Obtains a list of keytab objects in the wallet database and produces a
report of the types of principals contained therein and the total number
of principals registered.  This report is sent to standard output by
default, but see B<-m> below.

The classifications of principals are determined by a set of patterns at
the beginning of this script.  Modify it to add new classifications.

=head1 OPTIONS

=over 4

=item B<-h>, B<--help>

Print out this documentation (which is done simply by feeding the script to
C<perldoc -t>).

=item B<-m>, B<--mail>

Rather than printing the report to standard output, send the report via
e-mail to the address set at the beginning of this script and also archive
a copy under F</afs/ir/dept/itss/infrastructure/reports>.

=back

=head1 FILES

=over 4

=item F</afs/ir/dept/itss/infrastructure/reports>

The root directory for archived reports.  Archived reports will be saved
under this directory in a subdirectory for the year, the month, and
C<kerberos>, under the name C<wallet>.  In other words, for a report run
in March of 2003, the report will be saved in the file:

    /afs/ir/dept/itss/infrastructure/reports/2003/03/kerberos/srvtabs

=back

=head1 NOTES

Considerably more information could potentially be reported than is
currently here.  In particular, keytabs that have never been downloaded
are not distinguished from those that have, the number of keytabs
downloaded is not separately reported, and there aren't any statistics on
how recently the keytabs were downloaded.  These could be useful areas of
future development.

=head1 AUTHOR

Russ Allbery <rra@stanford.edu>

=head1 COPYRIGHT AND LICENSE

Copyright 2003, 2008, 2010, 2013 The Board of Trustees of the Leland
Stanford Junior University

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

=cut