#!/usr/bin/perl #$Id: sudoshell,v 1.14 2002/04/03 23:10:20 howen Exp $ $ENV{PATH}="/bin:/usr/bin:/usr/sbin:/usr/local/bin"; # Give ourselves a safe one. use strict; my $GREP = "grep"; my $SUDO="sudo"; my $PS; my $initscr; # OS dependencies if ($^O eq 'solaris' || $^O eq 'linux'){ $PS="ps -elf"; $initscr="/etc/init.d/sudoscriptd"; } elsif($^O eq 'freebsd' || $^O eq 'openbsd') { $PS="ps -aux"; $initscr="/usr/local/etc/rc.d/sudoscriptd.sh"; } else { print <<'EOM'; Sorry, but your OS is not among the ones I support Currently, that's linux, solaris, freebsd and openbsd That's because those are the ones I have access to. If you'd like support for your OS, either give me a root shell (!) on a representative system running your OS, or port it yourself and send me (hbo@egbok.com) the diffs. If system directory locations (i.e. /var/log and /var/run) don't have to change, and your system has Perl 5 and POSIX, That shouldn't be too hard. See the PORTING document in the distribution for details. EOM exit; } #' # No more failing open. Good rhetoric. Bad practicum. check_sudoscriptd(); my $rundir="/var/run/sudoscript"; my $fifo="$rundir/typescript"; if ($>){ # not run as root. Try sudo exec "$SUDO $0"; } if ($>){ print "Not root! Exiting.\n"; exit; } if (!defined $ENV{SHELL}){ print "Can't determine your shell. Set the SHELL env variable! Exiting.\n"; exit; } # Don't take their word for it. open SHELLS, "/etc/shells" || die $!; my $shellsre=join "|", map {chomp;s~/~\\\/~g;$_} (); close SHELLS; if ($ENV{SHELL}!~/$shellsre/){ print "Your shell (".$ENV{SHELL}.") isn't in /etc/shells! Exiting.\n"; exit; } my $name; if ($ENV{SUDO_UID}){ ($name) =getpwuid $ENV{SUDO_UID}; } else { $name = "root"; } if (-p $fifo){ # script to the fifo if it exists open FIFO, ">>$fifo" || die $!; print FIFO "sudoshell started by $name\n"; close FIFO; exec "script $fifo"; } else { print "The logging FIFO doesn't exist. Can't run shell!\n"; } sub check_sudoscriptd { if (!checkpid()){ my $ans; print "The sudoscriptd doesn't appear to be running!\n"; print "Would you like me to start it for you? (requires root privilege)? "; $ans=<>; chomp $ans; die "Can't run sudoshell without sudoscriptd" if ($ans!~/^[Y|y]/); print <<'EOM'; This will be a one-off startup of the daemon. You may have to arrange for it to be started when the system starts, if that's what you want. See the INSTALL file in the distribution for details. EOM # bloody emacs sytax highlighting doesn't grok here docs if (! -x $initscr){ print "Hmm.. I can't seem to find the sudoscriptd startup file.\n"; print "Please tell me where it is. or just return for exit "; $ans=<>; chomp $ans; if (-x $ans) { $initscr=$ans; } else { die "Can't run sudoshell without sudoscriptd"; } } system($SUDO, $initscr, "start",'&'); print "waiting for the daemon .."; sleep 3; print "done\n"; if (!checkpid()){ print "Sorry, but I can't seem to start sudoscriptd for you!\n"; print "exiting\n"; exit; } } } sub checkpid { my $dpidf="/var/run/sudoscriptd.pid"; my $dpid; my @ret; my $gotone=0; if (-e $dpidf){ open DPID, $dpidf || die $!; $dpid=; chomp $dpid; @ret=`$PS |$GREP $dpid | $GREP -v $GREP`; $gotone= ($#ret >-1 && $ret[0]=~/sudoscriptd/); } return $gotone; } =pod =head1 NAME sudoshell, ss - Run a root shell with logging =head1 SYNOPSIS sudoshell or sudo sudoshell =head1 DESCRIPTION I runs the I