#!/usr/bin/perl -w #pop_relayd# ##This script is for adding access to relay mail ##through a sendmail 8.9+ based e-mail server from a ##pop/imap client being used with an arbitrary ##outside host. It runs as a daemon, and continualy ##checks the last line in the system mail logfile. ##When it finds evidence of an authenticated pop/imap ##request, it adds the host name associated to the ##request in the /etc/mail/access file allowing the ##sendmail to relay mail from this client. A cron ##script ran in conjuction with this daemon can reset the ##access file each day. Probably works better if mail log ##is kept seperate from system logs. ## ##J.B. Gallagher 04/04/00 jbg@navigo.com ##System mail logfile## $MAILLOG = "/var/log/mail"; ##Sendmail access file## $access = "/etc/mail/access"; $killall = "/sbin/killall"; ##Programs## $makemap = "/usr/etc/makemap"; $tail = "/bin/tail"; ##Daemon stuff## use POSIX qw(setsid); $|=1; &daemon; $run_count = 0; while(1) { $rundate = `/bin/date`; chop($rundate); if($run_count eq '0') { print STDOUT "$0 started on $rundate\n"; print STDOUT "___________________________________\n\n"; } $init = `$tail -1 $MAILLOG`; sleep 1; $i2 = `$tail -1 $MAILLOG`; if($i2 ne $init) { if($i2 =~ /ipop3d/ && $i2 =~ /Login user=/) { $i2 =~ s/\s+//g; @host = split(/host=/ , $i2); @hostname = split(/\[/ , $host[1]); $hostname[0] =~ tr/\n//d; open(FILE, "$access"); while() { if($_ =~ $hostname[0]) { $there = '0'; } } close(FILE); if($there ne '0') { `echo "$hostname[0] RELAY" >>$access`; `$makemap hash $access < $access`; `$killall -HUP sendmail`; print STDOUT "$rundate: Added $hostname[0] to access database\n"; } print STDOUT "________________________________________\n"; } if($i2 =~ /imapd/ && $i2 =~ /Login user=/) { $i2 =~ s/\s+//g; @host = split(/host=/ , $i2); @hostname = split(/\[/ , $host[1]); $hostname[0] =~ tr/\n//d; open(FILE, "$access"); while() { if($_ =~ $hostname[0]) { $there = '0'; } } close(FILE); if($there ne '0') { `echo "$hostname[0] RELAY" >>$access`; `$makemap hash $access < $access`; `$killall -HUP sendmail`; print STDOUT "$rundate: Added $hostname[0] to access database\n"; } print STDOUT "________________________________________\n"; } #RESET dummy variables# $there = 1; } $run_count++; } sub daemon { $date = `date +"%y%m%d"`; chop($date); $logfile = "/var/log/access-relay.$date"; chdir '/' or die "Can't chdir to /: $!"; umask 0; open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; open STDOUT, ">>$logfile" or die "Can't write to $logfile: $!"; open STDERR, '/dev/null' or die "Can't write to err: $!"; defined(my $pid = fork) or die "Can't fork: $!"; exit if $pid; setsid or die "Can't start a new session: $!"; }