#! /usr/bin/env perl
#
# -*- Perl -*-
#
# This is part of X-RUST 1.0 Beta 6
#
# Copyright (C) 1996 Craig Ruefenacht <ruefenac@cs.utah.edu> and the
#                    University of Utah, Department of Computer Scinece
#
#                    Shawn T. Amundson <amundson@cs.umn.edu> and the
#                    University of Minnesota Computer Science Department
#
# Abstract: Used by X-Rust to do things which require the segid bit set
#           ...
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with the source distribution of RUST, in the license
# directory. If you don't have a copy of it, write to: 
#
# Free Software Foundation, Inc.
# 675 Mass Ave
# Cambridge, MA 02139, USA.
#

#
# $Id: xrust_server.pl,v 1.3 2013/01/07 16:53:41 adesmet Exp $
#

BEGIN {
    push(@INC,"/p/condor/home/rust/modules");
}

require 5.002;

require "ctime.pl";
use Getopt::Long;
use Rust_Configs;
use Rust_Mail;
use Rust_Exit;
use Rust_Action;
use Rust_Debug;
use Rust_Lib;
use Rust_Query;
use Mail::Internet;
use Mail::Address;

&read_globals($0);
if (!(Rust_Debug::start_debugging($program, @ARGV))) {
    &terminate("Had problems opening debug file\n");
}

$| = 1;

my $sub;
while ($sub = <STDIN>) {
    chop($sub);

    if ($sub eq "get_headers") {

	$opt_user      = <STDIN>;
	$opt_real_user = "";
	$opt_owner     = <STDIN>;
	$opt_unowned   = <STDIN>;
	$opt_type      = <STDIN>;
	$opt_status    = <STDIN>;
	$opt_priority  = <STDIN>;
	$opt_category  = <STDIN>;
	$opt_subject   = <STDIN>;
	$opt_sort      = <STDIN>;
	$opt_queue     = <STDIN>;

	chop($opt_user);
	chop($opt_owner);
	chop($opt_unowned);
	chop($opt_type);
	chop($opt_status);
	chop($opt_priority);
	chop($opt_category);
	chop($opt_subject);
	chop($opt_sort);
	chop($opt_queue);

	$queue_index = Rust_Lib::set_queue_index(0, $opt_queue);
	&debug_globals($queue_index);

	@h_a = &get_headers($caller, $opt_real_user, $opt_user, $opt_owner, $opt_unowned,
			    $opt_type, $opt_status, $opt_priority, $opt_category, 
			    $opt_subject, $opt_sort, 0, $queue_index);
	

	print "$h_a[0][0]\n";
	print "$h_a[0][1]\n";
	
	$count = 0;
	
	while ($count < $h_a[0][1]) {
	    $count++;
	    $index = 0;
	    while ($index < 14) {
		print "$h_a[$count][$index]\n";
		$index++;
	    }
	}
	
    } elsif ($sub eq "get_ticket_log") {
	
	$opt_realuser = "";
	$opt_show = <STDIN>;
	$queue_index = <STDIN>;
	
	chop($queue_index);
	chop($opt_show);

	&debug_globals($queue_index);

        @m_b = &get_ticket_log($caller, $opt_realuser, $opt_show,
                                $queue_index);

	$num = $#m_b;
	print "$num\n";
	foreach $k (@m_b) {
	    $k =~ s/\n$//g;
	    print "$k\n";
	}

    } elsif ($sub eq "do_action") {

	my $type = <STDIN>;
	my $opt_num = <STDIN>;
	my $opt_set = <STDIN>;
	my $opt_append = <STDIN>;
	my $opt_value = <STDIN>;
	my $queue_index = <STDIN>;
    
	chop($type);
	chop($opt_num);
	chop($opt_set);
	chop($opt_append);
	chop($opt_value);
	chop($queue_index);

	&debug_globals($queue_index);

	$queue_index = &untaint_obj($queue_index);
	my @array = &do_action($caller,$type,$opt_num,$opt_set,$opt_append,$opt_value,$queue_index);
	
	chomp($array[0]);
	chomp($array[1]);

	print "$array[0]\n";
	print "$array[1]\n";

    } elsif ($sub eq "do_assign") {

	my $opt_num = <STDIN>;
	my $opt_assign = <STDIN>;
	my $queue_index = <STDIN>;

	chop($opt_num);
	chop($opt_assign);
	chop($queue_index);

	&debug_globals($queue_index);

	$queue_index = &untaint_obj($queue_index);
	my @array = &do_assign($caller,$opt_num,$opt_assign,$queue_index);

	chomp($array[0]);
	chomp($array[1]);

	print "$array[0]\n";
	print "$array[1]\n";

    } elsif ($sub eq "do_comment") {

	my $type = <STDIN>;
	my $opt_num = <STDIN>;
	my $queue_index = <STDIN>;
	my $num = <STDIN>;

	chop($type);
	chop($opt_num);
	chop($queue_index);
	chop($num);

	my $c;
	my @contents;
	foreach (0..$num) {
	    $c = <STDIN>;
	    chomp($c);
	    push(@contents, $c);
	}

	$queue_index = &untaint_obj($queue_index);

	my @array = &do_comment($caller,$type,$opt_num,$queue_index,@contents);

	chomp($array[0]);
	chomp($array[1]);

	print "$array[0]\n";
	print "$array[1]\n";

    } elsif ($sub eq "do_merge") {

	my $caller = <STDIN>;
	my $opt_num1 = <STDIN>;
	my $opt_num2 = <STDIN>;
	my $queue_index = <STDIN>;

	chop($caller);
	chop($opt_num1);
	chop($opt_num2);
	chop($queue_index);

	$queue_index = &untaint_obj($queue_index);

	my @array = &do_merge($caller, $opt_num1, $opt_num2, $queue_index);

	chomp($array[0]);
	chomp($array[1]);

	print "$array[0]\n";
	print "$array[1]\n";

    } else {
	print STDERR "ERROR communicating to xrust_server.\n";
    }

}

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub read_globals {
    #
    # Abstract: Read in global configurations and variables
    #           from a configs file

    my ($program_path) = @_;

    ($program = $program_path) =~ s:^.*/::;

    # Need to set path to null because of setgid!
    $ENV{'PATH'} = "";

    my ($count) = 0;
    $caller = (getpwuid($<))[0];
    
    $SIG{'INT'}  = "quit_and_exit";
    $SIG{'QUIT'} = "quit_and_exit";
    
    $globals = &get_globals;
    &init_debug;
    &init_exit;
    &init_lib;
    &init_action;
    &init_mail;
    &init_query;

    umask(017);

}

1;

