.forward Script

This script enables privileged users to edit other users .forward files. This script required SUID, so its best to restrict it to certain users. I use the adm group, so make sure to add your users to that group.

# chown root.adm /usr/local/bin/forward.pl
# chmod 750 /usr/local/bin/forward.pl
# chmod o+s /usr/local/bin/forward.pl
#!/usr/bin/perl
use warnings;
use strict;
 
# Usage information
sub usage() {
print STDERR <<EOF;
usage: forward.pl [username]
 
A utility to create/edit a .forward for a user
 
EOF
exit 1;
}
 
#&usage() unless ($#ARGV > -1);
 
my $username = "";
my @user;
 
if (defined $ARGV[0]) {
        $username = $ARGV[0];
} else {
        print "Enter username: ";
        $username = <STDIN>;
        chomp($username);
}
 
@user = getpwnam($username);
 
unless ($#user > -1) {
        print "User $username not found\n";
        exit 1;
}
 
#print "Home directory $user[7]\n";
 
print "User: $user[0] Home: $user[7]\n";
print "Gecos: $user[7]\n";
 
my $hasForward = ( -r $user[7] . "/.forward" ) ? 1 :  0;
 
print "Has foward: " . (($hasForward) ? "true" : "false") . "\n";
 
if ($hasForward) {
        print "1. Remove .forward\n";
        print "2. View .forward\n";
        print "3. Exit\n";
        print "Enter Selection: ";
        my $res = "";
        while ($res = <STDIN>) {
                chomp $res;
                last if ($res =~ /[1-2]/);
                print "1. Create .forward\n";
                print "2. View .forward\n";
                print "3. Exit\n";
                print "Enter Selection: ";
        }
        exit 0 if ($res eq "3");
        my $filename = $user[7]."/.forward";
        if ($res eq "2") {
                open FH, "<$filename";
                my @f = <FH>;
                print @f;
                close FH;
                exit 0;
        } else {
                my $not = "";
                if ($filename =~ /(.*)/) {
                        $not = $1;
                }
                unlink $not;
                print "File $filename removed\n";
                exit 0;
        }
 
} else {
        print "1. Create .forward\n";
        print "2. Exit\n";
        print "Enter Selection: ";
        my $res = "";
        while ($res = <STDIN>) {
                chomp $res;
                last if ($res =~ /[1-2]/);
                print "1. Create .forward\n";
                print "2. Exit\n";
                print "Enter Selection: ";
        }
        exit 0 if ($res eq "2");
        print "Enter .forward lines followed by a CR.CR\n";
 
        my $line = "";
        my $buf = "";
        while ($line = <STDIN>) {
                chomp $line;
                last if ($line eq ".");
                $buf .= $line . "\n";
        }
        my $filename = $user[7]."/.forward";
        my $not = "";
        if ($filename =~ /(.*)/) {
                $not = $1;
        }
        open FH, ">$not";
        print FH $buf;
        close FH;
        print "File $filename written\n";
}
 
exit 0;