#!/usr/local/bin/perl
# restart-aolserver,v 3.2.4.1 2000/03/16 10:59:24 james Exp

## Restarts an AOLserver. Takes as its only argument the name of the
## server to kill.

## This is a perl script because it needs to run setuid root, 
## and perl has fewer security gotchas than most shells.

use strict;

## added to resolve a taint issue in rh linux due to ENV varables being passed - doug@arsdigita.com - 2000-06-08

undef %ENV;

$ENV{'PATH'} = '/sbin:/bin';
my @superusers=('roberto','nsadmin','brucek','dbryant');

my $name;
($name) = (getpwuid($<))[0];

my $superuser = 0;
if (grep ($name eq $_,@superusers) ) {
   $superuser = 1;
}

if (scalar(@ARGV) == 0 && !$superuser) {
    die "Don't run this without any arguments!\n";
}

my $server = shift;
# untaint this variable to make suidperl happy
$server =~ /^([\w-]*)$/;
my $servername = $1;

if ($server && !$servername) {
   die "An AOL servername should only have letters, numbers, underscores, or a dash.\nYou told us to restart $server, and we can't do that.

You just want to say something like \"restart-aolserver student000\".
"
} elsif (!$servername && !$superuser) {
   die "We need something besides the empty string to restart.\n"
}

$< = $>; # set realuid to effective uid (root)

## get the PIDs of all jobdirect servers
## changed '/usr/bin/ps -ef' to '/bin/ps -efw' - doug@arsdigita.com - 2000-06-08

open(PID, "/bin/ps -efw |") || die $!;
my @pids;
while (<PID>) {
  next unless /^\s*\S+\s+(\d+).*nsd.*\/$servername\.tcl/;
  my $pid = $1;
  push(@pids, $pid);
}
close PID;

print "Killing ", join(" ", @pids), "\n";
kill 'KILL', @pids;

