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

View file

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