#!/usr/bin/perl -w $AUTHOR_MAIL = "scoile\@patriot.net"; $AUTHOR_NAME = "Steve Coile"; $COPYRIGHT_DATE = "1997"; $COPYRIGHT_NAME = "Patriot Computer Group"; $LICENSE = "Distributed under the GNU General Public License"; $VERSION_DATE = "November 4, 1997"; $VERSION_NUMBER = "1.0"; $log_format = "access_log"; $log_name = "%s.log"; %logformat = ( "access_log" => '^[^\]]*\] "\S+ /(?:%7[Ee]|~)([^ /]+)', "acp_logfile" => ':([^:]+)$', "last" => '^(\S+)' ); $usage = sprintf( "Usage:\n" . "\n" . "\t%s [opt]...\n" . "\n" . "Where opt is:\n" . "\n" . "-c\n" . "\tIndicates that all usernames encountered should be verified\n" . "\tagainst the system user database. Only log entries for\n" . "\tusers that exist in the system user database are separated.\n" . "-f log-file-format\n" . "\tlog-file-format is an identifier indicating what format\n" . "\tthe input stream is in. The format identifier is used to\n" . "\tdermine where to look for the separating key (the username)\n" . "\tin each input line.\n" . "-l log-name-format\n" . "\tlog-name-format should contain a valid printf(3) style\n" . "\tformat string used to generate the user-specific log file\n" . "\tnames. The format string should contain a single \"%%s\"\n" . "\tat the location that the user's username should occur.\n" . "\tFor instance, \"/var/log/httpd/users/%%s.log\".\n" . "-h\n" . "\tDisplay this help message and exit.\n" . "-v\n" . "\tDisplay version information and exit.\n", $0 ); %users = (); $verify_users = 0; $version = sprintf( "%s v%s (%s)\n" . "By %s <%s>\n" . "Copyright %d by %s\n" . "%s\n", $0, $VERSION_NUMBER, $VERSION_DATE, $AUTHOR_NAME, $AUTHOR_MAIL, $COPYRIGHT_DATE, $COPYRIGHT_NAME, $LICENSE ); while ($#ARGV > -1) { if ($ARGV[0] eq "-c") { shift(@ARGV); $verify_users = 1; } elsif (($ARGV[0] eq "-f") && ($#ARGV > 0)) { shift(@ARGV); $log_format = shift(@ARGV); } elsif (($ARGV[0] eq "-l") && ($#ARGV > 0)) { shift(@ARGV); $log_name = shift(@ARGV); } elsif ($ARGV[0] eq "-h") { shift(@ARGV); print(STDERR $version,"\n",$usage); exit 0; } elsif ($ARGV[0] eq "-v") { shift(@ARGV); print(STDERR $version); exit 0; } else { printf(STDERR "%s: unrecognized option\n", shift(@ARGV) ); print(STDERR $usage); exit 1; }; }; if (defined($logformat{$log_format})) { $splitpattern = $logformat{$log_format}; } else { printf(STDERR "%s: unrecognized log format identifier\n", $log_format ); printf(STDERR "Recognized log file format identifiers are:\n\t%s\n", join("\n\t",sort(keys(%logformat))) ); exit 1; }; while ($_ = ) { chomp($_); $_ =~ m($splitpattern)o; my($key) = $1; if ( defined($key) && ( !$verify_users || defined($users{$key}) || ($users{$key} = getpwnam($key)) ) ) { my($log) = sprintf($log_name,$key); $keys{$key} = [] if (!defined($keys{$key})); push(@{$keys{$key}},$_); if (($#{$keys{$key}} > 100) && open(OUTFILE,">>$log")) { my($i); for ($i = 0; $i <= $#{$keys{$key}}; ++$i) { if (!print(OUTFILE ${$keys{$key}}[$i],"\n")) { printf(STDERR "Unable to write to %s\n", $log ); last; }; }; splice(@{$keys{$key}},0,$i); close(OUTFILE); }; } else { print($_,"\n"); }; }; foreach $key (keys(%keys)) { next if ($#{$keys{$key}} == -1); my($log) = sprintf($log_name,$key); if (!open(OUTFILE,">>$log")) { printf(STDERR "Unable to open %s for append.\n" . "%d records for key %s will be lost.\n", $log, ($#{$keys{$key}} + 1), $key ); next; }; my($i); for ($i = 0; $i <= $#{$keys{$key}}; ++$i) { if (!print(OUTFILE ${$keys{$key}}[$i],"\n")) { printf(STDERR "Unable to write to %s\n" . "%d records for key %s will be lost.\n", $log, ($#{$keys{$key}} - $i + 1), $key ); last; }; }; close(OUTFILE); };