Don't pass the password in the command line when doing listShares

This commit is contained in:
Robin Appelman 2013-05-01 17:37:07 +02:00
commit f57f29d277
3 changed files with 33 additions and 15 deletions

View file

@ -28,3 +28,20 @@ class InvalidHostException extends \Exception {
class AccessDeniedException extends \Exception {
}
class ErrorCodes {
/**
* connection errors
*/
const LogonFailure = 'NT_STATUS_LOGON_FAILURE';
const BadHostName = 'NT_STATUS_BAD_NETWORK_NAME';
const Unsuccessful = 'NT_STATUS_UNSUCCESSFUL';
const ConnectionRefused = 'NT_STATUS_CONNECTION_REFUSED';
const PathNotFound = 'NT_STATUS_OBJECT_PATH_NOT_FOUND';
const NoSuchFile = 'NT_STATUS_NO_SUCH_FILE';
const ObjectNotFound = 'NT_STATUS_OBJECT_NAME_NOT_FOUND';
const NameCollision = 'NT_STATUS_OBJECT_NAME_COLLISION';
const AccessDenied = 'NT_STATUS_ACCESS_DENIED';
const DirectoryNotEmpty = 'NT_STATUS_DIRECTORY_NOT_EMPTY';
}

View file

@ -78,26 +78,27 @@ class Server {
/**
* @return Share[]
* @throws AuthenticationException
* @throws InvalidHostException
*/
public function listShares() {
$auth = escapeshellarg($this->getAuthString()); //TODO: don't pass password as shell argument
$command = self::CLIENT . ' -N -U ' . $auth . ' ' . '-gL ' . escapeshellarg($this->getHost()); // . ' 2> /dev/null';
exec($command, $output);
$user = escapeshellarg($this->getUser());
$command = self::CLIENT . ' -U ' . $user . ' ' . '-gL ' . escapeshellarg($this->getHost());
$connection = new RawConnection($command);
$connection->write($this->getPassword() . PHP_EOL);
$output = $connection->readAll();
$line = $output[0];
$line = rtrim($line, ')');
if (substr($line, -23) === 'NT_STATUS_LOGON_FAILURE') {
if (substr($line, -23) === ErrorCodes::LogonFailure) {
throw new AuthenticationException();
}
if (substr($line, -26) === 'NT_STATUS_BAD_NETWORK_NAME') {
if (substr($line, -26) === ErrorCodes::BadHostName) {
throw new InvalidHostException();
}
if (substr($line, -22) === 'NT_STATUS_UNSUCCESSFUL') {
if (substr($line, -22) === ErrorCodes::Unsuccessful) {
throw new InvalidHostException();
}
if (substr($line, -28) === 'NT_STATUS_CONNECTION_REFUSED') {
if (substr($line, -28) === ErrorCodes::ConnectionRefused) {
throw new InvalidHostException();
}

View file

@ -200,15 +200,15 @@ class Share {
}
list($error,) = explode(' ', $lines[0]);
switch ($error) {
case 'NT_STATUS_OBJECT_PATH_NOT_FOUND':
case 'NT_STATUS_OBJECT_NAME_NOT_FOUND':
case 'NT_STATUS_NO_SUCH_FILE':
case ErrorCodes::PathNotFound:
case ErrorCodes::ObjectNotFound:
case ErrorCodes::NoSuchFile:
throw new NotFoundException();
case 'NT_STATUS_OBJECT_NAME_COLLISION':
case ErrorCodes::NameCollision:
throw new AlreadyExistsException();
case 'NT_STATUS_ACCESS_DENIED':
case ErrorCodes::AccessDenied:
throw new AccessDeniedException();
case 'NT_STATUS_DIRECTORY_NOT_EMPTY':
case ErrorCodes::DirectoryNotEmpty:
throw new NotEmptyException();
default:
throw new \Exception();