<?
# 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 (at your option) 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.
#
// ADV-SAMBA VERSION 1.0 - PHP class for batch SAMBA audit in network by Tomasz Malewski 2009
// -= PHP needs access to smbclient by /etc/sudoers ex.
// apache ALL=(ALL) NOPASSWD: ALL
#
# It base on Copyright (C) 2003-2005 Victor M. Varela <vmvarela@gmail.com>
# http://smbwebclient.sourceforge.net
class adv_samba {
var $path = 'sudo smbclient ';
var $target = 'localhost';
var $user = 'blah';
var $password = 'blah';
var $debug = '0';
var $sharename ='';
// var $sharerecurse ='recurse;';
// 2009/12/06 Fix SHARES without comment after type
var $parser = array(
"^added interface ip=([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) bcast=([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) nmask=([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\$" => 'SKIP',
"Anonymous login successful" => 'SKIP',
"^Domain=\[(.*)\] OS=\[(.*)\] Server=\[(.*)\]\$" => 'SKIP',
"^\tSharename[ ]+Type[ ]+Comment\$" => 'SHARES_MODE',
"^\t---------[ ]+----[ ]+-------\$" => 'SKIP',
"^\tServer [ ]+Comment\$" => 'SERVERS_MODE',
"^\t---------[ ]+-------\$" => 'SKIP',
"^\tWorkgroup[ ]+Master\$" => 'WORKGROUPS_MODE',
"^\t(.*)[ ]+(Disk|IPC)[ ]+IPC.*\$" => 'SKIP',
"^\tIPC\\\$(.*)[ ]+IPC" => 'SKIP',
"^\t(.*)[ ]+(Disk|Printer)[ ]+(.*)\$" => 'SHARES',
"^\t(.*)[ ]+(Disk|Printer)\$" => 'SHARES',
'([0-9]+) blocks of size ([0-9]+)\. ([0-9]+) blocks available' => 'SIZE',
"Got a positive name query response from ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})" => 'SKIP',
"^session setup failed: (.*)\$" => 'LOGON_FAILURE',
'^tree connect failed: ERRSRV - ERRbadpw' => 'LOGON_FAILURE',
"^Error returning browse list: (.*)\$" => 'ERROR',
"^tree connect failed: (.*)\$" => 'ERROR',
"^Connection to .* failed\$" => 'CONNECTION_FAILED',
'^NT_STATUS_INVALID_PARAMETER' => 'INVALID_PARAMETER',
'^NT_STATUS_DIRECTORY_NOT_EMPTY removing' => 'DIRECTORY_NOT_EMPTY',
'ERRDOS - ERRbadpath \(Directory invalid.\)' => 'NOT_A_DIRECTORY',
'NT_STATUS_NOT_A_DIRECTORY' => 'NOT_A_DIRECTORY',
'^NT_STATUS_NO_SUCH_FILE listing ' => 'NO_SUCH_FILE',
'^NT_STATUS_ACCESS_DENIED' => 'ACCESS_DENIED',
'^cd (.*): NT_STATUS_OBJECT_PATH_NOT_FOUND' => 'OBJECT_PATH_NOT_FOUND',
'^cd (.*): NT_STATUS_OBJECT_NAME_NOT_FOUND' => 'OBJECT_NAME_NOT_FOUND',
"^\t(.*)\$" => 'SERVERS_OR_WORKGROUPS',
"^([0-9]+)[ ]+([0-9]+)[ ]+(.*)\$" => 'PRINT_JOBS',
"^Job ([0-9]+) cancelled" => 'JOB_CANCELLED',
'^[ ]+(.*)[ ]+([0-9]+)[ ]+(Mon|Tue|Wed|Thu|Fri|Sat|Sun)[ ](Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[ ]+([0-9]+)[ ]+([0-9]{2}:[0-9]{2}:[0-9]{2})[ ]([0-9]{4})$' => 'FILES',
"^message start: ERRSRV - ERRmsgoff" => 'NOT_RECEIVING_MESSAGES',
"^NT_STATUS_CANNOT_DELETE" => 'CANNOT_DELETE'
);
function _SmbClient ($command, $path='', $message='', $dumpFile=false)
{
$this->line = '';
if ($command =='')
{ $smbcmd = " -L";}
else
{ $smbcmd = " -c '".$this->sharerecurse."ls '"; $sharename='/'.$this->sharename;}
$shell_command = $this->path." $smbcmd //".$this->target."$sharename --user=".$this->user.'%'.$this->password.' ';
// echo $shell_command;
exec ( $shell_command,$this->line);
} // _SmbClient
function connect()
{
$this->_SmbClient ($this->sharename,'','','');
// smbwebclient.php Begin
//foreach (split("\n", $output) as $line) if ($line <> '') {
foreach ($this->line as $line) if ($line <> '') {
$regs = array();
reset ($this->parser);
$linetype = 'skip';
$regs = array();
foreach ($this->parser as $regexp => $type) {
# preg_match is much faster than ereg (Bram Daams)
if (preg_match('/'.$regexp.'/', $line, $regs)) {
$lineType = $type;
break;
}
}
switch ($lineType) {
case 'SKIP': continue;
case 'SHARES_MODE': $mode = 'shares'; break;
case 'SERVERS_MODE': $mode = 'servers'; break;
case 'WORKGROUPS_MODE': $mode = 'workgroups'; break;
case 'SHARES':
$name = trim($regs[1]);
$type = strtolower($regs[2]);
// if ($this->cfgHideSystemShares == true AND $name[strlen($name)-1] == '$') break;
// if ($this->cfgHidePrinterShares == true AND $type == 'printer') break;
$this->targets[$this->target][shares][$name] = array (
'name' => $name,
'type' => $type,
'comment' => $regs[3]
);
break;
case 'SERVERS_OR_WORKGROUPS':
$name = trim(substr($line,1,21));
$comment = trim(substr($line, 22));
if ($mode == 'servers') {
$this->targets[$this->target][servers][$name] = array ('name' => $name, 'type' => 'server', 'comment' => $comment);
} else {
$this->targets[$this->target][workgroups][$name] = array ('name' => $name, 'type' => 'workgroup', 'comment' => $comment);
}
break;
case 'FILES':
# with attribute ?
if (preg_match("/^(.*)[ ]+([D|A|H|S|R]+)$/", trim($regs[1]), $regs2)) {
$attr = trim($regs2[2]);
$name = trim($regs2[1]);
} else {
$attr = '';
$name = trim($regs[1]);
}
if ($name <> '.' AND $name <> '..') {
$type = (strpos($attr,'D') === false) ? 'file' : 'folder';
$this->targets[$target][$name] = array (
'name' => $name,
'attr' => $attr,
'size' => $regs[2],
'time' => $regs[4].$regs[5].$regs[6].$regs[7],
'type' => $type
);
// 'time' => $this->_ParseTime($regs[4],$regs[5],$regs[7],$regs[6]),
}
break;
case 'PRINT_JOBS':
$name = $regs[1].' '.$regs[3];
$this->printjobs[$name] = array(
'name'=>$name,
'type'=>'printjob',
'id'=>$regs[1],
'size'=>$regs[2]
);
break;
case 'SIZE':
$this->size = $regs[1] * $regs[2];
$this->available = $regs[3] * $regs[2];
break;
case 'ERROR': $this->status = $regs[1]; break;
default: $this->status = $lineType;
}
}
// smbwebclient.php end
} // connect
} // class
?>
|