#!/usr/bin/perl # trainAll.osbf-lua - Invoke trainUser for all users, or a subset # # Author: # Steve Pellegrin (spellegrin at convoglio dot com) # # History: # 1.0 2006-August-6 Original code # 1.1 2008-September-29 Rework command line option processing # # Usage: # trainAll.osbf-lua [--spam | --nonspam | --good] # # Description: # This script can be run manually, but it probably makes more sense to run it # periodically as a cron job. Each time it runs, the script iterates over # each user account, invoking trainUser on that account. The set of users # processed is configurable in a number of ways (see Configuration parameters, below). # # Note that the script must be run from an account that can invoke trainUser # on behalf of user accounts. This probably imples root. use strict; # to catch stupid errors use Getopt::Long; use Pod::Usage; # ---------- Configuration ------------------------------------------ # There are three ways to collect user home directories: # 1. Assume that all users have directories inside of # some "base" directory (typically /home). # 2. Collect home directories from a range of UID values. # 3. Collect home directories from a list of UID values. # # ONE of $baseDir or @uidList should be defined. # Case #1 # User directories (and only user directories) are found here my $baseDir = "/home"; #my $baseDir = undef; # The list of UIDs to examine. # Case #2: Specify a range of values n..m # Case #3: Specify a list of discrete values (a, b, c, d) #my @uidList = (1019, 1012, 1018, 1014, 1013, 1006, 1007, 1008, 1010, 1011, 1009); my @uidList = undef; # Location of trainUser my $trainUserCmd = "/usr/local/bin/trainUser.osbf-lua"; # ---------- End of Configuration ----------------------------------- # Pre-check command line options. my @args = @ARGV; GetOptions ( 'spam' => \(my $spam = ''), 'nonspam|good' => \(my $good = ''), 'help' => \(my $help = ''), ) or pod2usage(2); # Print help text and exit if requested. pod2usage(1) if $help; # Can't train both ways at once. pod2usage(1) if ($spam && $good); # But we have to train one way! pod2usage(1) unless ($spam || $good); # Collect user names and home directories. my @users = defined($baseDir) ? collectBasedUserInfo() : collectUIDUserInfo(); foreach my $user (@users) { my $cmd = "/usr/local/bin/sudo -u $user -H $trainUserCmd @args"; system "$cmd"; } sub collectBasedUserInfo { my @list; # Read in all the user directories in the base directory, # e.g., /home/alice, /home/bob, /home/charles opendir(DIR,$baseDir); while (my $userDir = readdir(DIR)) { next if ($userDir =~ /^\./); push (@list, $userDir); } closedir(DIR); @list; } sub collectUIDUserInfo { my @list; foreach my $uid (@uidList) { my $user = (getpwuid $uid)[0]; if (defined($user)) { push (@list, $user); } } @list; } __END__ =head1 NAME trainAll.osbf-lua - Train all user accounts =head1 SYNOPSIS trainAll.osbf-lua --spam | [--nonspam | --good] | --help =head1 OPTIONS =over 8 =item B<--spam> Train as spam =item B<--good|nonspam> Train as non-spam =item B<--help> Print this text =back =cut