<?php


/***********************************************************/
/* last update: 5.13.2003                                 */
/* --------------------------------------------------------*/
/*                    Inprotect                            */
/* --------------------------------------------------------*/
/* Copyright (C) 2002 Inprotect.com                        */
/*                                                         */
/* This program is free software; you can redistribute it  */
/* and/or modify it under the terms of version 2 of the    */
/* GNU General Public License as published by the Free     */
/* Software Foundation.                                    */
/* 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 this program; if not, write   */
/* to the Free Software Foundation, Inc., 59 Temple Place, */
/* Suite 330, Boston, MA 02111-1307 USA                    */
/*                                                         */
/* Contact Information:                                    */
/* Alex Zimin (alex@inprotect.com)                         */
/* http://www.inprotect.com                                */
/***********************************************************/
/* See the README.txt and/or help files for more           */
/* information on how to use & config.                     */
/* See the LICENSE.txt file for more information on the    */
/* License this software is distributed under.             */
/*                                                         */
/* This program is intended for use in an authorized       */
/* manner only, and the author can not be held liable for  */
/* anything done with this program, code, or items         */
/* discovered with this program's use.                     */
/***********************************************************/

include ('header.php');

include ('permissions.inc.php');

$acl=checkacl("nessus", $dbconn);


if ($acl=="Y") {

$query = "select count(*) from nessus_plugins";
$result=$dbconn->execute($query);
list($pluginscount)=$result->fields;

if ($pluginscount==0) {
        die ("<h2>Please run updateplugins.pl script first before using web interface.</h2>");
}




Echo "<h2>Nessus Scans</h2>";

if ($page==1 or $page=="") {



if ($op=="delete" or $op=="stop") {



if (!is_numeric($schedid)) {die("Can not access this page");}
$query="select count(id) from nessus_schedule where username='$username' and id=$schedid";
$result=$dbconn->execute($query);

list($allowaccess)=$result->fields;
if ($allowaccess<>1) {die("Can not access this page");}

}


	if ($op=="new") {

		echo "New schedule<BR>
		<form method=\"post\" action=\"sched.php?page=1&amp;op=save\">
		Schedule Name: <input type=\"text\" name=\"schedulename\" value=\"\"><BR>		<input type=\"submit\" name=\"submit\" value=\"Save\">
		</form>";

	}

	else if ($op=="save") {

	$schedulename=htmlspecialchars($schedulename);
	$schedulename=mysql_escape_string($schedulename);


	$query="insert into nessus_schedule (username, name, schedule_type, status) values ('$username', '$schedulename','H','C')";
	$result=$dbconn->execute($query);
	$query="select id from nessus_schedule where name='$schedulename' and username='$username'";
	$result=$dbconn->execute($query);
	list($schedid)=$result->fields;
	
	echo "Schedule '<B>$schedulename</B>' is created.<BR>
	<A href=\"sched.php?page=2&amp;schedid=$schedid\">Manage Hosts</A><BR>
	<A href=\"sched.php?page=3&amp;schedid=$schedid\">Manage Schedule</A>";

	}
	else if ($op=="delete") {
		if ($conf=="yes") {
		$query="delete from nessus_schedule where id=$schedid";
		$result=$dbconn->execute($query);
		$query="delete from nessus_hosts where sched_id=$schedid";
		$result=$dbconn->execute($query);
		echo "Schedule has been deleted.
		Click <a href=\"sched.php\">here</a> to go to the main page.<BR>";
		}
		else {
		echo "Do you realy want to delete this schedule?<BR>
		<a href=\"sched.php?page=1&amp;op=delete&amp;schedid=$schedid&amp;conf=yes\">Yes</a> <a href=\"sched.php\">No</A>";
		}



	}
	else if ($op=="stop") {
		$query="update nessus_schedule set status='P' where id = $schedid and status='R'";
		$result=$dbconn->execute($query);
		$query="update nessus_scan set status='P' where sched_id=$schedid and status='R'";
		$result=$dbconn->execute($query);
		$query="update nessus_scan set status='C' where sched_id=$schedid and status='S' and server_id=0";
                $result=$dbconn->execute($query);
		echo "Scan will be stopped shortly<br>"; 
	}

	

	else{

	echo "<table border=\"1\" summary=\"Nessus Scans\" cellspacing=\"0\" cellpadding=\"0\" style=\"border-collapse: collapse\"><tr><td>Schedule Name</td><td>Type</td><td>Status</td><td>Run Time</td><td colspan=2>Action</td></tr>";
	$query="select id, name, schedule_type, status, date_format(next_scan, \"%W %M %e, %Y - %r\") from nessus_schedule where username='$username' order by name";
	$result=$dbconn->execute($query);

	while (!$result->EOF) {
		list ($schedid, $schedname, $schedtype, $schedstatus, $nextscan)=$result->fields;
		echo "<tr><td><a href=\"sched.php?page=2&amp;schedid=$schedid\">$schedname</A></td><td>";

	switch ($schedtype) {
		case "N":
			echo "Once (Now)";
			break;
		case "O":
                        echo "Once";
                        break;
		case "D":
                        echo "Daily";
                        break;
		case "W":
                        echo "Weekly";
                        break;
		case "M":
                        echo "Monthly";
                        break;
		case "H":
                        echo "On Hold";
                        break;
		default:
			echo "&nbsp;";
			break;
	}

echo "</td><td>";


switch ($schedstatus) {
    case "R":
        echo "<img alt=\"Running\" src=\"images/running.png\" border=0 title=\"Running\">";
        break;
    case "S":
        echo "<img alt=\"Scheduled\" src=\"images/sched.png\" border=0 title=\"Scheduled\">";
        break;
    case "P":
        echo "<img alt=\"Pending\" src=\"images/hourglass.png\" border=0 title=\"Pending\">";
        break;
    case "H":
        echo "<img alt=\"On Hold\"src=\"images/hourglass.png\" border=0 title=\"On Hold\">";
        break;
    case "C":
        echo "<img alt=\"Completed\" src=\"images/true.png\" border=0 title=\"Completed\">";
        break;
    default:
	echo "<img alt=\"Scheduling\" src=\"images/question.png\" border=0 title=\"Scheduling\">";

}
echo "</td><td>$nextscan</td><td>";
		if($schedstatus=="R"){
		echo"<a href=\"sched.php?page=1&amp;op=stop&amp;schedid=$schedid\"><img alt=\"Stop Scan\" src=\"images/stop.png\" border=0 title=\"Stop Scan\"></a>";
		}
		echo "</td><td><a href=\"sched.php?page=1&amp;op=delete&amp;schedid=$schedid\"><img alt=\"Delete Schedule\" src=\"images/delete.png\" border=0 title=\"Delete Schedule\"></a></td></tr>";


		$result->MoveNext();

	}
	echo "</table>";
	echo "<br><a href=\"sched.php?page=1&amp;op=new\">New Schedule</a> | <a href=\"livestats.php\">View runnings scans details</a><br><br>";
	}

}




if ($page==2) {

if (!is_numeric($schedid)) {die("Can not access this page");}
$query="select count(id) from nessus_schedule where username='$username' and id=$schedid";
$result=$dbconn->execute($query);

list($allowaccess)=$result->fields;
if ($allowaccess<>1) {die("Can not access this page");}


include ('validateip.inc.php');


if ($schedid=="") { die("Missing schedule ID parameter");}

if ($allowscan==1) {
        if($myhostname==""){
		//$myhostname = getenv ("REMOTE_ADDR");
                $myhostname=$_SERVER[REMOTE_ADDR];
        }
}
else
{
	//$myhostname = getenv ("REMOTE_ADDR");
        $myhostname=$_SERVER[REMOTE_ADDR];
};

echo "<A href=\"sched.php\">My Schedules</A> | <B>Manage Hosts</B> | <A href=\"sched.php?page=3&amp;schedid=$schedid\">Manage Schedule</A><HR>";



if ($hosts<>"") {

        $result=$dbconn->Execute("delete from nessus_hosts where username='$username' and hostip='$hosts' and sched_id=$schedid"); };




if (($allowscan==1 and strlen($hostname)>0) or ($allowscan<>1 and $hostname==$_SERVER[REMOTE_ADDR])) {

	$vals=preg_split("/\n|,|;/",$hostname,50);
	foreach($vals as $v)
	{
	$v=trim($v);
	if (strlen($v)>0 and inrange($v, $dbconn)) {
        $result=$dbconn->Execute("select count(*) as ipcount from nessus_hosts where hostip='$v' and username='$username' and sched_id=$schedid");
        list($countip)=$result->fields;
        if ($countip==0) {
		$acl=checkacl("owniponly", $dbconn);
		if ($acl=="Y") {
			if ($v == $myhostname) {
				$result=$dbconn->Execute("insert into nessus_hosts (username, hostip, sched_id) values ('$username','$v', $schedid)");
			}

		}
		else {
                	$result=$dbconn->Execute("insert into nessus_hosts (username, hostip, sched_id) values ('$username','$v', $schedid)");
		}
        }
        //else {
        //        echo "<b>This ip is already in the database</b>";
        //}
	}
	} //end of loop
};

$result=$dbconn->Execute("select hostip from nessus_hosts where username='$username' and sched_id=$schedid order by hostip");

$myhostname=htmlspecialchars($myhostname);

echo "<table summary=\"Target Hosts\"><tr><td>List of target hosts</td><td></td></tr>" ."<tr><td valign=\"top\">" ."<form method=\"post\" action=\"sched.php?page=2&amp;schedid=$schedid\">" ."Hostname/IP Address<BR><textarea name=\"hostname\" rows=\"6\">$myhostname</textarea><BR>" ."<input type=\"submit\" value=\"Add Host ->\">" ."</form></td>" ."<td valign=\"top\">" ."<form method=\"post\" action=\"sched.php?page=2&amp;schedid=$schedid\">" ."Submitted hosts<br>" ."<SELECT NAME=\"hosts\" SIZE=\"5\">"; $counter=1; while (list($hostip)=$result->fields) {
        echo "<OPTION VALUE=\"$hostip\">$hostip</option>";
        $counter++;
        $result->MoveNext();
}
echo "</SELECT><BR>"
."<input type=\"submit\" name=\"submit\" value=\"Remove\"></form>" ."</td></tr>" ."</table>";
$result->Close;

};




if ($page==3) {

if (!is_numeric($schedid)) {die("Can not access this page");}
$query="select count(id) from nessus_schedule where username='$username' and id=$schedid";
$result=$dbconn->execute($query);

list($allowaccess)=$result->fields;
if ($allowaccess<>1) {die("Can not access this page");}



if ($schedid=="") { die("Missing schedule ID parameter");}

echo "<A href=\"sched.php\">My Schedules</A> | <A href=\"sched.php?page=2&amp;schedid=$schedid\">Manage Hosts</A> | <B>Manage Schedule</B><HR>";


$query="select status from nessus_schedule where id=$schedid and username='$username'";
$result=$dbconn->execute($query);
list($running)=$result->fields;

if ($running=="R" or $running=="P") {
 echo "Can not change setting, while scan is running.";
 include ('footer.php');
 die();
}






$result=$dbconn->Execute("select username from nessus_schedule where username='$username' and id=$schedid"); list($userschedname)= $result->fields;
   
//if ($userschedname=="") {
//        $result=$dbconn->Execute("insert into nessus_schedule (username) values ('$username')");
//        $result=$dbconn->Execute("select schedule_type, day_of_week, day_of_month from nessus_schedule where username='$username'");
//        list($schedule_type, $week_days, $dayofmonth, $scantime )= $result->fields; };

if ($submit=="submit") {



//Check all variables

$submitscan=1;


switch ($schedule_type) {

	case "N":
		break;
	case "O":
		if (!is_numeric($ROYEAR)) {$submitscan=0;}
		if (!is_numeric($ROMONTH)) {$submitscan=0;}
		if (!is_numeric($ROday)) {$submitscan=0;}
		if ($ROYEAR<2003 or $ROYEAR>2004) {$submitscan=0;} //Must be changed to check for current and next year
		if ($ROMONTH<1 or $ROMONTH>12) {$submitscan=0;}
		if ($ROday<1 or $ROday>31) {$submitscan=0;}
		break;
	case "D":
		break;
	case "M":
		break;
	case "W":
		break;
	case "H":
                break;
	default:
		$submitscan=0;

}

if (!is_numeric($time_hour)) {$submitscan=0;}
if (!is_numeric($time_min)) {$submitscan=0;}
if (!is_numeric($timeout)) {$submitscan=0;}
if (!is_numeric($week_days)) {$submitscan=0;}
if (!is_numeric($dayofmonth)) {$submitscan=0;}
if (!is_numeric($sid)) {$submitscan=0;}

if ($time_hour<0 or $time_hour>23) {$submitscan=0;}
if ($time_min<0 or $time_min>59) {$submitscan=0;}
if ($timeout<0 or $timeout>65535) {$submitscan=0;}
if ($week_days<0 or $week_days>6) {$submitscan=0;}
if ($dayofmonth<1 or $dayofmonth>31) {$submitscan=0;}
if ($submitscan<>1) { die("Can not access this page");}


$query="select count(nessus_settings.id) from nessus_settings inner join nessus_settings_users on nessus_settings.id=nessus_settings_users.sid where nessus_settings_users.username='$username' and nessus_settings.id=$sid";
$result=$dbconn->execute($query);
list($allowaccess)=$result->fields;

if ($allowaccess<>1) {die("Can not access this page");}

//Should be safe


$scheduletime=$ROYEAR;

if ($ROMONTH<10) $scheduletime.="0";
$scheduletime.=$ROMONTH;
if ($ROday<10) $scheduletime.="0";
$scheduletime.=$ROday;


if ($time_hour<10)
{$scantime="0"; $scheduletime.="0";
}
$scantime .=$time_hour;
$scheduletime.=$time_hour;
if ($time_min<10) {$scantime .="0"; $scheduletime.="0";}
$scantime .= $time_min;
$scheduletime.=$time_min;
$scantime .="00";
$scheduletime.="00";

if ($schedule_type=="O"){
$query="update nessus_schedule set schedule_type='$schedule_type', day_of_week='$week_days', day_of_month=$dayofmonth, time=$scantime, next_scan='$scheduletime', status='S', sid=$sid, timeout=$timeout where username='$username' and id=$schedid";
$result=$dbconn->execute($query);
}
elseif ($schedule_type=="H"){
$query="update nessus_schedule set schedule_type='$schedule_type', day_of_week='$week_days', day_of_month=$dayofmonth, time=$scantime, next_scan=NULL, status='C', sid=$sid, timeout=$timeout where username='$username' and id=$schedid";
$result=$dbconn->execute($query);
}
else {
$result=$dbconn->Execute("update nessus_schedule set schedule_type='$schedule_type', day_of_week='$week_days', day_of_month=$dayofmonth, time=$scantime, next_scan=NULL, status=NULL, sid=$sid, timeout=$timeout where username='$username' and id=$schedid");
}

echo "<BR><h3>Your Nessus scan has been successfully scheduled.</h3>";

}
else {
$result=$dbconn->Execute("select schedule_type, day_of_week, day_of_month, time, status, name, sid, timeout from nessus_schedule where username='$username' and id=$schedid");
 list($schedule_type, $week_days, $dayofmonth, $scantime, $sstatus, $sname, $profileid, $timeout )= $result->fields;
$time_hour=substr($scantime,0,2);
$time_min=substr($scantime,3,2);


$today=date("Ymd");
$tyear=substr($today,0,4);
$tmonth = substr($today,4,2);
$tday = substr($today,6,2);


 echo "<form method=\"post\" action=\"sched.php?page=3&amp;schedid=$schedid\">";
	echo "Schedule - <B>$sname</B><BR>";
 echo "<table summary=\"Schedule Entry\">
<tr><td><table summary=\"Schedule Details\" align=\"left\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" style=\"border-collapse: collapse\">
<tr><td>Profile</td>";
        $query="select nessus_settings.id, nessus_settings.name, nessus_settings.description from nessus_settings inner join nessus_settings_users on nessus_settings.id=nessus_settings_users.sid where nessus_settings_users.username='$username' order by nessus_settings.name";
        $result=$dbconn->execute($query);
        echo "<td colspan=\"7\"><select name=\"sid\">";
        while (!$result->EOF) {
                list ($sid, $sname, $sdescription)=$result->fields;
                echo "<option value=\"$sid\" ";
		if ($profileid==$sid) {echo "selected";}

echo ">$sname-$sdescription";
		
                $result->MoveNext();
        }
        echo "</select></td></tr>
<tr><td> On Hold <input type=\"radio\" name=\"schedule_type\" value=\"H\"";
if ($schedule_type=="H") echo " checked";
echo "><BR></td><td colspan=\"7\"></td></tr>
<tr><td> Run now <input type=\"radio\" name=\"schedule_type\" value=\"N\"";
if ($schedule_type=="N") echo " checked";
echo "><BR></td><td colspan=\"7\"></td></tr>
<tr><td> Run once <input type=\"radio\" name=\"schedule_type\" value=\"O\"";
if ($schedule_type=="O") echo " checked";
echo "><BR></td><td colspan=\"7\">Year";
echo "<select name=\"ROYEAR\">
        <option value=\"$tyear\" selected>$tyear</option>
	<option value=\"";echo $tyear+1;echo "\">"; echo $tyear+1; echo"</option>
        </select> Month <select name=\"ROMONTH\">";
for ($i=1;$i<=12;$i++) {
        echo "<option value=\"$i\" ";
        if ($i==$tmonth) echo "selected";
        echo ">$i</option>";
};
        echo "</select> Day <select name=\"ROday\">";
for ($i=1;$i<=31;$i++) {
        echo "<option value=\"$i\" ";
        if ($i==$tday) echo "selected";
        echo ">$i</option>";
};
echo "</select>";
echo "</td></tr>";
echo "<tr><td>";
echo "Daily <input type=\"radio\" name=\"schedule_type\" value=\"D\"";
 if ($schedule_type=="D") echo " checked";
 echo "><BR>";
 echo "</td><td colspan=\"7\"></td></tr><tr><td rowspan=\"2\">";
 echo "Weekly <input type=\"radio\" name=\"schedule_type\" value=\"W\"";
 if ($schedule_type=="W") echo " checked";
 echo "></td>";
 echo "<td>Sunday</td>";
 echo "<td>Monday</td>";
 echo "<td>Tuesday</td>";
 echo "<td>Wednesday</td>";
 echo "<td>Thursday</td>";
 echo "<td>Friday</td>";
 echo "<td>Saturday</td></tr>";
 echo "<tr>";
 echo "<td><input type=\"radio\" name=\"week_days\" value=\"0\"";
 if ($week_days=="0") echo " checked";
 echo "></td>";
 echo "<td><input type=\"radio\" name=\"week_days\" value=\"1\"";
 if ($week_days=="1") echo " checked";
 echo "></td>";
 echo "<td><input type=\"radio\" name=\"week_days\" value=\"2\"";
 if ($week_days=="2") echo " checked";
 echo "></td>";
 echo "<td><input type=\"radio\" name=\"week_days\" value=\"3\"";
 if ($week_days=="3") echo " checked";
 echo "></td>";
 echo "<td><input type=\"radio\" name=\"week_days\" value=\"4\""; 
 if ($week_days=="4") echo " checked"; 
 echo "></td>"; 
 echo "<td><input type=\"radio\" name=\"week_days\" value=\"5\"";
 if ($week_days=="5") echo " checked"; 
 echo "></td>"; 
 echo "<td><input type=\"radio\" name=\"week_days\" value=\"6\""; 
 if ($week_days=="6") echo " checked"; 
 echo "></td>"; 

 echo "<tr><td>Monthly<input type=\"radio\" name=\"schedule_type\" value=\"M\""; if ($schedule_type=="M") echo " checked"; echo "></td>"; echo "<td colspan=\"7\"><select name=\"dayofmonth\">"; for ($i=1;$i<=31;$i++) {
        echo "<option value=\"$i\"";
        if ($dayofmonth==$i) echo " selected";
        echo ">$i</option>";
};
echo "</select>";
echo "</td></tr>";
echo "<tr><td rowspan=\"2\">Time</td>";
echo "<td>Hour</td><td>Minutes</td><td colspan=\"5\"><font color=\"red\">This time will be used for all schedule types</font></td>"; echo "</tr><tr><td><select name=\"time_hour\">"; for ($i=0;$i<=23;$i++){
        echo "<option  value=\"$i\"";
        if ($time_hour==$i) echo " selected";
        echo ">$i</option>";
};
echo "</select>";
echo "</td><td><select name=\"time_min\">";
for ($i=0;$i<60;$i=$i+15){
        echo "<option value=\"$i\"";
        if ($time_min == $i) echo " selected";
        echo ">$i</option>";
};
echo "</select>";
echo "</td><td colspan=5>";
echo "</td></tr>
	<tr><td>Timeout</td>
	<td colspan=7><input type=\"text\" name=\"timeout\" value=\"$timeout\"><font color=\"red\">Max scan run time in seconds</font></td></tr>";
echo "</table></td></tr><tr><td>";
echo "<input type=\"submit\" name=\"submit\" value=\"submit\"></td></table></form>";
}
}

}
else {

	echo "Access Denied!!!<br>";
}



include("footer.php");





?>

