#!/usr/bin/perl
#^^^^^^^^^^^^^^^^^^^^ Update pathname appropriately to where you have Perl installed
# And update the path below of the configuration file as appropriate.
$suid_ph = "/var/www/html/LPRng/admin/cgi-bin/suid.ph";

#-------------- Should be fairly boilerplate from here on out --------------
# lpc.suid - a setUID script to allow httpd to issue lpc commands ...
# This script would be setUID root and in a directory readable by httpd ONLY
# and protected via a .htaccess file
# Copyright 1997, 1998, 1999, 2000 by Alek Komarnitsky, alek@komar.org, http://www.komar.org/
# Use and distribution of this software is covered by the GNU GPL license.  
# Please see the LICENSE file and http://www.gnu.org/

# Grab stuff from suid.ph ...
if ( -r "$suid_ph" ) { 
   require "$suid_ph";
} else {
   print "could not open $suid_ph - serious error ... \n";
   exit(2);
}


if ($#ARGV < 4) {
   print ("Usage: lpc_suid adminmode browserhost queuename printserver lpc-options\n");
   exit(1);
}

# LPRng requires setting UID to EUID ...
$< = $>; 

$ARGV[0] =~ /^(\S+)$/;
$_ = $1;              # Not tainted
$adminmode = sanitize($_); 
$adminmode = nobody if ($adminmode eq 0 ) ; 

$ARGV[1] =~ /^(\S+)$/;
$_ = $1;              # Not tainted
$browserhost = sanitize($_); 

$ARGV[2] =~ /^(\S+)$/;
$_ = $1;              # Not tainted
$queuename = sanitize($_); 

$ARGV[3] =~ /^(\S+)$/;
$_ = $1;              # Not tainted
$printserver = sanitize($_); 

$temp = "@ARGV[4 .. $#ARGV]";
$temp =~ /^(.+)$/;
$_ = $1;              # Not tainted
$lpc_options = sanitize($_); 


if ( $printserver =~ /unix/ ) {
   $command = "$lpc -P$queuename $lpc_options";
   $command = "$lpc release $queuename all" if ( $lpc_options eq "release"); 
} else {
   $< = "$lprng_uid";
   $command = "rsh $printserver \"$nt_lpc $queuename /$lpc_options\"" ;
}
print "<h2>Executing: &nbsp; $command <br><br>\n";
$_ = `$command`;
print "Results: &nbsp; &nbsp; &nbsp; $_"; 

&openlog("lpc_suid","cons,pid","local2");
&syslog('info',"lpc -P$queuename $lpc_options from browserhost $browserhost by $adminmode");
&closelog();
exit(); 
