#!/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 --------------
# lpq_nt - a setUID script to determine NT queue status via a direct rsh
# The ONLY reason this exists is because the NT LPD server truncates the
# IP address when queried via an lpq ... and it is setUID because we restrict
# rsh's to a certain named user
# 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 != 1) {
   print ("Usage: lpq_nt queuename printserver\n");
   exit(1);
}

$< = "$lprng_uid";

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

$command = "rsh $printserver \"$nt_lpc $queuename /JL\"" ;
$output = `$command`;

print "                  Windows NT LPD Server \n Rank  Owner/NT-ID                   Class Job  Files                              Size\n";
print "----------------------------------------------------------------------------------------\n";
if ( ! ($output =~ /There are no print jobs for printer/) ) { 
   @lpqout = split(/\n/,$output);
   $count = 1; 
   foreach $line (@lpqout) {
      if ( ($line =~ /JOB LOG FOR PRINTER/) || ($line =~ /^Job:/) || ($line =~ /--------------------/) || ($line =~ /^\s*$/)) {
         next;
      } else {
         ($jobid,$owner,$_,$jobname,$size) = split(/\s+/,$line);
         s/\(// ; s/\)//;
         $owner = "$owner" . "@" . "$_" . "+1";
         printf "%-2s%5s%-31s%3s%5s%2s%-30s%9s%s",$count,"     ",$owner,"NT",$jobid,"  ",$jobname,$size,"\n";
         $count++;
      }
   }
}

exit(); 
