#!/usr/local/bin/perl #--------------------------------------------------------------------# # Program : parapipe # # Creator : Jason McLain # #--------------------------------------------------------------------# # Intention : To write a program that keeps track of who is # # : fingering you. And, to provide a random quote to the # # : fingerer # #--------------------------------------------------------------------# $nscmd ="netstat -nf inet"; #Your system's netstat cmd. $pscmd ="ps -ef"; #Your system's ps cmd. &gethome; #Figure out your home directory. $data_file ="$home/.fing"; #Where to keep track of finger info. $version =".99"; #Current Version # $pipe ="$home/.plan"; #Get the name of the data file. $rfile = shift(@ARGV); #If there weren't enough arguments print help and exit. if ($rfile eq "") { print < NOTES: Random quotes are read from the data file. The data file can be of any length. Each quote is separated by a % symbol on a blank line. The first two quotes are prepended and appended to the random quotes. The data file can be a gzipped or compressed file. Information about who is fingering you is kept in $data_file. It is not possible to detect remote user ID's. The site that originates the finger request is recorded instead. INFO: Please contact jmclain@st6000.sct.edu for more info. EOP exit; } # # Get the current date and time. # sub getdate { $date = `date +"%a, %h %d %I:%M %p"`; $date = `date +"%a, %h %d %r"` if defined($opt_u); chop $date; } # # Get user's home directory and name. # sub gethome { @pw=getpwuid($<); $home=$pw[7]; $name=$pw[0]; } #Make sure nothing will be overwritten... print "Checking for existing plan...\n"; if (-T $pipe) {print "Can't overwrite $pipe\n";exit;} print "No text plan found - continuing...\n"; #Create the pipe. print "Creating pipe.\n"; unlink $pipe if ( -p $pipe ); if ( ! -e $pipe ) { system ("/etc/mknod $pipe p"); } #Make it world readable. chmod 0644, "$pipe"; print "Daemonizing... Bye!\n"; fork && exit; # # Loop forever doing the following: # > get a random quote # > open the pipe # this causes the program to stop until the pipe is read # > when someone accesses the pipe tracker subroutine is called # > dump info into the pipe # > close the pipe # > run logger subroutine # > wait a bit # while () { &fortune ($rfile); open (FIFO, ">>$home/.plan"); &tracker; print FIFO "$prepend\n$quote\n\n$append\n"; close FIFO; &logger; sleep 2; } # # Picks a random quote from a text file where the # the quotes are separated by a single % on a line. # sub fortune { local ( $file ) = @_; local ($/) = "\n%\n"; srand(time|$$); open (QUOTE, "zcat $file |") if ( -B $file ); open (QUOTE, "$file") if ( -T $file ); @quotes = ; $prepend = shift(@quotes); $append = shift(@quotes); chop $prepend;chop $prepend; chop $append;chop $append; $quote = splice(@quotes, rand @quotes, 1); chop $quote; chop $quote; } # # Figures out who fingered you. If it's a local user # it logs their name. If it's a remote user it logs the # site from which the finger request originates. # Note: there is no way to identify remote user ID. # sub tracker { open (PS, "$pscmd|") || die "Error running $pscmd.\n"; while () { @caught=split(/\s+/, $_) if /.*[f|finger] jmclain.*/; } close PS; if ($caught[1] eq "nobody") { open (NS, "$nscmd|") || die "Error running $nscmd\n"; while () { @host=split(/\s+/, $_) if /.*131\.144\.80\.249\.79\s.*ESTABLISHED/; } close NS; @site=split(/.[0-9]+$/, $host[4]); $site=$site[0]; open (HOST, "host $site|"); @site=split(/\s+/, ); $site=$site[0]; $person="$site"; } else { @fingerer=getpwnam($caught[1]); $person=$fingerer[6]; } } # # Log the information # sub logger { open (LOG, ">>$data_file"); &getdate; print LOG "$date: $person\n"; close (LOG); }